Skip to content

Ability to compile JavaFileObjects and access Elements/Types #65

Open
@JakeWharton

Description

@JakeWharton

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();

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions