Search in sources :

Example 41 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 42 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)

Example 43 with TypeParameterElement

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

the class TypeSimplifier method formalTypeParametersString.

// The formal type parameters of the given type.
// If we have @AutoValue abstract class Foo<T extends SomeClass> then this method will
// return <T extends Something> for Foo. Likewise it will return the angle-bracket part of:
// Foo<SomeClass>
// Foo<T extends Number>
// Foo<E extends Enum<E>>
// Foo<K, V extends Comparable<? extends K>>
// Type variables need special treatment because we only want to include their bounds when they
// are declared, not when they are referenced. We don't want to include the bounds of the second E
// in <E extends Enum<E>> or of the second K in <K, V extends Comparable<? extends K>>. That's
// why we put the "extends" handling here and not in ToStringTypeVisitor.
String formalTypeParametersString(TypeElement type) {
    List<? extends TypeParameterElement> typeParameters = type.getTypeParameters();
    if (typeParameters.isEmpty()) {
        return "";
    } else {
        StringBuilder sb = new StringBuilder("<");
        String sep = "";
        for (TypeParameterElement typeParameter : typeParameters) {
            sb.append(sep);
            sep = ", ";
            appendTypeParameterWithBounds(sb, typeParameter);
        }
        return sb.append(">").toString();
    }
}
Also used : TypeParameterElement(javax.lang.model.element.TypeParameterElement)

Example 44 with TypeParameterElement

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

the class RobolectricModel method registerType.

