Search in sources :

Example 31 with TypeParameterElement

use of javax.lang.model.element.TypeParameterElement in project buck by facebook.

the class TreeBackedTypeParameterElementTest method testGetSimpleNameWithBoundedParameter.

@Test
public void testGetSimpleNameWithBoundedParameter() throws IOException {
    compile("class Foo<T extends java.lang.Runnable> { }");
    TypeElement fooElement = elements.getTypeElement("Foo");
    final List<? extends TypeParameterElement> typeParameters = fooElement.getTypeParameters();
    assertSame(1, typeParameters.size());
    TypeParameterElement typeParam = typeParameters.get(0);
    assertNameEquals("T", typeParam.getSimpleName());
}
Also used : TypeElement(javax.lang.model.element.TypeElement) TypeParameterElement(javax.lang.model.element.TypeParameterElement) Test(org.junit.Test)

Example 32 with TypeParameterElement

use of javax.lang.model.element.TypeParameterElement in project LoganSquare by bluelinelabs.

the class ObjectMapperInjector method getTypeSpec.

private TypeSpec getTypeSpec() {
    TypeSpec.Builder builder = TypeSpec.classBuilder(mJsonObjectHolder.injectedClassName).addModifiers(Modifier.PUBLIC, Modifier.FINAL);
    builder.addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "\"unsafe,unchecked\"").build());
    builder.superclass(ParameterizedTypeName.get(ClassName.get(JsonMapper.class), mJsonObjectHolder.objectTypeName));
    for (TypeParameterElement typeParameterElement : mJsonObjectHolder.typeParameters) {
        builder.addTypeVariable(TypeVariableName.get((TypeVariable) typeParameterElement.asType()));
    }
    if (mJsonObjectHolder.hasParentClass()) {
        FieldSpec.Builder parentMapperBuilder;
        if (mJsonObjectHolder.parentTypeParameters.size() == 0) {
            parentMapperBuilder = FieldSpec.builder(ParameterizedTypeName.get(ClassName.get(JsonMapper.class), mJsonObjectHolder.parentTypeName), PARENT_OBJECT_MAPPER_VARIABLE_NAME).addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL).initializer("$T.mapperFor($T.class)", LoganSquare.class, mJsonObjectHolder.parentTypeName);
        } else {
            parentMapperBuilder = FieldSpec.builder(ParameterizedTypeName.get(ClassName.get(JsonMapper.class), mJsonObjectHolder.getParameterizedParentTypeName()), PARENT_OBJECT_MAPPER_VARIABLE_NAME).addModifiers(Modifier.PRIVATE);
            if (mJsonObjectHolder.typeParameters.size() == 0) {
                parentMapperBuilder.initializer("$T.mapperFor(new $T<$T>() { })", LoganSquare.class, ParameterizedType.class, mJsonObjectHolder.getParameterizedParentTypeName());
            }
        }
        builder.addField(parentMapperBuilder.build());
    }
    // TypeConverters could be expensive to create, so just use one per class
    Set<ClassName> typeConvertersUsed = new HashSet<>();
    for (JsonFieldHolder fieldHolder : mJsonObjectHolder.fieldMap.values()) {
        if (fieldHolder.type instanceof TypeConverterFieldType) {
            typeConvertersUsed.add(((TypeConverterFieldType) fieldHolder.type).getTypeConverterClassName());
        }
    }
    for (ClassName typeConverter : typeConvertersUsed) {
        builder.addField(FieldSpec.builder(typeConverter, getStaticFinalTypeConverterVariableName(typeConverter)).addModifiers(Modifier.PROTECTED, Modifier.STATIC, Modifier.FINAL).initializer("new $T()", typeConverter).build());
    }
    MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC);
    List<String> createdJsonMappers = new ArrayList<>();
    if (mJsonObjectHolder.typeParameters.size() > 0) {
        constructorBuilder.addParameter(ClassName.get(ParameterizedType.class), "type");
        constructorBuilder.addStatement("partialMappers.put(type, this)");
        for (TypeParameterElement typeParameterElement : mJsonObjectHolder.typeParameters) {
            final String typeName = typeParameterElement.getSimpleName().toString();
            final String typeArgumentName = typeName + "Type";
            final String jsonMapperVariableName = getJsonMapperVariableNameForTypeParameter(typeName);
            if (!createdJsonMappers.contains(jsonMapperVariableName)) {
                createdJsonMappers.add(jsonMapperVariableName);
                // Add a JsonMapper reference
                builder.addField(FieldSpec.builder(ParameterizedTypeName.get(ClassName.get(JsonMapper.class), TypeVariableName.get(typeName)), jsonMapperVariableName).addModifiers(Modifier.PRIVATE, Modifier.FINAL).build());
                constructorBuilder.addParameter(ClassName.get(ParameterizedType.class), typeArgumentName);
                constructorBuilder.addStatement("$L = $T.mapperFor($L, partialMappers)", jsonMapperVariableName, LoganSquare.class, typeArgumentName);
            }
        }
        constructorBuilder.addParameter(ParameterizedTypeName.get(ClassName.get(SimpleArrayMap.class), ClassName.get(ParameterizedType.class), ClassName.get(JsonMapper.class)), "partialMappers");
    }
    for (JsonFieldHolder jsonFieldHolder : mJsonObjectHolder.fieldMap.values()) {
        if (jsonFieldHolder.type instanceof ParameterizedTypeField) {
            final String jsonMapperVariableName = getJsonMapperVariableNameForTypeParameter(((ParameterizedTypeField) jsonFieldHolder.type).getParameterName());
            if (!createdJsonMappers.contains(jsonMapperVariableName)) {
                ParameterizedTypeName parameterizedType = ParameterizedTypeName.get(ClassName.get(JsonMapper.class), jsonFieldHolder.type.getTypeName());
                createdJsonMappers.add(jsonMapperVariableName);
                builder.addField(FieldSpec.builder(parameterizedType, jsonMapperVariableName).addModifiers(Modifier.PRIVATE, Modifier.FINAL).build());
                String typeName = jsonMapperVariableName + "Type";
                constructorBuilder.addStatement("$T $L = new $T<$T>() { }", ParameterizedType.class, typeName, ParameterizedType.class, jsonFieldHolder.type.getTypeName());
                if (mJsonObjectHolder.typeParameters.size() > 0) {
                    constructorBuilder.beginControlFlow("if ($L.equals(type))", typeName);
                    constructorBuilder.addStatement("$L = ($T)this", jsonMapperVariableName, JsonMapper.class);
                    constructorBuilder.nextControlFlow("else");
                    constructorBuilder.addStatement("$L = $T.mapperFor($L, partialMappers)", jsonMapperVariableName, LoganSquare.class, typeName);
                    constructorBuilder.endControlFlow();
                } else {
                    constructorBuilder.addStatement("$L = $T.mapperFor($L)", jsonMapperVariableName, LoganSquare.class, typeName);
                }
            }
        }
    }
    if (createdJsonMappers.size() > 0) {
        if (mJsonObjectHolder.hasParentClass()) {
            constructorBuilder.addStatement("$L = $T.mapperFor(new $T<$T>() { })", PARENT_OBJECT_MAPPER_VARIABLE_NAME, LoganSquare.class, ParameterizedType.class, mJsonObjectHolder.getParameterizedParentTypeName());
        }
        builder.addMethod(constructorBuilder.build());
    }
    builder.addMethod(getParseMethod());
    builder.addMethod(getParseFieldMethod());
    builder.addMethod(getSerializeMethod());
    addUsedJsonMapperVariables(builder);
    addUsedTypeConverterMethods(builder);
    return builder.build();
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ParameterizedTypeField(com.bluelinelabs.logansquare.processor.type.field.ParameterizedTypeField) ArrayList(java.util.ArrayList) FieldSpec(com.squareup.javapoet.FieldSpec) TypeParameterElement(javax.lang.model.element.TypeParameterElement) JsonMapper(com.bluelinelabs.logansquare.JsonMapper) ParameterizedType(com.bluelinelabs.logansquare.ParameterizedType) TypeConverterFieldType(com.bluelinelabs.logansquare.processor.type.field.TypeConverterFieldType) TypeVariable(javax.lang.model.type.TypeVariable) LoganSquare(com.bluelinelabs.logansquare.LoganSquare) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec) HashSet(java.util.HashSet) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 33 with TypeParameterElement

