public void putEncodeCode(Builder cb, TypeMirror iterableTypeMirror, Collection<EncodeCodeProvider> allProviders, String varName) { Elements elements = UtilsProvider.getElements(); TypeElement typeElement = elements.getTypeElement(Map.Entry.class.getCanonicalName()); DeclaredType dt = (DeclaredType)iterableTypeMirror; Types types = UtilsProvider.getTypes(); DeclaredType declaredType = types.getDeclaredType(typeElement, dt.getTypeArguments().get(0), dt.getTypeArguments().get(1)); typeElement = UtilsProvider.getElements().getTypeElement(Set.class.getCanonicalName()); declaredType = UtilsProvider.getTypes().getDeclaredType(typeElement, declaredType); // Rely on set encoder for(EncodeCodeProvider ecp:allProviders){ if (ecp.applies(declaredType)) { ecp.putEncodeCode(cb, declaredType, allProviders, varName+".entrySet()"); break; } } }
private CodeBlock generateClassJavadoc(Set<String> glideExtensionClassNames) { Builder builder = CodeBlock.builder() .add("Automatically generated from {@link $T} annotated classes.\n", GlideExtension.class) .add("\n") .add("@see $T\n", requestOptionsName); for (String glideExtensionClass : glideExtensionClassNames) { builder.add("@see $T\n", ClassName.bestGuess(glideExtensionClass)); } return builder.build(); }
private MethodSpec generateRequestOptionOverride(ExecutableElement methodToOverride) { MethodSpec.Builder result = ProcessorUtil.overriding(methodToOverride) .returns(glideOptionsName) .addModifiers(Modifier.FINAL) .addCode(CodeBlock.builder() .add("return ($T) super.$N(", glideOptionsName, methodToOverride.getSimpleName()) .add(FluentIterable.from(methodToOverride.getParameters()) .transform(new Function<VariableElement, String>() { @Override public String apply(VariableElement input) { return input.getSimpleName().toString(); } }) .join(Joiner.on(", "))) .add(");\n") .build()); if (methodToOverride.getSimpleName().toString().equals("transforms")) { result .addAnnotation(SafeVarargs.class) .addAnnotation( AnnotationSpec.builder(SuppressWarnings.class) .addMember("value", "$S", "varargs") .build()); } for (AnnotationMirror mirror : methodToOverride.getAnnotationMirrors()) { result.addAnnotation(AnnotationSpec.get(mirror)); } return result.build(); }
@Override public CodeBlock getDecodeCode(Property property, DecodeCodeProviders decodeProviders) { Builder builder = CodeBlock.builder(); TypeMirror tm = property.getResolvedType(); String accessName = property.getSetterCall(); accessName += property.hasSetter() ? "(" : " = "; builder.add("instance." + accessName); putDecodeCode(builder, tm, decodeProviders, null, true); if ( property.hasSetter() ){ builder.add(")"); } builder.add(";\n"); return builder.build(); }
@Override public void putEncodeCode(Builder cb, TypeMirror iterableTypeMirror, Collection<EncodeCodeProvider> allProviders, String varName) { TypeMirror iterableTypeArgument = getIterableEnclosedType(iterableTypeMirror); TypeName iterableTypeArgumentTypeName = getIterableEnclosedTypeName(iterableTypeMirror); String instanceName = nameGenerator.instanceName(iterableTypeArgument); cb.addStatement("writer.writeStartArray()"); cb.beginControlFlow("for ($T $L:(($T)$L))", iterableTypeArgumentTypeName, instanceName, getIterableCast(iterableTypeMirror), varName); boolean primitive = iterableTypeArgument.getKind().isPrimitive(); if ( !primitive ){ cb.beginControlFlow("if ($L == null)", instanceName); cb.addStatement("writer.writeNull()"); cb.nextControlFlow("else"); } for(EncodeCodeProvider provider:allProviders){ if (provider.applies(iterableTypeArgument)){ String newVarName = nameGenerator.instanceName(iterableTypeArgument).toLowerCase(); provider.putEncodeCode(cb, iterableTypeArgument, allProviders, newVarName); break; } } if ( !primitive ){ cb.endControlFlow(); } cb.endControlFlow(); cb.addStatement("writer.writeEndArray()"); return; }
public void putDecodeCode(Builder cb, TypeMirror tm, String variableToUse, boolean declareVariable) { for(DecodeCodeProvider provider:allProviders){ if (provider.applies(tm)){ provider.putDecodeCode(cb, tm, this, variableToUse, declareVariable); break; } } }
@Override public CodeBlock getDecodeCode(Property property, DecodeCodeProviders decodeProviders) { Builder builder = CodeBlock.builder(); String getAccessName = property.getGetterCall(); String setAccessName = property.getSetterCall(); setAccessName += property.hasSetter() ? "($L)" : " = $L"; TypeMirror resolvedType = property.getResolvedType(); builder.addStatement("$T bsonType = reader.getCurrentBsonType()", CodeUtil.bsonTypeTypeName()); builder.beginControlFlow("if (bsonType == $T.NULL)", CodeUtil.bsonTypeTypeName()); builder.addStatement("reader.readNull()"); builder.addStatement("instance." + setAccessName, "null"); builder.addStatement("return"); builder.endControlFlow(); if ( initializeProperty() ){ TypeMirror iterableImplementationCollectionClass = getIterableImplementationType(resolvedType); TypeMirror iterableDeclarationCollectionClass = getIterableDeclarationType(resolvedType); builder.addStatement("$T value = instance.$L", iterableDeclarationCollectionClass, getAccessName); builder.beginControlFlow("if (value == null)"); builder.addStatement("value = new $T()", TypeName.get(iterableImplementationCollectionClass)); builder.addStatement("instance." + setAccessName, "value"); builder.endControlFlow(); } putDecodeCode(builder, resolvedType, decodeProviders, !initializeProperty(), "value"); if ( !initializeProperty() ){ builder.addStatement("instance." + setAccessName, "value"); } return builder.build(); }
@Override public void putDecodeCode(Builder cb, TypeMirror tm, DecodeCodeProviders decodeProviders, String variableToUse, boolean declareVariable) { DeclaredType entry = (DeclaredType)tm; TypeMirror keyArgument = entry.getTypeArguments().get(0); TypeMirror valueArgument = entry.getTypeArguments().get(1); cb.addStatement("reader.readStartDocument()"); cb.addStatement("reader.readName(\"key\")"); cb.addStatement("$T $L = null", keyArgument, variableToUse+"0"); cb.addStatement("$T $L = null", valueArgument, variableToUse+"1"); cb.addStatement("bsonType = reader.getCurrentBsonType()", CodeUtil.bsonTypeTypeName()); cb.beginControlFlow("if (bsonType == $T.NULL)", CodeUtil.bsonTypeTypeName()); cb.addStatement("reader.readNull()"); cb.addStatement("reader.skipName()"); cb.addStatement("reader.skipValue()"); cb.nextControlFlow("else"); Builder builder = CodeBlock.builder(); decodeProviders.putDecodeCode(builder, keyArgument, variableToUse+"0", false); boolean hasToInstantiate = decodeProviders.hasToInstantiate(keyArgument); if ( hasToInstantiate ){ cb.add(builder.build()); }else{ cb.add("$L = ", variableToUse+"0"); cb.add(builder.build()+";\n"); } cb.addStatement("reader.readName(\"value\")"); builder = CodeBlock.builder(); decodeProviders.putDecodeCode(builder, valueArgument, variableToUse+"1", false); hasToInstantiate = decodeProviders.hasToInstantiate(valueArgument); if ( hasToInstantiate ){ cb.add(builder.build()); }else{ cb.add("$L = ", variableToUse+"1"); cb.add(builder.build()+";\n"); } cb.endControlFlow(); cb.addStatement("reader.readEndDocument()"); }
protected void putDecodeCode(Builder cb, TypeMirror iterableTypeMirror, DecodeCodeProviders decodeProviders, String variableToUse, boolean instantiate, boolean declareVariable) { String temporalVariable = variableToUse+"tmp"; super.putDecodeCode(cb, iterableTypeMirror, decodeProviders, temporalVariable, instantiate, declareVariable); TypeMirror iterableType = getIterableType(iterableTypeMirror); if ( !iterableType.getKind().isPrimitive() ){ cb.addStatement("$T $L = $L.toArray(new $T[]{})", iterableTypeMirror, variableToUse, temporalVariable, iterableType); }else{ cb.addStatement("$T $L = new $T[$L.size()]", iterableTypeMirror, variableToUse, iterableType, temporalVariable); String idxName = variableToUse+"TmpIdx"; cb.beginControlFlow("for(int $L=0; $L < $L.length; $L++)", idxName, idxName, variableToUse, idxName); cb.addStatement("$L[$L] = $L.get($L)", variableToUse, idxName, temporalVariable, idxName); cb.endControlFlow(); } }
TypeSpec generate(String generatedCodePackageName, Set<String> glideExtensionClassNames) { glideOptionsName = ClassName.get(generatedCodePackageName, GENERATED_REQUEST_OPTIONS_SIMPLE_NAME); List<MethodAndStaticVar> methodsForExtensions = generateMethodsForExtensions(glideExtensionClassNames); Set<MethodSignature> extensionMethodSignatures = ImmutableSet.copyOf( Iterables.transform(methodsForExtensions, new Function<MethodAndStaticVar, MethodSignature>() { @Nullable @Override public MethodSignature apply(MethodAndStaticVar f) { return new MethodSignature(f.method); } })); List<MethodAndStaticVar> staticOverrides = generateStaticMethodOverridesForRequestOptions(); List<MethodSpec> instanceOverrides = generateInstanceMethodOverridesForRequestOptions(); List<MethodAndStaticVar> allMethodsAndStaticVars = new ArrayList<>(); for (MethodAndStaticVar item : staticOverrides) { if (extensionMethodSignatures.contains(new MethodSignature(item.method))) { continue; } allMethodsAndStaticVars.add(item); } for (MethodSpec methodSpec : instanceOverrides) { if (extensionMethodSignatures.contains(new MethodSignature(methodSpec))) { continue; } allMethodsAndStaticVars.add(new MethodAndStaticVar(methodSpec)); } allMethodsAndStaticVars.addAll(methodsForExtensions); TypeSpec.Builder classBuilder = TypeSpec.classBuilder(GENERATED_REQUEST_OPTIONS_SIMPLE_NAME) .addAnnotation( AnnotationSpec.builder(SuppressWarnings.class) .addMember("value", "$S", "deprecation") .build()) .addJavadoc(generateClassJavadoc(glideExtensionClassNames)) .addModifiers(Modifier.FINAL) .addModifiers(Modifier.PUBLIC) .superclass(requestOptionsName); for (MethodAndStaticVar methodAndStaticVar : allMethodsAndStaticVars) { if (methodAndStaticVar.method != null) { classBuilder.addMethod(methodAndStaticVar.method); } if (methodAndStaticVar.staticField != null) { classBuilder.addField(methodAndStaticVar.staticField); } } return classBuilder.build(); }
private MethodAndStaticVar generateStaticMethodEquivalentForRequestOptionsStaticMethod( ExecutableElement staticMethod) { boolean memoize = memoizeStaticMethodFromArguments(staticMethod); String staticMethodName = staticMethod.getSimpleName().toString(); String equivalentInstanceMethodName = getInstanceMethodNameFromStaticMethodName(staticMethodName); MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(staticMethodName) .addModifiers(Modifier.PUBLIC, Modifier.STATIC) .addJavadoc(processorUtil.generateSeeMethodJavadoc(staticMethod)) .returns(glideOptionsName); List<? extends VariableElement> parameters = staticMethod.getParameters(); String createNewOptionAndCall = "new $T().$N("; if (!parameters.isEmpty()) { for (VariableElement parameter : parameters) { methodSpecBuilder.addParameter(getParameterSpec(parameter)); createNewOptionAndCall += parameter.getSimpleName().toString(); // use the Application Context to avoid memory leaks. if (memoize && isAndroidContext(parameter)) { createNewOptionAndCall += ".getApplicationContext()"; } createNewOptionAndCall += ", "; } createNewOptionAndCall = createNewOptionAndCall.substring(0, createNewOptionAndCall.length() - 2); } createNewOptionAndCall += ")"; FieldSpec requiredStaticField = null; if (memoize) { // Generates code that looks like: // if (GlideOptions.<methodName> == null) { // GlideOptions.<methodName> = new GlideOptions().<methodName>().autoClone() // } // Mix in an incrementing unique id to handle method overloading. String staticVariableName = staticMethodName + nextStaticFieldUniqueId++; requiredStaticField = FieldSpec.builder(glideOptionsName, staticVariableName) .addModifiers(Modifier.PRIVATE, Modifier.STATIC) .build(); methodSpecBuilder.beginControlFlow( "if ($T.$N == null)", glideOptionsName, staticVariableName) .addStatement("$T.$N =\n" + createNewOptionAndCall + ".$N", glideOptionsName, staticVariableName, glideOptionsName, equivalentInstanceMethodName, "autoClone()") .endControlFlow() .addStatement("return $T.$N", glideOptionsName, staticVariableName); } else { // Generates code that looks like: // return new GlideOptions().<methodName>() methodSpecBuilder.addStatement( "return " + createNewOptionAndCall, glideOptionsName, equivalentInstanceMethodName); } List<? extends TypeParameterElement> typeParameters = staticMethod.getTypeParameters(); for (TypeParameterElement typeParameterElement : typeParameters) { methodSpecBuilder.addTypeVariable( TypeVariableName.get(typeParameterElement.getSimpleName().toString())); } return new MethodAndStaticVar(methodSpecBuilder.build(), requiredStaticField); }
TypeSpec generate(String generatedCodePackageName, Set<String> glideExtensionClassNames) { glideOptionsName = ClassName.get(generatedCodePackageName, GENERATED_REQUEST_OPTIONS_SIMPLE_NAME); List<MethodAndStaticVar> methodsForExtensions = generateMethodsForExtensions(glideExtensionClassNames); Set<MethodSignature> extensionMethodSignatures = ImmutableSet.copyOf( Iterables.transform(methodsForExtensions, new Function<MethodAndStaticVar, MethodSignature>() { @Nullable @Override public MethodSignature apply(MethodAndStaticVar f) { return new MethodSignature(f.method); } })); List<MethodAndStaticVar> staticOverrides = generateStaticMethodOverridesForRequestOptions(); List<MethodSpec> instanceOverrides = generateInstanceMethodOverridesForRequestOptions(); List<MethodAndStaticVar> allMethodsAndStaticVars = new ArrayList<>(); for (MethodAndStaticVar item : staticOverrides) { if (extensionMethodSignatures.contains(new MethodSignature(item.method))) { continue; } allMethodsAndStaticVars.add(item); } for (MethodSpec methodSpec : instanceOverrides) { if (extensionMethodSignatures.contains(new MethodSignature(methodSpec))) { continue; } allMethodsAndStaticVars.add(new MethodAndStaticVar(methodSpec)); } allMethodsAndStaticVars.addAll(methodsForExtensions); TypeSpec.Builder classBuilder = TypeSpec.classBuilder(GENERATED_REQUEST_OPTIONS_SIMPLE_NAME) .addAnnotation( AnnotationSpec.builder(SuppressWarnings.class) .addMember("value", "$S", "deprecation") .build()) .addJavadoc(generateClassJavadoc(glideExtensionClassNames)) .addModifiers(Modifier.FINAL) .addModifiers(Modifier.PUBLIC) .addSuperinterface(Cloneable.class) .superclass(requestOptionsName); for (MethodAndStaticVar methodAndStaticVar : allMethodsAndStaticVars) { if (methodAndStaticVar.method != null) { classBuilder.addMethod(methodAndStaticVar.method); } if (methodAndStaticVar.staticField != null) { classBuilder.addField(methodAndStaticVar.staticField); } } return classBuilder.build(); }
private MethodAndStaticVar generateStaticMethodEquivalentForRequestOptionsStaticMethod( ExecutableElement staticMethod) { boolean memoize = memoizeStaticMethodFromArguments(staticMethod); String staticMethodName = staticMethod.getSimpleName().toString(); String equivalentInstanceMethodName = getInstanceMethodNameFromStaticMethodName(staticMethodName); MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(staticMethodName) .addModifiers(Modifier.PUBLIC, Modifier.STATIC) .addJavadoc(processorUtil.generateSeeMethodJavadoc(staticMethod)) .returns(glideOptionsName); List<? extends VariableElement> parameters = staticMethod.getParameters(); StringBuilder createNewOptionAndCall = new StringBuilder("new $T().$N("); if (!parameters.isEmpty()) { methodSpecBuilder.addParameters(ProcessorUtil.getParameters(staticMethod)); for (VariableElement parameter : parameters) { createNewOptionAndCall.append(parameter.getSimpleName().toString()); // 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(")"); FieldSpec requiredStaticField = null; if (memoize) { // Generates code that looks like: // if (GlideOptions.<methodName> == null) { // GlideOptions.<methodName> = new GlideOptions().<methodName>().autoClone() // } // Mix in an incrementing unique id to handle method overloading. String staticVariableName = staticMethodName + nextStaticFieldUniqueId++; requiredStaticField = FieldSpec.builder(glideOptionsName, staticVariableName) .addModifiers(Modifier.PRIVATE, Modifier.STATIC) .build(); methodSpecBuilder.beginControlFlow( "if ($T.$N == null)", glideOptionsName, staticVariableName) .addStatement("$T.$N =\n" + createNewOptionAndCall + ".$N", glideOptionsName, staticVariableName, glideOptionsName, equivalentInstanceMethodName, "autoClone()") .endControlFlow() .addStatement("return $T.$N", glideOptionsName, staticVariableName); } else { // Generates code that looks like: // return new GlideOptions().<methodName>() methodSpecBuilder.addStatement( "return " + createNewOptionAndCall, glideOptionsName, equivalentInstanceMethodName); } List<? extends TypeParameterElement> typeParameters = staticMethod.getTypeParameters(); for (TypeParameterElement typeParameterElement : typeParameters) { methodSpecBuilder.addTypeVariable( TypeVariableName.get(typeParameterElement.getSimpleName().toString())); } methodSpecBuilder.addAnnotation(AnnotationSpec.builder(CHECK_RESULT_CLASS_NAME).build()); return new MethodAndStaticVar(methodSpecBuilder.build(), requiredStaticField); }
public void putDecodeCode(Builder cb, TypeMirror tm, String variableToUse) { putDecodeCode(cb, tm, variableToUse, true); }
@Override public void putDecodeCode(Builder cb, TypeMirror iterableTypeMirror, DecodeCodeProviders decodeProviders, String variableToUse, boolean declareVariable) { putDecodeCode(cb, iterableTypeMirror, decodeProviders, variableToUse, true, declareVariable); }
public void putDecodeCode(Builder cb, TypeMirror iterableTypeMirror, DecodeCodeProviders decodeProviders, boolean instantiate, String variableToUse) { putDecodeCode(cb, iterableTypeMirror, decodeProviders, variableToUse, instantiate, true); }
protected void putDecodeCode(Builder cb, TypeMirror iterableTypeMirror, DecodeCodeProviders decodeProviders, String variableToUse, boolean instantiate, boolean declareVariable) { TypeMirror iterableType = getIterableType(iterableTypeMirror); String addMethod = getAddMethod(); if ( instantiate ){ TypeMirror iterableImplementationCollectionClass = getIterableImplementationType(iterableTypeMirror); TypeMirror iterableDeclarationCollectionClass = getIterableDeclarationType(iterableTypeMirror); if ( declareVariable ){ cb.addStatement("$T $L = new $T()", iterableDeclarationCollectionClass, variableToUse, iterableImplementationCollectionClass); }else{ cb.addStatement("$L = new $T()", variableToUse, iterableImplementationCollectionClass); } } cb.addStatement("reader.readStartArray()"); cb.beginControlFlow("while (reader.readBsonType() != $T.END_OF_DOCUMENT)", CodeUtil.bsonTypeTypeName()); cb.addStatement("bsonType = reader.getCurrentBsonType()", CodeUtil.bsonTypeTypeName()); cb.beginControlFlow("if (bsonType == $T.NULL)", CodeUtil.bsonTypeTypeName()); cb.addStatement("reader.readNull()"); if ( supportsNull(iterableType) ){ cb.addStatement("$L.$L(null)", variableToUse, addMethod); } cb.nextControlFlow("else"); Builder builder2 = CodeBlock.builder(); if ( decodeProviders.applies(iterableType) ){ String varName = nameGenerator.instanceName(iterableType).toLowerCase(); decodeProviders.putDecodeCode(builder2, iterableType, varName); boolean hasToInstantiate = decodeProviders.hasToInstantiate(iterableType); if ( hasToInstantiate ){ cb.add(builder2.build()); } cb.add("$L.$L(",variableToUse, addMethod); if ( hasToInstantiate ){ List<String> namesUsed = nameGenerator.generateNames(varName, decodeProviders.variablesUsed(iterableType)); cb.add(Joiner.on(',').join(namesUsed)); }else{ cb.add(builder2.build()); } cb.add(");\n"); } cb.endControlFlow(); cb.endControlFlow(); cb.addStatement("reader.readEndArray()"); return; }