google / google/compile-testing

Ability to compile JavaFileObjects and access Elements/Types

Open
#65 0 comments 0 reactions 0 assignees View on GitHub
P3 type=enhancement
Dominant language
Java
Stars
722
Forks
129
Avg merge
11m
Merged PRs (30d)
4

Description

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:

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.