@Override public boolean apply(Type type, VisitorState state) { return type != null && state.getTypes().isArray(type); } };
/** Removes the message argument if it is present. */ private void removeMessageArgumentIfPresent(VisitorState state, List<Type> argumentTypes) { if (argumentTypes.size() == 2) { return; } Types types = state.getTypes(); Type firstType = argumentTypes.get(0); if (types.isSameType(firstType, state.getSymtab().stringType)) { argumentTypes.remove(0); } }
private static List<Type> typeArgsAsSuper(Type baseType, Type superType, VisitorState state) { Type projectedType = state.getTypes().asSuper(baseType, superType.tsym); if (projectedType != null) { return projectedType.getTypeArguments(); } return new ArrayList<>(); }
private boolean isValidArrayFill( VisitorState state, Type arrayComponentType, Type fillingObjectType) { if (arrayComponentType == null || fillingObjectType == null) { return true; // shrug } return ASTHelpers.isSubtype( state.getTypes().boxedTypeOrType(fillingObjectType), arrayComponentType, state); }
@Override public boolean matches(Tree t, VisitorState state) { Type type = getType(t); return type != null && state.getTypes().isArray(type); } };
@Nullable @Override public Type visitSwitch(SwitchTree node, Void unused) { if (current == node.getExpression()) { return state.getTypes().unboxedTypeOrType(getType(current)); } else { return null; } }
@Nullable @Override public Type visitNewArray(NewArrayTree node, Void aVoid) { if (node.getDimensions().contains(current)) { return state.getSymtab().intType; } if (node.getInitializers().contains(current)) { return state.getTypes().elemtype(ASTHelpers.getType(node)); } return null; }
@Override Type extractSourceType(MethodInvocationTree tree, VisitorState state) { return extractTypeArgAsMemberOfSupertype( ASTHelpers.getType(Iterables.get(tree.getArguments(), methodArgIndex)), state.getSymbolFromString(methodArgTypeName), methodArgTypeArgIndex, state.getTypes()); }
/** * Checks that the return value of a functional interface is void. Note, we do not use * ASTHelpers.isVoidType here, return values of Void are actually type-checked. Only * void-returning functions silently ignore return values of any type. */ private static boolean functionalInterfaceReturnsExactlyVoid( Type interfaceType, VisitorState state) { return state.getTypes().findDescriptorType(interfaceType).getReturnType().getKind() == TypeKind.VOID; }
@Override public boolean matches(T t, VisitorState state) { Type type = getType(t); return type != null && state.getTypes().isSameType(type, state.getSymtab().voidType); } };
@Override public boolean matches(Tree t, VisitorState state) { Type type = getType(t); return type != null && state.getTypes().unboxedTypeOrType(type).isPrimitive(); } };
@Override Type extractTargetType(MethodInvocationTree tree, VisitorState state) { return extractTypeArgAsMemberOfSupertype( ASTHelpers.getReceiverType(tree), state.getSymbolFromString(typeName), typeArgIndex, state.getTypes()); } }
/** Returns true if {@code erasure(s) == erasure(t)}. */ public static boolean isSameType(Type s, Type t, VisitorState state) { if (s == null || t == null) { return false; } Types types = state.getTypes(); return types.isSameType(types.erasure(s), types.erasure(t)); }
/** Returns true if {@code erasure(s) <: erasure(t)}. */ public static boolean isSubtype(Type s, Type t, VisitorState state) { if (s == null || t == null) { return false; } if (SUBTYPE_UNDEFINED.contains(s.getTag()) || SUBTYPE_UNDEFINED.contains(t.getTag())) { return false; } Types types = state.getTypes(); return types.isSubtype(types.erasure(s), types.erasure(t)); }
private boolean isIgnoredReturnType(JCMethodDecl method, VisitorState state) { Type returnType = method.sym.getReturnType(); // Methods returning a primitive cannot return null. Also ignore Void-returning methods as // the only valid Void value is null, so it's implied. // TODO(kmb): Technically we should assume NULL when we see a call to a method that returns Void return returnType.isPrimitiveOrVoid() || state.getTypes().isSameType(returnType, state.getTypeFromString("java.lang.Void")); }
private void processGroupMethods(List<MethodTree> groupMethodTrees, VisitorState state) { Preconditions.checkArgument(!groupMethodTrees.isEmpty()); for (ParameterOrderingViolation violation : getViolations(groupMethodTrees)) { MethodSymbol methodSymbol = getSymbol(violation.methodTree()); if (ASTHelpers.findSuperMethods(methodSymbol, state.getTypes()).isEmpty()) { Description.Builder description = buildDescription(violation.methodTree()); description.setMessage(violation.getDescription()); state.reportMatch(description.build()); } } }
@Override public Void visitMethod(MethodTree methodTree, Void unused) { MethodSymbol methodSymbol = getSymbol(methodTree); if (methodSymbol != null && findSuperMethods(methodSymbol, state.getTypes()).isEmpty()) { state.reportMatch( buildDescription(diagnosticPosition(getCurrentPath(), state)) .setMessage( "This method does not override anything to inherit documentation from.") .build()); } return null; }
private static Type getComparableTypeArgument(ClassTree tree, VisitorState state) { final Type comparable = state .getTypes() .asSuper(ASTHelpers.getType(tree), state.getSymtab().comparableType.asElement()); if (comparable != null && !comparable.getTypeArguments().isEmpty()) { return Iterables.getOnlyElement(comparable.getTypeArguments()); } return null; } }
private boolean overrides(MethodSymbol sym, MethodSymbol other) { return !sym.isStatic() && !other.isStatic() && (((sym.flags() | other.flags()) & Flags.SYNTHETIC) == 0) && sym.name.contentEquals(other.name) && sym.overrides( other, sym.owner.enclClass(), state.getTypes(), /* checkResult= */ false); } }.scan(method.getBody(), null),