@Override public void visitNode(Tree tree) { TypeSymbol classSymbol = ((ClassTree) tree).symbol(); Set<Symbol> fieldsReadOnAnotherInstance = FieldsReadOnAnotherInstanceVisitor.getFrom(tree); classSymbol.memberSymbols().stream() .filter(PrivateFieldUsedLocallyCheck::isPrivateField) .filter(s -> !(s.isFinal() && s.isStatic())) .filter(s -> !hasAnnotation(s)) .filter(s -> !s.usages().isEmpty()) .filter(s -> !fieldsReadOnAnotherInstance.contains(s)) .forEach(s -> checkPrivateField(s, classSymbol)); }
private static boolean hasOnlyStaticMethodsAndFields(Symbol.TypeSymbol newClassTypeSymbol) { Collection<Symbol> symbols = filterMethodsAndFields(newClassTypeSymbol.memberSymbols()); if (symbols.isEmpty()) { return false; } for (Symbol symbol : symbols) { if (!symbol.isStatic()) { return false; } } return superTypesHaveOnlyStaticMethods(newClassTypeSymbol); }
private static boolean hideStaticMethod(Symbol.MethodSymbol methodSymbol, Type superClass, Symbol symbolWithSameName) { return symbolWithSameName.isStatic() && !methodSymbol.isStatic() && BooleanUtils.isTrue(((MethodJavaSymbol) methodSymbol).checkOverridingParameters((MethodJavaSymbol) symbolWithSameName, (ClassJavaType) superClass)); }
void checkSymbol(Symbol symbol) { if (nonStaticReference || methodSymbol.equals(symbol) || symbol.isStatic()) { return; } Symbol scopeOwner = symbol.owner(); if (isConstructor(symbol)) { checkConstructor(scopeOwner); } else if (scopeOwner != null) { checkNonConstructor(scopeOwner); } }
private static boolean isStaticField(Symbol symbol) { return symbol.isVariableSymbol() && symbol.owner().isTypeSymbol() && symbol.isStatic(); }
private static boolean isLocalVariableNotInConstructorOrStaticMain(ExpressionTree expression) { if (expression.is(Kind.IDENTIFIER)) { Symbol symbol = ((IdentifierTree) expression).symbol().owner(); return symbol.isMethodSymbol() && !("<init>".equals(symbol.name()) || ("main".equals(symbol.name()) && symbol.isStatic())); } return false; }
private void checkVariable(CheckerContext context, MethodTree tree, final Symbol symbol) { String nonNullAnnotation = nonNullAnnotation(symbol); if (nonNullAnnotation == null || symbol.isStatic()) { return; } if (isUndefinedOrNull(context, symbol)) { context.reportIssue(tree.simpleName(), this, MessageFormat.format("\"{0}\" is marked \"{1}\" but is not initialized in this constructor.", symbol.name(), nonNullAnnotation)); } }
private void checkConstructor(Symbol constructorClass) { if (!constructorClass.isStatic()) { Symbol methodPackage = getPackage(methodScopeOwner); Symbol constructorPackage = getPackage(constructorClass); if (Objects.equals(methodPackage, constructorPackage) && !constructorClass.owner().isPackageSymbol()) { setNonStaticReference(); } } }
private void checkVariable(CheckerContext context, MethodTree tree, final Symbol symbol) { String nonNullAnnotation = nonNullAnnotation(symbol); if (nonNullAnnotation == null || symbol.isStatic()) { return; } if (isUndefinedOrNull(context, symbol)) { context.reportIssue(tree.simpleName(), this, MessageFormat.format("\"{0}\" is marked \"{1}\" but is not initialized in this constructor.", symbol.name(), nonNullAnnotation)); } }
MethodReference(Symbol.MethodSymbol symbol) { methodSymbol = symbol; methodScopeOwner = methodSymbol.owner(); if (methodScopeOwner != null && methodScopeOwner.isTypeSymbol()) { nonStaticReference = !methodScopeOwner.isStatic() && !methodScopeOwner.owner().isPackageSymbol(); } }
private static boolean isStaticFinal(ExpressionTree expressionTree) { return expressionTree.is(Tree.Kind.IDENTIFIER) && ((IdentifierTree) expressionTree).symbol().isStatic() && ((IdentifierTree) expressionTree).symbol().isFinal(); } }
private static boolean isInStaticInnerClass(VariableTree hiddenVariable, VariableTree variableTree) { Symbol hiddenVariableOwner = hiddenVariable.symbol().owner(); Symbol owner = variableTree.symbol().owner(); while (!owner.equals(hiddenVariableOwner)) { if (owner.isTypeSymbol() && owner.isStatic()) { return true; } owner = owner.owner(); } return false; }
private boolean checkMembers(IdentifierTree fieldSimpleName, Symbol.TypeSymbol symbol) { for (Symbol member : symbol.memberSymbols()) { if (member.isVariableSymbol() && !member.isPrivate() && !member.isStatic() && member.name().equals(fieldSimpleName.name())) { reportIssue(fieldSimpleName, String.format("\"%s\" is the name of a field in \"%s\".", fieldSimpleName.name(), symbol.name())); return true; } } return false; }
private boolean checkMembers(IdentifierTree fieldSimpleName, Symbol.TypeSymbol symbol) { for (Symbol member : symbol.memberSymbols()) { if (member.isVariableSymbol() && !member.isPrivate() && !member.isStatic() && member.name().equals(fieldSimpleName.name())) { reportIssue(fieldSimpleName, String.format("\"%s\" is the name of a field in \"%s\".", fieldSimpleName.name(), symbol.name())); return true; } } return false; }
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()); }
private void setSymbolicValueOnFields(MethodInvocationTree tree) { boolean threadSleepMatch = THREAD_SLEEP_MATCHER.matches(tree); boolean providingThisAsArgument = isProvidingThisAsArgument(tree); if (isLocalMethodInvocation(tree) || providingThisAsArgument || threadSleepMatch) { boolean resetOnlyStaticFields = tree.symbol().isStatic() && !threadSleepMatch && !providingThisAsArgument; resetFieldValues(resetOnlyStaticFields); } }
private static boolean methodInvocationOnThisInstance(MethodInvocationTree methodInvocation) { if (methodInvocation.symbol().isStatic()) { return false; } ExpressionTree expression = methodInvocation.methodSelect(); if (expression.is(Tree.Kind.MEMBER_SELECT)) { expression = ((MemberSelectExpressionTree) expression).expression(); return expression.is(Tree.Kind.IDENTIFIER) && ((IdentifierTree) expression).name().equals("this"); } return expression.is(Tree.Kind.IDENTIFIER); }
private static boolean isMemberAutowired(Tree member) { Symbol s = null; if (member.is(Kind.VARIABLE)) { s = ((VariableTree) member).symbol(); } else if (member.is(Kind.METHOD)) { s = ((MethodTree) member).symbol(); } return s != null && !s.isStatic() && isAutowired(s); }
@Override public void visitVariable(VariableTree tree) { Symbol symbol = tree.symbol(); if (symbol != null && !symbol.isStatic()) { scan(tree.modifiers()); scan(tree.type()); // skip the simple name scan(tree.initializer()); } }
@Override public void visitMethodInvocation(MethodInvocationTree tree) { if (tree.methodSelect().is(Tree.Kind.IDENTIFIER)) { Symbol symbol = ((IdentifierTree) tree.methodSelect()).symbol(); usesThisInstance |= symbol.isMethodSymbol() && !symbol.isStatic() && instanceType.isSubtypeOf(symbol.owner().type()); } super.visitMethodInvocation(tree); }