jOOQ / jOOR

jOOR - Fluent Reflection in Java jOOR is a very simple fluent API that gives access to your Java Class structures in a more intuitive way. The JDK's reflection APIs are hard and verbose to use. Other languages have much simpler constructs to access type meta information at runtime. Let us make Java reflection better.
http://www.jooq.org/products
Apache License 2.0
2.8k stars 377 forks source link

Is there anyway to set JOOR to create a test directory under \target ? #87

Closed cvgaviao closed 4 years ago

cvgaviao commented 4 years ago

I'm trying to create an annotation processor class and started to use JOOR to test it.

    @Test
    public void ensureAnnotationProcessorCreatesResourceBundleClassAndPropertiesWithMultipleLocales() throws Exception {
        MessageBundleGeneratorAnnotationProcessor processor = new MessageBundleGeneratorAnnotationProcessor();
        Reflect.compile(
                "br.com.c8tech.javalib.apt.i18n.SourceResourceBundleWithTwoMethods",
                "package br.com.c8tech.javalib.apt.i18n; "
                        + "@MessageBundle "
                        + "public interface SourceResourceBundleWithTwoMethods {"
                        + "@Message(value=\" worked {0} ! \", locale=\"pt-BR\" )"
                        + " public String m1(String pZero);" 
                        + "@Message(\" worked {0} {1}! \")"
                        + " public String m2(String pZero, String pOne);" 
                        + "}",
                        new CompileOptions().options("-source", "8")
                        .processors(processor))
        .type();
        assertTrue(processor.isProcessed());
    }

In the class under test I'm creating a resource file:

FileObject f = processingEnv.getFiler().createResource(
                    StandardLocation.SOURCE_OUTPUT, "", resourceName);

I'm running the test inside Eclipse IDE and I was expecting that the resulting files was being created inside a specific test directory under project's \target directory, but it is being created inside my annotation processor project's directory instead.

Am I missing something ?

lukaseder commented 4 years ago

Not sure what could be missing here. Would you mind creating a complete test case so we can reproduce it?

cvgaviao commented 4 years ago

Not sure what could be missing here. Would you mind creating a complete test case so we can reproduce it?

Hi @lukaseder, thanks for your time.

I've pushed an initial code of my annotation processor project to github: https://github.com/cvgaviao/c8tech-jlib-i18n/blob/master/c8tech-jlib-i18n-apt/src/test/java/br/com/c8tech/jlib/i18n/apt/MessageBundleAnnotationProcessorTest.java

Note that when running this test the resulting artifacts are being created inside the c8tech-jlib-i18n-apt/ directory.

lukaseder commented 4 years ago

I see, so the processor is here: https://github.com/cvgaviao/c8tech-jlib-i18n/blob/c9b1b11ddd74f767be2b1f72f04fa7b62ae8c92b/c8tech-jlib-i18n-apt/src/main/java/br/com/c8tech/jlib/i18n/apt/MessageBundleGeneratorAnnotationProcessor.java, for the record.

Have you tried specifying the -s option in your CompileOptions?

cvgaviao commented 4 years ago

Have you tried specifying the -s option in your CompileOptions?

Wow, this is it !!! I've passed too many years running tests with maven/eclipse and I never noted that they provide that -s to the compiler :( .

As I'm using Junit5, I've created a simple extension that creates and injects the method test's directory into a Path method parameter. So the developer just need to use it to set the -soption.

I'll will commit and post the link here soon. Perhaps others need it too.

thanks !

cvgaviao commented 4 years ago

@lukaseder, one more question:

Do I need to set any parameter in order to see the messages provided to processingEnv.getMessager().printMessage() method?

cvgaviao commented 4 years ago

Have you tried specifying the -s option in your CompileOptions?

Well, actually the use of -s do not resolved my problem. It was partially resolved by using -d.

When generating resource files using processingEnv.getFiler().createResource. The resource are correctly created in the directory passed to -doption.

But unfortunately, neither -s or -d is working when generating source files using processingEnv.getFiler().createSourceFile.

Somehow the calculated directory being used is something like this: org.joor.Compile$JavaFileObject[string:///br/com/c8tech/jlib/i18n/apt/SourceResourceBundleWithTwoMethodsAndMultipleLocalesImpl.java]

lukaseder commented 4 years ago

Do I need to set any parameter in order to see the messages provided to processingEnv.getMessager().printMessage() method?

I guess that's currently not possible yet. The output appears in the exception in case of a compilation error, but not in case of the happy path. I've created a feature request for this: https://github.com/jOOQ/jOOR/issues/88