private void checkGetter(String fieldName, MethodTree methodTree) { Symbol.TypeSymbol getterOwner = ((Symbol.TypeSymbol) methodTree.symbol().owner()); if (hasNoPrivateFieldMatchingNameAndType(fieldName, methodTree.symbol().returnType().type(), getterOwner)) { return; } firstAndOnlyStatement(methodTree) .filter(statementTree -> statementTree.is(Tree.Kind.RETURN_STATEMENT)) .map(statementTree -> ((ReturnStatementTree) statementTree).expression()) .flatMap(GettersSettersOnRightFieldCheck::symbolFromExpression) .filter(returnSymbol -> !fieldName.equals(returnSymbol.name())) .ifPresent(returnedSymbol -> context.reportIssue(this, methodTree.simpleName(), "Refactor this getter so that it actually refers to the field \"" + fieldName + "\".")); }
private static boolean isIteratorNextMethod(Symbol.MethodSymbol symbol) { return "next".equals(symbol.name()) && symbol.parameterTypes().isEmpty() && isIterator(symbol.enclosingClass()); }
private static boolean hasNonPrivateNoArgConstructor(Type type) { Collection<Symbol> constructors = type.symbol().lookupSymbols("<init>"); for (Symbol member : constructors) { if (member.isMethodSymbol()) { Symbol.MethodSymbol method = (Symbol.MethodSymbol) member; if (method.parameterTypes().isEmpty() && !method.isPrivate()) { return true; } } } return constructors.isEmpty(); }
@Override public void visitNode(Tree tree) { if (!hasSemantic()) { return; } MethodTree methodTree = (MethodTree) tree; Symbol.TypeSymbol enclosingClass = methodTree.symbol().enclosingClass(); String className = enclosingClass.type().fullyQualifiedName(); MethodMatcher writeObjectMatcher = SerializableContract.writeObjectMatcher(className); if (writeObjectMatcher.matches(methodTree) && hasModifier(methodTree.modifiers(), SYNCHRONIZED)) { SynchronizationVisitor visitor = new SynchronizationVisitor(methodTree); enclosingClass.declaration().accept(visitor); if (!visitor.moreThanSingleLock) { reportIssue(ModifiersUtils.getModifier(methodTree.modifiers(), SYNCHRONIZED), "Remove this \"synchronized\" keyword."); } } }
@Override public void visitNode(Tree tree) { if (!hasSemantic()) { return; } MethodTree methodTree = (MethodTree) tree; Symbol.TypeSymbol enclosingClass = methodTree.symbol().enclosingClass(); String className = enclosingClass.type().fullyQualifiedName(); MethodMatcher writeObjectMatcher = SerializableContract.writeObjectMatcher(className); if (writeObjectMatcher.matches(methodTree) && hasModifier(methodTree.modifiers(), SYNCHRONIZED)) { SynchronizationVisitor visitor = new SynchronizationVisitor(methodTree); enclosingClass.declaration().accept(visitor); if (!visitor.moreThanSingleLock) { reportIssue(ModifiersUtils.getModifier(methodTree.modifiers(), SYNCHRONIZED), "Remove this \"synchronized\" keyword."); } } }
private static boolean isIdentifierContainingCompareToResult(IdentifierTree identifier) { Symbol variableSymbol = identifier.symbol(); if (!variableSymbol.isVariableSymbol()) { return false; } VariableTree variableDefinition = ((Symbol.VariableSymbol) variableSymbol).declaration(); if (variableDefinition != null) { ExpressionTree initializer = variableDefinition.initializer(); if (initializer != null && initializer.is(Tree.Kind.METHOD_INVOCATION) && variableSymbol.owner().isMethodSymbol()) { MethodTree method = ((Symbol.MethodSymbol) variableSymbol.owner()).declaration(); return method != null && COMPARE_TO.matches((MethodInvocationTree) initializer) && !isReassigned(variableSymbol, method); } } return false; }
private static boolean isNotObjectMethod(Symbol.MethodSymbol method) { MethodTree declaration = method.declaration(); return declaration == null || !OBJECT_METHODS.anyMatch(declaration); }
private static boolean isPrivateConstructor(MethodTree constructor) { return constructor.symbol().isPrivate(); }
private static boolean isNotObjectMethod(MethodSymbol method) { MethodTree declaration = method.declaration(); return declaration == null || !OBJECT_METHODS.anyMatch(declaration); }
private boolean isMethodParameter(Symbol symbol) { Symbol owner = symbol.owner(); return owner.isMethodSymbol() && ((Symbol.MethodSymbol) owner).declaration().parameters().contains(symbol.declaration()); }
private static boolean isPrivateStaticOrAbstract(Symbol.MethodSymbol methodSymbol) { return methodSymbol.isPrivate() || methodSymbol.isStatic() || methodSymbol.isAbstract(); }
private static boolean hasProperEquals(MethodTree methodTree) { Symbol.TypeSymbol enclosingClass = methodTree.symbol().enclosingClass(); return enclosingClass != null && enclosingClass.lookupSymbols(EQUALS).stream().anyMatch(EQUALS_MATCHER::matches); }
private static boolean isPrivateStaticOrAbstract(Symbol.MethodSymbol methodSymbol) { return methodSymbol.isPrivate() || methodSymbol.isStatic() || methodSymbol.isAbstract(); }
private boolean isIteratorNextMethod(Symbol.MethodSymbol symbol) { return "next".equals(symbol.name()) && symbol.parameterTypes().isEmpty() && isIterator(symbol.enclosingClass()); }
private static boolean isIteratorNextMethod(Symbol.MethodSymbol symbol) { return "next".equals(symbol.name()) && symbol.parameterTypes().isEmpty() && isIterator(symbol.enclosingClass()); }
@Override public void visitMethod(MethodTree tree) { if (isExcluded(tree)) { return; } Symbol.MethodSymbol symbol = tree.symbol(); methodReferences.push(new MethodReference(symbol)); scan(tree.parameters()); scan(tree.block()); MethodReference reference = methodReferences.pop(); if (symbol.isPrivate() && !symbol.isStatic() && !reference.hasNonStaticReference()) { context.reportIssue(this, tree.simpleName(), "Make \"" + symbol.name() + "\" a \"static\" method."); } }
@Override public void visitMethod(MethodTree method) { if (sliceAnnotationPresent && !isConstructorOrAfterCreatedMethod(method) && !method.symbol().isPrivate()) { method.accept(new MethodInvocationVisitor(javaFileScanner, context)); } else { ignoreAnonymousClass = true; super.visitMethod(method); } ignoreAnonymousClass = false; }
private static boolean isDesignedForExtension(MethodTree tree) { if (tree.symbol().enclosingClass().isFinal()) { // methods of final class can not be overridden, because the class can not be extended return false; } ModifiersTree modifiers = tree.modifiers(); return ModifiersUtils.hasModifier(modifiers, Modifier.DEFAULT) || (!ModifiersUtils.hasModifier(modifiers, Modifier.PRIVATE) && isEmptyOrThrowStatement(tree.block())); }
public boolean matches(MethodTree methodTree) { MethodSymbol symbol = methodTree.symbol(); Symbol.TypeSymbol enclosingClass = symbol.enclosingClass(); return enclosingClass != null && matches(symbol, enclosingClass.type()); }
private static boolean isNotObjectMethod(MethodSymbol method) { MethodTree declaration = method.declaration(); return declaration == null || !OBJECT_METHODS.anyMatch(declaration); }