Search in sources :

Example 6 with WildcardType

use of javax.lang.model.type.WildcardType in project checker-framework by typetools.

the class TypesUtils method leastUpperBound.

/**
 * Returns the least upper bound of two {@link TypeMirror}s, ignoring any annotations on the
 * types.
 *
 * <p>Wrapper around Types.lub to add special handling for null types, primitives, and
 * wildcards.
 *
 * @param tm1 a {@link TypeMirror}
 * @param tm2 a {@link TypeMirror}
 * @param processingEnv the {@link ProcessingEnvironment} to use
 * @return the least upper bound of {@code tm1} and {@code tm2}.
 */
public static TypeMirror leastUpperBound(TypeMirror tm1, TypeMirror tm2, ProcessingEnvironment processingEnv) {
    Type t1 = TypeAnnotationUtils.unannotatedType(tm1);
    Type t2 = TypeAnnotationUtils.unannotatedType(tm2);
    JavacProcessingEnvironment javacEnv = (JavacProcessingEnvironment) processingEnv;
    com.sun.tools.javac.code.Types types = com.sun.tools.javac.code.Types.instance(javacEnv.getContext());
    if (types.isSameType(t1, t2)) {
        // Special case if the two types are equal.
        return t1;
    }
    // Handle the 'null' type manually (not done by types.lub).
    if (t1.getKind() == TypeKind.NULL) {
        return t2;
    }
    if (t2.getKind() == TypeKind.NULL) {
        return t1;
    }
    if (t1.getKind() == TypeKind.WILDCARD) {
        WildcardType wc1 = (WildcardType) t1;
        Type bound = (Type) wc1.getExtendsBound();
        if (bound == null) {
            // Implicit upper bound of java.lang.Object
            Elements elements = processingEnv.getElementUtils();
            return elements.getTypeElement("java.lang.Object").asType();
        }
        t1 = bound;
    }
    if (t2.getKind() == TypeKind.WILDCARD) {
        WildcardType wc2 = (WildcardType) t2;
        Type bound = (Type) wc2.getExtendsBound();
        if (bound == null) {
            // Implicit upper bound of java.lang.Object
            Elements elements = processingEnv.getElementUtils();
            return elements.getTypeElement("java.lang.Object").asType();
        }
        t2 = bound;
    }
    // Special case for primitives.
    if (isPrimitive(t1) || isPrimitive(t2)) {
        if (types.isAssignable(t1, t2)) {
            return t2;
        } else if (types.isAssignable(t2, t1)) {
            return t1;
        } else {
            Elements elements = processingEnv.getElementUtils();
            return elements.getTypeElement("java.lang.Object").asType();
        }
    }
    return types.lub(t1, t2);
}
Also used : ArrayType(javax.lang.model.type.ArrayType) CapturedType(com.sun.tools.javac.code.Type.CapturedType) DeclaredType(javax.lang.model.type.DeclaredType) WildcardType(javax.lang.model.type.WildcardType) Type(com.sun.tools.javac.code.Type) WildcardType(javax.lang.model.type.WildcardType) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) Elements(javax.lang.model.util.Elements)

Example 7 with WildcardType

use of javax.lang.model.type.WildcardType in project st-js by st-js.

the class InternalUtils method leastUpperBound.

/**
 * Returns the least upper bound of two {@link javax.lang.model.type.TypeMirror}s.
 *
 * @param processingEnv
 *            The {@link javax.annotation.processing.ProcessingEnvironment} to use.
 * @param tm1
 *            A {@link javax.lang.model.type.TypeMirror}.
 * @param tm2
 *            A {@link javax.lang.model.type.TypeMirror}.
 * @return The least upper bound of {@code tm1} and {@code tm2}.
 */
