Search in sources :

Example 66 with MethodSymbol

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

the class DeclarationsIntoElements method storeMethod.

/**
 * Add inherited declaration annotations from overridden methods into the corresponding Elements
 * so they are written into bytecode.
 *
 * @param env ProcessingEnvironment
 * @param atypeFactory the type factory
 * @param meth the MethodTree to add the annotations
 */
private static void storeMethod(ProcessingEnvironment env, AnnotatedTypeFactory atypeFactory, MethodTree meth) {
    ExecutableElement element = TreeUtils.elementFromDeclaration(meth);
    MethodSymbol sym = (MethodSymbol) element;
    java.util.List<? extends AnnotationMirror> elementAnnos = element.getAnnotationMirrors();
    Set<AnnotationMirror> declAnnotations = atypeFactory.getDeclAnnotations(sym);
    List<Compound> tcs = List.nil();
    for (AnnotationMirror anno : declAnnotations) {
        // Only add the annotation if it isn't in the Element already.
        if (!AnnotationUtils.containsSame(elementAnnos, anno)) {
            tcs = tcs.append(TypeAnnotationUtils.createCompoundFromAnnotationMirror(anno, env));
        }
    }
    sym.appendAttributes(tcs);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ExecutableElement(javax.lang.model.element.ExecutableElement) Compound(com.sun.tools.javac.code.Attribute.Compound)

Example 67 with MethodSymbol

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

the class DefaultReflectionResolver method getConstructorSymbolsfor.

/**
 * Get set of Symbols for constructors based on class name and parameter length.
 *
 * @return the (potentially empty) set of corresponding constructor Symbol(s)
 */
private List<Symbol> getConstructorSymbolsfor(String className, int paramLength, Env<AttrContext> env) {
    Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
    Resolve resolve = Resolve.instance(context);
    Names names = Names.instance(context);
    Symbol symClass = getSymbol(className, env, names, resolve);
    if (!symClass.exists()) {
        debugReflection("Unable to resolve class: " + className);
        return Collections.emptyList();
    }
    // TODO: Should this be used instead of the below??
    ElementFilter.constructorsIn(symClass.getEnclosedElements());
    // The common case is probably that `result` is a singleton at method exit.
    List<Symbol> result = new ArrayList<>();
    for (Symbol s : symClass.getEnclosedElements()) {
        // Check all constructors
        if (s.getKind() == ElementKind.CONSTRUCTOR) {
            // Check for number of parameters
            if (((MethodSymbol) s).getParameters().size() == paramLength) {
                result.add(s);
            }
        }
    }
    if (result.isEmpty()) {
        debugReflection("Unable to resolve constructor!");
    }
    return result;
}
Also used : Context(com.sun.tools.javac.util.Context) AttrContext(com.sun.tools.javac.comp.AttrContext) Names(com.sun.tools.javac.util.Names) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Symbol(com.sun.tools.javac.code.Symbol) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) ArrayList(java.util.ArrayList) Resolve(com.sun.tools.javac.comp.Resolve)

Example 68 with MethodSymbol

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

the class SizeGreaterThanOrEqualsZero method isProtoRepeatedFieldCountMethod.

private static boolean isProtoRepeatedFieldCountMethod(ExpressionTree tree, VisitorState state) {
    // Instance method, on proto class, named `get<Field>Count`.
    if (!PROTO_METHOD_NAMED_GET_COUNT.matches(tree, state)) {
        return false;
    }
    // Make sure it's the count method for a repeated field, not the get method for a non-repeated
    // field named <something>_count, by checking for other methods on the repeated field.
    MethodSymbol methodCallSym = getSymbol((MethodInvocationTree) tree);
    if (methodCallSym == null) {
        return false;
    }
    Scope protoClassMembers = methodCallSym.owner.members();
    java.util.regex.Matcher getCountRegexMatcher = PROTO_COUNT_METHOD_PATTERN.matcher(methodCallSym.getSimpleName().toString());
    if (!getCountRegexMatcher.matches()) {
        return false;
    }
    String fieldName = getCountRegexMatcher.group(1);
    return protoClassMembers.findFirst(state.getName("get" + fieldName + "List")) != null;
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Scope(com.sun.tools.javac.code.Scope)

Example 69 with MethodSymbol

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

the class ReferenceEquality method inEqualsOrCompareTo.

private boolean inEqualsOrCompareTo(Type classType, Type type, VisitorState state) {
    MethodTree methodTree = ASTHelpers.findEnclosingNode(state.getPath(), MethodTree.class);
    if (methodTree == null) {
        return false;
    }
    MethodSymbol sym = ASTHelpers.getSymbol(methodTree);
    if (sym == null || sym.isStatic()) {
        return false;
    }
    Symbol compareTo = getOnlyMember(state, state.getSymtab().comparableType, "compareTo");
    Symbol equals = getOnlyMember(state, state.getSymtab().objectType, "equals");
    if (!sym.overrides(compareTo, classType.tsym, state.getTypes(), /* checkResult= */
    false) && !sym.overrides(equals, classType.tsym, state.getTypes(), /* checkResult= */
    false)) {
        return false;
    }
    if (!ASTHelpers.isSameType(type, classType, state)) {
        return false;
    }
    return true;
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodTree(com.sun.source.tree.MethodTree) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Symbol(com.sun.tools.javac.code.Symbol)

Example 70 with MethodSymbol

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

the class RestrictedApiChecker method matchMethodInvocation.

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
    RestrictedApi annotation = ASTHelpers.getAnnotation(tree, RestrictedApi.class);
    if (annotation != null) {
        return checkRestriction(annotation, tree, state);
    }
    MethodSymbol methSymbol = ASTHelpers.getSymbol(tree);
    if (methSymbol == null) {
        // This shouldn't happen, but has. (See b/33758055)
        return Description.NO_MATCH;
    }
    // Try each super method for @RestrictedApi
    Optional<MethodSymbol> superWithRestrictedApi = ASTHelpers.findSuperMethods(methSymbol, state.getTypes()).stream().filter((t) -> ASTHelpers.hasAnnotation(t, RestrictedApi.class, state)).findFirst();
    if (!superWithRestrictedApi.isPresent()) {
        return Description.NO_MATCH;
    }
    return checkRestriction(ASTHelpers.getAnnotation(superWithRestrictedApi.get(), RestrictedApi.class), tree, state);
}
Also used : RestrictedApi(com.google.errorprone.annotations.RestrictedApi) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) TypeElement(javax.lang.model.element.TypeElement) NewClassTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.NewClassTreeMatcher) ArrayList(java.util.ArrayList) MirroredTypesException(javax.lang.model.type.MirroredTypesException) VisitorState(com.google.errorprone.VisitorState) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) NewClassTree(com.sun.source.tree.NewClassTree) BugPattern(com.google.errorprone.BugPattern) Category(com.google.errorprone.BugPattern.Category) Matcher(com.google.errorprone.matchers.Matcher) Tree(com.sun.source.tree.Tree) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) Nullable(javax.annotation.Nullable) MethodInvocationTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher) RestrictedApi(com.google.errorprone.annotations.RestrictedApi) JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) List(java.util.List) TypeMirror(javax.lang.model.type.TypeMirror) Matchers(com.google.errorprone.matchers.Matchers) Description(com.google.errorprone.matchers.Description) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) SeverityLevel(com.google.errorprone.BugPattern.SeverityLevel) ASTHelpers(com.google.errorprone.util.ASTHelpers) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol)

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