private static boolean hasDefaultConstructor(Collection<Symbol> members) { for (Symbol member : members) { if ("<init>".equals(member.name()) && member.declaration() == null) { return true; } } return false; }
private static boolean hasDefaultConstructor(Collection<Symbol> members) { for (Symbol member : members) { if ("<init>".equals(member.name()) && member.declaration() == null) { return true; } } return false; }
private static boolean isOverriding(Symbol symbol) { if (symbol.isMethodSymbol()) { MethodTree methodDeclaration = (MethodTree) symbol.declaration(); return methodDeclaration != null && Boolean.TRUE.equals(methodDeclaration.isOverriding()); } return false; }
private static boolean isOverriding(Symbol symbol) { if (symbol.isMethodSymbol()) { MethodTree methodDeclaration = (MethodTree) symbol.declaration(); return methodDeclaration != null && Boolean.TRUE.equals(methodDeclaration.isOverriding()); } return false; }
private void checkIncompatiblePropagation(MethodInvocationTree methodInvocation, @Nullable String callerPropagation, Symbol calleeMethodSymbol, String calleePropagation) { Set<String> incompatiblePropagation = INCOMPATIBLE_PROPAGATION_MAP.getOrDefault(callerPropagation, Collections.emptySet()); if (incompatiblePropagation.contains(calleePropagation)) { String message = "\"" + calleeMethodSymbol.name() + "'s\" @Transactional requirement is incompatible with the one for this method."; List<JavaFileScannerContext.Location> secondaryLocations = Collections.singletonList( new JavaFileScannerContext.Location("", ((MethodTree) calleeMethodSymbol.declaration()).simpleName())); reportIssue(ExpressionUtils.methodName(methodInvocation), message, secondaryLocations, null); } }
private void checkIncompatiblePropagation(MethodInvocationTree methodInvocation, @Nullable String callerPropagation, Symbol calleeMethodSymbol, String calleePropagation) { Set<String> incompatiblePropagation = INCOMPATIBLE_PROPAGATION_MAP.getOrDefault(callerPropagation, Collections.emptySet()); if (incompatiblePropagation.contains(calleePropagation)) { String message = "\"" + calleeMethodSymbol.name() + "'s\" @Transactional requirement is incompatible with the one for this method."; List<JavaFileScannerContext.Location> secondaryLocations = Collections.singletonList( new JavaFileScannerContext.Location("", ((MethodTree) calleeMethodSymbol.declaration()).simpleName())); reportIssue(ExpressionUtils.methodName(methodInvocation), message, secondaryLocations, null); } }
private boolean isMethodParameter(Symbol symbol) { Symbol owner = symbol.owner(); return owner.isMethodSymbol() && ((Symbol.MethodSymbol) owner).declaration().parameters().contains(symbol.declaration()); }
@Override public void visitReturnStatement(ReturnStatementTree tree) { if (tree.expression() != null && tree.expression().is(Kind.IDENTIFIER)) { IdentifierTree identifier = (IdentifierTree) tree.expression(); Tree declaration = identifier.symbol().declaration(); if (resourceResolvers.contains(declaration)) { resourceResolvers.remove(declaration); } } super.visitReturnStatement(tree); }
private void checkPrivateField(Symbol privateFieldSymbol, TypeSymbol classSymbol) { MethodTree methodWhereUsed = usedInOneMethodOnly(privateFieldSymbol, classSymbol); if (methodWhereUsed != null && !isLiveInMethodEntry(privateFieldSymbol, methodWhereUsed)) { IdentifierTree declarationIdentifier = ((VariableTree) privateFieldSymbol.declaration()).simpleName(); String message = String.format(MESSAGE, privateFieldSymbol.name()); reportIssue(declarationIdentifier, message); } }
@Override public void visitAssignmentExpression(AssignmentExpressionTree tree) { if (tree.variable().is(Kind.IDENTIFIER)) { Tree declaration = ((IdentifierTree) tree.variable()).symbol().declaration(); if (declaration != null && declaration.is(Kind.VARIABLE)) { excludedVariables.add((VariableTree) declaration); } } } }
private void checkPrivateField(Symbol privateFieldSymbol, TypeSymbol classSymbol) { MethodTree methodWhereUsed = usedInOneMethodOnly(privateFieldSymbol, classSymbol); if (methodWhereUsed != null && !isLiveInMethodEntry(privateFieldSymbol, methodWhereUsed)) { IdentifierTree declarationIdentifier = ((VariableTree) privateFieldSymbol.declaration()).simpleName(); String message = String.format(MESSAGE, privateFieldSymbol.name()); reportIssue(declarationIdentifier, message); } }
@CheckForNull private Tree getPreparedStatementReference(MethodInvocationTree mit) { ExpressionTree methodSelect = mit.methodSelect(); if (methodSelect.is(Kind.MEMBER_SELECT)) { ExpressionTree expression = ((MemberSelectExpressionTree) methodSelect).expression(); if (expression.is(Kind.IDENTIFIER)) { Symbol referenceSymbol = ((IdentifierTree) expression).symbol(); return referenceSymbol.declaration(); } } return null; }
private static boolean methodReturningVariableWithSameName(Symbol methodSymbol, Symbol variableSymbol) { if (!sameName(variableSymbol, methodSymbol)) { return false; } Tree declaration = methodSymbol.declaration(); if (declaration != null) { ReturnVisitor returnVisitor = new ReturnVisitor(variableSymbol); declaration.accept(returnVisitor); return returnVisitor.singleReturnWithVariableSymbol(); } return false; }
private void checkMethodTree(MethodTree methodTree, GetSetPredicate ownPredicate, GetSetPredicate pairPredicate) { if (isSynchronized(methodTree) && ownPredicate.apply(methodTree)) { // Synchronized getter, lookup the setter. Symbol.TypeSymbol owner = (Symbol.TypeSymbol) methodTree.symbol().owner(); Collection<Symbol> pairedMethods = owner.lookupSymbols(pairPredicate.getStartName() + methodTree.symbol().name().substring(ownPredicate.getStartName().length())); pairedMethods.stream() .filter(Symbol::isMethodSymbol) .map(symbol -> (MethodTree) symbol.declaration()) .filter(pairMethod -> pairPredicate.apply(pairMethod) && !isSynchronized(pairMethod)) .forEach(pairMethod -> reportIssue(pairMethod.simpleName(), "Synchronize this method to match the synchronization on \"" + methodTree.simpleName().name() + "\".", Lists.newArrayList(new JavaFileScannerContext.Location("", methodTree.simpleName())), null)); } }
private void handleParameter(Symbol parameter, boolean springInjectionAnnotated) { Type leastSpecificType = findLeastSpecificType(parameter); if (parameter.type() != leastSpecificType && !leastSpecificType.is("java.lang.Object")) { String suggestedType = getSuggestedType(springInjectionAnnotated, leastSpecificType); reportIssue(parameter.declaration(), String.format("Use '%s' here; it is a more general type than '%s'.", suggestedType, parameter.type().name())); } }
private void handleParameter(Symbol parameter, boolean springInjectionAnnotated) { Type leastSpecificType = findLeastSpecificType(parameter); if (parameter.type() != leastSpecificType && !leastSpecificType.is("java.lang.Object")) { String suggestedType = getSuggestedType(springInjectionAnnotated, leastSpecificType); reportIssue(parameter.declaration(), String.format("Use '%s' here; it is a more general type than '%s'.", suggestedType, parameter.type().name())); } }
private static int getDeclarationLine(Symbol symbol) { if (symbol.declaration() == null) { return -1; } if (symbol.isVariableSymbol()) { return ((Symbol.VariableSymbol) symbol).declaration().simpleName().identifierToken().line(); } return ((Symbol.MethodSymbol) symbol).declaration().simpleName().identifierToken().line(); }
private static int getDeclarationLine(Symbol symbol) { if (symbol.declaration() == null) { return -1; } if (symbol.isVariableSymbol()) { return ((Symbol.VariableSymbol) symbol).declaration().simpleName().identifierToken().line(); } return ((Symbol.MethodSymbol) symbol).declaration().simpleName().identifierToken().line(); }
private static boolean verifyCondition(ExpressionTree condition) { if(condition.is(Tree.Kind.IDENTIFIER)) { IdentifierTree identifierTree = (IdentifierTree) condition; if(identifierTree.symbol().isFinal() && identifierTree.symbol().isVariableSymbol()) { VariableTree declaration = (VariableTree) identifierTree.symbol().declaration(); return declaration == null || declaration.initializer() == null || !declaration.initializer().is(Tree.Kind.BOOLEAN_LITERAL); } } return !condition.is(Tree.Kind.BOOLEAN_LITERAL); }
private static boolean verifyCondition(ExpressionTree condition) { if(condition.is(Tree.Kind.IDENTIFIER)) { IdentifierTree identifierTree = (IdentifierTree) condition; if(identifierTree.symbol().isFinal() && identifierTree.symbol().isVariableSymbol()) { VariableTree declaration = (VariableTree) identifierTree.symbol().declaration(); return declaration == null || declaration.initializer() == null || !declaration.initializer().is(Tree.Kind.BOOLEAN_LITERAL); } } return !condition.is(Tree.Kind.BOOLEAN_LITERAL); }