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());
}
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;
}
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();
}
}
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);
}
}
}
}
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('}');
}
Aggregations