Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to compile JavaFileObjects and access Elements/Types #65

Open
JakeWharton opened this issue Feb 16, 2015 · 0 comments
Open

Ability to compile JavaFileObjects and access Elements/Types #65

JakeWharton opened this issue Feb 16, 2015 · 0 comments
Labels
P3 type=enhancement Make an existing feature better

Comments

@JakeWharton
Copy link
Contributor

I have a need to access the mirror-based instances of elements and types in order to unit test code which deals with them after compiling supplied JavaFileObject instances.

Currently this library only assumes I either want "empty" Elements or Types instance from the rule or that I want to assert about the compilation of JavaFileObjects (optionally processed through some processor). This would be an API that sat between the two allowing for the same compilation of JavaFileObjects but then providing me access to the Elements and Types instances from that compilation.

For this I was thinking of a Compilation class which facilitated building what was to be compiled and any annotation processors to run. Calling .compile produced a Compilation.Result containing methods for access to the exit code, Elements, Types, and generated files (this would solve #58).

Additionally, another subject factory would be added for asserting about a Compilation.Result. Ideally supporting this use-case would simply mean putting a cleaner API around what's already happening internally and then refactoring the current assertion-based usage to use this new API directly.

A contrived example:

String input = ""
    + "package example;\n"
    + "public final class Example {}";
String inputFile = JavaFileObjects.forSourceString("example.Example", input);

Compilation.Result result = Compilation.builder()
    .addFileObject(inputFile)
    //.addProcessor(..)
    .compile();

Elements elements = result.elements();
TypeElement example = elements.getTypeElement("example.Example");
assertThat(SomeUtility.validateSomething(example)).isTrue();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 type=enhancement Make an existing feature better
Projects
None yet
Development

No branches or pull requests

3 participants