當前位置: 首頁>>代碼示例>>Java>>正文


Java TypeSpec.classBuilder方法代碼示例

本文整理匯總了Java中com.squareup.javapoet.TypeSpec.classBuilder方法的典型用法代碼示例。如果您正苦於以下問題:Java TypeSpec.classBuilder方法的具體用法?Java TypeSpec.classBuilder怎麽用?Java TypeSpec.classBuilder使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.squareup.javapoet.TypeSpec的用法示例。


在下文中一共展示了TypeSpec.classBuilder方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: generateGlobalConstant

import com.squareup.javapoet.TypeSpec; //導入方法依賴的package包/類
private static JavaFile generateGlobalConstant(String packageName, Constant[] globalConstants) {
    TypeSpec.Builder javaClass = TypeSpec.classBuilder(CONSTANT_CLASS_NAME);
    javaClass.addModifiers(PUBLIC, FINAL);
    for (Constant constant : globalConstants) {
        String value = constant.value();
        if (constant.type().equals("Task") && value != null) {
            value = "greycat.Tasks.newTask().parse(\"" + value.replaceAll("\"", "'").trim() + "\",null);";
        } else if (constant.type().equals("String") && value != null) {
            value = "\""+value+"\"";
        }
        FieldSpec.Builder field = FieldSpec.builder(clazz(constant.type()), constant.name())
                .addModifiers(PUBLIC, STATIC);
        if (value != null) {
            field.addModifiers(FINAL).initializer(value);
        }
        javaClass.addField(field.build());
    }
    return JavaFile.builder(packageName, javaClass.build()).build();
}
 
開發者ID:datathings,項目名稱:greycat,代碼行數:20,代碼來源:ConstantGenerator.java

示例2: buildDaggerModule

import com.squareup.javapoet.TypeSpec; //導入方法依賴的package包/類
@Nonnull
private static TypeSpec buildDaggerModule( @Nonnull final ComponentDescriptor descriptor )
{
  final TypeSpec.Builder builder = TypeSpec.classBuilder( "DaggerModule" );

  builder.addModifiers( Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL );
  builder.addAnnotation( ClassName.bestGuess( Constants.DAGGER_MODULE_CLASSNAME ) );

  final MethodSpec.Builder method =
    MethodSpec.methodBuilder( "provideComponent" ).
      addAnnotation( ClassName.bestGuess( Constants.DAGGER_PROVIDES_CLASSNAME ) ).
      addModifiers( Modifier.STATIC ).
      addParameter( descriptor.getClassNameToConstruct(), "component", Modifier.FINAL ).
      returns( COMPONENT_CLASSNAME ).
      addStatement( "return component" );
  builder.addMethod( method.build() );

  return builder.build();
}
 
開發者ID:react4j,項目名稱:react4j,代碼行數:20,代碼來源:Generator.java

示例3: getTypeSpec

import com.squareup.javapoet.TypeSpec; //導入方法依賴的package包/類
/**
 * genera classe di test per clazz
 * 
 * @param clazz
 * @param prop
 * @param mongo
 * @return
 */
@Override
public TypeSpec getTypeSpec(Class<?> clazz) {
	Builder classTestBuilder = TypeSpec.classBuilder(clazz.getSimpleName() + TEST);
	ClassName superClass = ClassName.get(
			PropertiesUtils.getRequiredProperty(prop, PropertiesUtils.TEST_BASE_PACKAGE),
			PropertiesUtils.getRequiredProperty(prop, PropertiesUtils.TEST_BASE_CLASS));
	classTestBuilder.superclass(superClass);
	classTestBuilder.addJavadoc("@author \n");
	classTestBuilder.addModifiers(Modifier.PUBLIC);
	AnnotationSpec.Builder annSpecBuilder = AnnotationSpec.builder(Generated.class);
	annSpecBuilder.addMember("value", "\"it.fratta.jerkoff.Generator\"");
	annSpecBuilder.addMember("date", "\"" + Calendar.getInstance().getTime().toString() + "\"");
	AnnotationSpec annGenSpec = annSpecBuilder.build();
	classTestBuilder.addAnnotation(annGenSpec);
	/*
	 * for spring test
	 */
	// FieldSpec.Builder spec = FieldSpec.builder(clazz,
	// getNewInstanceOfNoParameters(clazz), Modifier.PRIVATE);
	// spec.addAnnotation(Autowired.class);
	// classTestBuilder.addField(spec.build());
	addClassMethodsToBuilder(classTestBuilder, clazz);
	return classTestBuilder.build();
}
 
