Java Code Examples for com.squareup.javapoet.JavaFile#Builder
The following examples show how to use
com.squareup.javapoet.JavaFile#Builder .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaGeneratorTest.java From raptor with Apache License 2.0 | 5 votes |
private void writeJavaFile(ClassName javaTypeName, TypeSpec typeSpec, Location location) throws IOException { JavaFile.Builder builder = JavaFile.builder(javaTypeName.packageName(), typeSpec) .addFileComment("$L", "Code generated by Wire protocol buffer compiler, do not edit."); if (location != null) { builder.addFileComment("\nSource file: $L", location); } JavaFile javaFile = builder.build(); try { javaFile.writeTo(new File(GENERATED_SOURCE_DIR)); } catch (IOException e) { throw new IOException("Failed to write " + javaFile.packageName + "." + javaFile.typeSpec.name + " to " + GENERATED_SOURCE_DIR, e); } }
Example 2
Source File: Proto2Java.java From raptor with Apache License 2.0 | 5 votes |
private void writeJavaFile(ClassName javaTypeName, TypeSpec typeSpec, Location location) throws IOException { JavaFile.Builder builder = JavaFile.builder(javaTypeName.packageName(), typeSpec) .addFileComment("$L", "Code generated by Wire protocol buffer compiler, do not edit."); if (location != null) { builder.addFileComment("\nSource file: $L", location); } JavaFile javaFile = builder.build(); try { javaFile.writeTo(new File(generatedSourceDirectory)); } catch (IOException e) { throw new IOException("Failed to write " + javaFile.packageName + "." + javaFile.typeSpec.name + " to " + generatedSourceDirectory, e); } }
Example 3
Source File: CodeGenerator.java From Barricade with Apache License 2.0 | 5 votes |
/** * Generates the code for the Barricade configuration based on the annotations found. * * @param processingEnv Processing environment * @param configs Configuration detected by annotation processing * @param messager Messager to print logs * @throws IOException */ static void generateClass(ProcessingEnvironment processingEnv, HashMap<String, BarricadeResponseSet> configs, Messager messager) throws IOException { messager.printMessage(Diagnostic.Kind.NOTE, "Generating configuration code..."); TypeSpec.Builder classBuilder = classBuilder(CLASS_NAME).addModifiers(PUBLIC, FINAL); FieldSpec valuesField = FieldSpec.builder(TYPE_CONFIG, "configs").addModifiers(PRIVATE).build(); FieldSpec instanceField = FieldSpec.builder(ClassName.get(PACKAGE_NAME, CLASS_NAME), "barricadeConfig") .addModifiers(PRIVATE, STATIC) .build(); MethodSpec.Builder instanceMethodBuilder = generateGetInstanceMethodBuilder(); MethodSpec.Builder constructorMethodBuilder = generateConstructorBuilder(configs, messager); MethodSpec.Builder valuesMethod = generateGetConfigsMethodBuilder(); MethodSpec.Builder getResponseMethodBuilder = generateGetResponseMethodBuilder(); classBuilder.addType(generateEndpointsInnerClass(configs.keySet())); classBuilder.addType(generateResponsesInnerClass(configs)); classBuilder.addField(instanceField); classBuilder.addField(valuesField); classBuilder.addMethod(instanceMethodBuilder.build()); classBuilder.addMethod(constructorMethodBuilder.build()); classBuilder.addMethod(valuesMethod.build()); classBuilder.addMethod(getResponseMethodBuilder.build()); classBuilder.addSuperinterface(IBarricadeConfig.class); JavaFile.Builder javaFileBuilder = builder(PACKAGE_NAME, classBuilder.build()); JavaFile javaFile = javaFileBuilder.build(); javaFile.writeTo(processingEnv.getFiler()); messager.printMessage(Diagnostic.Kind.NOTE, "Code generation complete!"); }
Example 4
Source File: CasesGenerator.java From motif with Apache License 2.0 | 5 votes |
/** * Generates the matching cases class and returns it as a {@link JavaFile}. */ public JavaFile generate() { MethodSpec privateConstructor = MethodSpec.constructorBuilder() .addModifiers(Modifier.PRIVATE) .build(); TypeSpec.Builder casesClassBuilder = TypeSpec.classBuilder(className) .addModifiers(Modifier.PUBLIC, Modifier.FINAL) .addMethod(privateConstructor) .addMethods(methods); for (String javadoc : javadocs) { casesClassBuilder.addJavadoc(javadoc); } TypeSpec casesClass = casesClassBuilder.build(); JavaFile.Builder javaFileBuilder = JavaFile.builder(packageName, casesClass); if (fileComment != null) { javaFileBuilder.addFileComment(fileComment); } javaFileBuilder.addFileComment("\nGenerated by Motif. Do Not Edit!\n"); JavaFile javaFile = javaFileBuilder.build(); return javaFile; }
Example 5
Source File: DataEnumProcessor.java From dataenum with Apache License 2.0 | 4 votes |
@Override public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) { Filer filer = processingEnv.getFiler(); Messager messager = processingEnv.getMessager(); AccessSelector accessSelector = new AccessSelector(roundEnvironment.getElementsAnnotatedWith(ConstructorAccess.class)); for (Element element : roundEnvironment.getElementsAnnotatedWith(DataEnum.class)) { try { Spec spec = SpecParser.parse(element, processingEnv); if (spec == null) { continue; } OutputSpec outputSpec = OutputSpecFactory.create(spec); TypeSpec outputTypeSpec = SpecTypeFactory.create( outputSpec, accessSelector.accessModifierFor(outputSpec.outputClass().packageName()), element); JavaFile.Builder javaFileBuilder = JavaFile.builder(outputSpec.outputClass().packageName(), outputTypeSpec); if (needsCheckNotNull(spec)) { javaFileBuilder.addStaticImport(DataenumUtils.class, "checkNotNull"); } if (needsNullSafeEquals(spec)) { javaFileBuilder.addStaticImport(DataenumUtils.class, "equal"); } JavaFile javaFile = javaFileBuilder.build(); javaFile.writeTo(filer); } catch (IOException | ParserException e) { messager.printMessage(Diagnostic.Kind.ERROR, e.getMessage()); } } return false; }
Example 6
Source File: PoetUtils.java From aws-sdk-java-v2 with Apache License 2.0 | 4 votes |
public static JavaFile buildJavaFile(ClassSpec spec) { JavaFile.Builder builder = JavaFile.builder(spec.className().packageName(), spec.poetSpec()).skipJavaLangImports(true); spec.staticImports().forEach(i -> i.memberNames().forEach(m -> builder.addStaticImport(i.className(), m))); return builder.build(); }