@Override public void onMatchMethod( NullAway analysis, MethodTree tree, VisitorState state, Symbol.MethodSymbol methodSymbol) { if (mapToFilterMap.containsKey(tree)) { bodyToMethodOrLambda.put(tree.getBody(), tree); } }
@Override public Description matchMethod(MethodTree tree, VisitorState state) { if (tree.getBody() == null) { return NO_MATCH; } tree.getBody() .accept( new TreeScanner<Void, Void>() { @Override public Void visitBlock(BlockTree block, Void unused) { Description description = scanBlock(tree, block, state); if (description != NO_MATCH) { state.reportMatch(description); } return super.visitBlock(block, unused); } }, null); return NO_MATCH; }
@Override protected Set<GuardedByExpression> getActual(MethodTree tree, VisitorState state) { return ImmutableSet.copyOf(HeldLockAnalyzer.AcquiredLockFinder.find(tree.getBody(), state)); }
@Override protected Set<GuardedByExpression> getUnwanted(MethodTree tree, VisitorState state) { return ImmutableSet.copyOf(HeldLockAnalyzer.ReleasedLockFinder.find(tree.getBody(), state)); }
@Override protected Set<GuardedByExpression> getUnwanted(MethodTree tree, VisitorState state) { return ImmutableSet.copyOf(HeldLockAnalyzer.AcquiredLockFinder.find(tree.getBody(), state)); }
@Override protected Set<GuardedByExpression> getActual(MethodTree tree, VisitorState state) { return ImmutableSet.copyOf(HeldLockAnalyzer.ReleasedLockFinder.find(tree.getBody(), state)); }
@Override public Description visitMethod(MethodTree node, Void aVoid) { BlockTree methodBody = node.getBody(); if (methodBody == null) { return Description.NO_MATCH; } return methodBody.getStatements().stream() .map(tree -> tree.accept(this, null)) .filter(notNull()) .findFirst() .orElse(Description.NO_MATCH); }
/** does the constructor invoke another constructor in the same class via this(...)? */ private boolean constructorInvokesAnother(MethodTree constructor, VisitorState state) { BlockTree body = constructor.getBody(); List<? extends StatementTree> statements = body.getStatements(); if (statements.size() > 0) { StatementTree statementTree = statements.get(0); if (isThisCall(statementTree, state)) { return true; } } return false; }
private static boolean callsSuperEquals(MethodTree method, VisitorState state) { if (method.getBody() == null) { return false; } List<? extends Tree> statements = method.getBody().getStatements(); if (statements.size() != 1) { return false; } Tree statement = getOnlyElement(statements); if (!(statement instanceof ReturnTree)) { return false; } ExpressionTree expression = ((ReturnTree) statement).getExpression(); if (expression == null) { return false; } return instanceEqualsInvocation().matches(expression, state); } }
@Override public Boolean visitMethod(MethodTree node, VisitorState state) { boolean prevInBoxedVoidReturningMethod = inBoxedVoidReturningMethod; Type returnType = ASTHelpers.getType(node.getReturnType()); Type boxedVoidType = state.getTypeFromString("java.lang.Void"); if (ASTHelpers.isSameType(returnType, boxedVoidType, state)) { inBoxedVoidReturningMethod = true; } boolean result = node.getBody() != null && node.getBody().accept(this, state); inBoxedVoidReturningMethod = prevInBoxedVoidReturningMethod; return result; }
private static boolean invokedConstructorMustBeClosed(VisitorState state, MethodTree methodTree) { // The first statement in a constructor should be an invocation of the super/this constructor. List<? extends StatementTree> statements = methodTree.getBody().getStatements(); if (statements.isEmpty()) { // Not sure how the body would be empty, but just filter it out in case. return false; } ExpressionStatementTree est = (ExpressionStatementTree) statements.get(0); MethodInvocationTree mit = (MethodInvocationTree) est.getExpression(); MethodSymbol invokedConstructorSymbol = ASTHelpers.getSymbol(mit); return ASTHelpers.hasAnnotation(invokedConstructorSymbol, MustBeClosed.class, state); } }
@Override public boolean matches(MethodTree method, VisitorState state) { List<? extends VariableTree> parameters = method.getParameters(); if (!parameters.isEmpty()) { return false; } BlockTree body = method.getBody(); if (body == null) { return false; } List<? extends StatementTree> statements = body.getStatements(); if (statements.size() != 1) { return false; } StatementTree onlyStatement = Iterables.getOnlyElement(statements); if (!onlyStatement.getKind().equals(RETURN)) { return false; } return EMPTY_SET.matches(((ReturnTree) onlyStatement).getExpression(), state); } };
@Override protected Description handleStatements( MethodTree tree, VisitorState state, JCExpression expectedException, SuggestedFix baseFix) { return describeMatch( tree, buildFix( state, SuggestedFix.builder().merge(baseFix), expectedException, tree.getBody().getStatements())); } }
@Override public Description matchMethod(MethodTree methodTree, VisitorState visitorState) { if (Matchers.equalsMethodDeclaration().matches(methodTree, visitorState)) { VariableTree variableTree = methodTree.getParameters().get(0); VarSymbol varSymbol = ASTHelpers.getSymbol(variableTree); TreeScannerEquals treeScannerEquals = new TreeScannerEquals(methodTree); treeScannerEquals.scan(methodTree.getBody(), varSymbol); if (treeScannerEquals.hasIllegalEquals) { return describeMatch(methodTree); } } return Description.NO_MATCH; }
private Set<Element> guaranteedNonNullForConstructor( FieldInitEntities entities, VisitorState state, Trees trees, MethodTree constructor) { Symbol.MethodSymbol symbol = ASTHelpers.getSymbol(constructor); Set<Element> safeInitMethods = getSafeInitMethods(constructor.getBody(), entities.classSymbol(), state); AccessPathNullnessAnalysis nullnessAnalysis = getNullnessAnalysis(state); Set<Element> guaranteedNonNull = new LinkedHashSet<>(); guaranteedNonNull.addAll( nullnessAnalysis.getNonnullFieldsOfReceiverAtExit( new TreePath(state.getPath(), constructor), state.context)); addGuaranteedNonNullFromInvokes( state, trees, safeInitMethods, nullnessAnalysis, guaranteedNonNull); return guaranteedNonNull; }
@Override public Description matchMethod(MethodTree methodTree, VisitorState state) { if (methodTree.getBody() == null) { // Method is not implemented (i.e. it's abstract). return Description.NO_MATCH; } if (ASTHelpers.isJUnitTestCode(state) && JUnitMatchers.wouldRunInJUnit4.matches(methodTree, state) && CONTAINS_ASSERT.matches(methodTree.getBody(), state)) { SuggestedFix.Builder fix = SuggestedFix.builder(); for (AssertTree foundAssert : scanAsserts(methodTree)) { replaceAssert(fix, foundAssert, state); } return buildDescription(methodTree).addFix(fix.build()).build(); } return Description.NO_MATCH; }
@Override public Description matchMethod(MethodTree tree, VisitorState state) { if (tree.getBody() == null) { return NO_MATCH; } SuggestedFix.Builder baseFixBuilder = SuggestedFix.builder(); JCExpression expectedException = deleteExpectedException( baseFixBuilder, ((JCMethodDecl) tree).getModifiers().getAnnotations(), state); SuggestedFix baseFix = baseFixBuilder.build(); if (expectedException == null) { return NO_MATCH; } return handleStatements(tree, state, expectedException, baseFix); }
@Override @Nullable public Choice<Unifier> visitMethod(MethodTree decl, @Nullable Unifier unifier) { return getName() .unify(decl.getName(), unifier) .thenChoose(unifications(getReturnType(), decl.getReturnType())) .thenChoose(unifications(getParameters(), decl.getParameters())) .thenChoose(unifications(getThrows(), decl.getThrows())) .thenChoose(unifications(getBody(), decl.getBody())); }
@Override public UMethodDecl visitMethod(MethodTree decl, Void v) { return UMethodDecl.create( visitModifiers(decl.getModifiers(), null), decl.getName(), templateType(decl.getReturnType()), cast(templateStatements(decl.getParameters()), UVariableDecl.class), templateExpressions(decl.getThrows()), (UBlock) template(decl.getBody())); }
private Description fixByModifyingMethod( VisitorState state, JCClassDecl enclosingClass, MethodTree method) { JCModifiers methodModifiers = ((JCMethodDecl) method).getModifiers(); String replacementModifiersString = createReplacementMethodModifiers(state, methodModifiers); JCModifiers enclosingClassModifiers = enclosingClass.getModifiers(); String enclosingClassReplacementModifiersString = createReplacementClassModifiers(state, enclosingClassModifiers); SuggestedFix.Builder fixBuilder = SuggestedFix.builder() .addImport("dagger.multibindings.Multibinds") .replace(methodModifiers, replacementModifiersString) .replace(method.getBody(), ";"); fixBuilder = (enclosingClassModifiers.pos == -1) ? fixBuilder.prefixWith(enclosingClass, enclosingClassReplacementModifiersString) : fixBuilder.replace(enclosingClassModifiers, enclosingClassReplacementModifiersString); return describeMatch(method, fixBuilder.build()); }