Search in sources :

Example 11 with TypeMirror

use of javax.lang.model.type.TypeMirror in project buck by facebook.

the class TreeBackedTypeParameterElementTest method testSuperclassBoundedTypeParameter.

@Test
public void testSuperclassBoundedTypeParameter() throws IOException {
    compile("class Foo<T extends java.lang.CharSequence> { }");
    TypeMirror charSequenceType = elements.getTypeElement("java.lang.CharSequence").asType();
    TypeElement fooElement = elements.getTypeElement("Foo");
    List<? extends TypeParameterElement> typeParameters = fooElement.getTypeParameters();
    assertSame(1, typeParameters.size());
    TypeParameterElement typeParam = typeParameters.get(0);
    List<? extends TypeMirror> bounds = typeParam.getBounds();
    assertSame(1, bounds.size());
    assertSameType(charSequenceType, bounds.get(0));
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) TypeParameterElement(javax.lang.model.element.TypeParameterElement) Test(org.junit.Test)

Example 12 with TypeMirror

use of javax.lang.model.type.TypeMirror in project buck by facebook.

the class TreeBackedTypesTest method testIsSameTypeJavacTypeTopLevelNoGenerics.

@Test
public void testIsSameTypeJavacTypeTopLevelNoGenerics() throws IOException {
    initCompiler();
    TypeElement objectElement = elements.getTypeElement("java.lang.Object");
    TypeMirror objectTypeMirror = types.getDeclaredType(objectElement);
    TypeMirror objectTypeMirror2 = types.getDeclaredType(objectElement);
    assertSameType(objectTypeMirror, objectTypeMirror2);
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) Test(org.junit.Test)

Example 13 with TypeMirror

use of javax.lang.model.type.TypeMirror in project RoboBinding by RoboBinding.

the class TypeMirrorWrapperTest method unsupportedTypeMirrors.

@DataPoints("unsupportedTypeMirrors")
public static TypeMirror[] unsupportedTypeMirrors() {
    Types types = compilation.getTypes();
    NullType nullType = types.getNullType();
    NoType noneType = types.getNoType(TypeKind.NONE);
    return new TypeMirror[] { nullType, noneType };
}
Also used : Types(javax.lang.model.util.Types) NoType(javax.lang.model.type.NoType) TypeMirror(javax.lang.model.type.TypeMirror) NullType(javax.lang.model.type.NullType) FromDataPoints(org.junit.experimental.theories.FromDataPoints) DataPoints(org.junit.experimental.theories.DataPoints)

Example 14 with TypeMirror

use of javax.lang.model.type.TypeMirror in project epoxy by airbnb.

the class ProcessorUtils method isSubtypeOfType.

static boolean isSubtypeOfType(TypeMirror typeMirror, String otherType) {
    if (otherType.equals(typeMirror.toString())) {
        return true;
    }
    if (typeMirror.getKind() != TypeKind.DECLARED) {
        return false;
    }
    DeclaredType declaredType = (DeclaredType) typeMirror;
    List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
    if (typeArguments.size() > 0) {
        StringBuilder typeString = new StringBuilder(declaredType.asElement().toString());
        typeString.append('<');
        for (int i = 0; i < typeArguments.size(); i++) {
            if (i > 0) {
                typeString.append(',');
            }
            typeString.append('?');
        }
        typeString.append('>');
        if (typeString.toString().equals(otherType)) {
            return true;
        }
    }
    Element element = declaredType.asElement();
    if (!(element instanceof TypeElement)) {
        return false;
    }
    TypeElement typeElement = (TypeElement) element;
    TypeMirror superType = typeElement.getSuperclass();
    if (isSubtypeOfType(superType, otherType)) {
        return true;
    }
    for (TypeMirror interfaceType : typeElement.getInterfaces()) {
        if (isSubtypeOfType(interfaceType, otherType)) {
            return true;
        }
    }
    return false;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 15 with TypeMirror

use of javax.lang.model.type.TypeMirror in project epoxy by airbnb.

the class EpoxyProcessor method addCreateHolderMethodIfNeeded.

/**
   * If the model is a holder and doesn't implement the "createNewHolder" method we can generate a
   * default implementation by getting the class type and creating a new instance of it.
   */
private void addCreateHolderMethodIfNeeded(TypeElement originalClassElement, List<MethodSpec> methods) {
    if (!isEpoxyModelWithHolder(originalClassElement)) {
        return;
    }
    MethodSpec createHolderMethod = MethodSpec.methodBuilder(CREATE_NEW_HOLDER_METHOD_NAME).addAnnotation(Override.class).addModifiers(Modifier.PROTECTED).build();
    if (implementsMethod(originalClassElement, createHolderMethod, typeUtils)) {
        return;
    }
    TypeMirror epoxyObjectType = getEpoxyObjectType(originalClassElement, typeUtils);
    if (epoxyObjectType == null) {
        logError("Return type for createNewHolder method could not be found. (class: %s)", originalClassElement.getSimpleName());
        return;
    }
    createHolderMethod = createHolderMethod.toBuilder().returns(TypeName.get(epoxyObjectType)).addStatement("return new $T()", epoxyObjectType).build();
    methods.add(createHolderMethod);
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) TypeMirror(javax.lang.model.type.TypeMirror)

Aggregations

TypeMirror (javax.lang.model.type.TypeMirror)475 TypeElement (javax.lang.model.element.TypeElement)197 ExecutableElement (javax.lang.model.element.ExecutableElement)105 Test (org.junit.Test)90 VariableElement (javax.lang.model.element.VariableElement)88 DeclaredType (javax.lang.model.type.DeclaredType)76 Element (javax.lang.model.element.Element)61 ArrayList (java.util.ArrayList)36 Expression (com.google.devtools.j2objc.ast.Expression)32 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)28 Elements (javax.lang.model.util.Elements)28 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)24 AbstractJClass (com.helger.jcodemodel.AbstractJClass)24 List (java.util.List)23 Types (javax.lang.model.util.Types)22 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)21 TypeParameterElement (javax.lang.model.element.TypeParameterElement)21 ArrayType (javax.lang.model.type.ArrayType)21 FunctionElement (com.google.devtools.j2objc.types.FunctionElement)20 IJExpression (com.helger.jcodemodel.IJExpression)20