private void registerType(TypeElement type) {
    if (!Objects.equal(ANYTHING, type) && !importMap.containsKey(type)) {
        typeMap.put(type.getSimpleName().toString(), type);
        importMap.put(type, type);
        for (TypeParameterElement typeParam : type.getTypeParameters()) {
            for (TypeMirror bound : typeParam.getBounds()) {
                // FIXME: get rid of cast using a visitor
                TypeElement boundElement = typeElementVisitor.visit(types.asElement(bound));
                registerType(boundElement);
            }
        }
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) TypeParameterElement(javax.lang.model.element.TypeParameterElement)

Example 45 with TypeParameterElement

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

the class ShadowProviderGenerator method generate.

void generate(String shadowPackage, PrintWriter writer) {
    writer.print("package " + shadowPackage + ";\n");
    for (String name : model.getImports()) {
        writer.println("import " + name + ';');
    }
    writer.println();
    writer.println("/**");
    writer.println(" * Shadow mapper. Automatically generated by the Robolectric Annotation Processor.");
    writer.println(" */");
    writer.println("@Generated(\"" + RobolectricProcessor.class.getCanonicalName() + "\")");
    writer.println("@SuppressWarnings({\"unchecked\",\"deprecation\"})");
    writer.println("public class " + GEN_CLASS + " implements ShadowProvider {");
    final int shadowSize = model.getAllShadowTypes().size();
    writer.println("  private static final Map<String, String> SHADOW_MAP = new HashMap<>(" + shadowSize + ");");
    writer.println();
    writer.println("  static {");
    for (Map.Entry<TypeElement, TypeElement> entry : model.getAllShadowTypes().entrySet()) {
        final String shadow = elements.getBinaryName(entry.getKey()).toString();
        final String actual = entry.getValue().getQualifiedName().toString();
        writer.println("    SHADOW_MAP.put(\"" + actual + "\", \"" + shadow + "\");");
    }
    for (Map.Entry<String, String> entry : model.getExtraShadowTypes().entrySet()) {
        final String shadow = entry.getKey();
        final String actual = entry.getValue();
        writer.println("    SHADOW_MAP.put(\"" + actual + "\", \"" + shadow + "\");");
    }
    writer.println("  }");
    writer.println();
    for (Map.Entry<TypeElement, TypeElement> entry : model.getShadowOfMap().entrySet()) {
        final TypeElement shadowType = entry.getKey();
        final TypeElement actualType = entry.getValue();
        if (!actualType.getModifiers().contains(Modifier.PUBLIC)) {
            continue;
        }
        int paramCount = 0;
        StringBuilder paramDef = new StringBuilder("<");
        StringBuilder paramUse = new StringBuilder("<");
        for (TypeParameterElement typeParam : entry.getValue().getTypeParameters()) {
            if (paramCount > 0) {
                paramDef.append(',');
                paramUse.append(',');
            }
            boolean first = true;
            paramDef.append(typeParam);
            paramUse.append(typeParam);
            for (TypeMirror bound : model.getExplicitBounds(typeParam)) {
                if (first) {
                    paramDef.append(" extends ");
                    first = false;
                } else {
                    paramDef.append(" & ");
                }
                paramDef.append(model.getReferentFor(bound));
            }
            paramCount++;
        }
        String paramDefStr = "";
        String paramUseStr = "";
        if (paramCount > 0) {
            paramDefStr = paramDef.append("> ").toString();
            paramUseStr = paramUse.append('>').toString();
        }
        final String actual = model.getReferentFor(actualType) + paramUseStr;
        final String shadow = model.getReferentFor(shadowType) + paramUseStr;
        if (shadowType.getAnnotation(Deprecated.class) != null) {
            writer.println("  @Deprecated");
        }
        writer.println("  public static " + paramDefStr + shadow + " shadowOf(" + actual + " actual) {");
        writer.println("    return (" + shadow + ") ShadowExtractor.extract(actual);");
        writer.println("  }");
        writer.println();
    }
    writer.println("  public void reset() {");
    for (Map.Entry<TypeElement, ExecutableElement> entry : model.getResetters().entrySet()) {
        Implements annotation = entry.getKey().getAnnotation(Implements.class);
        int minSdk = annotation.minSdk();
        int maxSdk = annotation.maxSdk();
        String ifClause;
        if (minSdk != -1 && maxSdk != -1) {
            ifClause = "if (org.robolectric.RuntimeEnvironment.getApiLevel() >= " + minSdk + " && org.robolectric.RuntimeEnvironment.getApiLevel() <= " + maxSdk + ") ";
        } else if (maxSdk != -1) {
            ifClause = "if (org.robolectric.RuntimeEnvironment.getApiLevel() <= " + maxSdk + ") ";
        } else if (minSdk != -1) {
            ifClause = "if (org.robolectric.RuntimeEnvironment.getApiLevel() >= " + minSdk + ") ";
        } else {
            ifClause = "";
        }
        writer.println("    " + ifClause + model.getReferentFor(entry.getKey()) + "." + entry.getValue().getSimpleName() + "();");
    }
    writer.println("  }");
    writer.println();
    writer.println("  @Override");
    writer.println("  public Map<String, String> getShadowMap() {");
    writer.println("    return SHADOW_MAP;");
    writer.println("  }");
    writer.println();
    writer.println("  @Override");
    writer.println("  public String[] getProvidedPackageNames() {");
    writer.println("    return new String[] {");
    if (shouldInstrumentPackages) {
        writer.println("      " + Joiner.on(",\n      ").join(model.getShadowedPackages()));
    }
    writer.println("    };");
    writer.println("  }");
    writer.println('}');
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) RobolectricProcessor(org.robolectric.annotation.processing.RobolectricProcessor) TypeParameterElement(javax.lang.model.element.TypeParameterElement) Implements(org.robolectric.annotation.Implements) TypeMirror(javax.lang.model.type.TypeMirror) Map(java.util.Map)

Aggregations

TypeParameterElement (javax.lang.model.element.TypeParameterElement)66 TypeElement (javax.lang.model.element.TypeElement)30 TypeMirror (javax.lang.model.type.TypeMirror)26 Test (org.junit.Test)18 ExecutableElement (javax.lang.model.element.ExecutableElement)13 TypeVariable (javax.lang.model.type.TypeVariable)13 Element (javax.lang.model.element.Element)10 VariableElement (javax.lang.model.element.VariableElement)9 ArrayList (java.util.ArrayList)7 LinkedHashMap (java.util.LinkedHashMap)7 List (java.util.List)7 DeclaredType (javax.lang.model.type.DeclaredType)7 MethodSpec (com.squareup.javapoet.MethodSpec)6 Map (java.util.Map)6 FieldSpec (com.squareup.javapoet.FieldSpec)5 HashSet (java.util.HashSet)4 Modifier (javax.lang.model.element.Modifier)4 ArrayType (javax.lang.model.type.ArrayType)4 Types (javax.lang.model.util.Types)4 MoreElements (com.google.auto.common.MoreElements)3