Java Code Examples for com.squareup.javapoet.TypeSpec.Builder

The following are Jave code examples for showing how to use Builder of the com.squareup.javapoet.TypeSpec class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: data-mediator   File: HashEqualsGenerator.java   View Source Code Vote up 6 votes
private static void generateHashCode(TypeSpec.Builder typeBuilder, Collection<FieldData> fields,
                                     Collection<FieldData> superFields, boolean hasSuperClass){
    if(fields.isEmpty()){
        return;
    }
    MethodSpec.Builder hashBuilder = MethodSpec.methodBuilder("hashCode")
            .returns(TypeName.INT)
            .addModifiers(Modifier.PUBLIC)
            .addAnnotation(Override.class);

    if(hasSuperClass && !superFields.isEmpty()){
        hashBuilder.addStatement("int result = super.hashCode()");
    }else{
        hashBuilder.addStatement("int result = 0");
    }
    for(FieldData fd : fields){
        addStatementForHashcode(hashBuilder, fd);
    }
    hashBuilder.addStatement("return result");
    sTemp_defined = false;

    typeBuilder.addMethod(hashBuilder.build());
}
 
Example 2
Project: remoter   File: MethodBuilder.java   View Source Code Vote up 6 votes
/**
 * getParcelObject method
 */
private void addGetParcelObject(TypeSpec.Builder classBuilder) {
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("getParcelerObject")
            .addModifiers(Modifier.PRIVATE)
            .returns(Object.class)
            .addParameter(String.class, "pClassName")
            .addParameter(ClassName.get("android.os", "Parcel"), "data")
            .beginControlFlow("try")
            .addStatement("$T p = null", ClassName.get("android.os", "Parcelable"))
            .addStatement("$T creator  = (Parcelable.Creator)Class.forName(pClassName+\"$$$$Parcelable\").getField(\"CREATOR\").get(null)", ClassName.get("android.os", "Parcelable.Creator"))
            .addStatement("Object pWrapper = creator.createFromParcel(data)")
            .addStatement("return pWrapper.getClass().getMethod(\"getParcel\", (Class[])null).invoke(pWrapper)")
            .endControlFlow()
            .beginControlFlow("catch (Exception ignored)")
            .addStatement("return null")
            .endControlFlow();
    classBuilder.addMethod(methodBuilder.build());
}
 
Example 3
Project: aws-sdk-java-v2   File: BaseClientBuilderClass.java   View Source Code Vote up 5 votes
@Override
public TypeSpec poetSpec() {
    TypeSpec.Builder builder =
            PoetUtils.createClassBuilder(builderClassName)
                     .addModifiers(Modifier.ABSTRACT)
                     .addAnnotation(SdkInternalApi.class)
                     .addTypeVariable(PoetUtils.createBoundedTypeVariableName("B", builderInterfaceName, "B", "C"))
                     .addTypeVariable(TypeVariableName.get("C"))
                     .superclass(PoetUtils.createParameterizedTypeName(DefaultClientBuilder.class, "B", "C"))
                     .addJavadoc("Internal base class for {@link $T} and {@link $T}.",
                                 ClassName.get(basePackage, model.getMetadata().getSyncBuilder()),
                                 ClassName.get(basePackage, model.getMetadata().getAsyncBuilder()));

    if (model.getCustomizationConfig().getServiceSpecificClientConfigClass() != null) {
        ClassName advancedConfiguration = ClassName.get(basePackage,
                                                        model.getCustomizationConfig().getServiceSpecificClientConfigClass());
        builder.addField(FieldSpec.builder(advancedConfiguration, "advancedConfiguration")
                                  .addModifiers(Modifier.PRIVATE)
                                  .build());
    }

    builder.addMethod(serviceEndpointPrefixMethod());
    builder.addMethod(serviceDefaultsMethod());
    builder.addMethod(defaultSignerProviderMethod());

    if (model.getCustomizationConfig().getServiceSpecificClientConfigClass() != null) {
        builder.addMethod(setAdvancedConfigurationMethod())
               .addMethod(getAdvancedConfigurationMethod())
               .addMethod(beanStyleSetAdvancedConfigurationMethod());
    }

    if (model.getCustomizationConfig().getServiceSpecificHttpConfig() != null) {
        builder.addMethod(serviceSpecificHttpConfigMethod());
    }

    return builder.build();
}
 