public static TypeMirror leastUpperBound(ProcessingEnvironment processingEnv, TypeMirror tm1, TypeMirror tm2) {
    Type t1 = (Type) tm1;
    Type t2 = (Type) tm2;
    JavacProcessingEnvironment javacEnv = (JavacProcessingEnvironment) processingEnv;
    Types types = Types.instance(javacEnv.getContext());
    if (types.isSameType(t1, t2)) {
        // Special case if the two types are equal.
        return t1;
    }
    // Handle the 'null' type manually (not done by types.lub).
    if (t1.getKind() == TypeKind.NULL) {
        return t2;
    }
    if (t2.getKind() == TypeKind.NULL) {
        return t1;
    }
    // Special case for primitives.
    if (TypesUtils.isPrimitive(t1) || TypesUtils.isPrimitive(t2)) {
        if (types.isAssignable(t1, t2)) {
            return t2;
        } else if (types.isAssignable(t2, t1)) {
            return t1;
        } else {
            return processingEnv.getTypeUtils().getNoType(TypeKind.NONE);
        }
    }
    if (t1.getKind() == TypeKind.WILDCARD) {
        WildcardType wc1 = (WildcardType) t1;
        Type bound = (Type) wc1.getExtendsBound();
        if (bound == null) {
            // Implicit upper bound of java.lang.Object
            Elements elements = processingEnv.getElementUtils();
            return elements.getTypeElement("java.lang.Object").asType();
        }
        t1 = bound;
    }
    if (t2.getKind() == TypeKind.WILDCARD) {
        WildcardType wc2 = (WildcardType) t2;
        Type bound = (Type) wc2.getExtendsBound();
        if (bound == null) {
            // Implicit upper bound of java.lang.Object
            Elements elements = processingEnv.getElementUtils();
            return elements.getTypeElement("java.lang.Object").asType();
        }
        t2 = bound;
    }
    return types.lub(t1, t2);
}
Also used : Types(com.sun.tools.javac.code.Types) WildcardType(javax.lang.model.type.WildcardType) Type(com.sun.tools.javac.code.Type) WildcardType(javax.lang.model.type.WildcardType) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) Elements(javax.lang.model.util.Elements)

Example 8 with WildcardType

use of javax.lang.model.type.WildcardType in project st-js by st-js.

the class DefaultJavaScriptNameProvider method getTypeName.

/**
 * {@inheritDoc}
 */
@Override
public String getTypeName(GenerationContext<?> context, TypeMirror type, DependencyType dependencyType) {
    TypeInfo typeInfo = resolvedTypes.get(type);
    if (typeInfo != null) {
        // make sure we have the strictest dep type
        addResolvedType(typeInfo.getRootTypeElement(), dependencyType);
        return typeInfo.getFullName();
    }
    if (type instanceof DeclaredType) {
        DeclaredType declaredType = (DeclaredType) type;
        String name = InternalUtils.getSimpleName(declaredType.asElement());
        Element rootTypeElement = declaredType.asElement();
        for (DeclaredType enclosingType = JavaNodes.getEnclosingType(declaredType); enclosingType != null; enclosingType = JavaNodes.getEnclosingType(enclosingType)) {
            rootTypeElement = enclosingType.asElement();
            name = InternalUtils.getSimpleName(rootTypeElement) + "." + name;
        }
        checkAllowedType(rootTypeElement, context);
        addResolvedType(rootTypeElement, dependencyType);
        String fullName = addNameSpace(rootTypeElement, context, name);
        resolvedTypes.put(type, new TypeInfo(fullName, rootTypeElement));
        return fullName;
    }
    if (type instanceof ArrayType) {
        return handleArrayType((ArrayType) type);
    }
    if (type instanceof WildcardType) {
        // XXX what to return here !?
        return "Object";
    }
    return type.toString();
}
Also used : ArrayType(javax.lang.model.type.ArrayType) WildcardType(javax.lang.model.type.WildcardType) Element(javax.lang.model.element.Element) DeclaredType(javax.lang.model.type.DeclaredType)

Example 9 with WildcardType

use of javax.lang.model.type.WildcardType in project graal by oracle.

the class ElementUtils method getTypeId.

public static String getTypeId(TypeMirror mirror) {
    switch(mirror.getKind()) {
        case BOOLEAN:
            return "Boolean";
        case BYTE:
            return "Byte";
        case CHAR:
            return "Char";
        case DOUBLE:
            return "Double";
        case FLOAT:
            return "Float";
        case SHORT:
            return "Short";
        case INT:
            return "Int";
        case LONG:
            return "Long";
        case DECLARED:
            return fixECJBinaryNameIssue(((DeclaredType) mirror).asElement().getSimpleName().toString());
        case ARRAY:
            return getTypeId(((ArrayType) mirror).getComponentType()) + "Array";
        case VOID:
            return "Void";
        case NULL:
            return "Null";
        case WILDCARD:
            StringBuilder b = new StringBuilder();
            WildcardType type = (WildcardType) mirror;
            if (type.getExtendsBound() != null) {
                b.append("Extends").append(getTypeId(type.getExtendsBound()));
            } else if (type.getSuperBound() != null) {
                b.append("Super").append(getTypeId(type.getExtendsBound()));
            }
            return b.toString();
        case TYPEVAR:
            return "Any";
        case ERROR:
            throw new CompileErrorException("Type error " + mirror);
        default:
            throw new RuntimeException("Unknown type specified " + mirror.getKind() + " mirror: " + mirror);
    }
}
Also used : ArrayType(javax.lang.model.type.ArrayType) WildcardType(javax.lang.model.type.WildcardType) CompileErrorException(com.oracle.truffle.dsl.processor.CompileErrorException)

