use of com.squareup.javapoet.JavaFile in project AndroidLife by CaMnter.
the class SaveTestProcessor method process.
/**
* {@inheritDoc}
*/
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
MethodSpec main = MethodSpec.methodBuilder("main").addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(void.class).addParameter(String[].class, "args").addStatement("$T.out.println($S)", System.class, "Save you from anything").build();
TypeSpec saveTest = TypeSpec.classBuilder("SaveTest").addModifiers(Modifier.PUBLIC, Modifier.FINAL).addMethod(main).build();
JavaFile javaFile = JavaFile.builder("com.camnter.annotation.processor.output", saveTest).build();
try {
javaFile.writeTo(this.processingEnv.getFiler());
} catch (IOException e) {
// e.printStackTrace();
}
return false;
}
use of com.squareup.javapoet.JavaFile in project AndroidLife by CaMnter.
the class R2ClassBuilder method brewJava.
/**
* JavaPoet 生成 R2
*
* @param rFile R.java File
* @param outputDir R2.java 输出文件夹
* @param packageName 包名
* @param className R2 name
* @throws Exception exception
*/
public static void brewJava(File rFile, File outputDir, String packageName, String className) throws Exception {
/*
* JavaParser 解析 R.java File
* 获取到 TypeDeclaration
*/
CompilationUnit compilationUnit = JavaParser.parse(rFile);
TypeDeclaration resourceClass = compilationUnit.getTypes().get(0);
/*
* 定义 R2.java class
*/
TypeSpec.Builder result = TypeSpec.classBuilder(className).addModifiers(PUBLIC).addModifiers(FINAL);
/*
* 遍历 R.java File 的每一个节点( 内部类或者接口 --> ClassOrInterfaceDeclaration )
* 添加到 R2.java 内
* 这里是给 TypeSpec 添加生成 内部类 的 语句
*/
for (Node node : resourceClass.getChildNodes()) {
if (node instanceof ClassOrInterfaceDeclaration) {
addResourceType(Arrays.asList(SUPPORTED_TYPES), result, (ClassOrInterfaceDeclaration) node);
}
}
JavaFile finalR = JavaFile.builder(packageName, result.build()).addFileComment("Generated code from gradle plugin. Do not modify!").build();
finalR.writeTo(outputDir);
}
use of com.squareup.javapoet.JavaFile in project web3sdk by FISCO-BCOS.
the class AbiTypesGenerator method write.
private void write(String packageName, TypeSpec typeSpec, Path destination) throws IOException {
JavaFile javaFile = JavaFile.builder(packageName, typeSpec).indent(" ").skipJavaLangImports(true).build();
javaFile.writeTo(destination);
}
use of com.squareup.javapoet.JavaFile in project microservice_framework by CJSCommonPlatform.
the class DynamicAggregateTestClassGenerator method generatedTestAggregateClassOf.
public <T extends Aggregate> Class<T> generatedTestAggregateClassOf(long serialVersionUid, String basePackage, String fileNameToBeGenerated) throws IOException, ClassNotFoundException {
final MethodSpec constructor = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).build();
final MethodSpec apply = MethodSpec.methodBuilder("apply").addModifiers(Modifier.PUBLIC).addCode(" return match(event).with(\n" + " when(uk.gov.justice.domain.event.EventA.class).apply(x -> {\n" + " ++count;\n" + " }\n" + " ));").returns(Object.class).addAnnotation(Override.class).addParameter(Object.class, "event").build();
final MethodSpec applyObjectStream = MethodSpec.methodBuilder("apply").addModifiers(Modifier.PUBLIC).addCode(" return events\n" + " .map(this::apply)\n" + " .collect(toList())\n" + " .stream();").addAnnotation(Override.class).returns(ParameterizedTypeName.get(Stream.class, Object.class)).addParameter(ParameterizedTypeName.get(Stream.class, Object.class), "events").build();
final FieldSpec.Builder fieldSpecSerialVersionId = FieldSpec.builder(TypeName.LONG.unbox(), "serialVersionUID", Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL);
fieldSpecSerialVersionId.initializer(serialVersionUid + "L");
final FieldSpec.Builder fieldSpecCount = FieldSpec.builder(TypeName.INT.unbox(), "count", Modifier.PRIVATE);
final TypeSpec dynamicTestAggregate = TypeSpec.classBuilder(fileNameToBeGenerated).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addMethod(constructor).addField(fieldSpecSerialVersionId.build()).addField(fieldSpecCount.build()).addMethod(apply).addMethod(applyObjectStream).addSuperinterface(Aggregate.class).build();
final ClassName eventSwitcherMatch = ClassName.get("uk.gov.justice.domain.aggregate", "matcher", "EventSwitcher");
final ClassName eventSwitcherWhen = ClassName.get("uk.gov.justice.domain.aggregate", "matcher", "EventSwitcher");
final ClassName toList = ClassName.get("java.util", "stream", "Collectors");
final JavaFile javaFile = JavaFile.builder(basePackage, dynamicTestAggregate).addStaticImport(eventSwitcherMatch, "match").addStaticImport(eventSwitcherWhen, "when").addStaticImport(toList, "toList").build();
final String path = DynamicAggregateTestClassGenerator.class.getClassLoader().getResource("").getPath();
final File pathRoot = new File(path);
javaFile.writeTo(pathRoot);
javaFile.writeTo(System.out);
final JavaCompilerUtil compiler = new JavaCompilerUtil(pathRoot, pathRoot);
final Class<?> generatedClass = compiler.compiledClassOf(basePackage, fileNameToBeGenerated);
return (Class<T>) generatedClass;
}
use of com.squareup.javapoet.JavaFile in project components by Talend.
the class MetaDataModelGen method genSearchRecordTypeEnumClass.
public void genSearchRecordTypeEnumClass() throws IOException {
List<SearchRecordTypeSpec> specs = new ArrayList<>(searchRecordTypeMap.values());
Collections.sort(specs, new Comparator<SearchRecordTypeSpec>() {
@Override
public int compare(SearchRecordTypeSpec o1, SearchRecordTypeSpec o2) {
return o1.getEnumConstantName().compareTo(o2.getEnumConstantName());
}
});
TypeSpec.Builder builder = TypeSpec.enumBuilder(searchRecordTypeEnumClassName.simpleName()).addSuperinterface(SearchRecordTypeDesc.class).addModifiers(javax.lang.model.element.Modifier.PUBLIC).addField(String.class, "type", javax.lang.model.element.Modifier.PRIVATE, javax.lang.model.element.Modifier.FINAL).addField(String.class, "typeName", javax.lang.model.element.Modifier.PRIVATE, javax.lang.model.element.Modifier.FINAL).addField(Class.class, "searchClass", javax.lang.model.element.Modifier.PRIVATE, javax.lang.model.element.Modifier.FINAL).addField(Class.class, "searchBasicClass", javax.lang.model.element.Modifier.PRIVATE, javax.lang.model.element.Modifier.FINAL).addField(Class.class, "searchAdvancedClass", javax.lang.model.element.Modifier.PRIVATE, javax.lang.model.element.Modifier.FINAL);
for (SearchRecordTypeSpec spec : specs) {
TypeSpec enumTypeSpec;
if (spec.getSearchClass() != null && spec.getSearchAdvancedClass() != null) {
enumTypeSpec = TypeSpec.anonymousClassBuilder("$S, $S, $T.class, $T.class, $T.class", spec.getName(), spec.getTypeName(), spec.getSearchClass(), spec.getSearchBasicClass(), spec.getSearchAdvancedClass()).build();
} else {
enumTypeSpec = TypeSpec.anonymousClassBuilder("$S, $S, null, $T.class, null", spec.getName(), spec.getTypeName(), spec.getSearchBasicClass()).build();
}
builder.addEnumConstant(spec.getEnumConstantName(), enumTypeSpec);
}
builder.addMethod(MethodSpec.constructorBuilder().addParameter(String.class, "type").addParameter(String.class, "typeName").addParameter(Class.class, "searchClass").addParameter(Class.class, "searchBasicClass").addParameter(Class.class, "searchAdvancedClass").addStatement("this.$N = $N", "type", "type").addStatement("this.$N = $N", "typeName", "typeName").addStatement("this.$N = $N", "searchClass", "searchClass").addStatement("this.$N = $N", "searchBasicClass", "searchBasicClass").addStatement("this.$N = $N", "searchAdvancedClass", "searchAdvancedClass").build());
builder.addMethod(MethodSpec.methodBuilder("getType").addAnnotation(Override.class).addModifiers(javax.lang.model.element.Modifier.PUBLIC).returns(String.class).addStatement("return this.type").build());
builder.addMethod(MethodSpec.methodBuilder("getTypeName").addAnnotation(Override.class).addModifiers(javax.lang.model.element.Modifier.PUBLIC).returns(String.class).addStatement("return this.typeName").build());
builder.addMethod(MethodSpec.methodBuilder("getSearchClass").addAnnotation(Override.class).addModifiers(javax.lang.model.element.Modifier.PUBLIC).returns(Class.class).addStatement("return this.searchClass").build());
builder.addMethod(MethodSpec.methodBuilder("getSearchBasicClass").addAnnotation(Override.class).addModifiers(javax.lang.model.element.Modifier.PUBLIC).returns(Class.class).addStatement("return this.searchBasicClass").build());
builder.addMethod(MethodSpec.methodBuilder("getSearchAdvancedClass").addAnnotation(Override.class).addModifiers(javax.lang.model.element.Modifier.PUBLIC).returns(Class.class).addStatement("return this.searchAdvancedClass").build());
builder.addMethod(MethodSpec.methodBuilder("getByTypeName").addModifiers(javax.lang.model.element.Modifier.PUBLIC, javax.lang.model.element.Modifier.STATIC).returns(searchRecordTypeEnumClassName).addParameter(String.class, "typeName").addCode(CodeBlock.builder().beginControlFlow("for ($T value : values())", searchRecordTypeEnumClassName).beginControlFlow("if (value.typeName.equals($N))", "typeName").addStatement("return value").endControlFlow().endControlFlow().addStatement("return null").build()).build());
builder.addJavadoc("Generated by $T\n", getClass());
TypeSpec typeSpec = builder.build();
JavaFile jfile = JavaFile.builder(searchRecordTypeEnumClassName.packageName(), typeSpec).indent(" ").build();
jfile.writeTo(outputFolder);
}
Aggregations