Example 4
Project: remoter   File: ClassBuilder.java   View Source Code Vote up 5 votes
public JavaFile.Builder buildProxyClass() {
    ClassName proxyClassName = getProxyClassName();

    TypeSpec.Builder proxyClassBuilder = TypeSpec
            .classBuilder(proxyClassName.simpleName())
            .addModifiers(Modifier.PUBLIC)
            .addSuperinterface(TypeName.get(getRemoterInterfaceElement().asType()));


    for (TypeParameterElement typeParameterElement : ((TypeElement) getRemoterInterfaceElement()).getTypeParameters()) {
        proxyClassBuilder.addTypeVariable(TypeVariableName.get(typeParameterElement.toString()));
    }

    //constructor
    proxyClassBuilder.addMethod(MethodSpec.constructorBuilder()
            .addModifiers(Modifier.PUBLIC)
            .addJavadoc("Initialize this {@link " + getProxyClassName().simpleName() + "} with the given {@link IBinder}\n\n")
            .addJavadoc("@param binder An {@link IBinder} that exposes a remote {@link " + getRemoterInterfaceClassName() + "}\n")
            .addParameter(ClassName.get("android.os", "IBinder"), "binder")
            .addStatement("this.mRemote = binder").build());


    getBindingManager().getFieldBuilder(getRemoterInterfaceElement()).addProxyFields(proxyClassBuilder);
    getBindingManager().getMethoddBuilder(getRemoterInterfaceElement()).addProxyMethods(proxyClassBuilder);

    proxyClassBuilder.addJavadoc("Wraps a remote {@link IBinder} that implements {@link " + getRemoterInterfaceClassName() + "} interface\n");
    proxyClassBuilder.addJavadoc("<p>\n");
    proxyClassBuilder.addJavadoc("Autogenerated by <a href=\"https://bit.ly/Remoter\">Remoter</a>\n");
    proxyClassBuilder.addJavadoc("@see " + getStubClassName().simpleName() + "\n");

    return JavaFile.builder(proxyClassName.packageName(), proxyClassBuilder.build());
}
 
Example 5
Project: auto-adapter   File: ViewHolderFactoryGenerator.java   View Source Code Vote up 5 votes
private TypeSpec.Builder createBuilder(final TypeElement model) {
    ClassName factoryClassName = ClassName.get(AutoAdapterProcessor.LIBRARY_PACKAGE, "ViewHolderFactory");

    ParameterizedTypeName factoryTypeName = ParameterizedTypeName.get(factoryClassName,
            ParameterizedTypeName.get(model.asType()));

    return TypeSpec.classBuilder(viewHolderFactoryClassName).addModifiers(Modifier.PUBLIC, Modifier.FINAL)
            .addSuperinterface(factoryTypeName);
}
 
