@Override public boolean matches(Type type) { return type.isArray(); }
private static boolean isArrayLength(Type type, String memberName) { return type.isArray() && "length".equals(memberName); }
static boolean isForbiddenType(final Type type) { return type.isArray() || MUTABLE_TYPES.stream().anyMatch(type::isSubtypeOf); } }
private static Type getExpectedType(Type providedType, List<Type> parametersTypes, int index, boolean varargs) { int lastParameterIndex = parametersTypes.size() - 1; Type lastParameterType = parametersTypes.get(lastParameterIndex); Type lastExpectedType = varargs ? ((Type.ArrayType) lastParameterType).elementType() : lastParameterType; if (index > lastParameterIndex || (index == lastParameterIndex && varargs && !providedType.isArray())) { return lastExpectedType; } return parametersTypes.get(index); }
private static Type getExpectedType(Type providedType, List<Type> parametersTypes, int index, boolean varargs) { int lastParameterIndex = parametersTypes.size() - 1; Type lastParameterType = parametersTypes.get(lastParameterIndex); Type lastExpectedType = varargs ? ((Type.ArrayType) lastParameterType).elementType() : lastParameterType; if (index > lastParameterIndex || (index == lastParameterIndex && varargs && !providedType.isArray())) { return lastExpectedType; } return parametersTypes.get(index); }
private static String getMessage(ExpressionTree argument, Type providedType, int index) { String array = providedType.isArray() ? "[]" : ""; return MessageFormat.format("\"{0}\" is a \"Thread{1}\".", getArgName(argument, index), array); }
private Type getExpectedType(Type providedType, List<Type> parametersTypes, int index, boolean varargs) { int lastParameterIndex = parametersTypes.size() - 1; Type lastParameterType = parametersTypes.get(lastParameterIndex); Type lastExpectedType = varargs ? ((Type.ArrayType) lastParameterType).elementType() : lastParameterType; if (index > lastParameterIndex || (index == lastParameterIndex && varargs && !providedType.isArray())) { return lastExpectedType; } return parametersTypes.get(index); }
private static boolean isMutableType(Type type) { if (type.isArray()) { return true; } for (String mutableType : MUTABLE_TYPES) { if (type.isSubtypeOf(mutableType) && isNotImmutable(type)) { return true; } } return false; }
private String convertGenericType(String expectedType, Type actualType, Map<String, String> genericTypeMapping) { if (genericTypes.isEmpty() || !genericTypes.contains(expectedType)) { return expectedType; } String convertedType = genericTypeMapping.get(expectedType); if (convertedType == null) { if (actualType.isPrimitive() || actualType.isVoid() || actualType.isArray() || actualType.isUnknown()) { return "!unknown!"; } convertedType = actualType.fullyQualifiedName(); genericTypeMapping.put(expectedType, convertedType); } return convertedType; }
private void checkArrayLength(MemberSelectExpressionTree testedValue, BinaryExpressionTree bet, boolean leftIsZero) { if (!"length".equals(testedValue.identifier().name())) { return; } ExpressionTree expression = testedValue.expression(); if (!expression.symbolType().isArray()) { return; } reportIssue(bet, leftIsZero, ARRAY_ISSUE_MSG, arrayName(expression)); }
private void checkArrayLength(MemberSelectExpressionTree testedValue, BinaryExpressionTree bet, boolean leftIsZero) { if (!"length".equals(testedValue.identifier().name())) { return; } ExpressionTree expression = testedValue.expression(); if (!expression.symbolType().isArray()) { return; } reportIssue(bet, leftIsZero, ARRAY_ISSUE_MSG, arrayName(expression)); }
private void checkCast(Type type, MethodInvocationTree mit) { if (type.isArray() && !type.is("java.lang.Object[]")) { Type elementType = ((Type.ArrayType) type).elementType(); if (!((JavaType) elementType).isTagged(JavaType.TYPEVAR)) { reportIssue(mit, "Pass \"new " + elementType.name() + "[0]\" as argument to \"toArray\"."); } } } }
private void checkCast(Type type, MethodInvocationTree mit) { if (type.isArray() && !type.is("java.lang.Object[]")) { Type elementType = ((Type.ArrayType) type).elementType(); if (!((JavaType) elementType).isTagged(JavaType.TYPEVAR)) { reportIssue(mit, "Pass \"new " + elementType.name() + "[0]\" as argument to \"toArray\"."); } } } }
private static boolean implementsSerializable(@Nullable Type type) { if (type == null || type.isUnknown() || type.isPrimitive()) { // do not raise an issue if type is unknown return true; } if (type.isArray()) { return implementsSerializable(((Type.ArrayType) type).elementType()); } if (type.isClass() || ((JavaType) type).isTagged(JavaType.TYPEVAR)) { return type.erasure().isSubtypeOf("java.io.Serializable"); } return false; }
private static boolean implementsSerializable(@Nullable Type type) { if (type == null || type.isUnknown() || type.isPrimitive()) { // do not raise an issue if type is unknown return true; } if (type.isArray()) { return implementsSerializable(((Type.ArrayType) type).elementType()); } if (type.isClass() || ((JavaType) type).isTagged(JavaType.TYPEVAR)) { return type.erasure().isSubtypeOf("java.io.Serializable"); } return false; }
@Override public void visitMethodInvocation(MethodInvocationTree tree) { if (tree.methodSelect().is(Tree.Kind.MEMBER_SELECT)) { MemberSelectExpressionTree mset = (MemberSelectExpressionTree) tree.methodSelect(); if ("equals".equals(mset.identifier().name()) && mset.expression().symbolType().isArray()) { context.addIssue(tree, this, "Use the '==' operator instead of calling the equals() method to prevent any misunderstandings"); } } super.visitMethodInvocation(tree); } }
private void checkWhenOwnerIsNotArray(IdentifierTree methodInvocationName, Type ownerType, Type argumentType) { if (argumentType.isArray() && !ownerType.is(JAVA_LANG_OBJECT)) { reportIssue(methodInvocationName, "Remove this call to \"equals\"; comparisons between a type and an array always return false."); } else if (argumentType.isClass() && areNotRelated(ownerType, argumentType) && (areTypesFinalClassAndInterface(ownerType, argumentType) || areNeitherInterfaces(ownerType, argumentType))) { reportIssue(methodInvocationName, MESSAGE); } }
private void checkWhenOwnerIsArray(IdentifierTree methodInvocationName, Type.ArrayType ownerType, Type argumentType) { if (argumentType.isArray()) { if (areNotRelated(ownerType.elementType(), ((Type.ArrayType) argumentType).elementType())) { reportIssue(methodInvocationName, "Remove this call to \"equals\"; comparisons between unrelated arrays always return false."); } else { reportIssue(methodInvocationName, "Use \"Arrays.equals(array1, array2)\" or the \"==\" operator instead of using the \"Object.equals(Object obj)\" method."); } } else if (!argumentType.is(JAVA_LANG_OBJECT)) { reportIssue(methodInvocationName, "Remove this call to \"equals\"; comparisons between an array and a type always return false."); } }
private void checkWhenOwnerIsArray(IdentifierTree methodInvocationName, Type.ArrayType ownerType, Type argumentType) { if (argumentType.isArray()) { if (areNotRelated(ownerType.elementType(), ((Type.ArrayType) argumentType).elementType())) { reportIssue(methodInvocationName, "Remove this call to \"equals\"; comparisons between unrelated arrays always return false."); } else { reportIssue(methodInvocationName, "Use \"Arrays.equals(array1, array2)\" or the \"==\" operator instead of using the \"Object.equals(Object obj)\" method."); } } else if (!argumentType.is(JAVA_LANG_OBJECT)) { reportIssue(methodInvocationName, "Remove this call to \"equals\"; comparisons between an array and a type always return false."); } }
private void checkWhenOwnerIsNotArray(IdentifierTree methodInvocationName, Type ownerType, Type argumentType) { if (argumentType.isArray() && !ownerType.is(JAVA_LANG_OBJECT)) { reportIssue(methodInvocationName, "Remove this call to \"equals\"; comparisons between a type and an array always return false."); } else if (argumentType.isClass() && areNotRelated(ownerType, argumentType) && (areTypesFinalClassAndInterface(ownerType, argumentType) || areNeitherInterfaces(ownerType, argumentType))) { reportIssue(methodInvocationName, MESSAGE); } }