use of com.sun.tools.javac.code.Symbol.CompletionFailure in project error-prone by google.
the class ErrorProneAnalyzer method finished.
@Override
public void finished(TaskEvent taskEvent) {
if (taskEvent.getKind() != Kind.ANALYZE) {
return;
}
if (JavaCompiler.instance(context).errorCount() > 0) {
return;
}
TreePath path = JavacTrees.instance(context).getPath(taskEvent.getTypeElement());
if (path == null) {
path = new TreePath(taskEvent.getCompilationUnit());
}
// Assert that the event is unique and scan the current tree.
verify(seen.add(path.getLeaf()), "Duplicate FLOW event for: %s", taskEvent.getTypeElement());
Context subContext = new SubContext(context);
subContext.put(ErrorProneOptions.class, errorProneOptions);
Log log = Log.instance(context);
JCCompilationUnit compilation = (JCCompilationUnit) path.getCompilationUnit();
DescriptionListener descriptionListener = descriptionListenerFactory.getDescriptionListener(log, compilation);
try {
if (path.getLeaf().getKind() == Tree.Kind.COMPILATION_UNIT) {
// We only get TaskEvents for compilation units if they contain no package declarations
// (e.g. package-info.java files). In this case it's safe to analyze the
// CompilationUnitTree immediately.
transformer.get().apply(path, subContext, descriptionListener);
} else if (finishedCompilation(path.getCompilationUnit())) {
// Otherwise this TaskEvent is for a ClassTree, and we can scan the whole
// CompilationUnitTree once we've seen all the enclosed classes.
transformer.get().apply(new TreePath(compilation), subContext, descriptionListener);
}
} catch (ErrorProneError e) {
e.logFatalError(log);
// terminate with Result.ABNORMAL
throw e;
} catch (CompletionFailure e) {
// A CompletionFailure can be triggered when error-prone tries to complete a symbol
// that isn't on the compilation classpath. This can occur when a check performs an
// instanceof test on a symbol, which requires inspecting the transitive closure of the
// symbol's supertypes. If javac didn't need to check the symbol's assignability
// then a normal compilation would have succeeded, and no diagnostics will have been
// reported yet, but we don't want to crash javac.
log.error("proc.cant.access", e.sym, e.getDetailValue(), Throwables.getStackTraceAsString(e));
}
}
use of com.sun.tools.javac.code.Symbol.CompletionFailure 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.CompletionFailure in project ceylon-compiler by ceylon.
the class JavacUtil method getTypeParameters.
public static List<TypeParameterMirror> getTypeParameters(Symbol symbol) {
try {
com.sun.tools.javac.util.List<TypeSymbol> typeParameters = symbol.getTypeParameters();
List<TypeParameterMirror> ret = new ArrayList<TypeParameterMirror>(typeParameters.size());
for (TypeSymbol typeParameter : typeParameters) ret.add(new JavacTypeParameter(typeParameter));
return ret;
} catch (CompletionFailure x) {
throw new ModelResolutionException("Failed to load type parameters", x);
}
}
use of com.sun.tools.javac.code.Symbol.CompletionFailure in project ceylon-compiler by ceylon.
the class CeylonModelLoader method isOverloadingMethod.
/**
* Returns true if the given method is overloading an inherited method (from super class or interfaces).
*/
private boolean isOverloadingMethod(final MethodSymbol method) {
/*
* Copied from getOverriddenMethod and adapted for overloading
*/
try {
// interfaces have a different way to work
if (method.owner.isInterface())
return overloaded(method, method.owner.type.tsym, types);
// so we stop there for it, especially since it does not have any overloading
if (method.owner.type.tsym.getQualifiedName().toString().equals("ceylon.language.Exception"))
return false;
for (Type superType = types.supertype(method.owner.type); superType.tsym != null; superType = types.supertype(superType)) {
TypeSymbol i = superType.tsym;
String fqn = i.getQualifiedName().toString();
// never go above this type since it has no supertype in Ceylon (does in Java though)
if (fqn.equals("ceylon.language.Anything"))
break;
try {
for (Entry e = i.members().lookup(method.name); e.scope != null; e = e.next()) {
// ignore some methods
if (isIgnored(e.sym))
continue;
if (!method.overrides(e.sym, (TypeSymbol) method.owner, types, false)) {
return true;
}
}
// try in the interfaces
if (overloaded(method, i, types))
return true;
} catch (Symbol.CompletionFailure x) {
// just ignore unresolved interfaces, error will be logged when we try to add it
}
// so we stop there for it, especially since it does not have any overloading
if (fqn.equals("ceylon.language.Exception"))
break;
}
// try in the interfaces
if (overloaded(method, method.owner.type.tsym, types))
return true;
return false;
} catch (CompletionFailure x) {
handleCompletionFailure(method, x);
return false;
}
}
use of com.sun.tools.javac.code.Symbol.CompletionFailure in project error-prone by google.
the class ASTHelpers method hasAnnotation.
/**
* Determines whether a symbol has an annotation of the given type. This includes annotations
* inherited from superclasses due to {@code @Inherited}.
*
* @param annotationClass the binary class name of the annotation (e.g.
* "javax.annotation.Nullable", or "some.package.OuterClassName$InnerClassName")
*/
public static boolean hasAnnotation(Symbol sym, String annotationClass, VisitorState state) {
Name annotationName = state.getName(annotationClass);
Symbol annotationSym;
synchronized (state.context) {
annotationSym = state.getSymtab().enterClass(annotationName);
}
try {
annotationSym.complete();
} catch (CompletionFailure e) {
// @Inherited won't work if the annotation isn't on the classpath, but we can still check
// if it's present directly
}
Symbol inheritedSym = state.getSymtab().inheritedType.tsym;
if ((sym == null) || (annotationSym == null)) {
return false;
}
if ((sym instanceof ClassSymbol) && (annotationSym.attribute(inheritedSym) != null)) {
while (sym != null) {
if (sym.attribute(annotationSym) != null) {
return true;
}
sym = ((ClassSymbol) sym).getSuperclass().tsym;
}
return false;
} else {
return sym.attribute(annotationSym) != null;
}
}
Aggregations