/** Determines if the type is a double, including reference types. */ private boolean isDouble(VisitorState state, Type type) { Type trueType = unboxedTypeOrType(state, type); return trueType.getKind() == TypeKind.DOUBLE; } }
/** * 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; }
/** Return true if the given type is 'void' or 'Void'. */ public static boolean isVoidType(Type type, VisitorState state) { if (type == null) { return false; } return type.getKind() == TypeKind.VOID || state.getTypes().isSameType(Suppliers.JAVA_LANG_VOID_TYPE.get(state), type); }
@Override public Choice<Unifier> visitType(Type target, Unifier unifier) { return Choice.condition(getKind().equals(target.getKind()), unifier); }
/** * 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; }
switch (type.getKind()) { case DOUBLE: value = new BigDecimal(Double.toString(constValue(tree, Double.class)));
/** * Recognize a small set of known-immutable types that are safe for DCL even without a volatile * field. */ private static boolean isImmutable(Type type, VisitorState state) { switch (type.getKind()) { case BOOLEAN: case BYTE: case SHORT: case INT: case CHAR: case FLOAT: return true; case LONG: case DOUBLE: // double-width primitives aren't written atomically return true; default: break; } return IMMUTABLE_WHITELIST.contains( state.getTypes().erasure(type).tsym.getQualifiedName().toString()); }
@Nullable private static Object getInstance(Type type, VisitorState state) { Types types = state.getTypes(); if (type.getKind() == TypeKind.NULL) { return null; if (unboxedType.isPrimitive()) { type = unboxedType; switch (type.getKind()) { case BOOLEAN: return false; return new Object[0]; default: throw new AssertionError(type.getKind());
@Override public Description matchMemberSelect(MemberSelectTree tree, VisitorState state) { JCExpression receiverTree = (JCExpression) tree.getExpression(); if (receiverTree == null || receiverTree.type == null || receiverTree.type.getKind() == TypeKind.PACKAGE) { return Description.NO_MATCH; } // sym = null on static field imports. See https://github.com/google/error-prone/issues/1138. Symbol sym = getSymbol(tree); if ((tree instanceof JCFieldAccess) && (sym == null || sym.isStatic())) { return Description.NO_MATCH; } Description result = checkExpression( receiverTree, state, qual -> String.format( "Dereferencing method/field \"%s\" of %s null receiver %s", tree.getIdentifier(), qual, receiverTree)); return result != null ? result : Description.NO_MATCH; }
@Override public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { if (!MATCHER.matches(tree, state)) { return NO_MATCH; } Tree parent = state.getPath().getParentPath().getLeaf(); if (!(parent instanceof TypeCastTree)) { return NO_MATCH; } if (!((TypeCastTree) parent).getExpression().equals(tree)) { return NO_MATCH; } Type type = ASTHelpers.getType(parent); if (type == null || !INTEGRAL.contains(type.getKind())) { return NO_MATCH; } return describeMatch(tree); } }
switch (methodSymbol.getReturnType().getKind()) { case TYPEVAR: retType = (TypeVar) methodSymbol.getReturnType();
private String getSimpleTypeName(Type typ) { if (typ.getKind() == TypeKind.TYPEVAR) return typ.getUpperBound().tsym.getSimpleName().toString(); else return typ.tsym.getSimpleName().toString(); }
&& funcInterfaceMethod.getReturnType().getKind() != TypeKind.VOID) { ExpressionTree resExpr = (ExpressionTree) tree.getBody(); return checkReturnExpression(tree, resExpr, funcInterfaceMethod, state);
@Override public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { if (!ARRAYS_AS_LIST_SINGLE_ARRAY.matches(tree, state)) { return NO_MATCH; } ExpressionTree array = Iterables.getOnlyElement(tree.getArguments()); Type componentType = ((ArrayType) ASTHelpers.getType(array)).getComponentType(); if (!componentType.isPrimitive()) { return NO_MATCH; } String guavaUtils = GUAVA_UTILS.get(componentType.getKind()); if (guavaUtils == null) { return NO_MATCH; } Fix fix = SuggestedFix.builder() .addImport("com.google.common.primitives." + guavaUtils) .replace(tree.getMethodSelect(), guavaUtils + ".asList") .build(); return describeMatch(tree, fix); } }
@Override public Description matchMethod(MethodTree tree, VisitorState state) { MethodSymbol sym = ASTHelpers.getSymbol(tree); if (sym == null) { return NO_MATCH; } if (sym.getReturnType().getKind() != TypeKind.VOID) { return NO_MATCH; } AnnotationTree annotation = ASTHelpers.getAnnotationWithSimpleName(tree.getModifiers().getAnnotations(), "Nullable"); if (annotation == null) { return NO_MATCH; } return describeMatch(annotation, SuggestedFix.delete(annotation)); } }
@Override public Description matchNewClass(NewClassTree tree, VisitorState state) { if (ASTHelpers.isSameType( state.getSymtab().stringBuilderType, ASTHelpers.getType(tree.getIdentifier()), state) && tree.getArguments().size() == 1) { ExpressionTree argument = tree.getArguments().get(0); Type type = ((JCTree) argument).type; if (type.getKind() == TypeKind.CHAR) { if (argument.getKind() == Kind.CHAR_LITERAL) { char ch = (Character) ((LiteralTree) argument).getValue(); return describeMatch( tree, SuggestedFix.replace(argument, "\"" + Convert.quote(Character.toString(ch)) + "\"")); } else { return describeMatch( tree, SuggestedFix.replace( tree, "new StringBuilder().append(" + state.getSourceForNode((JCTree) argument) + ")")); } } } return Description.NO_MATCH; } }
x -> EnumSet.of(TypeKind.INT, TypeKind.LONG) .contains(state.getTypes().unboxedTypeOrType(x.asType()).getKind())) .noneMatch(arg -> arg.isMemberOf(enclosing.enclClass(), state.getTypes()))) {