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);
}
}
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;
}
}
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;
}
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;
}
};
}
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;
}
Aggregations