Search in sources :

Example 1 with JavaFile

use of com.squareup.javapoet.JavaFile in project graylog2-server by Graylog2.

the class WithBeanGetterExtension method generateClass.

@Override
public String generateClass(Context context, String className, String classToExtend, boolean isFinal) {
    final TypeSpec.Builder typeSpecBuilder = newTypeSpecBuilder(context, className, classToExtend, isFinal);
    final Map<String, ExecutableElement> properties = context.properties();
    for (Map.Entry<String, ExecutableElement> entry : properties.entrySet()) {
        typeSpecBuilder.addMethod(generateGetterMethod(entry.getKey(), entry.getValue()));
    }
    final JavaFile javaFile = JavaFile.builder(context.packageName(), typeSpecBuilder.build()).build();
    return javaFile.toString();
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) JavaFile(com.squareup.javapoet.JavaFile) Map(java.util.Map) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 2 with JavaFile

use of com.squareup.javapoet.JavaFile in project bazel by bazelbuild.

the class JavaCodeGeneratorHelper method writeClassToDir.

private static void writeClassToDir(TypeSpec klass, String packageName, Path path) throws IOException {
    JavaFile javaFile = JavaFile.builder(packageName, klass).build();
    javaFile.writeTo(path);
}
Also used : JavaFile(com.squareup.javapoet.JavaFile)

Example 3 with JavaFile

use of com.squareup.javapoet.JavaFile in project web3sdk by FISCO-BCOS.

the class AbiTypesMapperGenerator 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 4 with JavaFile

use of com.squareup.javapoet.JavaFile in project web3sdk by FISCO-BCOS.

the class SolidityFunctionWrapper method generateJavaFiles.

public void generateJavaFiles(String contractName, String bin, String abi, String destinationDirLocation, String basePackageName) throws IOException, ClassNotFoundException {
    String className = Strings.capitaliseFirstLetter(contractName);
    TypeSpec.Builder classBuilder = createClassBuilder(className, bin, abi);
    classBuilder.addMethod(buildConstructor(Credentials.class, CREDENTIALS));
    classBuilder.addMethod(buildConstructor(TransactionManager.class, TRANSACTION_MANAGER));
    classBuilder.addMethod(buildConstructorWithDefaultParam(Credentials.class, CREDENTIALS));
    classBuilder.addMethod(buildConstructorWithDefaultParam(TransactionManager.class, TRANSACTION_MANAGER));
    classBuilder.addMethods(buildFunctionDefinitions(className, classBuilder, loadContractDefinition(abi)));
    classBuilder.addMethod(buildLoad(className, Credentials.class, CREDENTIALS));
    classBuilder.addMethod(buildLoad(className, TransactionManager.class, TRANSACTION_MANAGER));
    classBuilder.addMethod(buildLoadByName(className, Credentials.class, CREDENTIALS));
    classBuilder.addMethod(buildLoadByName(className, TransactionManager.class, TRANSACTION_MANAGER));
    JavaFile javaFile = JavaFile.builder(basePackageName, classBuilder.build()).indent("    ").skipJavaLangImports(true).build();
    javaFile.writeTo(new File(destinationDirLocation));
}
Also used : TransactionManager(org.bcos.web3j.tx.TransactionManager) JavaFile(com.squareup.javapoet.JavaFile) File(java.io.File) JavaFile(com.squareup.javapoet.JavaFile) Credentials(org.bcos.web3j.crypto.Credentials) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 5 with JavaFile

use of com.squareup.javapoet.JavaFile in project Bro by 2BAB.

the class CodeGenerator method generateMergeMapFile.

/**
 * Step 3 :
 * <p>
 * Generate all map file with temp-json by other module in app-module's build path
 *
 * @param packageName package name for generating host file
 * @param filer       filer tools
 * @param existFile   exist file generated by processor.init()
 */
public static void generateMergeMapFile(String packageName, Map<String, Map<String, BroProperties>> exposeMaps, Filer filer, File existFile, String rootProjectPath) {
    try {
        TypeSpec.Builder builder = TypeSpec.classBuilder(Constants.MERGED_MAP_FILE_NAME).addModifiers(Modifier.PUBLIC).addSuperinterface(IBroMap.class);
        for (Map.Entry<String, Map<String, BroProperties>> entry : exposeMaps.entrySet()) {
            builder.addMethod(generateMapMethod("get" + entry.getKey() + "Map", entry.getValue()));
        }
        JavaFile file = JavaFile.builder(packageName, builder.build()).addFileComment("Generated by Bro.").build();
        if (existFile == null) {
            // standard output
            file.writeTo(filer);
        } else {
            // hack file override exception
            File folder = existFile.getParentFile();
            int times = file.toJavaFileObject().toUri().toString().split("/").length;
            for (int i = 1; i < times; i++) {
                folder = folder.getParentFile();
            }
            file.writeTo(folder);
        }
        DocGenerator.generateDoc(rootProjectPath, exposeMaps);
    } catch (Exception e) {
        BroCompileLogger.e(e.getMessage());
    }
}
Also used : JavaFile(com.squareup.javapoet.JavaFile) HashMap(java.util.HashMap) IBroMap(me.xx2bab.bro.common.IBroMap) TreeMap(java.util.TreeMap) Map(java.util.Map) File(java.io.File) 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