use of com.squareup.javapoet.TypeSpec.Builder in project vue-gwt by Axellience.
the class ComponentJsTypeGenerator method getComponentJsTypeBuilder.
/**
* Create and return the builder for the JsType of our {@link VueComponent}.
* @param component The {@link VueComponent} we are generating for
* @param jsTypeClassName The name of the generated JsType class
* @return A Builder to build the class
*/
private Builder getComponentJsTypeBuilder(TypeElement component, ClassName jsTypeClassName) {
Builder componentJsTypeBuilder = TypeSpec.classBuilder(jsTypeClassName).addModifiers(Modifier.PUBLIC).superclass(TypeName.get(component.asType()));
// Add @JsType annotation. This ensure this class is included.
// As we use a class reference to use our Components, this class would be removed by GWT
// tree shaking.
componentJsTypeBuilder.addAnnotation(AnnotationSpec.builder(JsType.class).addMember("namespace", "\"VueGWT.javaComponentConstructors\"").addMember("name", "$S", component.getQualifiedName().toString().replaceAll("\\.", "_")).build());
// Add a block that registers the VueFactory for the VueComponent
componentJsTypeBuilder.addStaticBlock(CodeBlock.builder().addStatement("$T.onReady(() -> $T.register($S, () -> $T.get()))", VueGWT.class, VueGWT.class, component.getQualifiedName(), componentFactoryName(component)).build());
return componentJsTypeBuilder;
}
use of com.squareup.javapoet.TypeSpec.Builder in project tiger by google.
the class CoreInjectorGenerator method generateProvisionMethodIfNeeded.
private void generateProvisionMethodIfNeeded(BindingKey key, TypeElement referencingClass) {
messager.printMessage(Kind.NOTE, TAG + ".generateProvisionMethodIfNeeded, key: " + key + " ref: " + referencingClass);
// TODO: put all the dependency handling logic in one place
Set<DependencyInfo> dependencyInfos = utils.getDependencyInfosHandlingBox(dependencies, key);
DependencyInfo dependencyInfo = dependencyInfos == null ? null : Iterables.getFirst(dependencyInfos, null);
messager.printMessage(Kind.NOTE, TAG + ".generateProvisionMethodIfNeeded, dI: " + dependencyInfo);
// TODO: handle boxing better.
if (dependencyInfo != null) {
key = dependencyInfo.getDependant();
}
ClassName packagedInjectorClassName = getInjectorNameFor(key, referencingClass);
Builder injectorSpecBuilder = getInjectorTypeSpecBuilder(packagedInjectorClassName);
if (!generatedBindingsForPackagedInjector.put(packagedInjectorClassName, key)) {
return;
}
// messager.printMessage(Kind.NOTE, "generateProvisionMethodIfNeeded, DependencyInfo: " +
// dependencyInfo);
// messager.printMessage(Kind.NOTE, "generateProvisionMethodIfNeeded, scope: " +
// scopeCalculator.calculate(key));
boolean scoped = explicitScopes.contains(key);
String suffix = scoped ? UNSCOPED_SUFFIX : "";
/**
* TODO: revist this and handle it in a consistent way with the ones below. This is related with
* {@link Utils#getDependencyInfo(SetMultimap, BindingKey)}.
*/
if (utils.isOptional(key) && utils.isBindsOptionalOf(utils.getDependencyInfo(dependencies, key))) {
generateProvisionMethodForBindsOptionalOf(key, referencingClass, suffix);
} else if (dependencyInfo != null) {
switch(dependencyInfo.getType()) {
case SET:
case SET_VALUES:
// TODO: revisit scoped
scoped = false;
generateProvisionMethodForSet(key, referencingClass, "");
break;
case MAP:
// TODO: refactor here and below.
// TODO: revisit scoped
scoped = false;
generateProvisionMethodForMap(key, referencingClass, "");
break;
case UNIQUE:
switch(dependencyInfo.getDependencySourceType()) {
case MODULE:
generateUniqueTypeProvisionMethodFromModule(key, suffix);
break;
case CTOR_INJECTED_CLASS:
generateProvisionMethodFromClass(key, referencingClass, suffix);
break;
case DAGGER_MEMBERS_INJECTOR:
generateProvisionMethodForDaggerMembersInjector(key, referencingClass, suffix);
break;
case COMPONENT_DEPENDENCIES_METHOD:
generateProvisionMethodFromComponentDependency(key, referencingClass);
break;
case BINDS_INTANCE:
case COMPONENT_DEPENDENCIES_ITSELF:
case EITHER_COMPONENT:
generateProvisionMethodFromBindsInstance(key, referencingClass);
break;
case EITHER_COMPONENT_BUILDER:
generateProvisionMethodForEitherComponentBuilder(key, referencingClass);
break;
default:
throw new RuntimeException("Shouln't be here. dependencyInfo.dependencySourceType: " + dependencyInfo.getDependencySourceType());
}
break;
default:
throw new RuntimeException("Unknown dependencyInfo.type: " + dependencyInfo.getType());
}
} else if (utils.isEitherComponentBuilder(processingEnv, key)) {
// TODO: remove?
errors.add("should not generate provision method for (sub)component builder");
} else if (utils.isProviderOrLazy(key)) {
generateProvisionMethodForProviderOrLazy(key, referencingClass, suffix);
} else if (utils.isMap(key)) {
generateProvisionMethodForMap(key, referencingClass, suffix);
} else {
DependencyInfo genericDependencyInfo = utils.getDependencyInfoByGeneric(dependencies, key);
if (genericDependencyInfo != null) {
if (genericDependencyInfo.getProvisionMethodElement() == null) {
generateProvisionMethodFromClass(key, referencingClass, suffix);
} else {
errors.add(String.format("Generic provision method not supported yet: %s -> %s", key, genericDependencyInfo));
}
} else {
errors.add(String.format("Cannot resolve %s.", key));
throw new RuntimeException();
}
}
if (scoped) {
generateField(injectorSpecBuilder, key);
generateScopedProvisionMethod(injectorSpecBuilder, key);
}
}
use of com.squareup.javapoet.TypeSpec.Builder in project tiger by google.
the class NewInjectorGenerator method generateProvisionMethodFromClass.
/**
* Handles generic.
*/
private void generateProvisionMethodFromClass(NewBindingKey key, TypeElement referencingClass, String suffix) {
// System.out.println(
// "generateProvisionMethodFromClass. key: " + key + " referencingClass: " +
// referencingClass);
ClassName packagedInjectorClassName = getInjectorFor(key, referencingClass);
TypeSpec.Builder injectorSpecBuilder = getInjectorTypeSpecBuilder(packagedInjectorClassName);
TypeElement cls = getClassFromKey(key);
ExecutableElement ctor = Utils.findInjectedCtor(cls, processingEnv);
Preconditions.checkNotNull(ctor, String.format("Did not find ctor for %s", cls));
List<NewBindingKey> dependencyKeys = Utils.getDependenciesFromExecutableElement(ctor);
if (key.getTypeName() instanceof ParameterizedTypeName) {
List<NewBindingKey> specializedKeys = new ArrayList<>();
Map<TypeVariableName, TypeName> map = Utils.getMapFromTypeVariableToSpecialized((ParameterizedTypeName) key.getTypeName(), (ParameterizedTypeName) TypeName.get(cls.asType()));
for (NewBindingKey k : dependencyKeys) {
specializedKeys.add(Utils.specializeIfNeeded(k, map));
}
dependencyKeys = specializedKeys;
}
// System.out.println("generateProvisionMethodFromClass. dependencyKeys: " +
// dependencyKeys);
MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(getProvisionMethodName(key) + suffix);
methodSpecBuilder.addModifiers(suffix.isEmpty() ? Modifier.PUBLIC : Modifier.PRIVATE).returns(key.getTypeName());
StringBuilder builder = new StringBuilder("$T result = new $T(");
if (dependencyKeys.size() > 0) {
for (NewBindingKey dependencyKey : dependencyKeys) {
// System.out.println("generateProvisionMethodFromClass. dependencyKey: "
// + dependencyKey);
generateProvisionMethodAndAppendAsParameter(dependencyKey, cls, packagedInjectorClassName, builder);
}
builder.delete(builder.length() - 2, builder.length());
}
builder.append(")");
methodSpecBuilder.addStatement(builder.toString(), key.getTypeName(), key.getTypeName());
if (Utils.hasInjectedFieldsOrMethods(cls, processingEnv)) {
// System.out.println("generateProvisionMethodFromClass. hasInjected");
generateInjectionMethod(key, referencingClass);
methodSpecBuilder.addStatement("inject(result)");
}
methodSpecBuilder.addStatement("return result");
injectorSpecBuilder.addMethod(methodSpecBuilder.build());
}
use of com.squareup.javapoet.TypeSpec.Builder in project tiger by google.
the class NewInjectorGenerator method generateProvisionMethodForProviderOrLazy.
/**
* For key like javax.inject.Provider<Foo> and dagger.Lazy<Foo>. Qualifier, if presented, will
* also apply to element binding.
*/
private void generateProvisionMethodForProviderOrLazy(NewBindingKey key, TypeElement referencingClass, String suffix) {
// System.out.println(String.format(
// "generateProvisionMethodForProviderOrLazy: key %s, referencingClass: %s, suffix : %s.", key,
// referencingClass, suffix));
ClassName injectorClassName = getInjectorFor(key, referencingClass);
TypeSpec anonymousTypeSpec = createAnonymousBuiltinTypeForUniqueBinding(injectorClassName, key, referencingClass);
MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(getProvisionMethodName(key) + suffix).addModifiers(suffix.isEmpty() ? Modifier.PUBLIC : Modifier.PRIVATE).returns(key.getTypeName()).addStatement("return $L", anonymousTypeSpec);
Builder componentSpecBuilder = getInjectorTypeSpecBuilder(getInjectorFor(key, referencingClass));
componentSpecBuilder.addMethod(methodSpecBuilder.build());
}
use of com.squareup.javapoet.TypeSpec.Builder in project tiger by google.
the class NewInjectorGenerator method generateInjectionMethod.
private void generateInjectionMethod(TypeElement cls, ClassName packagedInjectorClassName, String methodName, boolean isPublic) {
if (!injectedClassNamesForPackagedInjector.put(packagedInjectorClassName, ClassName.get(cls))) {
return;
}
// messager.printMessage(Kind.NOTE,
// String.format("generateInjectionMethod. cls: %s, injector: %s, method: %s", cls,
// packagedInjectorClassName, methodName));
Builder componentSpecBuilder = getInjectorTypeSpecBuilder(packagedInjectorClassName);
MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(methodName).addModifiers(isPublic ? Modifier.PUBLIC : Modifier.PRIVATE).addParameter(ClassName.get(cls), "arg");
// Inject closest ancestor first.
TypeElement clsClosestInjectAncestor = getSuper(cls);
while (clsClosestInjectAncestor != null && !Utils.hasInjectedFieldsOrMethods(clsClosestInjectAncestor, processingEnv)) {
clsClosestInjectAncestor = getSuper(clsClosestInjectAncestor);
}
if (clsClosestInjectAncestor != null) {
TypeElement scope = getComponentFromPackagedInjectorClassName(packagedInjectorClassName).getScope();
generateInjectionMethod(clsClosestInjectAncestor, scope);
ClassName ancestorPackagedInjector = getPackagedInjectorNameOfScope(Utils.getPackageString(clsClosestInjectAncestor), scope);
StringBuilder stringBuilder = new StringBuilder();
if (!ancestorPackagedInjector.equals(packagedInjectorClassName)) {
stringBuilder.append(TOP_LEVEL_INJECTOR_FIELD).append(".$N().");
}
stringBuilder.append("inject(($T) arg)");
if (!ancestorPackagedInjector.equals(packagedInjectorClassName)) {
methodSpecBuilder.addStatement(stringBuilder.toString(), Utils.getGetMethodName(ancestorPackagedInjector), ClassName.get(clsClosestInjectAncestor));
} else {
// TODO(freeman): ClassName.get() removed the type parameters for now. Support it.
methodSpecBuilder.addStatement(stringBuilder.toString(), ClassName.get(clsClosestInjectAncestor));
}
}
for (VariableElement field : Utils.getInjectedFields(cls, processingEnv)) {
// System.out.println("generateInjectionMethod. field: " + field);
TypeMirror fieldType = field.asType();
AnnotationMirror fieldQualifier = Utils.getQualifier(field);
NewBindingKey fieldKey = NewBindingKey.get(fieldType, fieldQualifier);
StringBuilder stringBuilder = new StringBuilder("arg.").append(field.getSimpleName()).append(" = ");
addCallingProvisionMethod(stringBuilder, fieldKey, cls, packagedInjectorClassName);
methodSpecBuilder.addStatement(stringBuilder.toString());
}
for (ExecutableElement method : Utils.getInjectedMethods(cls, processingEnv)) {
// System.out.println("generateInjectionMethod. method: " + method);
StringBuilder builder = new StringBuilder("arg.").append(method.getSimpleName()).append("(");
List<NewBindingKey> methodArgs = Utils.getDependenciesFromExecutableElement(method);
if (methodArgs.size() > 0) {
for (NewBindingKey dependentKey : methodArgs) {
addCallingProvisionMethod(builder, dependentKey, cls, packagedInjectorClassName);
builder.append(", ");
}
builder.delete(builder.length() - 2, builder.length());
}
builder.append(")");
methodSpecBuilder.addStatement(builder.toString());
}
componentSpecBuilder.addMethod(methodSpecBuilder.build());
}
Aggregations