Java Code Examples for javax.lang.model.element.TypeElement#getTypeParameters()
The following examples show how to use
javax.lang.model.element.TypeElement#getTypeParameters() .
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: TreeBackedTypeParameterElementTest.java From buck with Apache License 2.0 | 6 votes |
@Test public void testMultipleBoundedTypeParameter() throws IOException { compile( "class Foo<T extends java.lang.CharSequence & java.lang.Runnable & java.io.Closeable> { }"); TypeMirror charSequenceType = elements.getTypeElement("java.lang.CharSequence").asType(); TypeMirror runnableType = elements.getTypeElement("java.lang.Runnable").asType(); TypeMirror closeableType = elements.getTypeElement("java.io.Closeable").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(3, bounds.size()); assertSameType(charSequenceType, bounds.get(0)); assertSameType(runnableType, bounds.get(1)); assertSameType(closeableType, bounds.get(2)); }
Example 2
Source File: InternalDomainMetaFactory.java From doma with Apache License 2.0 | 6 votes |
TypeMirror inferType( TypeVariable typeVariable, TypeElement classElement, TypeMirror classMirror) { DeclaredType declaredType = ctx.getMoreTypes().toDeclaredType(classMirror); if (declaredType == null) { return null; } List<? extends TypeMirror> args = declaredType.getTypeArguments(); if (args.isEmpty()) { return null; } int argsSize = args.size(); int index = 0; for (TypeParameterElement typeParam : classElement.getTypeParameters()) { if (index >= argsSize) { break; } if (ctx.getMoreTypes().isSameTypeWithErasure(typeVariable, typeParam.asType())) { return args.get(index); } index++; } return null; }
Example 3
Source File: ExportNonAccessibleElement.java From netbeans with Apache License 2.0 | 6 votes |
public Boolean visitType(TypeElement arg0, Void arg1) { for (TypeParameterElement e : arg0.getTypeParameters()) { if (stop) { return false; } for (TypeMirror b : e.getBounds()) { if (stop) { return false; } if (b.accept(this, arg1)) { return true; } } } TypeMirror superclass = arg0.getSuperclass(); if (superclass.getKind() == TypeKind.DECLARED) { if (!((DeclaredType) superclass).asElement().getKind().isInterface()) { return false; } } return superclass.accept(this, arg1); }
Example 4
Source File: ElementStructureTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
void writeType(TypeElement e) { if (!acceptType.test(task.getElements().getBinaryName(e).toString())) return ; try { analyzeElement(e); writeTypes(e.getInterfaces()); out.write(e.getNestingKind().toString()); out.write(e.getQualifiedName().toString()); write(e.getSuperclass()); for (TypeParameterElement param : e.getTypeParameters()) { visit(param, null); } List<Element> defs = new ArrayList<>(e.getEnclosedElements()); //XXX: forcing ordering for members - not completely correct! Collections.sort(defs, (e1, e2) -> e1.toString().compareTo(e2.toString())); for (Element def : defs) { visit(def, null); } out.write("\n"); } catch (IOException ex) { ex.printStackTrace(); } }
Example 5
Source File: SignatureFactory.java From buck with Apache License 2.0 | 6 votes |
@Override public Void visitType(TypeElement element, SignatureVisitor visitor) { if (!signatureRequired(element)) { return null; } for (TypeParameterElement typeParameterElement : element.getTypeParameters()) { typeParameterElement.accept(this, visitor); } TypeMirror superclass = element.getSuperclass(); if (superclass.getKind() != TypeKind.NONE) { superclass.accept(typeVisitorAdapter, visitor.visitSuperclass()); } else { // Interface type; implicit superclass of Object SignatureVisitor superclassVisitor = visitor.visitSuperclass(); superclassVisitor.visitClassType("java/lang/Object"); superclassVisitor.visitEnd(); } for (TypeMirror interfaceType : element.getInterfaces()) { interfaceType.accept(typeVisitorAdapter, visitor.visitInterface()); } return null; }
Example 6
Source File: SpecParser.java From dataenum with Apache License 2.0 | 6 votes |
public static Spec parse(Element element, ProcessingEnvironment processingEnv) { Messager messager = processingEnv.getMessager(); if (element.getKind() != ElementKind.INTERFACE) { messager.printMessage( Diagnostic.Kind.ERROR, "@DataEnum can only be used on interfaces.", element); return null; } TypeElement dataEnum = (TypeElement) element; List<TypeVariableName> typeVariableNames = new ArrayList<>(); for (TypeParameterElement typeParameterElement : dataEnum.getTypeParameters()) { typeVariableNames.add(TypeVariableName.get(typeParameterElement)); } List<Value> values = ValuesParser.parse(dataEnum, processingEnv); if (values == null) { return null; } ClassName enumInterface = ClassName.get(dataEnum); return new Spec(enumInterface, typeVariableNames, values); }
Example 7
Source File: TreeBackedTypeParameterElementTest.java From buck with Apache License 2.0 | 5 votes |
@Test public void testGetSimpleNameWithBoundedParameter() throws IOException { compile("class Foo<T extends java.lang.Runnable> { }"); TypeElement fooElement = elements.getTypeElement("Foo"); List<? extends TypeParameterElement> typeParameters = fooElement.getTypeParameters(); assertSame(1, typeParameters.size()); TypeParameterElement typeParam = typeParameters.get(0); assertNameEquals("T", typeParam.getSimpleName()); }
Example 8
Source File: TurbineElementTest.java From turbine with Apache License 2.0 | 5 votes |
@Test public void typeParameters() { TypeElement e = factory.typeElement(new ClassSymbol("java/util/HashMap")); assertThat(e.getTypeParameters().stream().map(Object::toString).collect(toImmutableList())) .containsExactly("K", "V"); for (TypeParameterElement t : e.getTypeParameters()) { assertThat(t.getGenericElement()).isEqualTo(e); assertThat(t.getEnclosingElement()).isEqualTo(e); assertThat(t.getBounds()).containsExactly(factory.asTypeMirror(ClassTy.OBJECT)); } }
Example 9
Source File: Utils.java From paperparcel with Apache License 2.0 | 5 votes |
@Nullable private static TypeMirror paramAsMemberOfImpl( Types types, DeclaredType type, TypeParameterElement param) { TypeElement paramEnclosingElement = (TypeElement) param.getEnclosingElement(); TypeElement typeAsElement = (TypeElement) type.asElement(); if (paramEnclosingElement.equals(typeAsElement)) { List<? extends TypeParameterElement> typeParamElements = typeAsElement.getTypeParameters(); for (int i = 0; i < typeParamElements.size(); i++) { TypeParameterElement typeParamElement = typeParamElements.get(i); if (typeParamElement.equals(param)) { List<? extends TypeMirror> typeArguments = type.getTypeArguments(); if (typeArguments.isEmpty()) { return types.erasure(param.asType()); } else { return type.getTypeArguments().get(i); } } } } List<? extends TypeMirror> superTypes = types.directSupertypes(type); for (TypeMirror superType : superTypes) { if (superType.getKind() == TypeKind.DECLARED) { TypeMirror result = paramAsMemberOfImpl(types, (DeclaredType) superType, param); if (result != null) { return result; } } } return null; }
Example 10
Source File: TypeSimplifier.java From RetroFacebook with Apache License 2.0 | 5 votes |
static String actualTypeParametersString(TypeElement type) { List<? extends TypeParameterElement> typeParameters = type.getTypeParameters(); if (typeParameters.isEmpty()) { return ""; } else { return "<" + FluentIterable.from(typeParameters) .transform(SimpleNameFunction.INSTANCE) .join(Joiner.on(", ")) + ">"; } }
Example 11
Source File: AutoValueOrOneOfProcessor.java From auto with Apache License 2.0 | 5 votes |
/** * Returns the {@code @AutoValue} or {@code @AutoOneOf} type parameters, with a ? for every type. * If we have {@code @AutoValue abstract class Foo<T extends Something>} then this method will * return just {@code <?>}. */ private static String wildcardTypeParametersString(TypeElement type) { List<? extends TypeParameterElement> typeParameters = type.getTypeParameters(); if (typeParameters.isEmpty()) { return ""; } else { return typeParameters.stream().map(e -> "?").collect(joining(", ", "<", ">")); } }
Example 12
Source File: RetroFacebookProcessor.java From RetroFacebook with Apache License 2.0 | 5 votes |
private static String wildcardTypeParametersString(TypeElement type) { List<? extends TypeParameterElement> typeParameters = type.getTypeParameters(); if (typeParameters.isEmpty()) { return ""; } else { return "<" + Joiner.on(", ").join( FluentIterable.from(typeParameters).transform(Functions.constant("?"))) + ">"; } }
Example 13
Source File: TreeBackedTypeParameterElementTest.java From buck with Apache License 2.0 | 5 votes |
@Test public void testGetSimpleName() throws IOException { compile("class Foo<T> { }"); TypeElement fooElement = elements.getTypeElement("Foo"); List<? extends TypeParameterElement> typeParameters = fooElement.getTypeParameters(); assertSame(1, typeParameters.size()); TypeParameterElement typeParam = typeParameters.get(0); assertNameEquals("T", typeParam.getSimpleName()); }
Example 14
Source File: AdapterValidator.java From paperparcel with Apache License 2.0 | 5 votes |
private List<TypeParameterElement> findMissingParameters(TypeElement element, TypeMirror adapted) { List<TypeParameterElement> result = new ArrayList<>(); Set<String> adaptedTypeArguments = Utils.getTypeVariableNames(adapted); List<? extends TypeParameterElement> parameters = element.getTypeParameters(); for (TypeParameterElement parameter : parameters) { if (!adaptedTypeArguments.contains(parameter.getSimpleName().toString())) { result.add(parameter); } } return result; }
Example 15
Source File: ElementNode.java From netbeans with Apache License 2.0 | 5 votes |
private static String createHtmlHeader(boolean deprecated, TypeElement e) { StringBuilder sb = new StringBuilder(); sb.append("<html>"); if (deprecated) sb.append("<s>"); sb.append(e.getSimpleName()); if (deprecated) sb.append("</s>"); List<? extends TypeParameterElement> typeParams = e.getTypeParameters(); if (typeParams != null && !typeParams.isEmpty()) { sb.append("<"); // NOI18N for(Iterator<? extends TypeParameterElement> it = typeParams.iterator(); it.hasNext();) { TypeParameterElement tp = it.next(); sb.append(tp.getSimpleName()); try { List<? extends TypeMirror> bounds = tp.getBounds(); if (!bounds.isEmpty()) { sb.append(translateToHTML(printBounds(bounds))); } } catch (NullPointerException npe) { } if (it.hasNext()) { sb.append(", "); // NOI18N } } sb.append(">"); // NOI18N } return sb.toString(); }
Example 16
Source File: AutoValueRedactedExtension.java From auto-value-redacted with Apache License 2.0 | 5 votes |
@Override public String generateClass(Context context, String className, String classToExtend, boolean isFinal) { String packageName = context.packageName(); TypeElement autoValueClass = context.autoValueClass(); List<? extends TypeParameterElement> typeParameters = autoValueClass.getTypeParameters(); Name superName = autoValueClass.getSimpleName(); Map<String, ExecutableElement> properties = context.properties(); TypeSpec.Builder subclass = TypeSpec.classBuilder(className) // .addModifiers(isFinal ? Modifier.FINAL : Modifier.ABSTRACT) // .addMethod(generateConstructor(properties)) // .addMethod(generateToString(superName, properties)); ClassName superclass = ClassName.get(packageName, classToExtend); if (typeParameters.isEmpty()) { subclass.superclass(superclass); } else { List<TypeVariableName> typeVariables = new ArrayList<>(); for (TypeParameterElement typeParameter : typeParameters) { typeVariables.add(TypeVariableName.get(typeParameter)); } subclass.addTypeVariables(typeVariables) .superclass( ParameterizedTypeName.get(superclass, typeVariables.toArray(new TypeName[0]))); } JavaFile javaFile = JavaFile.builder(packageName, subclass.build()).build(); return javaFile.toString(); }
Example 17
Source File: EntityMetaFactory.java From doma with Apache License 2.0 | 5 votes |
void validateGenericEntityListener( TypeElement classElement, EntityMeta entityMeta, TypeElement listenerElement) { EntityAnnot entityAnnot = entityMeta.getEntityAnnot(); List<? extends TypeParameterElement> typeParams = listenerElement.getTypeParameters(); if (typeParams.size() == 0) { throw new AptIllegalStateException("typeParams size should be more than 0"); } if (typeParams.size() > 1) { throw new AptException( Message.DOMA4227, classElement, entityAnnot.getAnnotationMirror(), entityAnnot.getListener(), new Object[] {}); } TypeParameterElement typeParam = typeParams.get(0); for (TypeMirror bound : typeParam.getBounds()) { if (!ctx.getMoreTypes().isAssignableWithErasure(classElement.asType(), bound)) { throw new AptException( Message.DOMA4229, classElement, entityAnnot.getAnnotationMirror(), entityAnnot.getListener(), new Object[] {typeParam.getSimpleName(), bound, classElement.getQualifiedName()}); } } if (findListenerTypeParam(listenerElement, 0) == null) { throw new AptException( Message.DOMA4228, classElement, entityAnnot.getAnnotationMirror(), entityAnnot.getListener(), new Object[] {typeParam.getSimpleName()}); } }
Example 18
Source File: ElementUtilities.java From netbeans with Apache License 2.0 | 5 votes |
private DeclaredType getDeclaredType(TypeElement e, HashMap<? extends Element, ? extends TypeMirror> map, Types types) { List<? extends TypeParameterElement> tpes = e.getTypeParameters(); TypeMirror[] targs = new TypeMirror[tpes.size()]; int i = 0; for (Iterator<? extends TypeParameterElement> it = tpes.iterator(); it.hasNext();) { TypeParameterElement tpe = it.next(); TypeMirror t = map.get(tpe); targs[i++] = t != null ? t : tpe.asType(); } Element encl = e.getEnclosingElement(); if ((encl.getKind().isClass() || encl.getKind().isInterface()) && !((TypeElement)encl).getTypeParameters().isEmpty()) return types.getDeclaredType(getDeclaredType((TypeElement)encl, map, types), e, targs); return types.getDeclaredType(e, targs); }
Example 19
Source File: TypeSimplifier.java From RetroFacebook with Apache License 2.0 | 5 votes |
static String actualTypeParametersString(TypeElement type) { List<? extends TypeParameterElement> typeParameters = type.getTypeParameters(); if (typeParameters.isEmpty()) { return ""; } else { return "<" + FluentIterable.from(typeParameters) .transform(SimpleNameFunction.INSTANCE) .join(Joiner.on(", ")) + ">"; } }
Example 20
Source File: TypeClass.java From FreeBuilder with Apache License 2.0 | 4 votes |
public static TypeClass from(TypeElement typeElement) { return new TypeClass(QualifiedName.of(typeElement), typeElement.getTypeParameters()); }