Search in sources :

Example 51 with MethodSymbol

use of com.sun.tools.javac.code.Symbol.MethodSymbol in project checker-framework by typetools.

the class TypeVarUseApplier method getParameterAnnos.

/**
 * Currently, the metadata for storing annotations (i.e. the Attribute.TypeCompounds) is null for
 * binary-only parameters and type parameters. However, it is present on the method. So in order
 * to ensure that we correctly retrieve the annotations we need to index from the method and
 * retrieve the annotations from its metadata.
 *
 * @return a list of annotations that were found on METHOD_FORMAL_PARAMETERS that match the
 *     parameter index of the input element in the parent methods formal parameter list
 */
private static List<Attribute.TypeCompound> getParameterAnnos(final Element paramElem) {
    final Element enclosingElement = paramElem.getEnclosingElement();
    if (!(enclosingElement instanceof ExecutableElement)) {
        throw new BugInCF("Bad element passed to TypeFromElement.getTypeParameterAnnotationAttributes: " + "element: " + paramElem + " not found in enclosing executable: " + enclosingElement);
    }
    final MethodSymbol enclosingMethod = (MethodSymbol) enclosingElement;
    if (enclosingMethod.getKind() != ElementKind.CONSTRUCTOR && enclosingMethod.getKind() != ElementKind.METHOD) {
        // Initializer blocks don't have parameters, so there is nothing to do.
        return Collections.emptyList();
    }
    // TODO: for the parameter in a lambda expression, the enclosingMethod isn't
    // the lambda expression. Does this read the correct annotations?
    final int paramIndex = enclosingMethod.getParameters().indexOf(paramElem);
    final List<Attribute.TypeCompound> annotations = enclosingMethod.getRawTypeAttributes();
    final List<Attribute.TypeCompound> result = new ArrayList<>();
    for (final Attribute.TypeCompound typeAnno : annotations) {
        if (typeAnno.position.type == TargetType.METHOD_FORMAL_PARAMETER) {
            if (typeAnno.position.parameter_index == paramIndex) {
                result.add(typeAnno);
            }
        }
    }
    return result;
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Attribute(com.sun.tools.javac.code.Attribute) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeParameterElement(javax.lang.model.element.TypeParameterElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) BugInCF(org.checkerframework.javacutil.BugInCF)

Example 52 with MethodSymbol

use of com.sun.tools.javac.code.Symbol.MethodSymbol in project checker-framework by typetools.

the class TypeVarUseApplier method getReturnAnnos.

/**
 * Returns the annotations on the return type of the input ExecutableElement.
 *
 * @param methodElem the method whose return type annotations to return
 * @return the annotations on the return type of the input ExecutableElement
 */
private static List<Attribute.TypeCompound> getReturnAnnos(final Element methodElem) {
    if (!(methodElem instanceof ExecutableElement)) {
        throw new BugInCF("Bad element passed to TypeVarUseApplier.getReturnAnnos:" + methodElem);
    }
    final MethodSymbol enclosingMethod = (MethodSymbol) methodElem;
    final List<Attribute.TypeCompound> annotations = enclosingMethod.getRawTypeAttributes();
    final List<Attribute.TypeCompound> result = new ArrayList<>();
    for (final Attribute.TypeCompound typeAnno : annotations) {
        if (typeAnno.position.type == TargetType.METHOD_RETURN) {
            result.add(typeAnno);
        }
    }
    return result;
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Attribute(com.sun.tools.javac.code.Attribute) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) BugInCF(org.checkerframework.javacutil.BugInCF)

Example 53 with MethodSymbol

use of com.sun.tools.javac.code.Symbol.MethodSymbol in project checker-framework by typetools.

the class TreeUtils method isSynthetic.

/**
 * Returns true if the given method is synthetic. Also returns true if the method is a generated
 * default constructor, which does not appear in source code but is not considered synthetic.
 *
 * @param ee a method or constructor element
 * @return true iff the given method is synthetic
 */
public static boolean isSynthetic(ExecutableElement ee) {
    MethodSymbol ms = (MethodSymbol) ee;
    long mod = ms.flags();
    // GENERATEDCONSTR is for generated constructors, which do not have SYNTHETIC set.
    return (mod & (Flags.SYNTHETIC | Flags.GENERATEDCONSTR)) != 0;
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol)

Example 54 with MethodSymbol

use of com.sun.tools.javac.code.Symbol.MethodSymbol in project bazel by bazelbuild.

the class TreeUtils method hasExplicitConstructor.

/**
     * Determine whether the given class contains an explicit constructor.
     *
     * @param node A class tree.
     * @return True, iff there is an explicit constructor.
     */
public static boolean hasExplicitConstructor(ClassTree node) {
    TypeElement elem = TreeUtils.elementFromDeclaration(node);
    for (ExecutableElement ee : ElementFilter.constructorsIn(elem.getEnclosedElements())) {
        MethodSymbol ms = (MethodSymbol) ee;
        long mod = ms.flags();
        if ((mod & Flags.SYNTHETIC) == 0) {
            return true;
        }
    }
    return false;
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 55 with MethodSymbol

use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.

the class AbstractExpectedExceptionChecker method buildBaseFix.

protected BaseFix buildBaseFix(VisitorState state, List<Tree> expectations) {
    String exceptionClass = "Throwable";
    // additional assertions to perform on the captured exception (if any)
    List<String> newAsserts = new ArrayList<>();
    Builder fix = SuggestedFix.builder();
    for (Tree expectation : expectations) {
        MethodInvocationTree invocation = (MethodInvocationTree) ((ExpressionStatementTree) expectation).getExpression();
        MethodSymbol symbol = ASTHelpers.getSymbol(invocation);
        Symtab symtab = state.getSymtab();
        List<? extends ExpressionTree> args = invocation.getArguments();
        switch(symbol.getSimpleName().toString()) {
            case "expect":
                if (isSubtype(getOnlyElement(symbol.getParameters()).asType(), symtab.classType, state)) {
                    // expect(Class<?>)
                    exceptionClass = state.getSourceForNode(getReceiver(getOnlyElement(args)));
                } else {
                    // expect(Matcher)
                    fix.addStaticImport("org.hamcrest.MatcherAssert.assertThat");
                    newAsserts.add(String.format("assertThat(thrown, %s);", state.getSourceForNode(getOnlyElement(args))));
                }
                break;
            case "expectCause":
                ExpressionTree matcher = getOnlyElement(invocation.getArguments());
                if (IS_A.matches(matcher, state)) {
                    fix.addStaticImport("com.google.common.truth.Truth.assertThat");
                    newAsserts.add(String.format("assertThat(thrown).hasCauseThat().isInstanceOf(%s);", state.getSourceForNode(getOnlyElement(((MethodInvocationTree) matcher).getArguments()))));
                } else {
                    fix.addStaticImport("org.hamcrest.MatcherAssert.assertThat");
                    newAsserts.add(String.format("assertThat(thrown.getCause(), %s);", state.getSourceForNode(getOnlyElement(args))));
                }
                break;
            case "expectMessage":
                if (isSubtype(getOnlyElement(symbol.getParameters()).asType(), symtab.stringType, state)) {
                    // expectedMessage(String)
                    fix.addStaticImport("com.google.common.truth.Truth.assertThat");
                    newAsserts.add(String.format("assertThat(thrown).hasMessageThat().contains(%s);", state.getSourceForNode(getOnlyElement(args))));
                } else {
                    // expectedMessage(Matcher)
                    fix.addStaticImport("org.hamcrest.MatcherAssert.assertThat");
                    newAsserts.add(String.format("assertThat(thrown.getMessage(), %s);", state.getSourceForNode(getOnlyElement(args))));
                }
                break;
            default:
                throw new AssertionError("unknown expect method: " + symbol.getSimpleName());
        }
    }
    // remove all interactions with the ExpectedException rule
    fix.replace(((JCTree) expectations.get(0)).getStartPosition(), state.getEndPosition(getLast(expectations)), "");
    return new BaseFix(fix.build(), exceptionClass, newAsserts);
}
Also used : Symtab(com.sun.tools.javac.code.Symtab) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) Builder(com.google.errorprone.fixes.SuggestedFix.Builder) ArrayList(java.util.ArrayList) MethodTree(com.sun.source.tree.MethodTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) JCTree(com.sun.tools.javac.tree.JCTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) StatementTree(com.sun.source.tree.StatementTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Aggregations

MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)98 Symbol (com.sun.tools.javac.code.Symbol)35 Type (com.sun.tools.javac.code.Type)32 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)27 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)22 MethodTree (com.sun.source.tree.MethodTree)22 Tree (com.sun.source.tree.Tree)21 ArrayList (java.util.ArrayList)18 ExpressionTree (com.sun.source.tree.ExpressionTree)17 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)13 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)13 Description (com.google.errorprone.matchers.Description)11 ClassTree (com.sun.source.tree.ClassTree)11 VisitorState (com.google.errorprone.VisitorState)10 JCTree (com.sun.tools.javac.tree.JCTree)9 Types (com.sun.tools.javac.code.Types)8 BugPattern (com.google.errorprone.BugPattern)7 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)7 MemberSelectTree (com.sun.source.tree.MemberSelectTree)7 MethodType (com.sun.tools.javac.code.Type.MethodType)7