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;
}
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();
}
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);
}
}
Aggregations