Search in sources :

Example 46 with JavaFile

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;
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) JavaFile(com.squareup.javapoet.JavaFile) IOException(java.io.IOException) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 47 with JavaFile

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);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Node(com.github.javaparser.ast.Node) JavaFile(com.squareup.javapoet.JavaFile) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 48 with JavaFile

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);
}
Also used : JavaFile(com.squareup.javapoet.JavaFile)

Example 49 with JavaFile

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;
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) FieldSpec(com.squareup.javapoet.FieldSpec) JavaCompilerUtil(uk.gov.justice.services.test.utils.core.compiler.JavaCompilerUtil) ClassName(com.squareup.javapoet.ClassName) JavaFile(com.squareup.javapoet.JavaFile) Stream(java.util.stream.Stream) Aggregate(uk.gov.justice.domain.aggregate.Aggregate) File(java.io.File) JavaFile(com.squareup.javapoet.JavaFile) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 50 with JavaFile

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);
}
Also used : ArrayList(java.util.ArrayList) JavaFile(com.squareup.javapoet.JavaFile) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

JavaFile (com.squareup.javapoet.JavaFile)68 TypeSpec (com.squareup.javapoet.TypeSpec)43 IOException (java.io.IOException)42 TypeElement (javax.lang.model.element.TypeElement)25 MethodSpec (com.squareup.javapoet.MethodSpec)21 ClassName (com.squareup.javapoet.ClassName)19 Element (javax.lang.model.element.Element)15 ExecutableElement (javax.lang.model.element.ExecutableElement)12 HashMap (java.util.HashMap)10 File (java.io.File)9 HashSet (java.util.HashSet)9 Map (java.util.Map)9 VariableElement (javax.lang.model.element.VariableElement)9 Builder (com.squareup.javapoet.TypeSpec.Builder)8 ArrayList (java.util.ArrayList)8 ProcessorException (com.github.mvp4g.mvp4g2.processor.ProcessorException)6 PackageElement (javax.lang.model.element.PackageElement)6 DeclaredType (javax.lang.model.type.DeclaredType)6 TypeMirror (javax.lang.model.type.TypeMirror)6 CodeBlock (com.squareup.javapoet.CodeBlock)5