本文整理汇总了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();
}
示例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();
}
示例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();
}
示例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;
}
示例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();
}
示例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();
}
示例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());
}
示例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());
}
示例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();
}