Search in sources :

Example 1 with CapturedType

use of com.sun.tools.javac.code.Type.CapturedType in project lombok by rzwitserloot.

the class JavacResolution method typeToJCTree0.

private static JCExpression typeToJCTree0(Type type, JavacAST ast, boolean allowCompound, boolean allowVoid) throws TypeNotConvertibleException {
    // NB: There's such a thing as maker.Type(type), but this doesn't work very well; it screws up anonymous classes, captures, and adds an extra prefix dot for some reason too.
    //  -- so we write our own take on that here.
    JavacTreeMaker maker = ast.getTreeMaker();
    if (CTC_BOT.equals(typeTag(type)))
        return createJavaLangObject(ast);
    if (CTC_VOID.equals(typeTag(type)))
        return allowVoid ? primitiveToJCTree(type.getKind(), maker) : createJavaLangObject(ast);
    if (type.isPrimitive())
        return primitiveToJCTree(type.getKind(), maker);
    if (type.isErroneous())
        throw new TypeNotConvertibleException("Type cannot be resolved");
    TypeSymbol symbol = type.asElement();
    List<Type> generics = type.getTypeArguments();
    JCExpression replacement = null;
    if (symbol == null)
        throw new TypeNotConvertibleException("Null or compound type");
    if (symbol.name.length() == 0) {
        // Anonymous inner class
        if (type instanceof ClassType) {
            List<Type> ifaces = ((ClassType) type).interfaces_field;
            Type supertype = ((ClassType) type).supertype_field;
            if (ifaces != null && ifaces.length() == 1) {
                return typeToJCTree(ifaces.get(0), ast, allowCompound, allowVoid);
            }
            if (supertype != null)
                return typeToJCTree(supertype, ast, allowCompound, allowVoid);
        }
        throw new TypeNotConvertibleException("Anonymous inner class");
    }
    if (type instanceof CapturedType || type instanceof WildcardType) {
        Type lower, upper;
        if (type instanceof WildcardType) {
            upper = ((WildcardType) type).getExtendsBound();
            lower = ((WildcardType) type).getSuperBound();
        } else {
            lower = type.getLowerBound();
            upper = type.getUpperBound();
        }
        if (allowCompound) {
            if (lower == null || CTC_BOT.equals(typeTag(lower))) {
                if (upper == null || upper.toString().equals("java.lang.Object")) {
                    return maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null);
                }
                if (upper.getTypeArguments().contains(type)) {
                    return maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null);
                }
                return maker.Wildcard(maker.TypeBoundKind(BoundKind.EXTENDS), typeToJCTree(upper, ast, false, false));
            } else {
                return maker.Wildcard(maker.TypeBoundKind(BoundKind.SUPER), typeToJCTree(lower, ast, false, false));
            }
        }
        if (upper != null) {
            if (upper.getTypeArguments().contains(type)) {
                return maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null);
            }
            return typeToJCTree(upper, ast, allowCompound, allowVoid);
        }
        return createJavaLangObject(ast);
    }
    String qName;
    if (symbol.isLocal()) {
        qName = symbol.getSimpleName().toString();
    } else if (symbol.type != null && symbol.type.getEnclosingType() != null && typeTag(symbol.type.getEnclosingType()).equals(typeTag("CLASS"))) {
        replacement = typeToJCTree0(type.getEnclosingType(), ast, false, false);
        qName = symbol.getSimpleName().toString();
    } else {
        qName = symbol.getQualifiedName().toString();
    }
    if (qName.isEmpty())
        throw new TypeNotConvertibleException("unknown type");
    if (qName.startsWith("<"))
        throw new TypeNotConvertibleException(qName);
    String[] baseNames = qName.split("\\.");
    int i = 0;
    if (replacement == null) {
        replacement = maker.Ident(ast.toName(baseNames[0]));
        i = 1;
    }
    for (; i < baseNames.length; i++) {
        replacement = maker.Select(replacement, ast.toName(baseNames[i]));
    }
    return genericsToJCTreeNodes(generics, ast, replacement);
}
Also used : ClassType(com.sun.tools.javac.code.Type.ClassType) CapturedType(com.sun.tools.javac.code.Type.CapturedType) ArrayType(com.sun.tools.javac.code.Type.ArrayType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Type(com.sun.tools.javac.code.Type) WildcardType(com.sun.tools.javac.code.Type.WildcardType) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) CapturedType(com.sun.tools.javac.code.Type.CapturedType) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) ClassType(com.sun.tools.javac.code.Type.ClassType)

Aggregations

TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)1 Type (com.sun.tools.javac.code.Type)1 ArrayType (com.sun.tools.javac.code.Type.ArrayType)1 CapturedType (com.sun.tools.javac.code.Type.CapturedType)1 ClassType (com.sun.tools.javac.code.Type.ClassType)1 WildcardType (com.sun.tools.javac.code.Type.WildcardType)1 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)1