Search in sources :

Example 91 with JavaFileObject

use of javax.tools.JavaFileObject in project storio by pushtorefresh.

the class StorIOSQLiteAnnotationsProcessorTest method shouldCompileWithMethodsReturningBoxedTypesAndMarkedAsIgnoreNullAndFactoryMethodAsCreator.

@Test
public void shouldCompileWithMethodsReturningBoxedTypesAndMarkedAsIgnoreNullAndFactoryMethodAsCreator() {
    JavaFileObject model = JavaFileObjects.forResource("BoxedTypesMethodsFactoryMethodIgnoreNull.java");
    JavaFileObject generatedTypeMapping = JavaFileObjects.forResource("BoxedTypesMethodsFactoryMethodIgnoreNullSQLiteTypeMapping.java");
    JavaFileObject generatedDeleteResolver = JavaFileObjects.forResource("BoxedTypesMethodsFactoryMethodIgnoreNullStorIOSQLiteDeleteResolver.java");
    JavaFileObject generatedGetResolver = JavaFileObjects.forResource("BoxedTypesMethodsFactoryMethodIgnoreNullStorIOSQLiteGetResolver.java");
    JavaFileObject generatedPutResolver = JavaFileObjects.forResource("BoxedTypesMethodsFactoryMethodIgnoreNullStorIOSQLitePutResolver.java");
    assert_().about(javaSource()).that(model).processedWith(new StorIOSQLiteProcessor()).compilesWithoutError().and().generatesSources(generatedTypeMapping, generatedDeleteResolver, generatedGetResolver, generatedPutResolver);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) StorIOSQLiteProcessor(com.pushtorefresh.storio.sqlite.annotations.processor.StorIOSQLiteProcessor) Test(org.junit.Test)

Example 92 with JavaFileObject

use of javax.tools.JavaFileObject in project storio by pushtorefresh.

the class StorIOSQLiteAnnotationsProcessorTest method shouldNotCompileIfCreatorInsideNotAnnotatedClass.

@Test
public void shouldNotCompileIfCreatorInsideNotAnnotatedClass() {
    JavaFileObject model = JavaFileObjects.forResource("CreatorInsideNotAnnotatedClass.java");
    assert_().about(javaSource()).that(model).processedWith(new StorIOSQLiteProcessor()).failsToCompile().withErrorContaining("Please annotate class CreatorInsideNotAnnotatedClass with StorIOSQLiteType");
}
Also used : JavaFileObject(javax.tools.JavaFileObject) StorIOSQLiteProcessor(com.pushtorefresh.storio.sqlite.annotations.processor.StorIOSQLiteProcessor) Test(org.junit.Test)

Example 93 with JavaFileObject

use of javax.tools.JavaFileObject in project querydsl by querydsl.

the class AbstractQuerydslProcessor method serialize.

private void serialize(Serializer serializer, Collection<EntityType> models) {
    for (EntityType model : models) {
        try {
            Type type = conf.getTypeMappings().getPathType(model, model, true);
            String packageName = type.getPackageName();
            String className = !packageName.isEmpty() ? (packageName + "." + type.getSimpleName()) : type.getSimpleName();
            // skip if type is excluded class or in excluded package
            if (conf.isExcludedPackage(model.getPackageName()) || conf.isExcludedClass(model.getFullName())) {
                continue;
            }
            Set<TypeElement> elements = context.typeElements.get(model.getFullName());
            if (elements == null) {
                elements = new HashSet<TypeElement>();
            }
            for (Property property : model.getProperties()) {
                if (property.getType().getCategory() == TypeCategory.CUSTOM) {
                    Set<TypeElement> customElements = context.typeElements.get(property.getType().getFullName());
                    if (customElements != null) {
                        elements.addAll(customElements);
                    }
                }
            }
            processingEnv.getMessager().printMessage(Kind.NOTE, "Generating " + className + " for " + elements);
            JavaFileObject fileObject = processingEnv.getFiler().createSourceFile(className, elements.toArray(new Element[elements.size()]));
            Writer writer = fileObject.openWriter();
            try {
                SerializerConfig serializerConfig = conf.getSerializerConfig(model);
                serializer.serialize(model, serializerConfig, new JavaWriter(writer));
            } finally {
                if (writer != null) {
                    writer.close();
                }
            }
        } catch (IOException e) {
            System.err.println(e.getMessage());
            processingEnv.getMessager().printMessage(Kind.ERROR, e.getMessage());
        }
    }
}
Also used : JavaWriter(com.mysema.codegen.JavaWriter) IOException(java.io.IOException) JavaFileObject(javax.tools.JavaFileObject) NoType(javax.lang.model.type.NoType) Type(com.mysema.codegen.model.Type) DeclaredType(javax.lang.model.type.DeclaredType) Writer(java.io.Writer) JavaWriter(com.mysema.codegen.JavaWriter)

Example 94 with JavaFileObject

