当前位置: 首页>>代码示例>>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;未经允许,请勿转载。