use of javax.lang.model.element.TypeParameterElement in project immutables by immutables.

the class Generics method computeParameters.

private static Parameter[] computeParameters(final Protoclass protoclass, final Element element) {
    if (!(element instanceof Parameterizable)) {
        return NO_PARAMETERS;
    }
    final List<? extends TypeParameterElement> typeParameters = ((Parameterizable) element).getTypeParameters();
    if (typeParameters.isEmpty()) {
        return NO_PARAMETERS;
    }
    class Creator {

        final String[] vars = collectVars(typeParameters);

        final DeclaringType declaringType = protoclass.environment().round().inferDeclaringTypeFor(element);

        Parameter[] create() {
            final Parameter[] parameters = new Parameter[typeParameters.size()];
            int i = 0;
            for (TypeParameterElement e : typeParameters) {
                parameters[i] = new Parameter(i, e.getSimpleName().toString(), boundsFrom(e));
                i++;
            }
            return parameters;
        }

        String[] boundsFrom(TypeParameterElement e) {
            List<? extends TypeMirror> boundMirrors = e.getBounds();
            if (boundMirrors.isEmpty()) {
                return NO_STRINGS;
            }
            String[] bounds = new String[boundMirrors.size()];
            int c = 0;
            for (TypeMirror m : boundMirrors) {
                TypeStringProvider provider = newProvider(m);
                provider.process();
                bounds[c++] = provider.returnTypeName();
            }
            if (bounds.length == 1 && bounds[0].equals(Object.class.getName())) {
                return NO_STRINGS;
            }
            return bounds;
        }

        TypeStringProvider newProvider(TypeMirror type) {
            return new TypeStringProvider(protoclass.report(), element, type, new ImportsTypeStringResolver(declaringType, declaringType), vars, null);
        }
    }
    return new Creator().create();
}
Also used : TypeParameterElement(javax.lang.model.element.TypeParameterElement) TypeMirror(javax.lang.model.type.TypeMirror) Parameterizable(javax.lang.model.element.Parameterizable) DeclaringType(org.immutables.value.processor.meta.Proto.DeclaringType)

