Search in sources :

Example 66 with JavaFile

use of com.squareup.javapoet.JavaFile in project RxCache by VictorAlbertos.

the class ActionsProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> elements, RoundEnvironment roundEnv) {
    for (Element element : roundEnv.getRootElements()) {
        try {
            ProvidersClass testClass = getProvidersClass.from(element);
            if (testClass == null)
                continue;
            if (testClass.methods.isEmpty())
                continue;
            JavaFile javaFile = brewJavaFile.from(testClass);
            javaFile.writeTo(filer);
        } catch (GetProvidersClass.ValidationException e) {
            messager.printMessage(Diagnostic.Kind.ERROR, e.getMessage(), e.getElement());
        } catch (IOException e) {
            messager.printMessage(Diagnostic.Kind.ERROR, e.getMessage(), element);
        }
    }
    return false;
}
Also used : Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) JavaFile(com.squareup.javapoet.JavaFile) IOException(java.io.IOException)

Example 67 with JavaFile

use of com.squareup.javapoet.JavaFile in project potato by eyeem.

the class CodeWriter method process.

private void process() throws ClassNotFoundException, IOException {
    // Build fields
    ArrayList<FieldSpec> fields = new ArrayList<>();
    FieldSpec field;
    // FieldSpec.builder(String.class, "instance", Modifier.PRIVATE, Modifier.STATIC);
    field = FieldSpec.builder(generatedClass, "instance").addModifiers(Modifier.PRIVATE, Modifier.STATIC).build();
    fields.add(field);
    // Build methods
    ArrayList<MethodSpec> methods = new ArrayList<>();
    MethodSpec method;
    method = MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE).addParameter(context, "context").addStatement("super(context)").build();
    methods.add(method);
    method = MethodSpec.methodBuilder("id").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).addParameter(dataClass, "object").returns(String.class).addStatement("return object." + id).build();
    methods.add(method);
    method = MethodSpec.methodBuilder("classname").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).returns(Class.class).addStatement("return $T.class", dataClass).build();
    methods.add(method);
    method = MethodSpec.methodBuilder("get").addModifiers(Modifier.PUBLIC, Modifier.STATIC).addParameter(context, "context").returns(generatedClass).addStatement("if(instance == null) initialise(context)").addStatement("return instance").build();
    methods.add(method);
    method = MethodSpec.methodBuilder("initialise").addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.SYNCHRONIZED).addParameter(context, "context").beginControlFlow("if(instance == null)").addStatement("instance = new $T(context)", generatedClass).addStatement("instance.init()").endControlFlow().build();
    methods.add(method);
    // build class
    TypeSpec.Builder builder = TypeSpec.classBuilder(generatedClassName).addModifiers(Modifier.PUBLIC).superclass(ParameterizedTypeName.get(ClassName.get("com.eyeem.storage", "Storage"), dataClass));
    for (FieldSpec fieldSpec : fields) builder.addField(fieldSpec);
    for (MethodSpec methodSpec : methods) builder.addMethod(methodSpec);
    TypeSpec typeSpec = builder.build();
    JavaFile javaFile = JavaFile.builder(packageName, typeSpec).build();
    Writer writer = filer.createSourceFile(packageName + "." + generatedClassName).openWriter();
    javaFile.writeTo(writer);
    writer.close();
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ArrayList(java.util.ArrayList) JavaFile(com.squareup.javapoet.JavaFile) FieldSpec(com.squareup.javapoet.FieldSpec) Writer(java.io.Writer) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 68 with JavaFile

use of com.squareup.javapoet.JavaFile in project webofneeds by researchstudio-sat.

the class SourceGenerator method writeClasses.

public static void writeClasses(Set<TypeSpec> types, Shacl2JavaConfig config) throws IOException {
    File outputDir = new File(config.getOutputDir());
    outputDir.mkdirs();
    for (TypeSpec typeSpec : types) {
        JavaFile file = JavaFile.builder(config.getPackageName(), typeSpec).build();
        file.writeTo(outputDir);
    }
}
Also used : JavaFile(com.squareup.javapoet.JavaFile) 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