Search in sources :

Example 51 with JavaFile

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);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Node(com.github.javaparser.ast.Node) JavaFile(com.squareup.javapoet.JavaFile) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 52 with JavaFile

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;
}
Also used : CodeGenerationIncompleteException(dagger.internal.codegen.Util.CodeGenerationIncompleteException) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) JavaFile(com.squareup.javapoet.JavaFile) Util.typeToString(dagger.internal.codegen.Util.typeToString) Util.elementToString(dagger.internal.codegen.Util.elementToString) IOException(java.io.IOException)

Example 53 with JavaFile

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());
}
Also used : TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ClassName(com.squareup.javapoet.ClassName) JavaFile(com.squareup.javapoet.JavaFile) Util.rawTypeToString(dagger.internal.codegen.Util.rawTypeToString) Util.elementToString(dagger.internal.codegen.Util.elementToString) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 54 with JavaFile

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();
}
Also used : ApiService(com.emc.apidocs.model.ApiService) ApiMethod(com.emc.apidocs.model.ApiMethod) JavaFile(com.squareup.javapoet.JavaFile) ApiReferenceTocOrganizer(com.emc.apidocs.generating.ApiReferenceTocOrganizer) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 55 with JavaFile

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());
        }
    }
}
Also used : HistoryMetaModel(com.github.mvp4g.mvp4g2.processor.model.HistoryMetaModel) ProcessorException(com.github.mvp4g.mvp4g2.processor.ProcessorException) MethodSpec(com.squareup.javapoet.MethodSpec) JavaFile(com.squareup.javapoet.JavaFile) IOException(java.io.IOException) History(com.github.mvp4g.mvp4g2.core.history.annotation.History) 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