Search in sources :

Example 41 with Symbol

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

the class FindIdentifiers method findIdent.

/** Finds a declaration with the given name that is in scope at the current location. */
public static Symbol findIdent(String name, VisitorState state) {
    ClassType enclosingClass = ASTHelpers.getType(state.findEnclosing(ClassTree.class));
    if (enclosingClass == null || enclosingClass.tsym == null) {
        return null;
    }
    Env<AttrContext> env = Enter.instance(state.context).getClassEnv(enclosingClass.tsym);
    MethodTree enclosingMethod = state.findEnclosing(MethodTree.class);
    if (enclosingMethod != null) {
        env = MemberEnter.instance(state.context).getMethodEnv((JCMethodDecl) enclosingMethod, env);
    }
    try {
        Method method = Resolve.class.getDeclaredMethod("findIdent", Env.class, Name.class, KindSelector.class);
        method.setAccessible(true);
        Symbol result = (Symbol) method.invoke(Resolve.instance(state.context), env, state.getName(name), KindSelector.VAR);
        return result.exists() ? result : null;
    } catch (ReflectiveOperationException e) {
        throw new LinkageError(e.getMessage(), e);
    }
}
Also used : JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) MethodTree(com.sun.source.tree.MethodTree) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Symbol(com.sun.tools.javac.code.Symbol) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) NewClassTree(com.sun.source.tree.NewClassTree) ClassTree(com.sun.source.tree.ClassTree) Method(java.lang.reflect.Method) ClassType(com.sun.tools.javac.code.Type.ClassType) AttrContext(com.sun.tools.javac.comp.AttrContext)

Example 42 with Symbol

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

the class VisitorState method getSymbolFromString.

/**
   * @param symStr the string representation of a symbol
   * @return the Symbol object, or null if it cannot be found
   */
public Symbol getSymbolFromString(String symStr) {
    try {
        Name symName = getName(symStr);
        Symbol result = getSymtab().classes.get(symName);
        if (result != null) {
            // Force a completion failure if the type is not available.
            result.complete();
        }
        return result;
    } catch (CompletionFailure failure) {
        return null;
    }
}
Also used : ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Symbol(com.sun.tools.javac.code.Symbol) CompletionFailure(com.sun.tools.javac.code.Symbol.CompletionFailure) Name(com.sun.tools.javac.util.Name)

Example 43 with Symbol

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

the class DescendantOf method matches.

@Override
public boolean matches(ExpressionTree expressionTree, VisitorState state) {
    Symbol sym = ASTHelpers.getSymbol(expressionTree);
    if (sym == null) {
        return false;
    }
    if (!(sym instanceof MethodSymbol)) {
        throw new IllegalArgumentException("DescendantOf matcher expects a method call but found " + sym.getClass() + ". Expression: " + expressionTree);
    }
    if (sym.isStatic()) {
        return false;
    }
    if (methodName.equals(sym.toString())) {
        Type accessedReferenceType = sym.owner.type;
        Type collectionType = state.getTypeFromString(fullClassName);
        if (collectionType != null) {
            return state.getTypes().isSubtype(accessedReferenceType, state.getTypes().erasure(collectionType));
        }
    }
    return false;
}
Also used : Type(com.sun.tools.javac.code.Type) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Symbol(com.sun.tools.javac.code.Symbol)

Example 44 with Symbol

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

the class Matchers method selectedIsInstance.

/**
   * Returns true if the expression is a member access on an instance, rather than a static type.
   * Supports member method invocations and field accesses.
   */
public static Matcher<ExpressionTree> selectedIsInstance() {
    return new Matcher<ExpressionTree>() {

        @Override
        public boolean matches(ExpressionTree expr, VisitorState state) {
            if (!(expr instanceof JCFieldAccess)) {
                // TODO(cushon): throw IllegalArgumentException?
                return false;
            }
            JCExpression selected = ((JCFieldAccess) expr).getExpression();
            if (selected instanceof JCNewClass) {
                return true;
            }
            Symbol sym = ASTHelpers.getSymbol(selected);
            return sym instanceof VarSymbol;
        }
    };
}
Also used : JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) InstanceMethodMatcher(com.google.errorprone.matchers.method.MethodMatchers.InstanceMethodMatcher) AnyMethodMatcher(com.google.errorprone.matchers.method.MethodMatchers.AnyMethodMatcher) StaticMethodMatcher(com.google.errorprone.matchers.method.MethodMatchers.StaticMethodMatcher) ConstructorMatcher(com.google.errorprone.matchers.method.MethodMatchers.ConstructorMatcher) VisitorState(com.google.errorprone.VisitorState) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Symbol(com.sun.tools.javac.code.Symbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) ExpressionTree(com.sun.source.tree.ExpressionTree) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 45 with Symbol

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

the class StrictJavaDepsPlugin method isAnnotationProcessorExempt.

/**
   * Returns true if the compilation unit contains a single top-level class generated by an exempt
   * annotation processor (according to its {@link @Generated} annotation).
   *
   * <p>Annotation processors are expected to never generate more than one top level class, as
   * required by the style guide.
   */
public ProcessorDependencyMode isAnnotationProcessorExempt(JCTree.JCCompilationUnit unit) {
    if (unit.getTypeDecls().size() != 1) {
        return ProcessorDependencyMode.DEFAULT;
    }
    Symbol sym = TreeInfo.symbolFor(getOnlyElement(unit.getTypeDecls()));
    if (sym == null) {
        return ProcessorDependencyMode.DEFAULT;
    }
    Generated generated = sym.getAnnotation(Generated.class);
    if (generated == null) {
        return ProcessorDependencyMode.DEFAULT;
    }
    for (String value : generated.value()) {
        // Relax strict deps for dagger-generated code (b/17979436).
        if (value.startsWith(DAGGER_PROCESSOR_PREFIX)) {
            return ProcessorDependencyMode.EXEMPT_NORECORD;
        }
        if (dependencyModule.getExemptGenerators().contains(value)) {
            return ProcessorDependencyMode.EXEMPT_RECORD;
        }
    }
    return ProcessorDependencyMode.DEFAULT;
}
Also used : Generated(javax.annotation.Generated) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Symbol(com.sun.tools.javac.code.Symbol)

Aggregations

Symbol (com.sun.tools.javac.code.Symbol)195 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)56 Type (com.sun.tools.javac.code.Type)54 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)53 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)45 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)36 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)29 JCTree (com.sun.tools.javac.tree.JCTree)28 ClassType (com.sun.tools.javac.code.Type.ClassType)18 Tree (com.sun.source.tree.Tree)17 ExpressionTree (com.sun.source.tree.ExpressionTree)15 DynamicMethodSymbol (com.sun.tools.javac.code.Symbol.DynamicMethodSymbol)15 OperatorSymbol (com.sun.tools.javac.code.Symbol.OperatorSymbol)15 ClassTree (com.sun.source.tree.ClassTree)14 MethodTree (com.sun.source.tree.MethodTree)14 Name (com.sun.tools.javac.util.Name)14 IdentifierTree (com.sun.source.tree.IdentifierTree)13 ArrayType (com.sun.tools.javac.code.Type.ArrayType)12 MethodType (com.sun.tools.javac.code.Type.MethodType)12 UnionClassType (com.sun.tools.javac.code.Type.UnionClassType)12