開發者ID:sap-nocops,項目名稱:Jerkoff,代碼行數:33,代碼來源:PojoCreatorImpl.java

示例4: build

import com.squareup.javapoet.TypeSpec; //導入方法依賴的package包/類
public TypeSpec.Builder build() {
    TypeSpec.Builder tsb = TypeSpec.classBuilder(name);
    for (Map.Entry<String, Class<?>> entry : nameTypeMap.entrySet()) {
        FieldSpec field = FieldSpec.builder(
                ClassName.get(entry.getValue()), entry.getKey(), Modifier.PRIVATE)
                .build();
        tsb.addField(field);
    }
    if (getter) {
        tsb = GetterSpec.forType(tsb).build();
    }
    if (setter) {
        tsb = SetterSpec.forType(tsb).build();
    }
    if (builder) {
        tsb = BuilderSpec.forType(packageName, tsb).build();
    }
    return tsb;
}
 
開發者ID:harmanpa,項目名稱:javabard,代碼行數:20,代碼來源:ValueClassSpec.java

示例5: process

import com.squareup.javapoet.TypeSpec; //導入方法依賴的package包/類
private JavaFile process(EnrichedTypeElement element) throws AbortProcessingException {
  TypeSpec.Builder builder = TypeSpec.classBuilder(element.getClassName(NO_OP_SUFFIX));

  if (element.getAnnotation(AutoNoOp.class).instance()) {
    builder
        .addField(createInstanceField(element))
        .addMethod(createInstanceMethod(element));
  }

  if (element.isInterface()) {
    builder.addSuperinterface(element.getTypeName());
  } else if (element.isAbstract()) {
    builder.superclass(element.getTypeName());
  } else {
    throw new AbortProcessingException("Cannot generate implementation for final class");
  }

  builder
      .addModifiers(element.getAccessModifier())
      .addTypeVariables(element.getTypeVariables())
      .addMethods(createOverridingMethods(element));

  return JavaFile.builder(element.getPackageName(), builder.build())
      .build();
}
 
開發者ID:ashdavies,項目名稱:auto,代碼行數:26,代碼來源:NoOpProcessor.java

示例6: build

import com.squareup.javapoet.TypeSpec; //導入方法依賴的package包/類
@Nonnull
@CheckReturnValue
public TypeSpec build(@Nonnull String name) {
    final TypeSpec.Builder classBuilder = TypeSpec.classBuilder(name);
    buildAnnotations().forEach(classBuilder::addAnnotation);
    buildModifiers().forEach(classBuilder::addModifiers);
    buildSuperClass().ifPresent(classBuilder::superclass);
    buildInterfaces().forEach(classBuilder::addSuperinterface);
    buildFields().forEach(classBuilder::addField);
    buildMethods().forEach(classBuilder::addMethod);
    buildInnerTypes().forEach(classBuilder::addType);
    return classBuilder.build();
}
 
開發者ID:t28hub,項目名稱:json2java4idea,代碼行數:14,代碼來源:ClassBuilder.java

示例7: applyShouldAddGeneratedAnnotation

import com.squareup.javapoet.TypeSpec; //導入方法依賴的package包/類
@Test
public void applyShouldAddGeneratedAnnotation() throws Exception {
    // setup
    final TypeSpec.Builder builder = TypeSpec.classBuilder("Test");

    // exercise
    underTest.apply(builder);

    // verify
    assertThat(builder.build())
            .hasName("Test")
            .hasAnnotation(AnnotationSpec.builder(SuppressWarnings.class)
                    .addMember("value", "$S", "all")
                    .build());
}
 
