本文整理汇总了Java中com.squareup.javapoet.TypeSpec.Builder类的典型用法代码示例。如果您正苦于以下问题:Java Builder类的具体用法?Java Builder怎么用?Java Builder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Builder类属于com.squareup.javapoet.TypeSpec包,在下文中一共展示了Builder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: generateRegisterComponents
import com.squareup.javapoet.TypeSpec.Builder; //导入依赖的package包/类
private MethodSpec generateRegisterComponents(Set<String> libraryGlideModuleClassNames,
Set<String> excludedGlideModuleClassNames) {
MethodSpec.Builder registerComponents =
MethodSpec.methodBuilder("registerComponents")
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override.class)
.addParameter(ClassName.get("android.content", "Context"), "context")
.addParameter(ClassName.get("com.bumptech.glide", "Registry"), "registry");
for (String glideModule : libraryGlideModuleClassNames) {
if (excludedGlideModuleClassNames.contains(glideModule)) {
continue;
}
ClassName moduleClassName = ClassName.bestGuess(glideModule);
registerComponents.addStatement(
"new $T().registerComponents(context, registry)", moduleClassName);
}
// Order matters here. The AppGlideModule must be called last.
registerComponents.addStatement("appGlideModule.registerComponents(context, registry)");
return registerComponents.build();
}
示例2: generateConstructor
import com.squareup.javapoet.TypeSpec.Builder; //导入依赖的package包/类
private MethodSpec generateConstructor(ClassName appGlideModule,
Set<String> libraryGlideModuleClassNames, Set<String> excludedGlideModuleClassNames) {
MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder();
constructorBuilder.addStatement("appGlideModule = new $T()", appGlideModule);
ClassName androidLogName = ClassName.get("android.util", "Log");
// Add some log lines to indicate to developers which modules where discovered.
constructorBuilder.beginControlFlow("if ($T.isLoggable($S, $T.DEBUG))",
androidLogName, GLIDE_LOG_TAG, androidLogName);
constructorBuilder.addStatement("$T.d($S, $S)", androidLogName, GLIDE_LOG_TAG,
"Discovered AppGlideModule from annotation: " + appGlideModule);
// Excluded GlideModule classes from the manifest are logged in Glide's singleton.
for (String glideModule : libraryGlideModuleClassNames) {
ClassName moduleClassName = ClassName.bestGuess(glideModule);
if (excludedGlideModuleClassNames.contains(glideModule)) {
constructorBuilder.addStatement("$T.d($S, $S)", androidLogName, GLIDE_LOG_TAG,
"AppGlideModule excludes LibraryGlideModule from annotation: " + moduleClassName);
} else {
constructorBuilder.addStatement("$T.d($S, $S)", androidLogName, GLIDE_LOG_TAG,
"Discovered LibraryGlideModule from annotation: " + moduleClassName);
}
}
constructorBuilder.endControlFlow();
return constructorBuilder.build();
}
示例3: generateRegisterComponents
import com.squareup.javapoet.TypeSpec.Builder; //导入依赖的package包/类
private MethodSpec generateRegisterComponents(Set<String> libraryGlideModuleClassNames,
Set<String> excludedGlideModuleClassNames) {
MethodSpec.Builder registerComponents =
MethodSpec.methodBuilder("registerComponents")
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override.class)
.addParameter(ClassName.get("android.content", "Context"), "context")
.addParameter(ClassName.get("com.bumptech.glide", "Glide"), "glide")
.addParameter(ClassName.get("com.bumptech.glide", "Registry"), "registry");
for (String glideModule : libraryGlideModuleClassNames) {
if (excludedGlideModuleClassNames.contains(glideModule)) {
continue;
}
ClassName moduleClassName = ClassName.bestGuess(glideModule);
registerComponents.addStatement(
"new $T().registerComponents(context, glide, registry)", moduleClassName);
}
// Order matters here. The AppGlideModule must be called last.
registerComponents.addStatement("appGlideModule.registerComponents(context, glide, registry)");
return registerComponents.build();
}
示例4: poetSpec
import com.squareup.javapoet.TypeSpec.Builder; //导入依赖的package包/类
@Override
public TypeSpec poetSpec() {
Builder enumBuilder = createEnumBuilder(className)
.addField(String.class, VALUE, Modifier.PRIVATE, Modifier.FINAL)
.addMethod(toStringBuilder().addStatement("return $T.valueOf($N)", String.class, VALUE).build())
.addMethod(fromValueSpec())
.addMethod(knownValuesSpec())
.addMethod(createConstructor());
addDeprecated(enumBuilder::addAnnotation, shape);
addJavadoc(enumBuilder::addJavadoc, shape);
shape.getEnums().forEach(
e -> enumBuilder.addEnumConstant(e.getName(), TypeSpec.anonymousClassBuilder("$S", e.getValue()).build())
);
enumBuilder.addEnumConstant(UNKNOWN_TO_SDK_VERSION, TypeSpec.anonymousClassBuilder("null").build());
return enumBuilder.build();
}
示例5: addClassMethodsToBuilder
import com.squareup.javapoet.TypeSpec.Builder; //导入依赖的package包/类
/**
* genera i metodi di test per clazz
*
* @param classTestBuilder
* @param clazz
* @param prop
* @param mongo
*/
private void addClassMethodsToBuilder(Builder classTestBuilder, Class<?> clazz) {
int count = 0;
String appName = PropertiesUtils.getRequiredProperty(prop, PropertiesUtils.APP_NAME);
List<Document> methodInfo;
for (Method method : clazz.getDeclaredMethods()) {
Document methodInputs = null, methodOutput = null;
if (java.lang.reflect.Modifier.isPublic(method.getModifiers())) {
LOG.info("app: " + appName + " - method: " + method);
count++;
methodInfo = mongo.find(appName, method.toString());
for (Document doc : methodInfo) {
LOG.debug("document: " + doc);
if (doc.containsKey("argsBefore")) {
methodInputs = doc;
} else {
methodOutput = doc;
}
}
MethodSpec methodSpec = getMethodSpec(count, method, clazz, methodInputs, methodOutput);
classTestBuilder.addMethod(methodSpec);
}
}
classTestBuilder.addMethod(getDeserializeMethod(clazz));
}
示例6: getMethodSpec
import com.squareup.javapoet.TypeSpec.Builder; //导入依赖的package包/类
/**
* genera il metodo di test per method di clazz
*
* @param count
* @param method
* @param clazz
* @param infoFromMongoDb
* @param methodOutput
* @return
*/
private MethodSpec getMethodSpec(int count, Method method, Class<?> clazz, Document methodInputs,
Document methodOutput) {
String result = getAssignmentOfMethodResult(method);
String expected = getExpectedResultAsBooleanAssert(method, methodOutput);
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(method.getName() + count + TEST);
/*
* for non spring test
*/
String invokerName = getInvokerName(method, clazz, methodInputs, methodBuilder);
String params = getParams(method, methodBuilder, methodInputs);
AnnotationSpec.Builder annSpecBuilder = AnnotationSpec.builder(Test.class);
addExpectedExceptionIfAny(methodInputs, annSpecBuilder);
AnnotationSpec annTestSpec = annSpecBuilder.build();
methodBuilder.addAnnotation(annTestSpec)
.addStatement(result + invokerName + ".$N(" + params + ")", method.getName())
.addModifiers(Modifier.PUBLIC);
methodBuilder.addStatement("$L.assertTrue(" + expected + ")", Assert.class.getName());
methodBuilder.addJavadoc("\n");
return methodBuilder.build();
}
示例7: newInstance
import com.squareup.javapoet.TypeSpec.Builder; //导入依赖的package包/类
/**
* inizializza l'object under test
*
* @param clazz
* @param methodBuilder
* @param method
* @param methodInputs
*/
private void newInstance(Class<?> clazz, MethodSpec.Builder methodBuilder, Method method, Document methodInputs) {
// TODO here we need to evaluate combination between constructors and
// setter to see if it is possible to instantiate the object properly,
// otherwise direct deserialization must be done
// methodBuilder.addStatement(new
// StringBuilder(clazz.getSimpleName()).append(" ")
// .append(getInstanceVariableName(clazz)).append(" = new
// ").append(clazz.getSimpleName()).append("(")
// .append(getValueWithApexIfRequired(methodReturnType,
// methodReturnValue)).append(")").toString());
Class<?> methodReturnType = method.getReturnType();
String returnValue = methodInputs.getString("returnValue");
// methodBuilder.addStatement("ObjectDeserializer objDes = new ObjectDeserializer();");
methodBuilder.addStatement("String json = \"" + methodInputs.toJson() + "\"");
methodBuilder.addStatement(clazz.getSimpleName() + " " + getInstanceVariableName(clazz)
+ " = getInstance(json, " + clazz.getSimpleName() + ".class)");
}
示例8: getParams
import com.squareup.javapoet.TypeSpec.Builder; //导入依赖的package包/类
/**
* genera l'inizializzazione dei parametri e ritorna la lista dei nomi
*
* @param method
* @param methodBuilder
* @param methodInputs
* @return
*/
private String getParams(Method method, MethodSpec.Builder methodBuilder, Document methodInputs) {
String params = "";
String[] methodArgsValues = methodInputs.getString("argsBefore").split(",");
int count = 0;
for (Class<?> parameterClass : method.getParameterTypes()) {
String parameterName = getInstanceVariableName(parameterClass) + count;
params += "," + parameterName;
methodBuilder
.addStatement(parameterClass.getName() + " " + parameterName + " = " + methodArgsValues[count]);
count++;
}
if (params.startsWith(",")) {
params = params.substring(1);
}
return params;
}
示例9: getTypeSpec
import com.squareup.javapoet.TypeSpec.Builder; //导入依赖的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();
}
示例10: generate
import com.squareup.javapoet.TypeSpec.Builder; //导入依赖的package包/类
/**
* Generate and save the Java file for the typeElement passed to the constructor
* @param directiveTypeElement The {@link VueDirective} class to
* generate {@link VueDirectiveOptions} from
*/
public void generate(TypeElement directiveTypeElement)
{
ClassName optionsClassName = GenerationNameUtil.directiveOptionsName(directiveTypeElement);
Builder componentClassBuilder = TypeSpec
.classBuilder(optionsClassName)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.superclass(VueDirectiveOptions.class)
.addAnnotation(JsType.class)
.addJavadoc("VueComponent Directive Options for directive {@link $S}",
directiveTypeElement.getQualifiedName().toString());
// Initialize constructor
MethodSpec.Builder constructorBuilder =
MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC);
// Add the Java Component Instance initialization
constructorBuilder.addStatement("this.$L = new $T()",
"vuegwt$javaDirectiveInstance",
TypeName.get(directiveTypeElement.asType()));
// Call the method to copy hooks functions
constructorBuilder.addStatement("this.copyHooks()");
// Finish building the constructor
componentClassBuilder.addMethod(constructorBuilder.build());
// Build the DirectiveOptions class
GenerationUtil.toJavaFile(filer,
componentClassBuilder,
optionsClassName,
directiveTypeElement);
}
示例11: processData
import com.squareup.javapoet.TypeSpec.Builder; //导入依赖的package包/类
/**
* Process data fields from the {@link VueComponent} Class.
* @param component {@link VueComponent} to process
* @param optionsBuilder A {@link MethodSpec.Builder} for the method that creates the
* {@link VueComponentOptions}
*/
private void processData(TypeElement component, MethodSpec.Builder optionsBuilder)
{
Component annotation = component.getAnnotation(Component.class);
List<String> fieldsName = ElementFilter
.fieldsIn(component.getEnclosedElements())
.stream()
.filter(ComponentGenerationUtil::isFieldVisibleInJS)
.filter(field -> field.getAnnotation(Prop.class) == null)
.map(field -> field.getSimpleName().toString())
.collect(Collectors.toList());
if (fieldsName.isEmpty())
return;
// Declare data fields
String fieldNamesParameters = fieldsName
.stream()
.map(fieldName -> "\"" + fieldName + "\"")
.collect(Collectors.joining(", "));
optionsBuilder.addStatement("options.initData($L, $L)",
annotation.useFactory(),
fieldNamesParameters);
}
示例12: processProps
import com.squareup.javapoet.TypeSpec.Builder; //导入依赖的package包/类
/**
* Process Vue Props from the {@link VueComponent} Class.
* @param component {@link VueComponent} to process
* @param optionsBuilder A {@link MethodSpec.Builder} for the method that creates the
* {@link VueComponentOptions}
*/
private void processProps(TypeElement component, MethodSpec.Builder optionsBuilder)
{
ElementFilter
.fieldsIn(component.getEnclosedElements())
.stream()
.filter(field -> hasAnnotation(field, Prop.class))
.forEach(field -> {
String fieldName = field.getSimpleName().toString();
Prop prop = field.getAnnotation(Prop.class);
if (!isFieldVisibleInJS(field))
{
printError("The field \""
+ fieldName
+ "\" annotated with @Prop must also be annotated with @JsProperty.",
component);
}
optionsBuilder.addStatement("options.addJavaProp($S, $L, $S)",
fieldName,
prop.required(),
prop.checkType() ? getNativeNameForJavaType(field.asType()) : null);
});
}
示例13: processComputed
import com.squareup.javapoet.TypeSpec.Builder; //导入依赖的package包/类
/**
* Process computed properties from the Component Class.
* @param component {@link VueComponent} to process
* @param optionsBuilder A {@link MethodSpec.Builder} for the method that creates the
* {@link VueComponentOptions}
* @param componentJsTypeBuilder Builder for the JsType class
*/
private void processComputed(TypeElement component, MethodSpec.Builder optionsBuilder,
Builder componentJsTypeBuilder)
{
getMethodsWithAnnotation(component, Computed.class).forEach(method -> {
String methodName = method.getSimpleName().toString();
ComputedKind kind = ComputedKind.GETTER;
if ("void".equals(method.getReturnType().toString()))
kind = ComputedKind.SETTER;
String propertyName = GenerationUtil.getComputedPropertyName(method);
optionsBuilder.addStatement("options.addJavaComputed($S, $S, $T.$L)",
methodName,
propertyName,
ComputedKind.class,
kind);
addProxyJsTypeMethodIfNecessary(componentJsTypeBuilder, method);
});
addFieldsForComputedMethod(component, componentJsTypeBuilder, new HashSet<>());
}
示例14: processTemplateMethods
import com.squareup.javapoet.TypeSpec.Builder; //导入依赖的package包/类
/**
* Process template methods for our {@link VueComponent} class.
* @param component {@link VueComponent} to process
* @param optionsBuilder A {@link MethodSpec.Builder} for the method that creates the
* {@link VueComponentOptions}
* @param componentJsTypeBuilder Builder for the JsType class
* @param hookMethodsFromInterfaces Hook methods from the interface the {@link VueComponent}
* implements
*/
private void processTemplateMethods(TypeElement component, MethodSpec.Builder optionsBuilder,
Builder componentJsTypeBuilder, Set<ExecutableElement> hookMethodsFromInterfaces)
{
List<ExecutableElement> templateMethods = ElementFilter
.methodsIn(component.getEnclosedElements())
.stream()
.filter(ComponentGenerationUtil::isMethodVisibleInTemplate)
.filter(method -> !isHookMethod(component, method, hookMethodsFromInterfaces))
.collect(Collectors.toList());
templateMethods.forEach(method -> addProxyJsTypeMethodIfNecessary(componentJsTypeBuilder,
method));
// Declare methods in the component
String methodNamesParameters = templateMethods
.stream()
.map(method -> "\"" + method.getSimpleName() + "\"")
.collect(Collectors.joining(", "));
optionsBuilder.addStatement("options.addMethods($L)", methodNamesParameters);
}
示例15: processPropValidators
import com.squareup.javapoet.TypeSpec.Builder; //导入依赖的package包/类
/**
* Process prop validators from the Component Class.
* @param component {@link VueComponent} to process
* @param optionsBuilder A {@link MethodSpec.Builder} for the method that creates the
* {@link VueComponentOptions}
*/
private void processPropValidators(TypeElement component, MethodSpec.Builder optionsBuilder,
Builder componentJsTypeBuilder)
{
getMethodsWithAnnotation(component, PropValidator.class).forEach(method -> {
PropValidator propValidator = method.getAnnotation(PropValidator.class);
if (!TypeName.get(method.getReturnType()).equals(TypeName.BOOLEAN))
{
printError("Method "
+ method.getSimpleName()
+ " annotated with PropValidator must return a boolean.", component);
}
String propertyName = propValidator.value();
optionsBuilder.addStatement("options.addJavaPropValidator($S, $S)",
method.getSimpleName().toString(),
propertyName);
addProxyJsTypeMethodIfNecessary(componentJsTypeBuilder, method);
});
}