Java Code Examples for javax.lang.model.util.Types#getWildcardType()

The following examples show how to use javax.lang.model.util.Types#getWildcardType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SortedSetProperty.java    From FreeBuilder with Apache License 2.0 5 votes vote down vote up
private static TypeMirror wildcardSuperSortedSet(
    TypeMirror elementType,
    Elements elements,
    Types types) {
  TypeElement setType = elements.getTypeElement(SortedSet.class.getName());
  return types.getWildcardType(null, types.getDeclaredType(setType, elementType));
}
 
Example 2
Source File: ListProperty.java    From FreeBuilder with Apache License 2.0 5 votes vote down vote up
/**
 * Returns {@code ? super List<elementType>}.
 */
private static TypeMirror wildcardSuperList(
    TypeMirror elementType,
    Elements elements,
    Types types) {
  TypeElement listType = elements.getTypeElement(List.class.getName());
  return types.getWildcardType(null, types.getDeclaredType(listType, elementType));
}
 
Example 3
Source File: SetProperty.java    From FreeBuilder with Apache License 2.0 5 votes vote down vote up
private static TypeMirror wildcardSuperSet(
    TypeMirror elementType,
    Elements elements,
    Types types) {
  TypeElement setType = elements.getTypeElement(Set.class.getName());
  return types.getWildcardType(null, types.getDeclaredType(setType, elementType));
}
 
Example 4
Source File: MapProperty.java    From FreeBuilder with Apache License 2.0 5 votes vote down vote up
private static TypeMirror wildcardSuperMap(
    TypeMirror keyType,
    TypeMirror valueType,
    Elements elements,
    Types types) {
  TypeElement mapType = elements.getTypeElement(Map.class.getName());
  return types.getWildcardType(null, types.getDeclaredType(mapType, keyType, valueType));
}
 
Example 5
Source File: TypeMirrors.java    From FreeBuilder with Apache License 2.0 5 votes vote down vote up
/** Returns a {@link TypeMirror} for the given type. */
public static TypeMirror typeMirror(Types typeUtils, Elements elementUtils, Type type) {
  if (type instanceof Class) {
    return typeMirror(typeUtils, elementUtils, (Class<?>) type);
  } else if (type instanceof GenericArrayType) {
    Type componentType = ((GenericArrayType) type).getGenericComponentType();
    return typeUtils.getArrayType(typeMirror(typeUtils, elementUtils, componentType));
  } else if (type instanceof ParameterizedType) {
    ParameterizedType pType = (ParameterizedType) type;
    DeclaredType rawType = (DeclaredType) typeMirror(typeUtils, elementUtils, pType.getRawType());
    List<TypeMirror> typeArgumentMirrors = new ArrayList<>();
    for (Type typeArgument : pType.getActualTypeArguments()) {
      typeArgumentMirrors.add(typeMirror(typeUtils, elementUtils, typeArgument));
    }
    DeclaredType owner = (DeclaredType) typeMirror(typeUtils, elementUtils, pType.getOwnerType());
    return typeUtils.getDeclaredType(
        owner,
        (TypeElement) rawType.asElement(),
        typeArgumentMirrors.toArray(new TypeMirror[typeArgumentMirrors.size()]));
  } else if (type instanceof WildcardType) {
    Type lowerBound = getOnlyType(((WildcardType) type).getLowerBounds());
    Type upperBound = getOnlyType(((WildcardType) type).getUpperBounds());
    if (Object.class.equals(upperBound)) {
      upperBound = null;
    }
    return typeUtils.getWildcardType(
        typeMirror(typeUtils, elementUtils, upperBound),
        typeMirror(typeUtils, elementUtils, lowerBound));
  } else if (type == null) {
    return null;
  } else if (type instanceof TypeVariable) {
    throw new IllegalArgumentException("Type variables not supported");
  } else {
    throw new IllegalArgumentException("Unrecognized Type subclass " + type.getClass());
  }
}
 
