private static boolean hasUniqueConstructor(ClassTree clazzTree) { return clazzTree.symbol().memberSymbols().stream() .filter(Symbol::isMethodSymbol) .map(s -> (Symbol.MethodSymbol) s) .filter(m -> m.name().equals("<init>")) .filter(m -> m.declaration() != null) .count() == 1; }
private static boolean hasUniqueConstructor(ClassTree clazzTree) { return clazzTree.symbol().memberSymbols().stream() .filter(Symbol::isMethodSymbol) .map(s -> (Symbol.MethodSymbol) s) .filter(m -> m.name().equals("<init>")) .filter(m -> m.declaration() != null) .count() == 1; }
private static Optional<String> lookupMatchingStandardInterface(MethodSymbol functionalMethod) { MethodTree declaration = functionalMethod.declaration(); if (!functionalMethod.thrownTypes().isEmpty() || (declaration != null && !declaration.typeParameters().isEmpty())) { return Optional.empty(); } Type returnType = declaration != null ? declaration.returnType().symbolType() : functionalMethod.returnType().type(); return STD_INTERFACE_BY_PARAMETER_COUNT.getOrDefault(functionalMethod.parameterTypes().size(), Collections.emptyList()).stream() .map(standardInterface -> standardInterface.matchingSpecialization(functionalMethod, returnType)) .filter(Objects::nonNull) .findFirst(); }
@CheckForNull private MethodBehavior get(String signature, @Nullable Symbol.MethodSymbol symbol) { MethodBehavior mb = behaviors.get(signature); if(mb != null) { return mb; } if (symbol != null) { MethodTree declaration = symbol.declaration(); if (SymbolicExecutionVisitor.methodCanNotBeOverriden(symbol) && declaration != null) { sev.execute(declaration); return behaviors.get(signature); } } // disabled x-file analysis, behavior based on source code can still be used if (!crossFileEnabled && !isKnownSignature(signature)) { return null; } if (!bytecodeBehaviors.containsKey(signature)) { new BytecodeEGWalker(this, semanticModel).getMethodBehavior(signature, classLoader); } return bytecodeBehaviors.get(signature); }
@Override public void visitNode(Tree tree) { if (!hasSemantic()) { return; } MethodTree methodTree = (MethodTree) tree; Symbol.MethodSymbol methodSymbol = methodTree.symbol(); Symbol.MethodSymbol overriddenSymbol = methodSymbol.overriddenSymbol(); if (overriddenSymbol == null || overriddenSymbol.isUnknown()) { return; } if (isSynchronized(overriddenSymbol) && !isSynchronized(methodSymbol)) { List<JavaFileScannerContext.Location> secondaries = Collections.emptyList(); MethodTree overridenMethodTree = overriddenSymbol.declaration(); if (overridenMethodTree != null) { secondaries = Collections.singletonList(new JavaFileScannerContext.Location("", overridenMethodTree.simpleName())); } reportIssue(methodTree.simpleName(), MESSAGE, secondaries, null); } }
@CheckForNull private MethodBehavior get(String signature, @Nullable Symbol.MethodSymbol symbol) { MethodBehavior mb = behaviors.get(signature); if(mb != null) { return mb; } if (symbol != null) { MethodTree declaration = symbol.declaration(); if (SymbolicExecutionVisitor.methodCanNotBeOverriden(symbol) && declaration != null) { sev.execute(declaration); return behaviors.get(signature); } } // disabled x-file analysis, behavior based on source code can still be used if (!crossFileEnabled && !isKnownSignature(signature)) { return null; } if (!bytecodeBehaviors.containsKey(signature)) { new BytecodeEGWalker(this, semanticModel).getMethodBehavior(signature, classLoader); } return bytecodeBehaviors.get(signature); }
@Override public void visitNode(Tree tree) { if (!hasSemantic()) { return; } MethodTree methodTree = (MethodTree) tree; Symbol.MethodSymbol methodSymbol = methodTree.symbol(); Symbol.MethodSymbol overriddenSymbol = methodSymbol.overriddenSymbol(); if (overriddenSymbol == null || overriddenSymbol.isUnknown()) { return; } if (isSynchronized(overriddenSymbol) && !isSynchronized(methodSymbol)) { List<JavaFileScannerContext.Location> secondaries = Collections.emptyList(); MethodTree overridenMethodTree = overriddenSymbol.declaration(); if (overridenMethodTree != null) { secondaries = Collections.singletonList(new JavaFileScannerContext.Location("", overridenMethodTree.simpleName())); } reportIssue(methodTree.simpleName(), MESSAGE, secondaries, null); } }
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 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 && isCompareToInvocation((MethodInvocationTree) initializer) && !isReassigned(variableSymbol, method); } } return false; }
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(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 isNotObjectMethod(Symbol.MethodSymbol method) { MethodTree declaration = method.declaration(); return declaration == null || !OBJECT_METHODS.anyMatch(declaration); }
private static boolean isNotObjectMethod(MethodSymbol method) { MethodTree declaration = method.declaration(); return declaration == null || !OBJECT_METHODS.anyMatch(declaration); }
private static boolean isNotObjectMethod(Symbol.MethodSymbol method) { MethodTree declaration = method.declaration(); return declaration == null || !OBJECT_METHODS.anyMatch(declaration); }
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 Optional<String> lookupMatchingStandardInterface(MethodSymbol functionalMethod) { MethodTree declaration = functionalMethod.declaration(); if (!functionalMethod.thrownTypes().isEmpty() || (declaration != null && !declaration.typeParameters().isEmpty())) { return Optional.empty(); } Type returnType = declaration != null ? declaration.returnType().symbolType() : functionalMethod.returnType().type(); return STD_INTERFACE_BY_PARAMETER_COUNT.getOrDefault(functionalMethod.parameterTypes().size(), Collections.emptyList()).stream() .map(standardInterface -> standardInterface.matchingSpecialization(functionalMethod, returnType)) .filter(Objects::nonNull) .findFirst(); }
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 boolean isMethodParameter(Symbol symbol) { Symbol owner = symbol.owner(); return owner.isMethodSymbol() && ((Symbol.MethodSymbol) owner).declaration().parameters().contains(symbol.declaration()); }