use of com.squareup.javapoet.TypeName in project RxCache by VictorAlbertos.
the class BrewJavaFile method getActionProvider.
private MethodSpec getActionProvider(ClassName providersClassName, ProvidersClass.Method method) {
String methodName = method.name;
TypeName list = ClassName.get(method.enclosingTypeObservable);
TypeName enclosingTypeList = ClassName.get(method.enclosingTypeObservable.getTypeArguments().get(0));
String enclosingTypeListName = enclosingTypeList.toString();
ParameterizedTypeName action = ParameterizedTypeName.get(ClassName.get(ActionsList.class), enclosingTypeList);
ParameterizedTypeName evict = ParameterizedTypeName.get(ClassName.get(ActionsList.Evict.class), enclosingTypeList);
ParameterizedTypeName arrayList = ParameterizedTypeName.get(ClassName.get(ArrayList.class), enclosingTypeList);
ParameterizedTypeName observable = ParameterizedTypeName.get(ClassName.get(Observable.class), list);
ParameterSpec rxProvidersInstance = ParameterSpec.builder(providersClassName, "proxy").addModifiers(Modifier.FINAL).build();
MethodSpec.Builder builder = MethodSpec.methodBuilder(methodName).addModifiers(Modifier.PUBLIC, Modifier.STATIC).addParameter(rxProvidersInstance).returns(action);
if (method.hasDynamicKey) {
builder.addParameter(DynamicKey.class, "dynamicKey", Modifier.FINAL);
} else if (method.hasDynamicKeyGroup) {
builder.addParameter(DynamicKeyGroup.class, "dynamicKeyGroup", Modifier.FINAL);
}
builder.beginControlFlow("$T evict = new $T()", evict, evict).beginControlFlow("@Override public $T call($T elements)", observable, observable);
if (method.hasDynamicKey) {
setReturnEvictForEvictDynamicKey(builder, methodName);
} else if (method.hasDynamicKeyGroup) {
setReturnEvictForEvictDynamicKeyGroup(builder, methodName);
} else {
setReturnEvictForEvictProvider(builder, methodName);
}
builder.endControlFlow().endControlFlow(";");
if (method.hasDynamicKey) {
setCacheForEvictDynamicKey(builder, observable, arrayList, methodName, enclosingTypeListName);
} else if (method.hasDynamicKeyGroup) {
setCacheForEvictDynamicKeyGroup(builder, observable, arrayList, methodName, enclosingTypeListName);
} else {
setCacheForEvictProvider(builder, observable, arrayList, methodName, enclosingTypeListName);
}
builder.addStatement("return ActionsList.with(evict, oCache)");
return builder.build();
}
use of com.squareup.javapoet.TypeName in project dagger by square.
the class ModuleAdapterProcessor method generateProvidesAdapter.
private TypeSpec generateProvidesAdapter(ClassName moduleClassName, ClassName adapterName, ExecutableElement providerMethod, Map<ExecutableElement, ClassName> methodToClassName, Map<String, AtomicInteger> methodNameToNextId, boolean library) {
String methodName = providerMethod.getSimpleName().toString();
TypeMirror moduleType = providerMethod.getEnclosingElement().asType();
ClassName className = bindingClassName(adapterName, providerMethod, methodToClassName, methodNameToNextId);
TypeName returnType = Util.injectableType(providerMethod.getReturnType());
List<? extends VariableElement> parameters = providerMethod.getParameters();
boolean dependent = !parameters.isEmpty();
TypeSpec.Builder result = TypeSpec.classBuilder(className.simpleName()).addJavadoc("$L", bindingTypeDocs(returnType, false, false, dependent)).addModifiers(PUBLIC, STATIC, FINAL).superclass(ParameterizedTypeName.get(ClassName.get(ProvidesBinding.class), returnType));
result.addField(moduleClassName, "module", PRIVATE, FINAL);
for (Element parameter : parameters) {
result.addField(bindingOf(parameter.asType()), parameterName(parameter), PRIVATE);
}
boolean singleton = providerMethod.getAnnotation(Singleton.class) != null;
String key = GeneratorKeys.get(providerMethod);
result.addMethod(MethodSpec.constructorBuilder().addModifiers(PUBLIC).addParameter(moduleClassName, "module").addStatement("super($S, $L, $S, $S)", key, (singleton ? "IS_SINGLETON" : "NOT_SINGLETON"), typeToString(moduleType), methodName).addStatement("this.module = module").addStatement("setLibrary($L)", library).build());
if (dependent) {
MethodSpec.Builder attachBuilder = MethodSpec.methodBuilder("attach").addJavadoc(AdapterJavadocs.ATTACH_METHOD).addAnnotation(Override.class).addAnnotation(Util.UNCHECKED).addModifiers(PUBLIC).addParameter(Linker.class, "linker");
for (VariableElement parameter : parameters) {
String parameterKey = GeneratorKeys.get(parameter);
attachBuilder.addStatement("$N = ($T) linker.requestBinding($S, $T.class, getClass().getClassLoader())", parameterName(parameter), bindingOf(parameter.asType()), parameterKey, moduleClassName);
}
result.addMethod(attachBuilder.build());
MethodSpec.Builder getDependenciesBuilder = MethodSpec.methodBuilder("getDependencies").addJavadoc(AdapterJavadocs.GET_DEPENDENCIES_METHOD).addAnnotation(Override.class).addModifiers(PUBLIC).addParameter(Util.SET_OF_BINDINGS, "getBindings").addParameter(Util.SET_OF_BINDINGS, "injectMembersBindings");
for (Element parameter : parameters) {
getDependenciesBuilder.addStatement("getBindings.add($N)", parameterName(parameter));
}
result.addMethod(getDependenciesBuilder.build());
}
MethodSpec.Builder getBuilder = MethodSpec.methodBuilder("get").addJavadoc(AdapterJavadocs.GET_METHOD, returnType).addAnnotation(Override.class).addModifiers(PUBLIC).returns(returnType).addCode("return module.$N(", methodName);
boolean first = true;
for (Element parameter : parameters) {
if (!first)
getBuilder.addCode(", ");
getBuilder.addCode("$N.get()", parameterName(parameter));
first = false;
}
getBuilder.addCode(");\n");
result.addMethod(getBuilder.build());
return result.build();
}
use of com.squareup.javapoet.TypeName in project wire by square.
the class JavaGenerator method optionsField.
// Example:
//
// public static final FieldOptions FIELD_OPTIONS_FOO = new FieldOptions.Builder()
// .setExtension(Ext_custom_options.count, 42)
// .build();
//
private FieldSpec optionsField(ProtoType optionsType, String fieldName, Options options) {
TypeName optionsJavaType = typeName(optionsType);
CodeBlock.Builder initializer = CodeBlock.builder();
initializer.add("$[new $T.Builder()", optionsJavaType);
boolean empty = true;
for (Map.Entry<ProtoMember, ?> entry : options.map().entrySet()) {
if (entry.getKey().equals(FIELD_DEPRECATED) || entry.getKey().equals(PACKED)) {
continue;
}
Field optionField = schema.getField(entry.getKey());
initializer.add("\n.$L($L)", fieldName(optionsType, optionField), fieldInitializer(optionField.type(), entry.getValue()));
empty = false;
}
initializer.add("\n.build()$]");
if (empty)
return null;
return FieldSpec.builder(optionsJavaType, fieldName).addModifiers(PUBLIC, STATIC, FINAL).initializer(initializer.build()).build();
}
use of com.squareup.javapoet.TypeName in project wire by square.
the class JavaGenerator method messageEquals.
// Example:
//
// @Override
// public boolean equals(Object other) {
// if (other == this) return true;
// if (!(other instanceof SimpleMessage)) return false;
// SimpleMessage o = (SimpleMessage) other;
// return equals(unknownFields(), o.unknownFields())
// && equals(optional_int32, o.optional_int32);
//
private MethodSpec messageEquals(NameAllocator nameAllocator, MessageType type) {
NameAllocator localNameAllocator = nameAllocator.clone();
String otherName = localNameAllocator.newName("other");
String oName = localNameAllocator.newName("o");
TypeName javaType = typeName(type.type());
MethodSpec.Builder result = MethodSpec.methodBuilder("equals").addAnnotation(Override.class).addModifiers(PUBLIC).returns(boolean.class).addParameter(Object.class, otherName);
List<Field> fields = type.fieldsAndOneOfFields();
if (fields.isEmpty()) {
result.addStatement("return $N instanceof $T", otherName, javaType);
return result.build();
}
result.addStatement("if ($N == this) return true", otherName);
result.addStatement("if (!($N instanceof $T)) return false", otherName, javaType);
result.addStatement("$T $N = ($T) $N", javaType, oName, javaType, otherName);
result.addCode("$[return unknownFields().equals($N.unknownFields())", oName);
for (Field field : fields) {
String fieldName = localNameAllocator.get(field);
if (field.isRequired() || field.isRepeated() || field.type().isMap()) {
result.addCode("\n&& $1L.equals($2N.$1L)", fieldName, oName);
} else {
result.addCode("\n&& $1T.equals($2L, $3N.$2L)", Internal.class, fieldName, oName);
}
}
result.addCode(";\n$]");
return result.build();
}
use of com.squareup.javapoet.TypeName in project wire by square.
the class JavaGenerator method messageFieldsConstructor.
// Example:
//
// public SimpleMessage(int optional_int32, long optional_int64) {
// this(builder.optional_int32, builder.optional_int64, null);
// }
//
private MethodSpec messageFieldsConstructor(NameAllocator nameAllocator, MessageType type) {
MethodSpec.Builder result = MethodSpec.constructorBuilder();
result.addModifiers(PUBLIC);
result.addCode("this(");
for (Field field : type.fieldsAndOneOfFields()) {
TypeName javaType = fieldType(field);
String fieldName = nameAllocator.get(field);
ParameterSpec.Builder param = ParameterSpec.builder(javaType, fieldName);
if (emitAndroid && field.isOptional()) {
param.addAnnotation(NULLABLE);
}
result.addParameter(param.build());
result.addCode("$L, ", fieldName);
}
result.addCode("$T.EMPTY);\n", BYTE_STRING);
return result.build();
}
Aggregations