Example 6
Project: auto-nav   File: NavProcessor.java   View Source Code Vote up 5 votes
private void createNavigator(Set<ActivityIntentModel> activityModels) {

        TypeSpec.Builder navigatorBuilder = TypeSpec.classBuilder(navigatorClassName)
                .addModifiers(Modifier.PUBLIC, Modifier.FINAL);

        for (ActivityIntentModel model : activityModels) {
            createActivityIntent(model);

            /**
             *         public static MainActivityIntent toMainActivity(Context context){
             *             MainActivityIntent intent = new MainActivityIntent(context, "com.com.MainActivity");
             *             return intent;
             *         }
             */

            ClassName returnType = ClassName.get(model.getPackageName(), model.getIntentClzName());
            MethodSpec.Builder methodSpecBuilder = MethodSpec
                    .methodBuilder(METHOD_PREFIX + model.getClzName());
            methodSpecBuilder.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
                    .addParameter(classContext, "context")
                    .returns(ClassName.get(model.getPackageName(), model.getIntentClzName()))
                    .addStatement("$T intent = new $T($L,$S)", returnType, returnType, "context", model.getQualifiedName())
                    .addStatement("return intent");

            navigatorBuilder.addMethod(methodSpecBuilder.build());
        }

        addPreGoListener(navigatorBuilder);


        try {
            JavaFile.builder(navigatorPackageName, navigatorBuilder.build()).build().writeTo(filer);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
 
Example 7
Project: GitHub   File: FinalRClassBuilder.java   View Source Code Vote up 5 votes
private static void addResourceField(TypeSpec.Builder resourceType, VariableDeclarator variable,
    ClassName annotation) {
  String fieldName = variable.getId().getName();
  String fieldValue = variable.getInit().toString();
  FieldSpec.Builder fieldSpecBuilder = FieldSpec.builder(int.class, fieldName)
      .addModifiers(PUBLIC, STATIC, FINAL)
      .initializer(fieldValue);

  if (annotation != null) {
    fieldSpecBuilder.addAnnotation(annotation);
  }

  resourceType.addField(fieldSpecBuilder.build());
}
 
Example 8
Project: GitHub   File: DelegateClassGenerator.java   View Source Code Vote up 5 votes
@Override
public JavaFile build() {
    TypeSpec.Builder result =
            TypeSpec.classBuilder(getClassName()).addModifiers(Modifier.PUBLIC)
                    .addSuperinterface(
                            ParameterizedTypeName.get(VIEW_DELEGATE, viewClass,
                                                      getPresenterFactoryTypeName()));
    decorator.build(result);
    return JavaFile.builder(getPackageName(), result.build())
            .addFileComment("Generated class from EasyMVP. Do not modify!").build();
}
 
Example 9
Project: react4j   File: ProcessorUtil.java   View Source Code Vote up 5 votes
static void copyAccessModifiers( @Nonnull final TypeElement element, @Nonnull final TypeSpec.Builder builder )
{
  if ( element.getModifiers().contains( Modifier.PUBLIC ) )
  {
    builder.addModifiers( Modifier.PUBLIC );
  }
}
 
Example 10
Project: GitHub   File: BaseDecorator.java   View Source Code Vote up 5 votes
protected void addMethods(TypeSpec.Builder result) {
    result
            .addMethod(getInitializeMethod())
            .addMethod(getInitializeMethodWithFactory())
            .addMethod(getAttachViewMethod())
            .addMethod(getDetachViewMethod())
            .addMethod(getLoaderManagerMethod(getLoaderMethodSignature()))
    ;
}
 
Example 11
Project: remoter   File: ClassBuilder.java   View Source Code Vote up 5 votes
public JavaFile.Builder buildStubClass() {
    ClassName stubClassName = getStubClassName();

    TypeSpec.Builder stubClassBuilder = TypeSpec
            .classBuilder(stubClassName.simpleName())
            .addModifiers(Modifier.PUBLIC)
            .superclass(TypeName.get(getBindingManager().getType("android.os.Binder")));

    for (TypeParameterElement typeParameterElement : ((TypeElement) getRemoterInterfaceElement()).getTypeParameters()) {
        stubClassBuilder.addTypeVariable(TypeVariableName.get(typeParameterElement.toString()));
    }


    //constructor
    stubClassBuilder.addMethod(MethodSpec.constructorBuilder()
            .addModifiers(Modifier.PUBLIC)
            .addJavadoc("Initialize this {@link " + getStubClassName().simpleName() + "} with the given {@link " + getRemoterInterfaceClassName() + "} implementation \n\n")
            .addJavadoc("@param serviceImpl An implementation of {@link " + getRemoterInterfaceClassName() + "}\n")
            .addParameter(TypeName.get(getRemoterInterfaceElement().asType()), "serviceImpl")
            .addStatement("this.serviceImpl = serviceImpl")
            .beginControlFlow("this.attachInterface(new $T()", ClassName.get("android.os", "IInterface"))
            .beginControlFlow("public $T asBinder()", ClassName.get("android.os", "IBinder"))
            .addStatement("return " + stubClassName.simpleName() + ".this")
            .endControlFlow()
            .endControlFlow()
            .addStatement(", DESCRIPTOR)")
            .build());


    getBindingManager().getFieldBuilder(getRemoterInterfaceElement()).addStubFields(stubClassBuilder);
    getBindingManager().getMethoddBuilder(getRemoterInterfaceElement()).addStubMethods(stubClassBuilder);

    stubClassBuilder.addJavadoc("Wraps a {@link " + getRemoterInterfaceClassName() + "} implementation and expose it as a remote {@link IBinder}\n");
    stubClassBuilder.addJavadoc("<p>\n");
    stubClassBuilder.addJavadoc("Autogenerated by <a href=\"https://bit.ly/Remoter\">Remoter</a>\n");
    stubClassBuilder.addJavadoc("@see " + getProxyClassName().simpleName() + "\n");

    return JavaFile.builder(stubClassName.packageName(), stubClassBuilder.build());
}
 
Example 12
Project: GitHub   File: PresenterLoaderGenerator.java   View Source Code Vote up 5 votes
@Override
public JavaFile build() {
    TypeSpec.Builder result =
            TypeSpec.classBuilder(getClassName()).addModifiers(Modifier.PUBLIC)
                    .addTypeVariable(TypeVariableName.get("P", Presenter.class))
                    .superclass(ParameterizedTypeName.get(AndroidLoaderUtils.getLoader(supportLibrary),
                                                          TypeVariableName.get("P")));
    addConstructor(result);
    addFields(result);
    addMethods(result);
    return JavaFile.builder(getPackageName(), result.build())
            .addFileComment("Generated class from EasyMVP. Do not modify!").build();
}
 
Example 13
Project: GitHub   File: RequestOptionsGenerator.java   View Source Code Vote up 4 votes
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();
}
 
Example 14
Project: aws-sdk-java-v2   File: PoetUtils.java   View Source Code Vote up 4 votes
public static TypeSpec.Builder createEnumBuilder(ClassName name) {
    return TypeSpec.enumBuilder(name).addAnnotation(GENERATED).addModifiers(Modifier.PUBLIC);
}
 
Example 15
Project: aws-sdk-java-v2   File: PoetUtils.java   View Source Code Vote up 4 votes
public static TypeSpec.Builder createInterfaceBuilder(ClassName name) {
    return TypeSpec.interfaceBuilder(name).addAnnotation(GENERATED).addModifiers(Modifier.PUBLIC);
}
 
Example 16
Project: domino   File: HandlersRegisterMethodWriter.java   View Source Code Vote up 4 votes
public HandlersRegisterMethodWriter(TypeSpec.Builder serverModuleTypeBuilder) {
    super(serverModuleTypeBuilder);
}
 
Example 17
Project: remoter   File: MethodBuilder.java   View Source Code Vote up 4 votes
/**
 * Add common extra methods
 */
private void addCommonExtras(TypeSpec.Builder classBuilder) {
    addGetParcelClass(classBuilder);
    addGetParcelObject(classBuilder);
}
 
Example 18
Project: javabard   File: GetterSpec.java   View Source Code Vote up 2 votes
/**
 *
 * @param typeSpecBuilder
 * @return
 */
public static GetterSpec.Builder forType(TypeSpec.Builder typeSpecBuilder) {
    return new Builder(typeSpecBuilder);
}
 
Example 19
Project: json2java4idea   File: AnnotationPolicy.java   View Source Code Vote up 0 votes
void apply(@Nonnull TypeSpec.Builder builder); 
Example 20
Project: data-mediator   File: TypeInsertDelegate.java   View Source Code Vote up 0 votes
/**
 * override method for proxy class
 * @param typeBuilder the type builder
 * @param fields the fields
 */
public void overrideMethodsForProxy(TypeSpec.Builder typeBuilder, Collection<FieldData> fields){

}