-
Notifications
You must be signed in to change notification settings - Fork 29
Add sample annotation processor using code models #554
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
Add sample annotation processor using code models #554
Conversation
👋 Welcome back mcimadamore! A progress list of the required criteria for merging this PR into |
@mcimadamore This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be:
You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been no new commits pushed to the ➡️ To integrate this PR with the above commit message to the |
@@ -191,7 +193,9 @@ public void clear() { | |||
} | |||
|
|||
protected ClassLoader getClassLoader(URL[] urls) { | |||
ClassLoader thisClassLoader = getClass().getClassLoader(); | |||
ClassLoader thisClassLoader = CodeReflectionSupport.CODE_LAYER != null ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to use a "base" classloader which is the same as the one used to build the jdk.incubator.code
module layer (if it exists), otherwise we won't be able to load types defined there.
@@ -1169,6 +1171,8 @@ public <S> ServiceLoader<S> getServiceLoader(Location location, Class<S> service | |||
Configuration cf = bootLayer.configuration().resolveAndBind(ModuleFinder.of(), finder, Collections.emptySet()); | |||
ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, ClassLoader.getSystemClassLoader()); | |||
return ServiceLoader.load(layer, service); | |||
} else if (CodeReflectionSupport.CODE_LAYER != null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In some cases javac uses the service loader API to load processors and plugins directly -- again, we need to make sure the jdk.incubator.code
layer is used if present.
attribBlock -> { | ||
try { | ||
return reflectMethods.getMethodBody(enclosingClass, methodTree, attribBlock, make); | ||
} catch (Throwable ex) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This avoids crashing if reflectMethods
throws (which can happen if the source code we come from contains type checking errors -- such as calling methods with wrong arity)
|
||
@SupportedAnnotationTypes("jdk.incubator.code.CodeReflection") | ||
@SupportedSourceVersion(SourceVersion.RELEASE_26) | ||
public class CodeReflectionProcessor extends AbstractProcessor { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once we agree this example is sufficient, I can add some documentation to describe what it does, and how to use it
@@ -1169,6 +1171,8 @@ public <S> ServiceLoader<S> getServiceLoader(Location location, Class<S> service | |||
Configuration cf = bootLayer.configuration().resolveAndBind(ModuleFinder.of(), finder, Collections.emptySet()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would expect something like this:
ModuleLayer augmentedModuleLayer;
ClassLoader parentCL;
if (CodeReflectionSupport.CODE_LAYER != null) {
augmentedModuleLayer = CodeReflectionSupport.CODE_LAYER;
parentCL = CodeReflectionSupport.CODE_LAYER.findLoader("jdk.incubator.code");
} else {
augmentedModuleLayer = bootLayer;
parentCL = ClassLoader.getSystemClassLoader();
}
Configuration cf = augmentedModuleLayer.configuration().resolveAndBind(ModuleFinder.of(), finder, Collections.emptySet());
ModuleLayer layer = augmentedModuleLayer.defineModulesWithOneLoader(cf, parentCL);
return ServiceLoader.load(layer, service);
here rather than the CODE_LAYER != null
test below.
// We also need to add exports all java.base packages so that the plugin can use them | ||
// But we need to do so by calling a method in java.base reflectively | ||
try { | ||
Class<?> codeModuleLayerInit = Class.forName("jdk.internal.access.code.CodeModuleLayerInit"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the babylon module is not in the module graph when running javac, then we need to explicitly export all java.base packages to that module, to make sure it runs correctly (this might be too broad).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note - we have to call through reflection, otherwise javac might fail to compile against a bootstrap JDK
@mcimadamore this pull request can not be integrated into git checkout codemodel_anno_processor
git fetch https://git.openjdk.org/babylon.git code-reflection
git merge FETCH_HEAD
# resolve conflicts and follow the instructions given by git merge
git commit -m "Merge code-reflection"
git push |
|
58e493e
to
2e5767f
Compare
@mcimadamore Please do not rebase or force-push to an active PR as it invalidates existing review comments. Note for future reference, the bots always squash all changes into a single commit automatically as part of the integration. See OpenJDK Developers’ Guide for more information. |
/integrate |
Going to push as commit c163836. |
@mcimadamore Pushed as commit c163836. 💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored. |
This change adds a new sample of an annotation processor that uses code models to:
System.gc
,System.exit
)try
,throw
)The annotation processor is somewhat configurable, and can be used by developers as an initial sketch upon which to build more custom compile-time checks.
When using the annotation processor to compile this:
The following error messages are generated:
When putting this together, I noted some issues, reported below:
jdk.incubator.code
modulejdk.incubator.code
Messager
API does not allow for thisMany thanks to @lahodaj for the invaluable help when fighting with module layers :-)
Progress
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/babylon.git pull/554/head:pull/554
$ git checkout pull/554
Update a local copy of the PR:
$ git checkout pull/554
$ git pull https://git.openjdk.org/babylon.git pull/554/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 554
View PR using the GUI difftool:
$ git pr show -t 554
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/babylon/pull/554.diff
Using Webrev
Link to Webrev Comment