Search in sources :

Example 76 with ExecutableType

use of javax.lang.model.type.ExecutableType in project tiger by google.

the class Tiger2InjectorGenerator method generateImplicitMethods.

/**
 * Generates those methods that are not declared in the (sub)component interface.
 */
protected void generateImplicitMethods() {
    utils.traverseAndDo(types, (DeclaredType) eitherComponent.asType(), eitherComponent, p -> {
        Element e = p.second;
        messager.printMessage(Kind.NOTE, "generatePackagedInjectors: element: " + e);
        if (!utils.isMethod(e)) {
            return null;
        }
        ExecutableElement method = (ExecutableElement) e;
        ExecutableType methodType = (ExecutableType) p.first;
        if (utils.isInjectionMethod(method)) {
            TypeElement injectedTypeElement = (TypeElement) ((DeclaredType) Iterables.getOnlyElement(methodType.getParameterTypes())).asElement();
            messager.printMessage(Kind.NOTE, TAG + ".generatePackagedInjectors: injection method for: " + injectedTypeElement);
            generateInjectionMethod(injectedTypeElement, "inject");
        } else if (utils.isProvisionMethodInInjector(method)) {
            generateProvisionMethodIfNeeded(utils.getKeyProvidedByMethod(method));
        } else {
            // TODO: ignore known elements like builders.
            messager.printMessage(Kind.WARNING, String.format("Unknown element %s from injector %s.", method, eitherComponent));
        }
        return null;
    });
    messager.printMessage(Kind.NOTE, TAG + ".generatePackagedInjectors. packagedInjectorBuilders: " + packagedInjectorBuilders);
// Inherited provision methods.
// for (CoreInjectorInfo component : orderedCoreinjectors) {
// if (componentTree.get(component) == null) {
// continue;
// }
// for (Map.Entry<ClassName, Builder> entry : packagedInjectorBuilders.entrySet()) {
// ClassName packagedInjectorClassName = entry.getKey();
// if (!component
// .equals(getComponentFromPackagedInjectorClassName(packagedInjectorClassName))) {
// continue;
// }
// generateInheritedProvisionMethods(packagedInjectorClassName);
// }
// }
// Inherited injection methods.
// for (CoreInjectorInfo component : orderedCoreinjectors) {
// if (componentTree.get(component) == null) {
// continue;
// }
// for (Map.Entry<ClassName, Builder> entry : packagedInjectorBuilders.entrySet()) {
// ClassName packagedInjectorClassName = entry.getKey();
// if (!component
// .equals(getComponentFromPackagedInjectorClassName(packagedInjectorClassName))) {
// continue;
// }
// generateInheritedInjectionMethods(packagedInjectorClassName);
// }
// }
// JavaFile javaFile =
// JavaFile.builder(utils.getPackage(eitherComponent).getQualifiedName().toString(),
// builder.build()).build();
// messager.printMessage(
// Kind.NOTE, "javaFile for: " + builder.build() + "\n" + javaFile.toString());
// messager.printMessage(Kind.NOTE, String.format("java file: %s", javaFile));
// try {
// // javaFile.writeTo(processingEnv.getFiler());
// } catch (IOException e) {
// Throwables.propagate(e);
// }
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) TypeElement(javax.lang.model.element.TypeElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 77 with ExecutableType

use of javax.lang.model.type.ExecutableType in project tiger by google.

the class Tiger2InjectorGenerator method getProduced.

@Override
protected Pair<Set<BindingKey>, Set<BindingKey>> getProduced() {
    Set<BindingKey> provided = new HashSet<>();
    Set<BindingKey> injected = new HashSet<>();
    DeclaredType eitherComponentType = (DeclaredType) eitherComponent.asType();
    utils.traverseAndDo(types, eitherComponentType, eitherComponent, p -> {
        Element element = p.second;
        messager.printMessage(Kind.NOTE, "method: " + element);
        if (!element.getKind().equals(ElementKind.METHOD)) {
            return null;
        }
        ExecutableElement method = (ExecutableElement) element;
        ExecutableType methodType = (ExecutableType) processingEnv.getTypeUtils().asMemberOf(eitherComponentType, method);
        // Injection methods.
        if (utils.isInjectionMethod(element)) {
            TypeMirror typeMirror = Iterables.getOnlyElement(methodType.getParameterTypes());
            injected.add(BindingKey.get(typeMirror));
        } else if (utils.isProvisionMethodInInjector(element)) {
            provided.add(utils.getKeyProvidedByMethod(method));
        } else if (isIrrelevantMethodInInjector(element)) {
        // do nothing
        } else {
            messager.printMessage(Kind.WARNING, String.format("Element %s ignored from injector %s.", element, eitherComponentType));
        }
        return null;
    });
    return Pair.of(provided, injected);
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) TypeMirror(javax.lang.model.type.TypeMirror) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) HashSet(java.util.HashSet) DeclaredType(javax.lang.model.type.DeclaredType)

