javax.lang.model.element.TypeParameterElement Java Examples
The following examples show how to use
javax.lang.model.element.TypeParameterElement.
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: AnnotatedDurableEntityClass.java From mnemonic with Apache License 2.0 | 6 votes |
private int getFactoryProxyIndex(TypeName gtname) throws AnnotationProcessingException { int ret = -1; boolean found = false; if (gtname instanceof TypeVariableName) { for (TypeParameterElement tpe : m_elem.getTypeParameters()) { ++ret; if (tpe.toString().equals(gtname.toString())) { found = true; break; } } if (!found) { throw new AnnotationProcessingException(null, "%s type is not found during factory proxy query.", gtname.toString()); } } else { throw new AnnotationProcessingException(null, "%s type is not generic type for factory proxy query.", gtname.toString()); } return ret; }
Example #2
Source File: BuilderSpec.java From auto with Apache License 2.0 | 6 votes |
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; }
Example #3
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 #4
Source File: NameUtil.java From j2objc with Apache License 2.0 | 6 votes |
private void buildTypeArguments(DeclaredType type, StringBuilder sb) { Element elem = type.asElement(); List<? extends TypeMirror> typeArguments = type.getTypeArguments(); if (!typeArguments.isEmpty()) { sb.append('<'); for (TypeMirror typeArg : typeArguments) { TypeParameterElement typeParam = TypeUtil.asTypeParameterElement(typeArg); if (typeParam != null && elem.equals(typeParam.getEnclosingElement())) { // The type param is directly declared by the type being emitted so we don't need to fully // qualify it as buildTypeSignature() would. sb.append('T'); sb.append(ElementUtil.getName(typeParam)); sb.append(';'); } else { buildTypeSignature(typeArg, sb); } } sb.append('>'); } }
Example #5
Source File: TypeUtils.java From Mixin with MIT License | 6 votes |
/** * Convenience method to convert element to string representation for error * messages * * @param element Element to inspect * @return string representation of element name */ public static String getElementType(Element element) { if (element instanceof TypeElement) { return "TypeElement"; } else if (element instanceof ExecutableElement) { return "ExecutableElement"; } else if (element instanceof VariableElement) { return "VariableElement"; } else if (element instanceof PackageElement) { return "PackageElement"; } else if (element instanceof TypeParameterElement) { return "TypeParameterElement"; } return element.getClass().getSimpleName(); }
Example #6
Source File: PresenterInjectorRules.java From Moxy with MIT License | 6 votes |
private Map<TypeParameterElement, TypeMirror> getChildInstanceOfClassFromGeneric(final TypeElement typeElement, final Class<?> aClass) { Map<TypeParameterElement, TypeMirror> result = new HashMap<>(); for (TypeParameterElement element : typeElement.getTypeParameters()) { List<? extends TypeMirror> bounds = element.getBounds(); for (TypeMirror bound : bounds) { if (bound instanceof DeclaredType && ((DeclaredType) bound).asElement() instanceof TypeElement) { Collection<TypeMirror> viewsType = getViewsType((TypeElement) ((DeclaredType) bound).asElement()); boolean isViewType = false; for (TypeMirror viewType : viewsType) { if (((DeclaredType) viewType).asElement().toString().equals(aClass.getCanonicalName())) { isViewType = true; } } if (isViewType) { result.put(element, bound); break; } } } } return result; }
Example #7
Source File: PrintingProcessor.java From javaide with GNU General Public License v3.0 | 6 votes |
private void printFormalTypeParameters(Parameterizable e, boolean pad) { List<? extends TypeParameterElement> typeParams = e.getTypeParameters(); if (typeParams.size() > 0) { writer.print("<"); boolean first = true; for(TypeParameterElement tpe: typeParams) { if (!first) writer.print(", "); printAnnotationsInline(tpe); writer.print(tpe.toString()); first = false; } writer.print(">"); if (pad) writer.print(" "); } }
Example #8
Source File: TypeVariablesExtractor.java From litho with Apache License 2.0 | 6 votes |
/** Get the type variables from the given {@link TypeElement}. */ public static List<TypeVariableName> getTypeVariables(TypeElement typeElement) { final List<? extends TypeParameterElement> typeParameters = typeElement.getTypeParameters(); final int typeParameterCount = typeParameters.size(); final List<TypeVariableName> typeVariables = new ArrayList<>(typeParameterCount); for (TypeParameterElement typeParameterElement : typeParameters) { final int boundTypesCount = typeParameterElement.getBounds().size(); final TypeName[] boundsTypeNames = new TypeName[boundTypesCount]; for (int i = 0; i < boundTypesCount; i++) { boundsTypeNames[i] = TypeName.get(typeParameterElement.getBounds().get(i)); } final TypeVariableName typeVariable = TypeVariableName.get(typeParameterElement.getSimpleName().toString(), boundsTypeNames); typeVariables.add(typeVariable); } return typeVariables; }
Example #9
Source File: TreeBackedExecutableElement.java From buck with Apache License 2.0 | 6 votes |
@Override public StandaloneTypeMirror asType() { if (typeMirror == null) { typeMirror = new StandaloneExecutableType( getReturnType(), getTypeParameters().stream() .map(TypeParameterElement::asType) .map(type -> (TypeVariable) type) .collect(Collectors.toList()), getParameters().stream().map(VariableElement::asType).collect(Collectors.toList()), getThrownTypes(), getAnnotationMirrors()); } return typeMirror; }
Example #10
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 #11
Source File: CallBuilderProcessor.java From CallBuilder with Apache License 2.0 | 6 votes |
/** * The type parameters to place on the builder, with the "extends ..." bounds. */ String alligatorWithBounds() { List<TypeParameterElement> allParameters = allParameters(); if (allParameters.isEmpty()) { return ""; } StringBuilder alligator = new StringBuilder("<"); String separator = ""; for (TypeParameterElement param : allParameters) { alligator.append(separator); separator = ", "; alligator.append(param.toString()); for (TypeMirror bound : param.getBounds()) { alligator.append(" extends ").append(bound); } } return alligator.append(">").toString(); }
Example #12
Source File: TreeBackedTypeParameterElementTest.java From buck with Apache License 2.0 | 6 votes |
@Test public void testMultipleTypeParameters() throws IOException { compile("class Foo<T, U extends java.lang.CharSequence> { }"); TypeMirror objectType = elements.getTypeElement("java.lang.Object").asType(); TypeMirror charSequenceType = elements.getTypeElement("java.lang.CharSequence").asType(); TypeElement fooElement = elements.getTypeElement("Foo"); List<? extends TypeParameterElement> typeParameters = fooElement.getTypeParameters(); assertSame(2, typeParameters.size()); TypeParameterElement tParam = typeParameters.get(0); List<? extends TypeMirror> bounds = tParam.getBounds(); assertSame(1, bounds.size()); assertSameType(objectType, bounds.get(0)); TypeParameterElement uParam = typeParameters.get(1); bounds = uParam.getBounds(); assertSame(1, bounds.size()); assertSameType(charSequenceType, bounds.get(0)); }
Example #13
Source File: TypeVariableName.java From JReFrameworker with MIT License | 6 votes |
/** * Make a TypeVariableName for the given TypeMirror. This form is used internally to avoid * infinite recursion in cases like {@code Enum<E extends Enum<E>>}. When we encounter such a * thing, we will make a TypeVariableName without bounds and add that to the {@code typeVariables} * map before looking up the bounds. Then if we encounter this TypeVariable again while * constructing the bounds, we can just return it from the map. And, the code that put the entry * in {@code variables} will make sure that the bounds are filled in before returning. */ static TypeVariableName get( TypeVariable mirror, Map<TypeParameterElement, TypeVariableName> typeVariables) { TypeParameterElement element = (TypeParameterElement) mirror.asElement(); TypeVariableName typeVariableName = typeVariables.get(element); if (typeVariableName == null) { // Since the bounds field is public, we need to make it an unmodifiableList. But we control // the List that that wraps, which means we can change it before returning. List<TypeName> bounds = new ArrayList<>(); List<TypeName> visibleBounds = Collections.unmodifiableList(bounds); typeVariableName = new TypeVariableName(element.getSimpleName().toString(), visibleBounds); typeVariables.put(element, typeVariableName); for (TypeMirror typeMirror : element.getBounds()) { bounds.add(TypeName.get(typeMirror, typeVariables)); } bounds.remove(OBJECT); } return typeVariableName; }
Example #14
Source File: AnnotatedNonVolatileEntityClass.java From Mnemonic with Apache License 2.0 | 6 votes |
private int getFactoryProxyIndex(TypeName gtname) throws AnnotationProcessingException { int ret = -1; boolean found = false; if (gtname instanceof TypeVariableName) { for (TypeParameterElement tpe : m_elem.getTypeParameters()) { ++ret; if (tpe.toString().equals(gtname.toString())) { found = true; break; } } if (!found) { throw new AnnotationProcessingException(null, "%s type is not found during factory proxy query.", gtname.toString()); } } else { throw new AnnotationProcessingException(null, "%s type is not generic type for factory proxy query.", gtname.toString()); } return ret; }
Example #15
Source File: MoreTypesTest.java From doma with Apache License 2.0 | 6 votes |
@Test void getTypeName() { addProcessor( new TestProcessor() { @Override protected void run() { TypeMirror intType = ctx.getMoreTypes().getPrimitiveType(TypeKind.INT); assertEquals("int", ctx.getMoreTypes().getTypeName(intType)); TypeMirror listType = ctx.getMoreElements().getTypeElement(List.class).asType(); assertEquals("java.util.List<E>", ctx.getMoreTypes().getTypeName(listType)); } }, new TestProcessor() { @Override protected void run() { TypeElement typeElement = ctx.getMoreElements().getTypeElement(NumberList.class); TypeParameterElement typeParameterElement = typeElement.getTypeParameters().iterator().next(); assertEquals("E", ctx.getMoreTypes().getTypeName(typeParameterElement.asType())); } }); }
Example #16
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 #17
Source File: JavadocCompletionQuery.java From netbeans with Apache License 2.0 | 6 votes |
private void completeTypeVarName(Element forElement, String prefix, int substitutionOffset) { if (prefix.length() > 0) { if (prefix.charAt(0) == '<') { prefix = prefix.substring(1, prefix.length()); } else { // not type param return; } } List<? extends TypeParameterElement> tparams = (forElement.getKind().isClass() || forElement.getKind().isInterface()) ? ((TypeElement) forElement).getTypeParameters() : ((ExecutableElement) forElement).getTypeParameters(); for (TypeParameterElement typeVariable : tparams) { String name = typeVariable.getSimpleName().toString(); if (name.startsWith(prefix)) { items.add(JavadocCompletionItem.createNameItem( '<' + name + '>', substitutionOffset)); } } }
Example #18
Source File: SignatureFactory.java From buck with Apache License 2.0 | 6 votes |
@Override public Void visitTypeParameter(TypeParameterElement element, SignatureVisitor visitor) { visitor.visitFormalTypeParameter(element.getSimpleName().toString()); for (TypeMirror boundType : element.getBounds()) { boolean isClass; try { if (boundType.getKind() == TypeKind.DECLARED) { isClass = ((DeclaredType) boundType).asElement().getKind().isClass(); } else { isClass = boundType.getKind() == TypeKind.TYPEVAR; } } catch (CannotInferException e) { // We can't know whether an inferred type is a class or interface, but it turns out // the compiler does not distinguish between them when reading signatures, so we can // write inferred types as interface bounds. We go ahead and write all bounds as // interface bounds to make the SourceAbiCompatibleSignatureVisitor possible. isClass = false; } boundType.accept( typeVisitorAdapter, isClass ? visitor.visitClassBound() : visitor.visitInterfaceBound()); } return null; }
Example #19
Source File: WildcardTypeName.java From JReFrameworker with MIT License | 5 votes |
static TypeName get( javax.lang.model.type.WildcardType mirror, Map<TypeParameterElement, TypeVariableName> typeVariables) { TypeMirror extendsBound = mirror.getExtendsBound(); if (extendsBound == null) { TypeMirror superBound = mirror.getSuperBound(); if (superBound == null) { return subtypeOf(Object.class); } else { return supertypeOf(TypeName.get(superBound, typeVariables)); } } else { return subtypeOf(TypeName.get(extendsBound, typeVariables)); } }
Example #20
Source File: ApNavigator.java From hottub with GNU General Public License v2.0 | 5 votes |
@Override public TypeMirror visitTypeVariable(TypeVariable t, TypeElement typeElement) { // we are checking if T (declared as T extends A&B&C) is assignable to sup. // so apply bounds recursively. for (TypeMirror typeMirror : ((TypeParameterElement) t.asElement()).getBounds()) { TypeMirror m = visit(typeMirror, typeElement); if (m != null) return m; } return null; }
Example #21
Source File: AnalyserTest.java From FreeBuilder with Apache License 2.0 | 5 votes |
@Test public void genericInterfaceWithNoProperties() throws CannotGenerateCodeException { TypeElement typeElement = model.newType( "package com.example;", "public interface DataType<K, V> {", " class Builder<K, V> extends DataType_Builder<K, V> { }", "}"); TypeParameterElement k = typeElement.getTypeParameters().get(0); TypeParameterElement v = typeElement.getTypeParameters().get(1); DeclaredType mirror = (DeclaredType) typeElement.asType(); TypeVariable kVar = (TypeVariable) mirror.getTypeArguments().get(0); TypeVariable vVar = (TypeVariable) mirror.getTypeArguments().get(1); GeneratedType builder = analyser.analyse(typeElement); QualifiedName dataType = QualifiedName.of("com.example", "DataType"); QualifiedName generatedType = QualifiedName.of("com.example", "DataType_Builder"); assertThat(builder).isEqualTo(new GeneratedBuilder( new Datatype.Builder() .setBuilder(dataType.nestedType("Builder").withParameters(kVar, vVar)) .setBuilderFactory(NO_ARGS_CONSTRUCTOR) .setBuilderSerializable(false) .setExtensible(true) .setGeneratedBuilder(generatedType.withParameters(k, v)) .setHasToBuilderMethod(false) .setInterfaceType(true) .setPartialType(generatedType.nestedType("Partial").withParameters(k, v)) .setPropertyEnum(generatedType.nestedType("Property").withParameters()) .setRebuildableType(generatedType.nestedType("Rebuildable").withParameters(k, v)) .setType(dataType.withParameters(k, v)) .setValueType(generatedType.nestedType("Value").withParameters(k, v)) .build(), ImmutableMap.of())); }
Example #22
Source File: SignatureGenerator.java From j2objc with Apache License 2.0 | 5 votes |
/** * FormalTypeParameters: * < FormalTypeParameter+ > * * FormalTypeParameter: * Identifier ClassBound InterfaceBound* */ private void genOptFormalTypeParameters( List<? extends TypeParameterElement> typeParameters, StringBuilder sb) { if (!typeParameters.isEmpty()) { sb.append('<'); for (TypeParameterElement typeParam : typeParameters) { genFormalTypeParameter(typeParam, sb); } sb.append('>'); } }
Example #23
Source File: TestSymtabItems.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
@Override public Void visitTypeParameter(TypeParameterElement e, Void p) { show("type parameter", e); indent(+1); super.visitTypeParameter(e, p); indent(-1); return null; }
Example #24
Source File: MoreTypesTest.java From auto with Apache License 2.0 | 5 votes |
@Test public void asElement() { Elements elements = compilationRule.getElements(); TypeElement stringElement = elements.getTypeElement("java.lang.String"); assertThat(MoreTypes.asElement(stringElement.asType())).isEqualTo(stringElement); TypeParameterElement setParameterElement = Iterables.getOnlyElement( compilationRule.getElements().getTypeElement("java.util.Set").getTypeParameters()); assertThat(MoreTypes.asElement(setParameterElement.asType())).isEqualTo(setParameterElement); // we don't test error types because those are very hard to get predictably }
Example #25
Source File: TreeBackedEnter.java From buck with Apache License 2.0 | 5 votes |
private TreeBackedTypeParameterElement newTreeBackedTypeParameter( TypeParameterElement underlyingTypeParameter) { TreeBackedParameterizable enclosingElement = (TreeBackedParameterizable) getCurrentContext(); // TreeBackedExecutables with a null tree occur only for compiler-generated methods such // as default construvtors. Those never have type parameters, so we should never find // ourselves here without a tree. TreeBackedTypeParameterElement result = new TreeBackedTypeParameterElement( types, underlyingTypeParameter, getCurrentPath(), enclosingElement, canonicalizer); enterAnnotationMirrors(result); enclosingElement.addTypeParameter(result); return result; }
Example #26
Source File: ElementVisitor.java From netbeans with Apache License 2.0 | 5 votes |
@Override public Void visitTypeParameter(TypeParameterElement arg0, PrintStream arg1) { if(!canceled) { arg1.print("TypeParameter Element: "); arg1.print(arg0.getSimpleName()); long[] pos = getPosition(arg0); arg1.println(" "+pos[0]+" "+pos[1]); if(pos[1]!=-1) arg1.println(text.substring((int)pos[0],(int)pos[1])); return super.visitTypeParameter(arg0, arg1); } return null; }
Example #27
Source File: CodeGenerator.java From netbeans with Apache License 2.0 | 5 votes |
@Override public Tree visitTypeParameter(TypeParameterElement e, Void p) { List<ExpressionTree> bounds = new LinkedList<ExpressionTree>(); for (TypeMirror b : e.getBounds()) { bounds.add((ExpressionTree) make.Type(b)); } return make.TypeParameter(e.getSimpleName(), bounds); }
Example #28
Source File: DebugASTPrinter.java From j2objc with Apache License 2.0 | 5 votes |
protected void printTypeParameters(List<? extends TypeParameterElement> typeParams) { Iterator<? extends TypeParameterElement> it = typeParams.iterator(); if (it.hasNext()) { sb.print('<'); printTypeParameter(it.next()); while (it.hasNext()) { sb.print(','); printTypeParameter(it.next()); } sb.print('>'); } }
Example #29
Source File: TreeBackedTypeParameterElementTest.java From buck with Apache License 2.0 | 5 votes |
@Test public void testTypeParameterBoundedTypeParameter() throws IOException { compile("class Foo<T, U extends T> { }"); TypeElement fooElement = elements.getTypeElement("Foo"); TypeParameterElement uElement = fooElement.getTypeParameters().get(1); TypeMirror tType = fooElement.getTypeParameters().get(0).asType(); assertSameType(tType, uElement.getBounds().get(0)); }
Example #30
Source File: JavaUtils.java From netbeans with Apache License 2.0 | 5 votes |
private static HashMap<String, String> getErasureTypesMap(List<? extends TypeParameterElement> erasureArguments, List<? extends TypeMirror> typeArguments) { HashMap<String, String> result = new HashMap<String, String>(erasureArguments.size()); for (int i = 0; i < erasureArguments.size(); i++) { if (erasureArguments.size() == i || typeArguments.size() == i) { return result; } result.put(erasureArguments.get(i).toString(), typeArguments.get(i).toString()); } return result; }