Search in sources :

Example 1 with CompletionFailure

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

Example 2 with CompletionFailure

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;
    }
}
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 3 with CompletionFailure

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);
    }
}
Also used : ModelResolutionException(com.redhat.ceylon.model.loader.ModelResolutionException) TypeParameterMirror(com.redhat.ceylon.model.loader.mirror.TypeParameterMirror) CompletionFailure(com.sun.tools.javac.code.Symbol.CompletionFailure) ArrayList(java.util.ArrayList) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol)

Example 4 with CompletionFailure

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;
    }
}
Also used : Type(com.sun.tools.javac.code.Type) UnknownType(com.redhat.ceylon.model.typechecker.model.UnknownType) Entry(com.sun.tools.javac.code.Scope.Entry) CompletionFailure(com.sun.tools.javac.code.Symbol.CompletionFailure) 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) CompletionFailure(com.sun.tools.javac.code.Symbol.CompletionFailure) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol)

Example 5 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