use of com.squareup.javapoet.JavaFile in project butterknife by JakeWharton.
the class FinalRClassBuilder method brewJava.
public static void brewJava(File rFile, File outputDir, String packageName, String className) throws Exception {
CompilationUnit compilationUnit = JavaParser.parse(rFile);
TypeDeclaration resourceClass = compilationUnit.getTypes().get(0);
TypeSpec.Builder result = TypeSpec.classBuilder(className).addModifiers(PUBLIC).addModifiers(FINAL);
for (Node node : resourceClass.getChildNodes()) {
if (node instanceof ClassOrInterfaceDeclaration) {
addResourceType(Arrays.asList(SUPPORTED_TYPES), result, (ClassOrInterfaceDeclaration) node);
}
}
JavaFile finalR = JavaFile.builder(packageName, result.build()).addFileComment("Generated code from Butter Knife gradle plugin. Do not modify!").build();
finalR.writeTo(outputDir);
}
use of com.squareup.javapoet.JavaFile in project dagger by square.
the class ModuleAdapterProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> types, RoundEnvironment env) {
remainingTypes.putAll(providerMethodsByClass(env));
for (Iterator<String> i = remainingTypes.keySet().iterator(); i.hasNext(); ) {
String typeName = i.next();
TypeElement type = processingEnv.getElementUtils().getTypeElement(typeName);
List<ExecutableElement> providesTypes = remainingTypes.get(typeName);
try {
// Attempt to get the annotation. If types are missing, this will throw
// CodeGenerationIncompleteException.
Map<String, Object> parsedAnnotation = getAnnotation(Module.class, type);
if (parsedAnnotation == null) {
error(type + " has @Provides methods but no @Module annotation", type);
continue;
}
JavaFile javaFile = generateModuleAdapter(type, parsedAnnotation, providesTypes);
javaFile.writeTo(processingEnv.getFiler());
} catch (CodeGenerationIncompleteException e) {
// A dependent type was not defined, we'll try to catch it on another pass.
continue;
} catch (IOException e) {
error("Code gen failed: " + e, type);
}
i.remove();
}
if (env.processingOver() && remainingTypes.size() > 0) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Could not find types required by provides methods for " + remainingTypes.keySet());
}
// FullGraphProcessor needs an opportunity to process.
return false;
}
use of com.squareup.javapoet.JavaFile in project dagger by square.
the class InjectAdapterProcessor method generateStaticInjection.
/**
* Write a companion class for {@code type} that extends {@link StaticInjection}.
*/
private void generateStaticInjection(TypeElement type, List<Element> fields) throws IOException {
ClassName typeName = ClassName.get(type);
ClassName adapterClassName = adapterName(ClassName.get(type), STATIC_INJECTION_SUFFIX);
TypeSpec.Builder result = TypeSpec.classBuilder(adapterClassName.simpleName()).addOriginatingElement(type).addJavadoc(AdapterJavadocs.STATIC_INJECTION_TYPE, type).addModifiers(PUBLIC, FINAL).superclass(StaticInjection.class);
for (Element field : fields) {
result.addField(memberBindingField(false, field));
}
result.addMethod(attachMethod(null, fields, false, typeName, null, true));
result.addMethod(staticInjectMethod(fields, typeName));
String packageName = getPackage(type).getQualifiedName().toString();
JavaFile javaFile = JavaFile.builder(packageName, result.build()).addFileComment(AdapterJavadocs.GENERATED_BY_DAGGER).build();
javaFile.writeTo(processingEnv.getFiler());
}
use of com.squareup.javapoet.JavaFile in project coprhd-controller by CoprHD.
the class ApiPrimitiveMaker method makePrimitives.
/**
* Accept a list of ApiServices and make primitives for each HTTP method in
* the service
*
* @param services
* - a list of services
*
* @return a JavaFile representing java source code for the primitive
*/
public static Iterable<JavaFile> makePrimitives(final List<ApiService> services) {
final Builder<JavaFile> builder = ImmutableList.<JavaFile>builder();
final ApiReferenceTocOrganizer grouping = new ApiReferenceTocOrganizer(KnownPaths.getReferenceFile("ApiReferenceGrouping.txt"));
final Map<String, List<ApiService>> groups = grouping.organizeServices(services);
for (final Entry<String, List<ApiService>> groupEntry : groups.entrySet()) {
for (final ApiService service : groupEntry.getValue()) {
for (final ApiMethod method : service.methods) {
if (blackListed(method)) {
_log.info("Method " + method.apiService.getFqJavaClassName() + "::" + method.javaMethodName + " is black listed");
} else if (method.isDeprecated) {
_log.info("Method " + method.apiService.getFqJavaClassName() + "::" + method.javaMethodName + " is deprecated");
} else {
builder.add(makePrimitive(groupEntry.getKey(), method));
}
}
}
}
return builder.build();
}
use of com.squareup.javapoet.JavaFile in project mvp4g2 by mvp4g.
the class HistoryGenerator method generate.
public void generate(HistoryMetaModel metaModel) throws ProcessorException {
for (String historyConverterClassName : metaModel.getHistoryConverterClassNames()) {
HistoryMetaModel.HistoryData historyData = metaModel.getHistoryData(historyConverterClassName);
TypeSpec.Builder typeSpec = TypeSpec.classBuilder(this.processorUtils.createHistoryMetaDataClassName(historyData.getHistoryConverter().getClassName())).superclass(ClassName.get(HistoryMetaData.class)).addModifiers(Modifier.PUBLIC, Modifier.FINAL);
// constructor ...
MethodSpec constructor = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addStatement("super($S, $T.$L)", historyConverterClassName, ClassName.get(History.HistoryConverterType.class), historyData.getHistoryConverterType()).build();
typeSpec.addMethod(constructor);
JavaFile javaFile = JavaFile.builder(historyData.getHistoryConverter().getPackage(), typeSpec.build()).build();
try {
javaFile.writeTo(this.processingEnvironment.getFiler());
} catch (IOException e) {
throw new ProcessorException("Unable to write generated file: >>" + this.processorUtils.createHistoryMetaDataClassName(historyData.getHistoryConverter().getClassName()) + "<< -> exception: " + e.getMessage());
}
}
}
Aggregations