use of com.squareup.javapoet.ParameterSpec in project glide by bumptech.
the class GlideGenerator method overrideGlideStaticMethod.
private MethodSpec overrideGlideStaticMethod(ExecutableElement methodToOverride) {
List<ParameterSpec> parameters = ProcessorUtil.getParameters(methodToOverride);
TypeElement element = (TypeElement) processingEnv.getTypeUtils().asElement(methodToOverride.getReturnType());
MethodSpec.Builder builder = MethodSpec.methodBuilder(methodToOverride.getSimpleName().toString()).addModifiers(Modifier.PUBLIC, Modifier.STATIC).addJavadoc(processorUtil.generateSeeMethodJavadoc(methodToOverride)).addParameters(parameters);
addReturnAnnotations(builder, methodToOverride);
boolean returnsValue = element != null;
if (returnsValue) {
builder.returns(ClassName.get(element));
}
StringBuilder code = new StringBuilder(returnsValue ? "return " : "");
code.append("$T.$N(");
List<Object> args = new ArrayList<>();
args.add(ClassName.get(glideType));
args.add(methodToOverride.getSimpleName());
if (!parameters.isEmpty()) {
for (ParameterSpec param : parameters) {
code.append("$L, ");
args.add(param.name);
}
code = new StringBuilder(code.substring(0, code.length() - 2));
}
code.append(")");
builder.addStatement(code.toString(), args.toArray(new Object[0]));
return builder.build();
}
use of com.squareup.javapoet.ParameterSpec in project glide by bumptech.
the class RequestBuilderGenerator method generateRequestBuilderOverride.
/**
* Generates an override of a particular method in {@code com.bumptech.glide.RequestBuilder} that
* returns {@code com.bumptech.glide.RequestBuilder} so that it returns our generated subclass
* instead.
*/
private MethodSpec generateRequestBuilderOverride(ExecutableElement methodToOverride) {
// We've already verified that this method returns a RequestBuilder and RequestBuilders have
// exactly one type argument, so this is safe unless those assumptions change.
TypeMirror typeArgument = ((DeclaredType) methodToOverride.getReturnType()).getTypeArguments().get(0);
ParameterizedTypeName generatedRequestBuilderOfType = ParameterizedTypeName.get(generatedRequestBuilderClassName, ClassName.get(typeArgument));
MethodSpec.Builder builder = ProcessorUtil.overriding(methodToOverride).returns(generatedRequestBuilderOfType);
builder.addCode(CodeBlock.builder().add("return ($T) super.$N(", generatedRequestBuilderOfType, methodToOverride.getSimpleName()).add(FluentIterable.from(builder.build().parameters).transform(new Function<ParameterSpec, String>() {
@Override
public String apply(ParameterSpec input) {
return input.name;
}
}).join(Joiner.on(", "))).add(");\n").build());
for (AnnotationMirror mirror : methodToOverride.getAnnotationMirrors()) {
builder = builder.addAnnotation(AnnotationSpec.get(mirror));
}
if (methodToOverride.isVarArgs()) {
builder = builder.addModifiers(Modifier.FINAL).addAnnotation(SafeVarargs.class).addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "varargs").build());
}
return builder.build();
}
use of com.squareup.javapoet.ParameterSpec in project glide by bumptech.
the class RequestOptionsGenerator method createNewOptionAndCall.
private StringBuilder createNewOptionAndCall(boolean memoize, MethodSpec.Builder methodSpecBuilder, String start, List<ParameterSpec> specs) {
StringBuilder createNewOptionAndCall = new StringBuilder(start);
if (!specs.isEmpty()) {
methodSpecBuilder.addParameters(specs);
for (ParameterSpec parameter : specs) {
createNewOptionAndCall.append(parameter.name);
// use the Application Context to avoid memory leaks.
if (memoize && isAndroidContext(parameter)) {
createNewOptionAndCall.append(".getApplicationContext()");
}
createNewOptionAndCall.append(", ");
}
createNewOptionAndCall = new StringBuilder(createNewOptionAndCall.substring(0, createNewOptionAndCall.length() - 2));
}
createNewOptionAndCall.append(")");
return createNewOptionAndCall;
}
use of com.squareup.javapoet.ParameterSpec in project glide by bumptech.
the class RequestOptionsGenerator method generateMethodsForRequestOptionsExtensionNew.
private List<MethodAndStaticVar> generateMethodsForRequestOptionsExtensionNew(ExecutableElement element) {
int overrideType = processorUtil.getOverrideType(element);
String methodName = element.getSimpleName().toString();
MethodSpec.Builder builder = MethodSpec.methodBuilder(methodName).addModifiers(Modifier.PUBLIC).addJavadoc(processorUtil.generateSeeMethodJavadoc(element)).varargs(element.isVarArgs()).returns(glideOptionsName);
// The 0th element is expected to be a RequestOptions object.
List<? extends VariableElement> paramElements = element.getParameters().subList(1, element.getParameters().size());
List<ParameterSpec> parameters = ProcessorUtil.getParameters(paramElements);
builder.addParameters(parameters);
String extensionRequestOptionsArgument;
if (overrideType == OVERRIDE_EXTEND) {
builder.addJavadoc(processorUtil.generateSeeMethodJavadoc(requestOptionsName, methodName, paramElements)).addAnnotation(Override.class);
List<Object> methodArgs = new ArrayList<>();
methodArgs.add(element.getSimpleName().toString());
StringBuilder methodLiterals = new StringBuilder();
if (!parameters.isEmpty()) {
for (ParameterSpec parameter : parameters) {
methodLiterals.append("$L, ");
methodArgs.add(parameter.name);
}
methodLiterals = new StringBuilder(methodLiterals.substring(0, methodLiterals.length() - 2));
}
extensionRequestOptionsArgument = CodeBlock.builder().add("super.$N(" + methodLiterals + ")", methodArgs.toArray(new Object[0])).build().toString();
} else {
extensionRequestOptionsArgument = "this";
}
List<Object> args = new ArrayList<>();
StringBuilder code = new StringBuilder("return ($T) $T.$L($L, ");
args.add(glideOptionsName);
args.add(ClassName.get(element.getEnclosingElement().asType()));
args.add(element.getSimpleName().toString());
args.add(extensionRequestOptionsArgument);
if (!parameters.isEmpty()) {
for (ParameterSpec parameter : parameters) {
code.append("$L, ");
args.add(parameter.name);
}
}
code = new StringBuilder(code.substring(0, code.length() - 2));
code.append(")");
builder.addStatement(code.toString(), args.toArray(new Object[0]));
builder.addAnnotation(AnnotationSpec.builder(CHECK_RESULT_CLASS_NAME).build()).addAnnotation(nonNull());
List<MethodAndStaticVar> result = new ArrayList<>();
result.add(new MethodAndStaticVar(builder.build()));
MethodAndStaticVar methodAndVar = generateStaticMethodEquivalentForExtensionMethod(element);
if (methodAndVar != null) {
result.add(methodAndVar);
}
return result;
}
use of com.squareup.javapoet.ParameterSpec in project EnderIO by SleepyTrousers.
the class Processor method generateContainerProxy.
private void generateContainerProxy(TypeElement typeElement) throws IOException {
Filer filer = processingEnv.getFiler();
String modid = typeElement.getAnnotation(RemoteCall.class).modid();
if (modid == null || modid.isEmpty()) {
modid = findModid(typeElement);
if (modid == null || modid.isEmpty()) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Parameter 'modid' must be set on class level if there's no @Mod class in hierarchy", typeElement);
return;
}
}
if (modid.equals("enderiobase")) {
// I hate hardcoding this stuff
modid = "enderio";
}
String packageName = ((PackageElement) typeElement.getEnclosingElement()).getQualifiedName().toString();
String sourceClassName = typeElement.getSimpleName().toString();
String proxyInterfaceClassName = sourceClassName + "Proxy";
TypeSpec.Builder proxyInterfaceBuilder = TypeSpec.interfaceBuilder(proxyInterfaceClassName);
for (ExecutableElement method : processingEnv.getElementUtils().getAllMembers(typeElement).stream().filter(el -> el.getKind() == ElementKind.METHOD).map(el -> (ExecutableElement) el).filter(el -> el.getAnnotation(RemoteCall.class) != null).collect(Collectors.toList())) {
String methodName = method.getSimpleName().toString();
String methodProxyClassName = proxyInterfaceClassName + "$" + methodName;
if (method.getAnnotation(RemoteCall.class).modid() != null && !method.getAnnotation(RemoteCall.class).modid().isEmpty()) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Parameter 'modid' must NOT be set on method level", method);
return;
}
//
FieldSpec instanceField = FieldSpec.builder(I_REMOTE_EXEC, "INSTANCE", Modifier.STATIC, Modifier.FINAL, Modifier.PUBLIC).initializer("new $1L().setRegistryName($2S, $3S)", methodProxyClassName, modid, (sourceClassName + "_" + methodName).toLowerCase(Locale.ENGLISH)).build();
TypeSpec.Builder methodProxyClassBuilder = TypeSpec.classBuilder(methodProxyClassName).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addAnnotation(AnnotationSpec.builder(EVENT_BUS_SUBSCRIBER).addMember("modid", "$S", modid).build()).superclass(ParameterizedTypeName.get(I_FORGE_REGISTRY_ENTRY__IMPL, I_REMOTE_EXEC)).addSuperinterface(I_REMOTE_EXEC).addField(instanceField).addMethod(//
MethodSpec.methodBuilder(//
"register").addModifiers(Modifier.PUBLIC, //
Modifier.STATIC).returns(void.class).addAnnotation(//
SUBSCRIBE_EVENT).addParameter(ParameterizedTypeName.get(REGISTRY_EVENT__REGISTER, I_REMOTE_EXEC), //
"event").addStatement("event.getRegistry().register($N)", instanceField).build());
MethodSpec.Builder proxyMethodBuilder = MethodSpec.methodBuilder(methodName).addModifiers(Modifier.PUBLIC, Modifier.DEFAULT).returns(void.class);
//
CodeBlock.Builder parameterListCode = null;
MethodSpec.Builder writerBuilder = MethodSpec.methodBuilder("makeWriter").addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(ParameterizedTypeName.get(CONSUMER, BYTE_BUF)).beginControlFlow("return buf ->").addStatement("buf.writeInt($T.getMinecraft().player.openContainer.windowId)", MINECRAFT);
MethodSpec.Builder runnerBuilder = MethodSpec.methodBuilder("apply").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).returns(ParameterizedTypeName.get(CONSUMER, ENTITY_PLAYER_MP)).addParameter(BYTE_BUF, "buf").addStatement("final int _windowId = buf.readInt()");
Networkbuilder b = Networkbuilder.builder(writerBuilder, runnerBuilder);
for (VariableElement parameter : method.getParameters()) {
TypeMirror paramTypeMirror = parameter.asType();
TypeName typeName = TypeName.get(paramTypeMirror);
ParameterSpec parameterSpec = ParameterSpec_get(parameter);
writerBuilder.addParameter(parameterSpec);
proxyMethodBuilder.addParameter(parameterSpec);
if (parameterListCode == null) {
parameterListCode = CodeBlock.builder().add("$N", parameterSpec);
} else {
parameterListCode.add(", $N", parameterSpec);
}
if (typeName.isPrimitive()) {
String byteBuffCall = ucfirst(typeName.toString());
b.addWriterStatement("buf.write$2L($1N)", parameterSpec, byteBuffCall);
b.addReaderStatement("final $2T $1N = buf.read$3L()", parameterSpec, typeName, byteBuffCall);
} else {
if (!isNonnull(parameter)) {
try {
b.beginNullable(parameterSpec);
} catch (TooManyNullableParametersException e) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Too many nullable parameters", parameter);
}
}
if (typeName.isBoxedPrimitive()) {
String byteBuffCall = ucfirst(typeName.unbox().toString());
b.addWriterStatement("buf.write$1L($2N)", byteBuffCall, parameterSpec);
b.addReaderStatement(typeName, parameterSpec, "buf.read$1L()", byteBuffCall);
} else if (paramTypeMirror.getKind() == TypeKind.DECLARED && ((TypeElement) ((DeclaredType) paramTypeMirror).asElement()).getKind() == ElementKind.ENUM) {
b.addWriterStatement("buf.writeInt($1T.put($2N))", ENUM_READER, parameterSpec);
b.addReaderStatement(typeName, parameterSpec, "$1T.get($2T.class, buf.readInt())", ENUM_READER, typeName);
} else if (typeName.equals(STRING)) {
b.addWriterStatement("$T.writeUTF8String(buf, $N)", BYTE_BUF_UTILS, parameterSpec);
b.addReaderStatement(typeName, parameterSpec, "$1T.readUTF8String(buf)", BYTE_BUF_UTILS);
} else if (typeName.equals(ITEM_STACK)) {
b.addWriterStatement("$T.writeItemStack(buf, $N)", BYTE_BUF_UTILS, parameterSpec);
b.addReaderStatement(typeName, parameterSpec, "$1T.readItemStack(buf)", BYTE_BUF_UTILS);
} else if (typeName.equals(NBT_TAG_COMPOUND)) {
b.addWriterStatement("$T.writeTag(buf, $N)", BYTE_BUF_UTILS, parameterSpec);
b.addReaderStatement(typeName, parameterSpec, "$1T.readTag(buf)", BYTE_BUF_UTILS);
} else if (typeName.equals(BLOCKPOS)) {
b.addWriterStatement("buf.writeLong($1N.toLong())", parameterSpec);
b.addReaderStatement(typeName, parameterSpec, "$1T.fromLong(buf.readLong())", BLOCKPOS);
} else {
// TODO: []/List<>/NNList<> of supported types.
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Cannot serialize '" + typeName + "' into a byte stream", parameter);
return;
}
b.endNullable();
}
}
b.build();
writerBuilder.endControlFlow("");
MethodSpec makeWriterMethod = writerBuilder.build();
methodProxyClassBuilder.addMethod(makeWriterMethod);
runnerBuilder.beginControlFlow("return player ->").beginControlFlow("if (player.openContainer instanceof $T && player.openContainer.windowId == _windowId)", ClassName.get(typeElement)).addCode("(($T) player.openContainer).$L(", ClassName.get(typeElement), methodName);
if (parameterListCode != null) {
runnerBuilder.addCode(parameterListCode.build());
}
runnerBuilder.addStatement(")").endControlFlow().endControlFlow("");
methodProxyClassBuilder.addMethod(runnerBuilder.build());
//
TypeSpec methodProxyClass = methodProxyClassBuilder.build();
JavaFile.builder(packageName, methodProxyClass).build().writeTo(filer);
//
proxyMethodBuilder.addCode("$1T.send($3N.$2N, $3N.$4N(", EXEC_PACKET, instanceField, methodProxyClass, makeWriterMethod);
if (parameterListCode != null) {
proxyMethodBuilder.addCode(parameterListCode.build());
}
proxyMethodBuilder.addCode("));");
proxyInterfaceBuilder.addMethod(proxyMethodBuilder.build());
}
JavaFile.builder(packageName, proxyInterfaceBuilder.build()).build().writeTo(filer);
}
Aggregations