Example 6
Source File: AbstractTypesTest.java    From javapoet with Apache License 2.0 5 votes vote down vote up
@Test public void wildcardMirrorExtendsType() throws Exception {
  Types types = getTypes();
  Elements elements = getElements();
  TypeMirror charSequence = elements.getTypeElement(CharSequence.class.getName()).asType();
  WildcardType wildcard = types.getWildcardType(charSequence, null);
  TypeName type = TypeName.get(wildcard);
  assertThat(type.toString()).isEqualTo("? extends java.lang.CharSequence");
}
 
Example 7
Source File: AbstractTypesTest.java    From javapoet with Apache License 2.0 5 votes vote down vote up
@Test public void wildcardMirrorSuperType() throws Exception {
  Types types = getTypes();
  Elements elements = getElements();
  TypeMirror string = elements.getTypeElement(String.class.getName()).asType();
  WildcardType wildcard = types.getWildcardType(null, string);
  TypeName type = TypeName.get(wildcard);
  assertThat(type.toString()).isEqualTo("? super java.lang.String");
}
 
Example 8
Source File: BindingFieldHelper.java    From AutoBundle with Apache License 2.0 4 votes vote down vote up
static String getOperationName(TypeName target, Elements elements, Types types) {
    if (fieldTypes.containsKey(target)) {
        return fieldTypes.get(target);
    }
    if (target.isBoxedPrimitive()) {
        TypeName unboxed = target.unbox();
        if (fieldTypes.containsKey(unboxed)) {
            return fieldTypes.get(unboxed);
        }
    }

    // Array
    TypeMirror parcelable = elements.getTypeElement("android.os.Parcelable").asType();
    if (target.toString().endsWith("[]")) {
        String removed = target.toString().substring(0, target.toString().length() - 2);
        for (TypeName arrayType : fieldArrayTypes.keySet()) {
            if (removed.equals(arrayType.toString())) {
                return fieldArrayTypes.get(arrayType);
            }
        }
        // Parcelable[]
        TypeElement element = elements.getTypeElement(removed);
        if (element != null && types.isAssignable(element.asType(), parcelable)) {
            return "ParcelableArray";
        }
    }

    String[] splits = detectTypeArgument(target.toString());
    TypeMirror targetType;
    if (splits.length == 1) {
        targetType = elements.getTypeElement(target.toString()).asType();
    } else {
        TypeElement genericType = elements.getTypeElement(splits[0]);
        TypeMirror argType = elements.getTypeElement(splits[1]).asType();
        targetType = types.getDeclaredType(genericType, argType);
    }

    // Parcelable
    if (types.isAssignable(targetType, parcelable)) {
        return "Parcelable";
    }

    // ArrayList<? extend Parcelable>
    TypeElement arrayList = elements.getTypeElement(ArrayList.class.getName());
    TypeMirror wildCardParcelable = types.getWildcardType(parcelable, null);
    TypeMirror parcelableArrayList = types.getDeclaredType(arrayList, wildCardParcelable);
    if (types.isAssignable(targetType, parcelableArrayList)) {
        return "ParcelableArrayList";
    }

    // SparseArray<? extend Parcelable>
    TypeElement sparseArray = elements.getTypeElement("android.util.SparseArray");
    TypeMirror sparceParcelableArray = types.getDeclaredType(sparseArray, wildCardParcelable);
    if (types.isAssignable(targetType, sparceParcelableArray)) {
        return "SparseParcelableArray";
    }

    // IBinder
    TypeMirror iBinder = elements.getTypeElement("android.os.IBinder").asType();
    if (types.isAssignable(targetType, iBinder)) {
        return "Binder";
    }

    // Serializable
    TypeMirror serializable = elements.getTypeElement(Serializable.class.getName()).asType();
    if (types.isAssignable(targetType, serializable)) {
        return "Serializable";
    }

    return null;
}