use of com.sun.tools.javac.code.Symbol.CompletionFailure in project ceylon-compiler by ceylon.
the class Attr method attribTopLevel.
/**
* Attribute a top level tree. These trees are encountered when the
* package declaration has annotations.
*/
public void attribTopLevel(Env<AttrContext> env) {
JCCompilationUnit toplevel = env.toplevel;
try {
annotate.flush();
chk.validateAnnotations(toplevel.packageAnnotations, toplevel.packge);
} catch (CompletionFailure ex) {
chk.completionError(toplevel.pos(), ex);
}
}
use of com.sun.tools.javac.code.Symbol.CompletionFailure in project ceylon-compiler by ceylon.
the class CeylonModelLoader method lookupNewClassMirror.
private ClassMirror lookupNewClassMirror(String name) {
ClassSymbol classSymbol = null;
String outerName = name;
/*
* This madness here tries to look for a class, and if it fails, tries to resolve it
* from its parent class. This is required because a.b.C.D (where D is an inner class
* of C) is not found in symtab.classes but in C's ClassSymbol.enclosedElements.
*/
// make sure we load the class file, since we no longer complete packages unless we absolutely must
loadClass(outerName);
do {
// we must first try with no postfix, because we can have a valid class foo.bar in Java,
// when in Ceylon it would be foo.bar_
classSymbol = symtab.classes.get(names.fromString(Util.quoteJavaKeywords(outerName)));
// try again with a postfix "_"
if (classSymbol == null && lastPartHasLowerInitial(outerName) && !outerName.endsWith("_")) {
classSymbol = symtab.classes.get(names.fromString(Util.quoteJavaKeywords(outerName + "_")));
}
if (classSymbol != null) {
// if we got a source symbol for something non-Java it's a slipery slope
if (Util.isLoadedFromSource(classSymbol) && !Util.isJavaSource(classSymbol))
return null;
if (outerName.length() != name.length()) {
try {
classSymbol = lookupInnerClass(classSymbol, name.substring(outerName.length() + 1).split("\\."));
} catch (CompletionFailure x) {
// something wrong, we will report it properly elsewhere
classSymbol = null;
}
}
if (classSymbol != null && classSymbol.classfile == null && classSymbol.sourcefile == null) {
// try to complete it if that changes anything
try {
classSymbol.complete();
} catch (CompletionFailure x) {
// if we can't complete it it doesn't exist, its classfile will remain null
}
if (classSymbol.classfile == null) {
PackageSymbol pkg = classSymbol.packge();
// do not log an error for missing oracle jdk stuff
if (pkg == null || !JDKUtils.isOracleJDKAnyPackage(pkg.getQualifiedName().toString())) {
// do not log an error because it will be logged elsewhere
logVerbose("Unable to find required class file for " + name);
}
return null;
}
}
return classSymbol != null ? new JavacClass(classSymbol) : null;
}
int lastDot = outerName.lastIndexOf(".");
if (lastDot == -1 || lastDot == 0)
return null;
outerName = outerName.substring(0, lastDot);
} while (classSymbol == null);
return null;
}
use of com.sun.tools.javac.code.Symbol.CompletionFailure in project ceylon-compiler by ceylon.
the class T6400303 method main.
public static void main(String... args) {
javax.tools.JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
JavacTaskImpl task = (JavacTaskImpl) tool.getTask(null, null, null, null, null, null);
JavaCompiler compiler = JavaCompiler.instance(task.getContext());
try {
compiler.resolveIdent("Test$1").complete();
} catch (CompletionFailure ex) {
System.err.println("Got expected completion failure: " + ex.getLocalizedMessage());
return;
}
throw new AssertionError("No error reported");
}
use of com.sun.tools.javac.code.Symbol.CompletionFailure in project error-prone by google.
the class VisitorState method getTypeFromStringInternal.
private Type getTypeFromStringInternal(String typeStr) {
validateTypeStr(typeStr);
if (isPrimitiveType(typeStr)) {
return getPrimitiveType(typeStr);
}
Name typeName = getName(typeStr);
try {
ClassSymbol typeSymbol = getSymtab().classes.get(typeName);
if (typeSymbol == null) {
JavaCompiler compiler = JavaCompiler.instance(context);
Symbol sym = compiler.resolveIdent(typeStr);
if (!(sym instanceof ClassSymbol)) {
return null;
}
typeSymbol = (ClassSymbol) sym;
}
Type type = typeSymbol.asType();
// Throws CompletionFailure if the source/class file for this type is not available.
// This is hacky but the best way I can think of to handle this case.
type.complete();
if (type.isErroneous()) {
return null;
}
return type;
} catch (CompletionFailure failure) {
return null;
}
}
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