Java Code Examples for javax.lang.model.util.Types#asMemberOf()
The following examples show how to use
javax.lang.model.util.Types#asMemberOf() .
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: MoreTypes.java From auto-parcel with Apache License 2.0 | 6 votes |
/** * Resolves a {@link VariableElement} parameter to a method or constructor based on the given * container, or a member of a class. For parameters to a method or constructor, the variable's * enclosing element must be a supertype of the container type. For example, given a * {@code container} of type {@code Set<String>}, and a variable corresponding to the {@code E e} * parameter in the {@code Set.add(E e)} method, this will return a TypeMirror for {@code String}. */ public static TypeMirror asMemberOf(Types types, DeclaredType container, VariableElement variable) { if (variable.getKind().equals(ElementKind.PARAMETER)) { ExecutableElement methodOrConstructor = MoreElements.asExecutable(variable.getEnclosingElement()); ExecutableType resolvedMethodOrConstructor = MoreTypes.asExecutable( types.asMemberOf(container, methodOrConstructor)); List<? extends VariableElement> parameters = methodOrConstructor.getParameters(); List<? extends TypeMirror> parameterTypes = resolvedMethodOrConstructor.getParameterTypes(); checkState(parameters.size() == parameterTypes.size()); for (int i = 0; i < parameters.size(); i++) { // We need to capture the parameter type of the variable we're concerned about, // for later printing. This is the only way to do it since we can't use // types.asMemberOf on variables of methods. if (parameters.get(i).equals(variable)) { return parameterTypes.get(i); } } throw new IllegalStateException("Could not find variable: " + variable); } else { return types.asMemberOf(container, variable); } }
Example 2
Source File: MethodSpec.java From JReFrameworker with MIT License | 6 votes |
/** * Returns a new method spec builder that overrides {@code method} as a member of {@code * enclosing}. This will resolve type parameters: for example overriding {@link * Comparable#compareTo} in a type that implements {@code Comparable<Movie>}, the {@code T} * parameter will be resolved to {@code Movie}. * * <p>This will copy its visibility modifiers, type parameters, return type, name, parameters, and * throws declarations. An {@link Override} annotation will be added. * * <p>Note that in JavaPoet 1.2 through 1.7 this method retained annotations from the method and * parameters of the overridden method. Since JavaPoet 1.8 annotations must be added separately. */ public static Builder overriding( ExecutableElement method, DeclaredType enclosing, Types types) { ExecutableType executableType = (ExecutableType) types.asMemberOf(enclosing, method); List<? extends TypeMirror> resolvedParameterTypes = executableType.getParameterTypes(); TypeMirror resolvedReturnType = executableType.getReturnType(); Builder builder = overriding(method); builder.returns(TypeName.get(resolvedReturnType)); for (int i = 0, size = builder.parameters.size(); i < size; i++) { ParameterSpec parameter = builder.parameters.get(i); TypeName type = TypeName.get(resolvedParameterTypes.get(i)); builder.parameters.set(i, parameter.toBuilder(type, parameter.name).build()); } return builder; }
Example 3
Source File: MethodSpec.java From JReFrameworker with MIT License | 6 votes |
/** * Returns a new method spec builder that overrides {@code method} as a member of {@code * enclosing}. This will resolve type parameters: for example overriding {@link * Comparable#compareTo} in a type that implements {@code Comparable<Movie>}, the {@code T} * parameter will be resolved to {@code Movie}. * * <p>This will copy its visibility modifiers, type parameters, return type, name, parameters, and * throws declarations. An {@link Override} annotation will be added. * * <p>Note that in JavaPoet 1.2 through 1.7 this method retained annotations from the method and * parameters of the overridden method. Since JavaPoet 1.8 annotations must be added separately. */ public static Builder overriding( ExecutableElement method, DeclaredType enclosing, Types types) { ExecutableType executableType = (ExecutableType) types.asMemberOf(enclosing, method); List<? extends TypeMirror> resolvedParameterTypes = executableType.getParameterTypes(); TypeMirror resolvedReturnType = executableType.getReturnType(); Builder builder = overriding(method); builder.returns(TypeName.get(resolvedReturnType)); for (int i = 0, size = builder.parameters.size(); i < size; i++) { ParameterSpec parameter = builder.parameters.get(i); TypeName type = TypeName.get(resolvedParameterTypes.get(i)); builder.parameters.set(i, parameter.toBuilder(type, parameter.name).build()); } return builder; }
Example 4
Source File: FunctionalType.java From FreeBuilder with Apache License 2.0 | 6 votes |
public static Optional<FunctionalType> maybeFunctionalType( DeclaredType type, Elements elements, Types types) { TypeElement typeElement = asElement(type); if (!typeElement.getKind().isInterface()) { return Optional.empty(); } Set<ExecutableElement> abstractMethods = only(ABSTRACT, methodsOn(typeElement, elements, errorType -> { })); if (abstractMethods.size() != 1) { return Optional.empty(); } ExecutableElement method = getOnlyElement(abstractMethods); ExecutableType methodType = (ExecutableType) types.asMemberOf(type, method); return Optional.of(new FunctionalType( Type.from(type), method.getSimpleName().toString(), methodType.getParameterTypes(), methodType.getReturnType())); }
Example 5
Source File: Helper.java From vertx-codegen with Apache License 2.0 | 6 votes |
/** * Return the type of a type parameter element of a given type element when that type parameter * element is parameterized by a sub type, directly or indirectly. When the type parameter cannot * be resolved, null is returned. * * @param typeUtils the type utils * @param subType the sub type for which the type parameter is parameterized * @param typeParam the type parameter to resolve * @return the type parameterizing the type parameter */ public static TypeMirror resolveTypeParameter(Types typeUtils, DeclaredType subType, TypeParameterElement typeParam) { TypeMirror erased = typeUtils.erasure(typeParam.getGenericElement().asType()); TypeMirror erasedSubType = typeUtils.erasure(subType); if (typeUtils.isSameType(erased, erasedSubType)) { return typeUtils.asMemberOf(subType, ((TypeVariable) typeParam.asType()).asElement()); } else if (typeUtils.isSubtype(erasedSubType, erased)) { for (TypeMirror superType : typeUtils.directSupertypes(subType)) { TypeMirror resolved = resolveTypeParameter(typeUtils, (DeclaredType) superType, typeParam); if (resolved != null) { return resolved; } } } return null; }
Example 6
Source File: Elements2.java From auto with Apache License 2.0 | 6 votes |
/** * Given an executable element in a supertype, returns its ExecutableType when it is viewed as a * member of a subtype. */ static ExecutableType getExecutableElementAsMemberOf( Types types, ExecutableElement executableElement, TypeElement subTypeElement) { checkNotNull(types); checkNotNull(executableElement); checkNotNull(subTypeElement); TypeMirror subTypeMirror = subTypeElement.asType(); if (!subTypeMirror.getKind().equals(TypeKind.DECLARED)) { throw new IllegalStateException( "Expected subTypeElement.asType() to return a class/interface type."); } TypeMirror subExecutableTypeMirror = types.asMemberOf( (DeclaredType) subTypeMirror, executableElement); if (!subExecutableTypeMirror.getKind().equals(TypeKind.EXECUTABLE)) { throw new IllegalStateException("Expected subExecutableTypeMirror to be an executable type."); } return (ExecutableType) subExecutableTypeMirror; }
Example 7
Source File: MoreTypes.java From auto with Apache License 2.0 | 6 votes |
/** * Resolves a {@link VariableElement} parameter to a method or constructor based on the given * container, or a member of a class. For parameters to a method or constructor, the variable's * enclosing element must be a supertype of the container type. For example, given a * {@code container} of type {@code Set<String>}, and a variable corresponding to the {@code E e} * parameter in the {@code Set.add(E e)} method, this will return a TypeMirror for {@code String}. */ public static TypeMirror asMemberOf(Types types, DeclaredType container, VariableElement variable) { if (variable.getKind().equals(ElementKind.PARAMETER)) { ExecutableElement methodOrConstructor = MoreElements.asExecutable(variable.getEnclosingElement()); ExecutableType resolvedMethodOrConstructor = MoreTypes.asExecutable(types.asMemberOf(container, methodOrConstructor)); List<? extends VariableElement> parameters = methodOrConstructor.getParameters(); List<? extends TypeMirror> parameterTypes = resolvedMethodOrConstructor.getParameterTypes(); checkState(parameters.size() == parameterTypes.size()); for (int i = 0; i < parameters.size(); i++) { // We need to capture the parameter type of the variable we're concerned about, // for later printing. This is the only way to do it since we can't use // types.asMemberOf on variables of methods. if (parameters.get(i).equals(variable)) { return parameterTypes.get(i); } } throw new IllegalStateException("Could not find variable: " + variable); } else { return types.asMemberOf(container, variable); } }
Example 8
Source File: MethodSpec.java From javapoet with Apache License 2.0 | 6 votes |
/** * Returns a new method spec builder that overrides {@code method} as a member of {@code * enclosing}. This will resolve type parameters: for example overriding {@link * Comparable#compareTo} in a type that implements {@code Comparable<Movie>}, the {@code T} * parameter will be resolved to {@code Movie}. * * <p>This will copy its visibility modifiers, type parameters, return type, name, parameters, and * throws declarations. An {@link Override} annotation will be added. * * <p>Note that in JavaPoet 1.2 through 1.7 this method retained annotations from the method and * parameters of the overridden method. Since JavaPoet 1.8 annotations must be added separately. */ public static Builder overriding( ExecutableElement method, DeclaredType enclosing, Types types) { ExecutableType executableType = (ExecutableType) types.asMemberOf(enclosing, method); List<? extends TypeMirror> resolvedParameterTypes = executableType.getParameterTypes(); List<? extends TypeMirror> resolvedThrownTypes = executableType.getThrownTypes(); TypeMirror resolvedReturnType = executableType.getReturnType(); Builder builder = overriding(method); builder.returns(TypeName.get(resolvedReturnType)); for (int i = 0, size = builder.parameters.size(); i < size; i++) { ParameterSpec parameter = builder.parameters.get(i); TypeName type = TypeName.get(resolvedParameterTypes.get(i)); builder.parameters.set(i, parameter.toBuilder(type, parameter.name).build()); } builder.exceptions.clear(); for (int i = 0, size = resolvedThrownTypes.size(); i < size; i++) { builder.addException(TypeName.get(resolvedThrownTypes.get(i))); } return builder; }
Example 9
Source File: BaseTask.java From netbeans with Apache License 2.0 | 5 votes |
TypeMirror asMemberOf(Element element, TypeMirror type, Types types) { TypeMirror ret = element.asType(); TypeMirror enclType = element.getEnclosingElement().asType(); if (enclType.getKind() == TypeKind.DECLARED) { enclType = types.erasure(enclType); } while (type != null && type.getKind() == TypeKind.DECLARED) { if ((enclType.getKind() != TypeKind.DECLARED || ((DeclaredType) enclType).asElement().getSimpleName().length() > 0) && types.isSubtype(type, enclType)) { ret = types.asMemberOf((DeclaredType) type, element); break; } type = ((DeclaredType) type).getEnclosingType(); } return ret; }
Example 10
Source File: JavadocCompletionQuery.java From netbeans with Apache License 2.0 | 5 votes |
private static TypeMirror asMemberOf(Element element, TypeMirror type, Types types) { TypeMirror ret = element.asType(); TypeMirror enclType = element.getEnclosingElement().asType(); if (enclType.getKind() == TypeKind.DECLARED) enclType = types.erasure(enclType); while(type != null && type.getKind() == TypeKind.DECLARED) { if (types.isSubtype(type, enclType)) { ret = types.asMemberOf((DeclaredType)type, element); break; } type = ((DeclaredType)type).getEnclosingType(); } return ret; }
Example 11
Source File: ModelUtils.java From FreeBuilder with Apache License 2.0 | 5 votes |
/** * Determines the return type of {@code method}, if called on an instance of type {@code type}. * * <p>For instance, in this example, myY.getProperty() returns List<T>, not T:<pre><code> * interface X<T> { * T getProperty(); * } * @FreeBuilder interface Y<T> extends X<List<T>> { }</code></pre> * * <p>(Unfortunately, a bug in Eclipse prevents us handling these cases correctly at the moment. * javac works fine.) */ public static TypeMirror getReturnType(TypeElement type, ExecutableElement method, Types types) { try { ExecutableType executableType = (ExecutableType) types.asMemberOf((DeclaredType) type.asType(), method); return executableType.getReturnType(); } catch (IllegalArgumentException e) { // Eclipse incorrectly throws an IllegalArgumentException here: // "element is not valid for the containing declared type" // As a workaround for the common case, fall back to the declared return type. return method.getReturnType(); } }
Example 12
Source File: Descriptor.java From auto-matter with Apache License 2.0 | 5 votes |
private void enumerateFields(final Types types) { final List<ExecutableElement> methods = methods(valueTypeElement); for (final Element member : methods) { if (member.getKind() != ElementKind.METHOD || isStaticOrDefault(member)) { continue; } final ExecutableElement method = (ExecutableElement) member; if (member.getSimpleName().toString().equals("builder")) { final TypeMirror returnType = (method).getReturnType(); // TODO: javac does not seem to want to provide the name of the return type if it is not yet present and generic if (!isGeneric && !returnType.toString().equals(concreteBuilderName) && !returnType.toString().equals(fullyQualifiedBuilderName)) { throw new AutoMatterProcessorException( "builder() return type must be " + concreteBuilderName, valueTypeElement); } toBuilder = true; continue; } verifyResolved(method.getReturnType()); fields.add(method); // Resolve inherited members final ExecutableType methodType = (ExecutableType) types.asMemberOf(valueType, member); final TypeMirror fieldType = methodType.getReturnType(); // Resolve types fieldTypes.put(method, TypeName.get(fieldType)); } }
Example 13
Source File: CreateRegistrationProcessor.java From netbeans with Apache License 2.0 | 4 votes |
private void checkMimeLocation(TypeElement clazz, AnnotationMirror am) { for (Entry<? extends ExecutableElement, ? extends AnnotationValue> e : am.getElementValues().entrySet()) { if (!e.getKey().getSimpleName().contentEquals("instanceProviderClass")) continue; TypeMirror ipc = (TypeMirror) e.getValue().getValue(); if (ipc == null || ipc.getKind() != TypeKind.DECLARED) continue; //the compiler should have given the error TypeElement instanceProvider = processingEnv.getElementUtils().getTypeElement("org.netbeans.spi.editor.mimelookup.InstanceProvider"); if (instanceProvider == null) { return ; } ExecutableElement createInstance = null; for (ExecutableElement ee : ElementFilter.methodsIn(instanceProvider.getEnclosedElements())) { if (ee.getSimpleName().contentEquals("createInstance")) { //TODO: check parameters createInstance = ee; break; } } if (createInstance == null) { throw new IllegalStateException("No instanceCreate in InstanceProvider!"); } DeclaredType dipc = (DeclaredType) ipc; Types tu = processingEnv.getTypeUtils(); ExecutableType member = (ExecutableType) tu.asMemberOf(dipc, createInstance); TypeMirror result = member.getReturnType(); TypeMirror jlObject = processingEnv.getElementUtils().getTypeElement("java.lang.Object").asType(); if (!tu.isSameType(tu.erasure(result), jlObject)) { if (!tu.isSubtype(tu.erasure(result), tu.erasure(clazz.asType()))) { processingEnv.getMessager().printMessage(Kind.ERROR, "The InstanceProvider does not create instances of type " + clazz.getQualifiedName(), clazz, am, e.getValue()); } } TypeElement tipc = (TypeElement) dipc.asElement(); if (!tipc.getModifiers().contains(Modifier.PUBLIC)) { processingEnv.getMessager().printMessage(Kind.ERROR, "The InstanceProvider implementation is not public.", clazz, am, e.getValue()); } for (ExecutableElement c : ElementFilter.constructorsIn(tipc.getEnclosedElements())) { if (c.getParameters().isEmpty() && c.getModifiers().contains(Modifier.PUBLIC)) { //OK return; } } processingEnv.getMessager().printMessage(Kind.ERROR, "The InstanceProvider implementation does not provide a public no-arg constructor.", clazz, am, e.getValue()); } }