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 isExpectedValue(Object annotationValue, String expectedValue) { if (annotationValue instanceof Tree) { // from sources return containsValue((Tree) annotationValue, expectedValue); } // from binaries if (annotationValue instanceof Object[]) { return containsValue((Object[]) annotationValue, expectedValue); } return expectedValue.equals(((Symbol) annotationValue).name()); }
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 boolean mitHeuristics(MethodInvocationTree mit) { Symbol methodSymbol = mit.symbol(); return !methodSymbol.isUnknown() && invocationOfMethodFromOtherClass(mit) && methodSymbol.name().matches("new.*|create.*|open.*"); }
@Override protected void onMethodInvocationFound(MethodInvocationTree mit) { if(mit.symbol().name().equals("setAccessible")) { checkAccessibilityUpdate(mit); } else { reportIssue(mit, "Make sure that this accessibility bypass is safe here."); } }
private boolean checkEmptyParams(MethodInvocationTree mit, Collection<?> params) { if (params.isEmpty() && (!LEVELS.contains(mit.symbol().name()) || mit.arguments().size() > 1)) { reportIssue(mit, "String contains no format specifiers."); return true; } return false; }
@Override protected void onMethodInvocationFound(MethodInvocationTree mit) { String methodName = mit.symbol().name(); reportIssue(ExpressionUtils.methodName(mit), "Use \"Arrays." + methodName + "(array)\" instead."); } }
private static boolean isExcluded(MethodInvocationTree mit) { String methodName = mit.symbol().name(); return mit.symbol().isUnknown() || EXCLUDED.anyMatch(mit) || (isInTryCatch(mit) && (EXCLUDED_PREFIX.stream().anyMatch(methodName::startsWith) || STRING_GET_BYTES.matches(mit))); }
private void checkSymbolAssignment(Tree variable) { if (variable.is(Kind.IDENTIFIER)) { if (field.equals(((IdentifierTree) variable).symbol())) { reportIssue( synchronizedStatement, String.format("\"%s\" is not \"private final\", and should not be used for synchronization. ", field.name())); } } else if (variable.is(Kind.MEMBER_SELECT)) { checkSymbolAssignment(((MemberSelectExpressionTree) variable).identifier()); } }
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 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); } }
private void addBoxingIssue(Tree tree, Symbol classSymbol, Tree boxingArg) { if (boxingArg.is(Tree.Kind.IDENTIFIER)) { IdentifierTree identifier = (IdentifierTree) boxingArg; reportIssue(tree, "Remove the boxing of \"" + identifier.name() + "\"."); } else { reportIssue(tree, "Remove the boxing to \"" + classSymbol.name() + "\"."); } }
private void checkSymbol(Symbol symbol) { for (IdentifierTree usageIdentifier : symbol.usages()) { Tree parent = usageIdentifier.parent(); if (parent.is(Kind.MEMBER_SELECT) && ((MemberSelectExpressionTree) parent).expression().is(Kind.MEMBER_SELECT)) { reportIssue(parent, String.format(MESSAGE, symbol.name())); } } }
@Override protected void onMethodInvocationFound(MethodInvocationTree methodTree) { if (CONSTRUCTOR_NAME.equals(methodTree.symbol().name())) { // when calling super() for classes extending SearchControls checkConstructorArguments(methodTree.arguments()); } else { ExpressionTree setValue = methodTree.arguments().get(0); reportIfTrue(setValue); } }
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 isGetPropertyWithDefaultValue(MethodInvocationTree mit) { Symbol symbol = mit.symbol(); if (symbol.isMethodSymbol() && symbol.owner().type().is("java.util.Properties")) { return "getProperty".equals(symbol.name()) && mit.arguments().size() == 2; } return false; } }
@Override public void visitMethodInvocation(MethodInvocationTree tree) { Symbol symbol = tree.symbol(); if (!symbol.isUnknown() && symbol.owner().type().isSubtypeOf("java.lang.Thread") && "interrupt".equals(symbol.name()) && tree.arguments().isEmpty()) { threadInterrupted = true; return; } super.visitMethodInvocation(tree); }
@Override protected void onMethodInvocationFound(MethodInvocationTree mit) { ExpressionTree arg = mit.arguments().get(0); if (arg.is(Tree.Kind.STRING_LITERAL)) { String argValue = LiteralUtils.trimQuotes(((LiteralTree) arg).value()); if (argValue.length() == 1 || isEscapedChar(argValue)) { reportIssue(arg, "Put single-quotes around '" + argValue + "' to use the faster \"" + mit.symbol().name() + "(char)\" method."); } } }
@Override protected void onMethodInvocationFound(MethodInvocationTree mit) { Tree parent = mit.parent(); if (parent.is(Tree.Kind.EXPRESSION_STATEMENT) || (parent.is(Tree.Kind.VARIABLE) && ((VariableTree) parent).symbol().usages().isEmpty())) { reportIssue(parent, "Do something with the \"" + mit.symbolType().name() + "\" value returned by \"" + mit.symbol().name() + "\"."); } }
private static String name(Tree tree) { if (tree.is(Tree.Kind.NEW_CLASS)) { return ((NewClassTree) tree).symbolType().name(); } MethodInvocationTree mit = (MethodInvocationTree) tree; if (mit.symbolType().isVoid()) { return mit.symbol().owner().name(); } return mit.symbolType().name(); }