Example 10 with WildcardType

use of javax.lang.model.type.WildcardType in project narchy by automenta.

the class PJAnnotationProcessor method init.

/*
     * unused
    private boolean is(TypeMirror _this, DeclaredType _that) {
        DeclaredType dt = (DeclaredType)_this;
        if (dt.asElement().equals(_that.asElement())) {        
            return !env.getTypeUtils().isSameType(_this,termType);            
        }
        else {
            return false;
        }
    }
    */
/*
     * unused
    private boolean contains(TypeMirror containing, DeclaredType contained) {
        if (containing instanceof DeclaredType) {  
            DeclaredType dt = (DeclaredType)containing;
            if (is(dt,contained)) {
                return true;
            }
            for (TypeMirror t : dt.getTypeArguments()) {
                if (contains(t,contained)) {
                    return true;
                }
            }
        }
        else if (containing instanceof WildcardType) {
            WildcardType wt = (WildcardType)containing;
            if (wt.getExtendsBound()!=null) {
                return contains(wt.getExtendsBound(),contained); 
            }
            else if (wt.getSuperBound()!=null) {
                return contains(wt.getSuperBound(),contained); 
            }
            else {
                return false;
            }
        }
        return false;        
    }
    */
@Override
public void init(ProcessingEnvironment processingEnv) {
    env = processingEnv;
    // teCompound = env.getElementUtils().getTypeElement("alice.tuprologx.pj.model.Compound");
    // teCompound1 = env.getElementUtils().getTypeElement("alice.tuprologx.pj.model.Compound1");
    teCompound2 = env.getElementUtils().getTypeElement("alice.tuprologx.pj.model.Compound2");
    teCons = env.getElementUtils().getTypeElement("alice.tuprologx.pj.model.Cons");
    teNil = env.getElementUtils().getTypeElement("alice.tuprologx.pj.model.Nil");
    teTerm = env.getElementUtils().getTypeElement("alice.tuprologx.pj.model.Term");
    teVar = env.getElementUtils().getTypeElement("alice.tuprologx.pj.model.Var");
    teBool = env.getElementUtils().getTypeElement("java.lang.Boolean");
    // teBool = env.getElementUtils().getTypeElement("java.lang.Boolean");
    teIterable = env.getElementUtils().getTypeElement("java.lang.Iterable");
    teJ2PException = env.getElementUtils().getTypeElement("alice.tuprologx.pj.engine.NoSolutionException");
    WildcardType wt = env.getTypeUtils().getWildcardType(null, null);
    // compoundType = env.getTypeUtils().getDeclaredType(teCompound,wt);
    // compound1Type = env.getTypeUtils().getDeclaredType(teCompound1,wt);
    // compound2Type = env.getTypeUtils().getDeclaredType(teCompound2,wt,wt);
    // consType = env.getTypeUtils().getDeclaredType(teCons,wt,wt);
    nilType = env.getTypeUtils().getDeclaredType(teNil);
    // termType = env.getTypeUtils().getDeclaredType(teTerm,wt);
    // varType = env.getTypeUtils().getDeclaredType(teVar,wt);
    boolType = env.getTypeUtils().getDeclaredType(teBool);
    // boolType = env.getTypeUtils().getPrimitiveType(TypeKind.BOOLEAN);
    iterableType = env.getTypeUtils().getDeclaredType(teIterable, wt);
    j2PExceptionType = env.getTypeUtils().getDeclaredType(teJ2PException);
}
Also used : WildcardType(javax.lang.model.type.WildcardType)

Aggregations

WildcardType (javax.lang.model.type.WildcardType)28 TypeMirror (javax.lang.model.type.TypeMirror)12 Test (org.junit.Test)9 DeclaredType (javax.lang.model.type.DeclaredType)8 Elements (javax.lang.model.util.Elements)7 TypeVariable (javax.lang.model.type.TypeVariable)6 ArrayType (javax.lang.model.type.ArrayType)5 Types (javax.lang.model.util.Types)5 Type (com.sun.tools.javac.code.Type)3 JavacProcessingEnvironment (com.sun.tools.javac.processing.JavacProcessingEnvironment)3 TypeElement (javax.lang.model.element.TypeElement)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 Types (com.sun.tools.javac.code.Types)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 PrimitiveType (javax.lang.model.type.PrimitiveType)2 TypeKind (javax.lang.model.type.TypeKind)2 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)2 AnnotatedWildcardType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType)2 MoreElements (com.google.auto.common.MoreElements)1