Search in sources :

Example 6 with JavaFile

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;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) JavaFile(com.squareup.javapoet.JavaFile) IOException(java.io.IOException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 7 with JavaFile

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();
    }
}
Also used : JavaFileObject(javax.tools.JavaFileObject) JavaFile(com.squareup.javapoet.JavaFile) IOException(java.io.IOException) Writer(java.io.Writer)

Example 8 with JavaFile

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);
}
Also used : SearchRecordTypeDesc(org.talend.components.netsuite.client.model.SearchRecordTypeDesc) RecordTypeDesc(org.talend.components.netsuite.client.model.RecordTypeDesc) ArrayList(java.util.ArrayList) JavaFile(com.squareup.javapoet.JavaFile) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 9 with JavaFile

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();
}
Also used : Generated(javax.annotation.Generated) JavaFile(com.squareup.javapoet.JavaFile) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 10 with JavaFile

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