Example 34 with TypeParameterElement

use of javax.lang.model.element.TypeParameterElement in project immutables by immutables.

the class Generics method collectVars.

private static String[] collectVars(List<? extends TypeParameterElement> typeParameters) {
    String[] vars = new String[typeParameters.size()];
    int c = 0;
    for (TypeParameterElement p : typeParameters) {
        vars[c++] = p.getSimpleName().toString();
    }
    return vars;
}
Also used : TypeParameterElement(javax.lang.model.element.TypeParameterElement)

Example 35 with TypeParameterElement

use of javax.lang.model.element.TypeParameterElement in project auto by google.

the class BuilderSpec method sameTypeParameters.

private static boolean sameTypeParameters(TypeElement a, TypeElement b) {
    int nTypeParameters = a.getTypeParameters().size();
    if (nTypeParameters != b.getTypeParameters().size()) {
        return false;
    }
    for (int i = 0; i < nTypeParameters; i++) {
        TypeParameterElement aParam = a.getTypeParameters().get(i);
        TypeParameterElement bParam = b.getTypeParameters().get(i);
        if (!aParam.getSimpleName().equals(bParam.getSimpleName())) {
            return false;
        }
        Set<TypeMirror> autoValueBounds = new TypeMirrorSet(aParam.getBounds());
        Set<TypeMirror> builderBounds = new TypeMirrorSet(bParam.getBounds());
        if (!autoValueBounds.equals(builderBounds)) {
            return false;
        }
    }
    return true;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeParameterElement(javax.lang.model.element.TypeParameterElement)

Aggregations

TypeParameterElement (javax.lang.model.element.TypeParameterElement)40 TypeElement (javax.lang.model.element.TypeElement)20 TypeMirror (javax.lang.model.type.TypeMirror)17 Test (org.junit.Test)17 TypeVariable (javax.lang.model.type.TypeVariable)6 ArrayList (java.util.ArrayList)5 ExecutableElement (javax.lang.model.element.ExecutableElement)5 DeclaredType (javax.lang.model.type.DeclaredType)5 Element (javax.lang.model.element.Element)4 VariableElement (javax.lang.model.element.VariableElement)4 AbstractJClass (com.helger.jcodemodel.AbstractJClass)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 ArrayType (javax.lang.model.type.ArrayType)2 PrimitiveType (javax.lang.model.type.PrimitiveType)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 JsonMapper (com.bluelinelabs.logansquare.JsonMapper)1 LoganSquare (com.bluelinelabs.logansquare.LoganSquare)1 ParameterizedType (com.bluelinelabs.logansquare.ParameterizedType)1