Search in sources :

Example 61 with MethodTree

use of com.sun.source.tree.MethodTree in project bazel by bazelbuild.

the class Analysis method init.

/** Initialize the analysis with a new control flow graph. */
protected void init(ControlFlowGraph cfg) {
    this.cfg = cfg;
    thenStores = new IdentityHashMap<>();
    elseStores = new IdentityHashMap<>();
    inputs = new IdentityHashMap<>();
    storesAtReturnStatements = new IdentityHashMap<>();
    worklist = new Worklist(cfg);
    nodeValues = new IdentityHashMap<>();
    finalLocalValues = new HashMap<>();
    worklist.add(cfg.getEntryBlock());
    List<LocalVariableNode> parameters = null;
    UnderlyingAST underlyingAST = cfg.getUnderlyingAST();
    if (underlyingAST.getKind() == Kind.METHOD) {
        MethodTree tree = ((CFGMethod) underlyingAST).getMethod();
        parameters = new ArrayList<>();
        for (VariableTree p : tree.getParameters()) {
            LocalVariableNode var = new LocalVariableNode(p);
            parameters.add(var);
        // TODO: document that LocalVariableNode has no block that it
        // belongs to
        }
    } else if (underlyingAST.getKind() == Kind.LAMBDA) {
        LambdaExpressionTree lambda = ((CFGLambda) underlyingAST).getLambdaTree();
        parameters = new ArrayList<>();
        for (VariableTree p : lambda.getParameters()) {
            LocalVariableNode var = new LocalVariableNode(p);
            parameters.add(var);
        // TODO: document that LocalVariableNode has no block that it
        // belongs to
        }
    } else {
    // nothing to do
    }
    S initialStore = transferFunction.initialStore(underlyingAST, parameters);
    Block entry = cfg.getEntryBlock();
    thenStores.put(entry, initialStore);
    elseStores.put(entry, initialStore);
    inputs.put(entry, new TransferInput<>(null, this, initialStore));
}
Also used : MethodTree(com.sun.source.tree.MethodTree) CFGMethod(org.checkerframework.dataflow.cfg.UnderlyingAST.CFGMethod) VariableTree(com.sun.source.tree.VariableTree) ArrayList(java.util.ArrayList) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ExceptionBlock(org.checkerframework.dataflow.cfg.block.ExceptionBlock) SpecialBlock(org.checkerframework.dataflow.cfg.block.SpecialBlock) Block(org.checkerframework.dataflow.cfg.block.Block) RegularBlock(org.checkerframework.dataflow.cfg.block.RegularBlock) ConditionalBlock(org.checkerframework.dataflow.cfg.block.ConditionalBlock) UnderlyingAST(org.checkerframework.dataflow.cfg.UnderlyingAST)

Example 62 with MethodTree

use of com.sun.source.tree.MethodTree in project error-prone by google.

the class FragmentInjection method matchClass.

@Override
public Description matchClass(ClassTree tree, VisitorState state) {
    // Only examine classes that extend PreferenceActivity.
    Type preferenceActivityType = state.getTypeFromString("android.preference.PreferenceActivity");
    if (!isSubtype(getType(tree), preferenceActivityType, state)) {
        return NO_MATCH;
    }
    // Examine each method in the class. Complain if isValidFragment not implemented.
    TypeSymbol preferenceActivityTypeSymbol = preferenceActivityType.tsym;
    boolean methodNotImplemented = true;
    try {
        MethodSymbol isValidFragmentMethodSymbol = resolveExistingMethod(state, getSymbol(tree), state.getName("isValidFragment"), ImmutableList.<Type>of(state.getTypeFromString("java.lang.String")), ImmutableList.<Type>of());
        methodNotImplemented = isValidFragmentMethodSymbol.owner.equals(preferenceActivityTypeSymbol);
    } catch (FatalError e) {
    // If isValidFragment method symbol is not found, then we must be compiling against an old SDK
    // version (< 19) in which isValidFragment is not yet implemented, and neither this class nor
    // any of its super classes have implemented it.
    }
    // isValidFragment, and this is not an abstract class, emit warning.
    if (methodNotImplemented && not(hasModifier(Modifier.ABSTRACT)).matches(tree, state)) {
        return buildDescription(tree).setMessage("Class extending PreferenceActivity does not implement isValidFragment.").build();
    }
    // Check the implementation of isValidFragment. Complain if it always returns true.
    MethodTree isValidFragmentMethodTree = getMethod(OVERRIDES_IS_VALID_FRAGMENT, tree, state);
    if (isValidFragmentMethodTree != null) {
        if (isValidFragmentMethodTree.accept(ALWAYS_RETURNS_TRUE, null)) {
            return buildDescription(isValidFragmentMethodTree).setMessage("isValidFragment unconditionally returns true.").build();
        }
    }
    return NO_MATCH;
}
Also used : FatalError(com.sun.tools.javac.util.FatalError) ASTHelpers.getType(com.google.errorprone.util.ASTHelpers.getType) Matchers.isSameType(com.google.errorprone.matchers.Matchers.isSameType) Type(com.sun.tools.javac.code.Type) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodTree(com.sun.source.tree.MethodTree) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol)

Example 63 with MethodTree

