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