use of com.squareup.javapoet.JavaFile in project butterknife by JakeWharton.
the class ButterKnifeProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> elements, RoundEnvironment env) {
Map<TypeElement, BindingSet> bindingMap = findAndParseTargets(env);
for (Map.Entry<TypeElement, BindingSet> entry : bindingMap.entrySet()) {
TypeElement typeElement = entry.getKey();
BindingSet binding = entry.getValue();
JavaFile javaFile = binding.brewJava(sdk, debuggable);
try {
javaFile.writeTo(filer);
} catch (IOException e) {
error(typeElement, "Unable to write binding for type %s: %s", typeElement, e.getMessage());
}
}
return false;
}
use of com.squareup.javapoet.JavaFile in project vue-gwt by Axellience.
the class GeneratorsUtil method toJavaFile.
public static void toJavaFile(Filer filer, Builder classBuilder, ClassName className, TypeElement... originatingElement) {
try {
JavaFile javaFile = JavaFile.builder(className.packageName(), classBuilder.build()).build();
JavaFileObject javaFileObject = filer.createSourceFile(className.reflectionName(), originatingElement);
Writer writer = javaFileObject.openWriter();
javaFile.writeTo(writer);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
use of com.squareup.javapoet.JavaFile in project components by Talend.
the class MetaDataModelGen method genRecordTypeEnumClass.
public void genRecordTypeEnumClass() throws IOException {
List<RecordTypeSpec> specs = new ArrayList<>(recordTypeMap.values());
Collections.sort(specs, new Comparator<RecordTypeSpec>() {
@Override
public int compare(RecordTypeSpec o1, RecordTypeSpec o2) {
return o1.getEnumConstantName().compareTo(o2.getEnumConstantName());
}
});
TypeSpec.Builder builder = TypeSpec.enumBuilder(recordTypeEnumClassName.simpleName()).addSuperinterface(RecordTypeDesc.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, "recordClass", javax.lang.model.element.Modifier.PRIVATE, javax.lang.model.element.Modifier.FINAL).addField(String.class, "searchRecordType", javax.lang.model.element.Modifier.PRIVATE, javax.lang.model.element.Modifier.FINAL);
for (RecordTypeSpec spec : specs) {
builder.addEnumConstant(spec.getEnumConstantName(), TypeSpec.anonymousClassBuilder("$S, $S, $T.class, $S", spec.getName(), spec.getTypeName(), spec.getRecordClass(), spec.getSearchRecordTypeName()).build());
}
builder.addMethod(MethodSpec.constructorBuilder().addParameter(String.class, "type").addParameter(String.class, "typeName").addParameter(Class.class, "recordClass").addParameter(String.class, "searchRecordType").addStatement("this.$N = $N", "type", "type").addStatement("this.$N = $N", "typeName", "typeName").addStatement("this.$N = $N", "recordClass", "recordClass").addStatement("this.$N = $N", "searchRecordType", "searchRecordType").build());
builder.addMethod(MethodSpec.methodBuilder("getType").addAnnotation(Override.class).addModifiers(javax.lang.model.element.Modifier.PUBLIC).returns(String.class).addStatement("return this.$N", "type").build());
builder.addMethod(MethodSpec.methodBuilder("getTypeName").addAnnotation(Override.class).addModifiers(javax.lang.model.element.Modifier.PUBLIC).returns(String.class).addStatement("return this.$N", "typeName").build());
builder.addMethod(MethodSpec.methodBuilder("getRecordClass").addAnnotation(Override.class).addModifiers(javax.lang.model.element.Modifier.PUBLIC).returns(Class.class).addStatement("return this.$N", "recordClass").build());
builder.addMethod(MethodSpec.methodBuilder("getSearchRecordType").addAnnotation(Override.class).addModifiers(javax.lang.model.element.Modifier.PUBLIC).returns(String.class).addStatement("return this.$N", "searchRecordType").build());
builder.addMethod(MethodSpec.methodBuilder("getByTypeName").addModifiers(javax.lang.model.element.Modifier.PUBLIC, javax.lang.model.element.Modifier.STATIC).returns(recordTypeEnumClassName).addParameter(String.class, "typeName").addCode(CodeBlock.builder().beginControlFlow("for ($T value : values())", recordTypeEnumClassName).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(recordTypeEnumClassName.packageName(), typeSpec).indent(" ").build();
jfile.writeTo(outputFolder);
}
use of com.squareup.javapoet.JavaFile in project atlasdb by palantir.
the class TableFactoryRenderer method render.
public String render() {
TypeSpec.Builder tableFactory = TypeSpec.classBuilder(getClassName()).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addAnnotation(AnnotationSpec.builder(Generated.class).addMember("value", "$S", TableFactoryRenderer.class.getName()).build());
getFields().forEach(tableFactory::addField);
getSubTypes().forEach(tableFactory::addType);
getConstructors().forEach(tableFactory::addMethod);
getMethods().forEach(tableFactory::addMethod);
JavaFile javaFile = JavaFile.builder(packageName, tableFactory.build()).indent(" ").build();
return javaFile.toString();
}
use of com.squareup.javapoet.JavaFile in project mvp4g2 by mvp4g.
the class ApplicationGenerator method generate.
public void generate(ApplicationMetaModel metaModel) throws ProcessorException {
// check if element is existing (to avoid generating code for deleted items)
if (!this.processorUtils.doesExist(metaModel.getApplication())) {
return;
}
// generate code
TypeSpec.Builder typeSpec = TypeSpec.classBuilder(metaModel.getApplication().getSimpleName() + ApplicationGenerator.IMPL_NAME).superclass(ParameterizedTypeName.get(ClassName.get(AbstractApplication.class), metaModel.getEventBus().getTypeName())).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addSuperinterface(metaModel.getApplication().getTypeName());
// constructor ...
MethodSpec constructor = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addStatement("super()").addStatement("super.eventBus = new $N.$N()", metaModel.getEventBus().getPackage(), metaModel.getEventBus().getSimpleName() + ApplicationGenerator.IMPL_NAME).addStatement("super.historyOnStart = $L", metaModel.getHistoryOnStart()).build();
typeSpec.addMethod(constructor);
// method "getApplicaitonLoader"
MethodSpec getApplicaitonLaoderMethod = MethodSpec.methodBuilder("getApplicationLoader").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).returns(IsApplicationLoader.class).addStatement("return new $T()", metaModel.getLoader().getTypeName()).build();
typeSpec.addMethod(getApplicaitonLaoderMethod);
JavaFile javaFile = JavaFile.builder(metaModel.getEventBus().getPackage(), typeSpec.build()).build();
try {
javaFile.writeTo(this.processingEnvironment.getFiler());
} catch (IOException e) {
throw new ProcessorException("Unable to write generated file: >>" + metaModel.getEventBus().getSimpleName() + ApplicationGenerator.IMPL_NAME + "<< -> exception: " + e.getMessage());
}
}
Aggregations