use of com.sun.source.tree.MethodTree in project error-prone by google.

the class Util method addPrivateConstructor.

// TODO(dpb): Account for indentation level.
private static SuggestedFix.Builder addPrivateConstructor(ClassTree classTree) {
    SuggestedFix.Builder fix = SuggestedFix.builder();
    String indent = "  ";
    for (Tree member : classTree.getMembers()) {
        if (member.getKind().equals(METHOD) && !isGeneratedConstructor((MethodTree) member)) {
            fix.prefixWith(member, indent + "private " + classTree.getSimpleName() + "() {} // no instances\n" + indent);
            break;
        }
        if (!member.getKind().equals(METHOD)) {
            indent = "";
        }
    }
    return fix;
}
Also used : SuggestedFix(com.google.errorprone.fixes.SuggestedFix) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 64 with MethodTree

use of com.sun.source.tree.MethodTree in project error-prone by google.

the class JUnit3TestNotRun method matchMethod.

/**
   * Matches if:
   * 1) Method's name begins with misspelled variation of "test".
   * 2) Method is public, returns void, and has no parameters.
   * 3) Enclosing class is JUnit3 test (extends TestCase, has no RunWith annotation,
   *    and is not abstract).
   */
@Override
public Description matchMethod(MethodTree methodTree, VisitorState state) {
    Matcher<MethodTree> methodMatcher = allOf(not(methodNameStartsWith("test")), Matchers.<MethodTree>hasModifier(Modifier.PUBLIC), methodReturns(VOID_TYPE), methodHasParameters(), enclosingClass(isJUnit3TestClass));
    if (!methodMatcher.matches(methodTree, state)) {
        return Description.NO_MATCH;
    }
    String name = methodTree.getName().toString();
    String fixedName;
    // regex.Matcher class name collides with errorprone.Matcher
    java.util.regex.Matcher matcher = MISSPELLED_NAME.matcher(name);
    if (matcher.lookingAt()) {
        fixedName = matcher.replaceFirst("test");
    } else if (wouldRunInJUnit4.matches(methodTree, state)) {
        fixedName = "test" + name.substring(0, 1).toUpperCase() + name.substring(1);
    } else {
        return Description.NO_MATCH;
    }
    // We don't have start position for a method symbol, so we replace everything between result
    // type and body.
    JCMethodDecl decl = (JCMethodDecl) methodTree;
    Fix fix = SuggestedFix.replace(decl.restype.getStartPosition() + 4, decl.body.getStartPosition(), " " + fixedName + "() ");
    return describeMatch(methodTree, fix);
}
Also used : JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Fix(com.google.errorprone.fixes.Fix) MethodTree(com.sun.source.tree.MethodTree)

Example 65 with MethodTree

use of com.sun.source.tree.MethodTree in project error-prone by google.

the class InputStreamSlowMultibyteRead method matchClass.

@Override
public Description matchClass(ClassTree classTree, VisitorState state) {
    if (!IS_INPUT_STREAM.matches(classTree, state)) {
        return Description.NO_MATCH;
    }
    TypeSymbol thisClassSymbol = ASTHelpers.getSymbol(classTree);
    if (thisClassSymbol.getKind() != ElementKind.CLASS) {
        return Description.NO_MATCH;
    }
    // Find the method that overrides the single-byte read. It should also override the multibyte
    // read.
    MethodTree readByteMethod = classTree.getMembers().stream().filter(MethodTree.class::isInstance).map(MethodTree.class::cast).filter(m -> READ_INT_METHOD.matches(m, state)).findFirst().orElse(null);
    if (readByteMethod == null) {
        return Description.NO_MATCH;
    }
    Type byteArrayType = state.arrayTypeForType(state.getSymtab().byteType);
    Type intType = state.getSymtab().intType;
    MethodSymbol multiByteReadMethod = ASTHelpers.resolveExistingMethod(state, thisClassSymbol, state.getName("read"), ImmutableList.of(byteArrayType, intType, intType), ImmutableList.of());
    return multiByteReadMethod.owner.equals(thisClassSymbol) ? Description.NO_MATCH : maybeMatchReadByte(readByteMethod, state);
}
Also used : Type(com.sun.tools.javac.code.Type) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodTree(com.sun.source.tree.MethodTree) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol)

Aggregations

MethodTree (com.sun.source.tree.MethodTree)127 ClassTree (com.sun.source.tree.ClassTree)66 Tree (com.sun.source.tree.Tree)65 VariableTree (com.sun.source.tree.VariableTree)58 ExpressionTree (com.sun.source.tree.ExpressionTree)54 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)43 ExecutableElement (javax.lang.model.element.ExecutableElement)39 NewClassTree (com.sun.source.tree.NewClassTree)38 TreePath (com.sun.source.util.TreePath)33 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)32 MemberSelectTree (com.sun.source.tree.MemberSelectTree)28 AnnotationTree (com.sun.source.tree.AnnotationTree)25 IdentifierTree (com.sun.source.tree.IdentifierTree)25 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)23 ReturnTree (com.sun.source.tree.ReturnTree)22 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)22 ArrayList (java.util.ArrayList)22 TypeElement (javax.lang.model.element.TypeElement)21 AssignmentTree (com.sun.source.tree.AssignmentTree)20 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)20