private static String getSuggestedType(boolean springInjectionAnnotated, Type leastSpecificType) { if (springInjectionAnnotated && leastSpecificType.is("java.lang.Iterable")) { return "java.util.Collection"; } return leastSpecificType.fullyQualifiedName().replace('$', '.'); }
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 static boolean isServletOrEjb(Symbol symbol) { if (SERVLET_AND_EJB_CLASSES.stream().anyMatch(symbol.type()::isSubtypeOf)) { return true; } return symbol.metadata().annotations().stream().anyMatch(annotation -> annotation.symbol().type().fullyQualifiedName().startsWith("javax.ejb.")); }
private static List<String> getThrownCheckedExceptions(MethodTree methodTree) { ImmutableList.Builder<String> builder = ImmutableList.builder(); for (Type thrownClass : methodTree.symbol().thrownTypes()) { if (!thrownClass.isUnknown() && !isSubClassOfRuntimeException(thrownClass)) { builder.add(thrownClass.fullyQualifiedName()); } } return builder.build(); }
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 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 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 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 checkTypeToReplace(VariableTree variableTree) { String fullyQualifiedTypeName = variableTree.type().symbolType().fullyQualifiedName(); if (GUAVA_TO_JAVA_UTIL_TYPES.containsKey(fullyQualifiedTypeName)) { reportIssue(variableTree.type(), replacementMessage(GUAVA_TO_JAVA_UTIL_TYPES.get(fullyQualifiedTypeName))); } }
private static boolean isIntermediateOperation(MethodInvocationTree mit) { if (BASE_STREAM_INTERMEDIATE_OPERATIONS.anyMatch(mit)) { return true; } Symbol method = mit.symbol(); return method.isMethodSymbol() && !method.isStatic() && STREAM_TYPES.contains(method.owner().type().fullyQualifiedName()) && STREAM_TYPES.contains(((Symbol.MethodSymbol) method).returnType().type().fullyQualifiedName()); }
@Override protected void onConstructorFound(NewClassTree newClassTree) { String message = DeprecatedSpringPasswordEncoder.MESSAGE_PER_CLASS.get(newClassTree.identifier().symbolType().fullyQualifiedName()); reportIssue(newClassTree.identifier(), message); }
private static boolean isSecurityAuthenticationConstant(ExpressionTree tree) { if (tree.is(Kind.MEMBER_SELECT)) { MemberSelectExpressionTree constantExpression = (MemberSelectExpressionTree) tree; return "javax.naming.Context".equals(constantExpression.expression().symbolType().fullyQualifiedName()) && "SECURITY_AUTHENTICATION".equals(constantExpression.identifier().name()); } return LiteralUtils.hasValue(tree, "java.naming.security.authentication"); } }
private static Symbol.TypeSymbol wrapperClassSymbol(NewClassTree newClassTree) { Symbol.TypeSymbol classSymbol = newClassTree.symbolType().symbol(); if (PRIMITIVE_TYPES_BY_WRAPPER.containsKey(newClassTree.symbolType().fullyQualifiedName()) && !newClassTree.arguments().isEmpty()) { return classSymbol; } return null; }
@Override public void visitClass(ClassTree classTree) { TypeTree superClass = classTree.superClass(); if (superClass != null) { String superClassTypeName = superClass.symbolType().fullyQualifiedName(); checkIfDisallowed(superClassTypeName, superClass); } super.visitClass(classTree); }
@Override public void visitAnnotation(AnnotationTree annotationTree) { String annotationTypeName = annotationTree.symbolType().fullyQualifiedName(); checkIfDisallowed(annotationTypeName, annotationTree.annotationType()); super.visitAnnotation(annotationTree); }
@Override public void visitVariable(VariableTree variableTree) { String variableTypeName = variableTree.type().symbolType().fullyQualifiedName(); checkIfDisallowed(variableTypeName, variableTree.type()); super.visitVariable(variableTree); }
private static String getDefinitionPlace(Symbol symbol, Symbol knownMemberSymbol) { if (sameOwner(symbol, knownMemberSymbol)) { int declarationLine = getDeclarationLine(knownMemberSymbol); if (declarationLine == -1) { return ""; } return " defined on line " + declarationLine; } return " defined in " + (knownMemberSymbol.owner().isInterface() ? "interface" : "superclass") + " \"" + knownMemberSymbol.owner().type().fullyQualifiedName() + "\""; }
@Override public void visitMethod(MethodTree methodTree) { if (methodTree.returnType() != null ) { String returnTypeName = methodTree.returnType().symbolType().fullyQualifiedName(); checkIfDisallowed(returnTypeName, methodTree.returnType()); } super.visitMethod(methodTree); }
@Override public void visitMethodInvocation(MethodInvocationTree tree) { if(tree.symbol().isMethodSymbol()) { String className = tree.symbol().owner().type().fullyQualifiedName(); checkIfDisallowed(className, tree.methodSelect()); } super.visitMethodInvocation(tree); }
private void visitMethodInvocation(MethodInvocationTree methodInvocation) { if (WRITE_OBJECT_MATCHER.matches(methodInvocation)) { ExpressionTree argument = methodInvocation.arguments().get(0); if (!isAcceptableType(argument.symbolType()) && !isTestedSymbol(argument)) { reportIssue(argument, "Make the \"" + argument.symbolType().fullyQualifiedName() + "\" class \"Serializable\" or don't write it."); } } }