Search in sources :

Example 1 with JavacClass

use of org.eclipse.ceylon.compiler.java.loader.mirror.JavacClass in project ceylon by eclipse.

the class CeylonModelLoader method getFunctionalInterfaceType.

@Override
protected FunctionalInterfaceType getFunctionalInterfaceType(TypeMirror typeMirror) throws ModelResolutionException {
    if (typeMirror.getKind() != TypeKind.DECLARED)
        throw new ModelResolutionException("Failed to find functional interface type in " + typeMirror);
    // FIXME: possibly apply other optimisations to lighten the lookup cost? see what javac does
    Type type = ((JavacType) typeMirror).type;
    try {
        Type descriptorType = types.findDescriptorType(type);
        // Let's be honest I've no idea what this means, but it happens and Javac seems to refuse it too
        if (descriptorType.hasTag(TypeTag.FORALL))
            throw new ModelResolutionException("Failed to find functional interface type in " + typeMirror);
        MethodType methodDescriptorType = (MethodType) descriptorType;
        MethodSymbol methodSymbol = (MethodSymbol) types.findDescriptorSymbol(type.tsym);
        List<Type> parameterTypes = methodDescriptorType.getParameterTypes();
        ListBuffer<TypeMirror> mirrorParameterTypes = new ListBuffer<>();
        for (int i = 0; i < parameterTypes.size(); i++) {
            Type parameterType = parameterTypes.get(i);
            if (methodSymbol.isVarArgs() && i == parameterTypes.size() - 1)
                parameterType = ((ArrayType) parameterType).getComponentType();
            mirrorParameterTypes.add(new JavacType(parameterType));
        }
        return new FunctionalInterfaceType(new JavacMethod(new JavacClass(methodSymbol.enclClass()), methodSymbol), new JavacType(methodDescriptorType.getReturnType()), mirrorParameterTypes.toList(), methodSymbol.isVarArgs());
    } catch (Symbol.CompletionFailure err) {
        // bad luck
        throw new ModelResolutionException("Failed to find functional interface type in " + typeMirror, err);
    } catch (FunctionDescriptorLookupError err) {
        throw new ModelResolutionException("Failed to find functional interface type in " + typeMirror, err);
    }
}
Also used : JavacType(org.eclipse.ceylon.compiler.java.loader.mirror.JavacType) MethodType(org.eclipse.ceylon.langtools.tools.javac.code.Type.MethodType) FunctionalInterfaceType(org.eclipse.ceylon.model.loader.mirror.FunctionalInterfaceType) JavacMethod(org.eclipse.ceylon.compiler.java.loader.mirror.JavacMethod) CompletionFailure(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.CompletionFailure) MethodSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.MethodSymbol) TypeSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.TypeSymbol) ClassSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.ClassSymbol) Symbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol) PackageSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.PackageSymbol) ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) ArrayType(org.eclipse.ceylon.langtools.tools.javac.code.Type.ArrayType) ModelResolutionException(org.eclipse.ceylon.model.loader.ModelResolutionException) MethodType(org.eclipse.ceylon.langtools.tools.javac.code.Type.MethodType) Type(org.eclipse.ceylon.langtools.tools.javac.code.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) ArrayType(org.eclipse.ceylon.langtools.tools.javac.code.Type.ArrayType) JavacType(org.eclipse.ceylon.compiler.java.loader.mirror.JavacType) FunctionalInterfaceType(org.eclipse.ceylon.model.loader.mirror.FunctionalInterfaceType) MethodSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.MethodSymbol) TypeMirror(org.eclipse.ceylon.model.loader.mirror.TypeMirror) JavacClass(org.eclipse.ceylon.compiler.java.loader.mirror.JavacClass) FunctionDescriptorLookupError(org.eclipse.ceylon.langtools.tools.javac.code.Types.FunctionDescriptorLookupError)

Example 2 with JavacClass

use of org.eclipse.ceylon.compiler.java.loader.mirror.JavacClass in project ceylon by eclipse.

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 || !jdkProvider.isImplementationSpecificJDKPackage(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(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.PackageSymbol) JavacClass(org.eclipse.ceylon.compiler.java.loader.mirror.JavacClass) ClassSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.ClassSymbol) CompletionFailure(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.CompletionFailure)

Aggregations

JavacClass (org.eclipse.ceylon.compiler.java.loader.mirror.JavacClass)2 ClassSymbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol.ClassSymbol)2 CompletionFailure (org.eclipse.ceylon.langtools.tools.javac.code.Symbol.CompletionFailure)2 PackageSymbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol.PackageSymbol)2 JavacMethod (org.eclipse.ceylon.compiler.java.loader.mirror.JavacMethod)1 JavacType (org.eclipse.ceylon.compiler.java.loader.mirror.JavacType)1 Symbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol)1 MethodSymbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol.MethodSymbol)1 TypeSymbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol.TypeSymbol)1 Type (org.eclipse.ceylon.langtools.tools.javac.code.Type)1 ArrayType (org.eclipse.ceylon.langtools.tools.javac.code.Type.ArrayType)1 MethodType (org.eclipse.ceylon.langtools.tools.javac.code.Type.MethodType)1 FunctionDescriptorLookupError (org.eclipse.ceylon.langtools.tools.javac.code.Types.FunctionDescriptorLookupError)1 ListBuffer (org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer)1 ModelResolutionException (org.eclipse.ceylon.model.loader.ModelResolutionException)1 FunctionalInterfaceType (org.eclipse.ceylon.model.loader.mirror.FunctionalInterfaceType)1 TypeMirror (org.eclipse.ceylon.model.loader.mirror.TypeMirror)1 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)1