private static boolean isAcceptableType(org.sonar.plugins.java.api.semantic.Type argType) { return argType.isSubtypeOf("java.io.Serializable") || argType.is("java.lang.Object") || argType.isPrimitive() || !argType.isClass(); }
private static String isSubclassOfAny(Type type, ListTree<TypeTree> thrownList) { for (TypeTree thrown : thrownList) { String name = thrown.symbolType().fullyQualifiedName(); if (!type.is(name) && type.isSubtypeOf(name)) { return name; } } return null; } }
private void checkForUselessUnboxing(Type targetType, Tree reportTree, ExpressionTree arg0) { Type argType = arg0.symbolType(); if (argType.is(targetType.fullyQualifiedName())) { reportIssue(reportTree, String.format("Remove the boxing to \"%s\"; The argument is already of the same type.", argType.name())); } }
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 superTypesHaveOnlyStaticMethods(Symbol.TypeSymbol newClassTypeSymbol) { Type superClass = newClassTypeSymbol.superClass(); if (superClass != null && !superClass.is("java.lang.Object") && !hasOnlyStaticMethodsAndFields(superClass.symbol())) { return false; } for (Type superInterface : newClassTypeSymbol.interfaces()) { if (!hasOnlyStaticMethodsAndFields(superInterface.symbol())) { return false; } } return true; }
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 isThreadAsRunnable(Type providedType, List<Type> parametersTypes, int index, boolean varargs) { Type expectedType = getExpectedType(providedType, parametersTypes, index, varargs); return (expectedType.is("java.lang.Runnable") && providedType.isSubtypeOf("java.lang.Thread")) || (expectedType.is("java.lang.Runnable[]") && providedType.isSubtypeOf("java.lang.Thread[]")); }
private static boolean isValueBasedType(Type type) { if (type.isUnknown() || type.is(JAVA_TIME_CLOCK)) { return false; } return VALUE_BASED_TYPES.stream().anyMatch(type::is) || JAVA_TIME_PACKAGE_PATTERN.matcher(type.fullyQualifiedName()).matches(); }
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); } }
@Override public void visitIdentifier(IdentifierTree tree) { Type type = tree.symbolType(); if (type instanceof MethodJavaType) { type = ((MethodJavaType) type).resultType(); } if (!sameErasure(type) && type.isSubtypeOf(classType.erasure())) { reportIssue(tree, String.format("Remove this reference to \"%s\".", type.symbol().name())); } }
private boolean isInvokedOnType(String type, ExpressionTree expressionTree) { Type selectorType = expressionTree.symbolType(); if (selectorType.isClass()) { return type.equals(selectorType.fullyQualifiedName()) || checkInterfaces(type, selectorType.symbol()); } return false; }
private void checkScopeAnnotation(ClassTree tree) { tree.modifiers().annotations().stream() .filter(a -> a.annotationType().symbolType().fullyQualifiedName().equals(SCOPE_ANNOTATION_FQN)) .forEach(a -> reportIssue(a, "Remove this \"@Scope\" annotation.")); }
private boolean isParameterStringArray() { VariableTree variableTree = parameters.get(0); boolean result = false; if (variableTree.type().is(Tree.Kind.ARRAY_TYPE)) { ArrayTypeTree arrayTypeTree = (ArrayTypeTree) variableTree.type(); result = arrayTypeTree.type().symbolType().isClass() && "String".equals(arrayTypeTree.type().symbolType().name()); } return result; }
private static boolean isNotSerializable(Symbol.TypeSymbol symbol) { for (Type superType : ((TypeJavaSymbol) symbol).superTypes()) { if (superType.isUnknown()) { return false; } } return !symbol.type().isSubtypeOf("java.io.Serializable"); }
private static boolean isNotImmutable(Type type) { for (String immutableType : IMMUTABLE_TYPES) { if (type.isSubtypeOf(immutableType)) { return false; } } return true; }
private boolean reportIssueOnDeprecatedType(Tree tree, Type type) { if (isDeprecatedType(type)) { reportIssue(tree, "Replace the synchronized class \"" + type.name() + "\" by an unsynchronized one such as " + REPLACEMENTS.get(type.fullyQualifiedName()) + "."); return true; } return false; }
private boolean isInherited(Symbol symbol) { Type methodOwnerType = symbol.owner().type().erasure(); Type innerType = classSymbol.type().erasure(); return !symbol.isStatic() && innerType.isSubtypeOf(methodOwnerType) && !classSymbol.owner().type().equals(methodOwnerType) && !innerType.equals(methodOwnerType); }