Example 78 with ExecutableType

use of javax.lang.model.type.ExecutableType in project tiger by google.

the class Utils method isBuilderSetter.

public boolean isBuilderSetter(Types types, TypeElement builder, Element element) {
    if (!isMethod(element)) {
        return false;
    }
    ExecutableElement method = (ExecutableElement) element;
    if (!method.getModifiers().contains(Modifier.ABSTRACT)) {
        return false;
    }
    List<? extends VariableElement> parameters = method.getParameters();
    if (parameters.size() != 1) {
        return false;
    }
    ExecutableType executableType = (ExecutableType) types.asMemberOf((DeclaredType) builder.asType(), method);
    // messager.printMessage(Kind.NOTE, TAG + " getBuilderSetterName. After " + executableType);
    boolean returnVoid = method.getReturnType().getKind() == TypeKind.VOID;
    if (!types.isSubtype(builder.asType(), executableType.getReturnType()) && !returnVoid) {
        return false;
    }
    return true;
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) ExecutableElement(javax.lang.model.element.ExecutableElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 79 with ExecutableType

use of javax.lang.model.type.ExecutableType in project tiger by google.

the class Utils method getBuilderSetter.

/**
 * Returns the setter in the builder that sets the given key, null if not found.
 */
@Nullable
public ExecutableElement getBuilderSetter(Types types, Elements elementUtils, TypeElement builder, BindingKey key) {
    logger.n("builder: %s key: %s", builder, key);
    for (Element element : elementUtils.getAllMembers(builder)) {
        if (!isBuilderSetter(types, builder, element)) {
            continue;
        }
        logger.n("builder: %s setter: %s", builder, element);
        ExecutableElement method = (ExecutableElement) element;
        ExecutableType executableType = (ExecutableType) types.asMemberOf((DeclaredType) builder.asType(), method);
        TypeMirror argumentType = Iterables.getOnlyElement(executableType.getParameterTypes());
        AnnotationMirror annotationTypeMirror = getQualifier(Iterables.getOnlyElement(method.getParameters()));
        if (!BindingKey.get(argumentType, annotationTypeMirror).equals(key)) {
            continue;
        }
        return method;
    }
    return null;
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) DeclaredType(javax.lang.model.type.DeclaredType) Nullable(javax.annotation.Nullable)

Example 80 with ExecutableType

use of javax.lang.model.type.ExecutableType in project javapoet by square.

the class MethodSpec method overriding.

/**
 * 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;
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) TypeMirror(javax.lang.model.type.TypeMirror)

Aggregations

ExecutableType (javax.lang.model.type.ExecutableType)83 ExecutableElement (javax.lang.model.element.ExecutableElement)64 DeclaredType (javax.lang.model.type.DeclaredType)47 TypeMirror (javax.lang.model.type.TypeMirror)45 TypeElement (javax.lang.model.element.TypeElement)44 Element (javax.lang.model.element.Element)33 VariableElement (javax.lang.model.element.VariableElement)33 Nonnull (javax.annotation.Nonnull)22 ArrayList (java.util.ArrayList)18 MethodSpec (com.squareup.javapoet.MethodSpec)15 HashMap (java.util.HashMap)15 Map (java.util.Map)15 AnnotationMirror (javax.lang.model.element.AnnotationMirror)15 TypeSpec (com.squareup.javapoet.TypeSpec)14 HashSet (java.util.HashSet)14 TypeKind (javax.lang.model.type.TypeKind)14 IOException (java.io.IOException)13 Nullable (javax.annotation.Nullable)13 PackageElement (javax.lang.model.element.PackageElement)13 List (java.util.List)12