use of javax.tools.JavaFileObject in project realm-java by realm.

the class RealmProxyInterfaceGenerator method generate.

public void generate() throws IOException {
    String qualifiedGeneratedInterfaceName = String.format("%s.%s", Constants.REALM_PACKAGE_NAME, Utils.getProxyInterfaceName(className));
    JavaFileObject sourceFile = processingEnvironment.getFiler().createSourceFile(qualifiedGeneratedInterfaceName);
    JavaWriter writer = new JavaWriter(new BufferedWriter(sourceFile.openWriter()));
    writer.setIndent(Constants.INDENT);
    writer.emitPackage(Constants.REALM_PACKAGE_NAME).emitEmptyLine().beginType(qualifiedGeneratedInterfaceName, "interface", EnumSet.of(Modifier.PUBLIC));
    for (VariableElement field : metaData.getFields()) {
        // The field is neither static nor ignored
        if (!field.getModifiers().contains(Modifier.STATIC) && field.getAnnotation(Ignore.class) == null) {
            String fieldName = field.getSimpleName().toString();
            String fieldTypeCanonicalName = field.asType().toString();
            writer.beginMethod(fieldTypeCanonicalName, metaData.getInternalGetter(fieldName), EnumSet.of(Modifier.PUBLIC)).endMethod().beginMethod("void", metaData.getInternalSetter(fieldName), EnumSet.of(Modifier.PUBLIC), fieldTypeCanonicalName, "value").endMethod();
        }
    }
    // backlinks are final and have only a getter.
    for (Backlink backlink : metaData.getBacklinkFields()) {
        writer.beginMethod(backlink.getTargetFieldType(), metaData.getInternalGetter(backlink.getTargetField()), EnumSet.of(Modifier.PUBLIC)).endMethod();
    }
    writer.endType();
    writer.close();
}
Also used : JavaFileObject(javax.tools.JavaFileObject) JavaWriter(com.squareup.javawriter.JavaWriter) VariableElement(javax.lang.model.element.VariableElement) BufferedWriter(java.io.BufferedWriter)

Example 95 with JavaFileObject

use of javax.tools.JavaFileObject in project lombok by rzwitserloot.

the class TestClassFileMetaData method compile.

static byte[] compile(File file) {
    try {
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        File tempDir = getTempDir();
        tempDir.mkdirs();
        List<String> options = Arrays.asList("-proc:none", "-d", tempDir.getAbsolutePath());
        StringWriter captureWarnings = new StringWriter();
        final StringBuilder compilerErrors = new StringBuilder();
        DiagnosticListener<JavaFileObject> diagnostics = new DiagnosticListener<JavaFileObject>() {

            @Override
            public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
                compilerErrors.append(diagnostic.toString()).append("\n");
            }
        };
        CompilationTask task = compiler.getTask(captureWarnings, null, diagnostics, options, null, Collections.singleton(new ContentBasedJavaFileObject(file.getPath(), readFileAsString(file))));
        Boolean taskResult = task.call();
        assertTrue("Compilation task didn't succeed: \n<Warnings and Errors>\n" + compilerErrors.toString() + "\n</Warnings and Errors>", taskResult);
        return PostCompilerApp.readFile(new File(tempDir, file.getName().replaceAll("\\.java$", ".class")));
    } catch (Exception e) {
        throw Lombok.sneakyThrow(e);
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) Diagnostic(javax.tools.Diagnostic) DiagnosticListener(javax.tools.DiagnosticListener) CompilationTask(javax.tools.JavaCompiler.CompilationTask) IOException(java.io.IOException) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) StringWriter(java.io.StringWriter) File(java.io.File)

Aggregations

JavaFileObject (javax.tools.JavaFileObject)663 Test (org.junit.Test)473 ButterKnifeProcessor (butterknife.compiler.ButterKnifeProcessor)121 IOException (java.io.IOException)52 JavaCompiler (javax.tools.JavaCompiler)40 StorIOContentResolverProcessor (com.pushtorefresh.storio.contentresolver.annotations.processor.StorIOContentResolverProcessor)38 StorIOSQLiteProcessor (com.pushtorefresh.storio.sqlite.annotations.processor.StorIOSQLiteProcessor)35 File (java.io.File)32 Diagnostic (javax.tools.Diagnostic)30 DiagnosticCollector (javax.tools.DiagnosticCollector)28 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)25 StandardJavaFileManager (javax.tools.StandardJavaFileManager)25 ArrayList (java.util.ArrayList)24 TypeElement (javax.lang.model.element.TypeElement)21 Result (com.sun.tools.javac.main.Main.Result)20 Writer (java.io.Writer)17 PrintWriter (java.io.PrintWriter)16 CompilationTask (javax.tools.JavaCompiler.CompilationTask)14 JCTree (com.sun.tools.javac.tree.JCTree)10 StringWriter (java.io.StringWriter)10