Search in sources :

Example 6 with CompletionFailure

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);
    }
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) CompletionFailure(com.sun.tools.javac.code.Symbol.CompletionFailure)

Example 7 with CompletionFailure

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;
}
Also used : PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) JavacClass(com.redhat.ceylon.compiler.java.loader.mirror.JavacClass) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) CompletionFailure(com.sun.tools.javac.code.Symbol.CompletionFailure)

Example 8 with CompletionFailure

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");
}
Also used : JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) CompletionFailure(com.sun.tools.javac.code.Symbol.CompletionFailure) JavaCompiler(com.sun.tools.javac.main.JavaCompiler)

Example 9 with CompletionFailure

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;
    }
}
Also used : ClassType(com.sun.tools.javac.code.Type.ClassType) ArrayType(com.sun.tools.javac.code.Type.ArrayType) Type(com.sun.tools.javac.code.Type) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Symbol(com.sun.tools.javac.code.Symbol) CompletionFailure(com.sun.tools.javac.code.Symbol.CompletionFailure) JavaCompiler(com.sun.tools.javac.main.JavaCompiler) Name(com.sun.tools.javac.util.Name)

Example 10 with CompletionFailure

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;
    }
}
Also used : 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) CompletionFailure(com.sun.tools.javac.code.Symbol.CompletionFailure) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Name(com.sun.tools.javac.util.Name)

Aggregations

CompletionFailure (com.sun.tools.javac.code.Symbol.CompletionFailure)12 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)5 Symbol (com.sun.tools.javac.code.Symbol)4 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)3 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)3 Type (com.sun.tools.javac.code.Type)3 Name (com.sun.tools.javac.util.Name)3 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)2 ArrayType (com.sun.tools.javac.code.Type.ArrayType)2 ClassType (com.sun.tools.javac.code.Type.ClassType)2 JavaCompiler (com.sun.tools.javac.main.JavaCompiler)2 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)2 CeylonClassWriter (com.redhat.ceylon.compiler.java.codegen.CeylonClassWriter)1 CeylonFileObject (com.redhat.ceylon.compiler.java.codegen.CeylonFileObject)1 JavacClass (com.redhat.ceylon.compiler.java.loader.mirror.JavacClass)1 ImportModule (com.redhat.ceylon.compiler.typechecker.tree.Tree.ImportModule)1 ModelResolutionException (com.redhat.ceylon.model.loader.ModelResolutionException)1 TypeParameterMirror (com.redhat.ceylon.model.loader.mirror.TypeParameterMirror)1 Module (com.redhat.ceylon.model.typechecker.model.Module)1 Package (com.redhat.ceylon.model.typechecker.model.Package)1