開發者ID:t28hub,項目名稱:json2java4idea,代碼行數:16,代碼來源:SuppressWarningsAnnotationPolicyTest.java

示例8: applyShouldAddGeneratedAnnotation

import com.squareup.javapoet.TypeSpec; //導入方法依賴的package包/類
@Test
public void applyShouldAddGeneratedAnnotation() throws Exception {
    // setup
    final TypeSpec.Builder builder = TypeSpec.classBuilder("Test");

    // exercise
    underTest.apply(builder);

    // verify
    assertThat(builder.build())
            .hasName("Test")
            .hasAnnotation(AnnotationSpec.builder(Generated.class)
                    .addMember("value", "$S", this.getClass().getCanonicalName())
                    .build());
}
 
開發者ID:t28hub,項目名稱:json2java4idea,代碼行數:16,代碼來源:GeneratedAnnotationPolicyTest.java

示例9: createValidatorFactory

import com.squareup.javapoet.TypeSpec; //導入方法依賴的package包/類
private TypeSpec createValidatorFactory(List<TypeElement> elements,
    String packageName,
    String factoryName) {
  TypeSpec.Builder factory =
      TypeSpec.classBuilder(ClassName.get(packageName, "InspectorFactory_" + factoryName));
  factory.addModifiers(PUBLIC, FINAL);
  factory.superclass(ClassName.get(packageName, factoryName));

  ParameterSpec type = TYPE_SPEC;
  ParameterSpec annotations = ANNOTATIONS_SPEC;
  ParameterSpec moshi = INSPECTOR_SPEC;

  MethodSpec.Builder create = MethodSpec.methodBuilder("create")
      .addModifiers(PUBLIC)
      .addAnnotation(Override.class)
      .addParameters(ImmutableSet.of(type, annotations, moshi))
      .returns(FACTORY_RETURN_TYPE_NAME);

  CodeBlock.Builder classes = null;
  CodeBlock.Builder generics = null;

  int numGenerics = 0;
  int numClasses = 0;

  // Avoid providing an adapter for an annotated type.
  create.addStatement("if (!$N.isEmpty()) return null", annotations);

  for (TypeElement element : elements) {
    TypeName elementTypeName = TypeName.get(element.asType());

    if (elementTypeName instanceof ParameterizedTypeName) {
      if (generics == null) {
        generics = CodeBlock.builder()
            .beginControlFlow("if ($N instanceof $T)", type, ParameterizedType.class)
            .addStatement("$T rawType = (($T) $N).getRawType()",
                Type.class,
                ParameterizedType.class,
                type);
      }

      addControlFlowGeneric(generics, elementTypeName, element, numGenerics);
      numGenerics++;
    } else {
      ExecutableElement jsonAdapterMethod = getValidatorMethod(element);
      if (jsonAdapterMethod != null) {
        if (classes == null) {
          classes = CodeBlock.builder();
        }

        addControlFlow(classes, CodeBlock.of("$N", type), elementTypeName, numClasses);
        numClasses++;

        classes.addStatement("return $T.$L($N)",
            element,
            jsonAdapterMethod.getSimpleName(),
            moshi);
      }
    }
  }

  if (generics != null) {
    generics.endControlFlow();
    generics.addStatement("return null");
    generics.endControlFlow();
    create.addCode(generics.build());
  }

  if (classes != null) {
    classes.endControlFlow();
    create.addCode(classes.build());
  }

  create.addStatement("return null");
  factory.addMethod(create.build());
  return factory.build();
}
 
開發者ID:hzsweers,項目名稱:inspector,代碼行數:77,代碼來源:InspectorFactoryProcessor.java


注:本文中的com.squareup.javapoet.TypeSpec.classBuilder方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。