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

Add support for java 9 #7

Merged
merged 10 commits into from May 14, 2022
Merged

Add support for java 9 #7

merged 10 commits into from May 14, 2022

Conversation

Sineaggi
Copy link
Contributor

@Sineaggi Sineaggi commented May 14, 2022

This adds support for the new javax.annotation.processing.Generated annotation under java 9. Under java 8 (and below) it will fall back to using the old annotation.

Fixes #6

@skinny85
Copy link
Owner

Super clever, thanks for the contribution @Sineaggi!

Only one comment - any chance we can switch the GitHub Actions workflow to use matrix builds for the JDK version? Right now, we're only building for Java 1.8. Can we maybe make it build for 8, 9 and 17 (all of the stable ones)? That should allow us to exercise both paths of the change.

The workflow file is here.

Let me know if this makes sense!

@Sineaggi
Copy link
Contributor Author

So off the top of my head we should be able to add support for 9 relatively easily, as 9 has been supported since gradle 4.3. Unfortunately for newer versions of java like 11 and 17, we'd either need to use java toolchains which were added in gradle 6.7 or upgrade all the way to gradle 7 (which removed the maven plugin in favor of the maven-publish plugin).

@skinny85
Copy link
Owner

So off the top of my head we should be able to add support for 9 relatively easily, as 9 has been supported since gradle 4.3. Unfortunately for newer versions of java like 11 and 17, we'd either need to use java toolchains which were added in gradle 6.7 or upgrade all the way to gradle 7 (which removed the maven plugin in favor of the maven-publish plugin).

Yeah, asking you to upgrade Gradle would be taking advantage of your good nature, I'm pretty sure 😉. So let's only add Java 9 for now.

@Sineaggi
Copy link
Contributor Author

@skinny85 I've got a first pass of a partial upgrade to gradle 6.x, not sure if the test matrix is correct.

The testing is a bit ugly since the import of javax.annotation.Generated requires the compiler for the main classes to be 8. Then we can set the test classes compiler/launcher to be the parameterized java version (since we need to validate that the annotation processor 1 runs under newer versions of java and 2 the tests pass under newer versions of java).

@Sineaggi
Copy link
Contributor Author

Sineaggi commented May 14, 2022

Ugh just hit junit-team/junit4#1717, disabling 17 for now

- name: Build and test the project
env:
JAVA_VERSION: ${{ matrix.version }}
run: ./gradlew check javadoc
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason we're introducing a new job here, instead of changing the existing one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None at all, will collapse them.

steps:
- uses: actions/checkout@v2

- name: Set up JDK 1.8
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this name needs to be updated.

- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to use ${{ matrix.version }} here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, but gradle plays catch-up with every new version of java. By using the matrix version of java with the toolchain we can use unsupported versions of gradle (like 17 for example) in gradle 6 for compilation/testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind either way, but it does mean testing on java 17 would require gradle 7.

Which shouldn't be too hard, considering we're now on gradle 6.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with skipping Java 17 for now.

build.gradle Show resolved Hide resolved
@skinny85
Copy link
Owner

The testing is a bit ugly since the import of javax.annotation.Generated requires the compiler for the main classes to be 8. Then we can set the test classes compiler/launcher to be the parameterized java version (since we need to validate that the annotation processor 1 runs under newer versions of java and 2 the tests pass under newer versions of java).

I'd be fine with changing to a Class.forName() for javax.annotation.Generated too, if it makes this easier.

@Sineaggi
Copy link
Contributor Author

I'd be fine with changing to a Class.forName() for javax.annotation.Generated too, if it makes this easier.

That would make compiling under newer versions of java easier. We'd also be able to align main and test source sets, which would then make it easier to just use ${{ matrix.version }} for java-version.

@Sineaggi
Copy link
Contributor Author

LMK what you'd prefer, I think the current way of using javaCompiler/javaLauncher ended up being pretty obtuse.

@skinny85
Copy link
Owner

LMK what you'd prefer, I think the current way of using javaCompiler/javaLauncher ended up being pretty obtuse.

I agree. Let's simplify by switching to Class.forName().

@Sineaggi
Copy link
Contributor Author

@skinny85 I've switched to using Class.forName for all java versions. Simplified the build.gradle and the test matrix. The build process should be clearer now.

Copy link
Owner

@skinny85 skinny85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@skinny85 I've switched to using Class.forName for all java versions. Simplified the build.gradle and the test matrix. The build process should be clearer now.

Thanks for the contribution - it looks fantastic now!

I just have a few last small finishing comments, if you'd be so kind 🙂.

@@ -5,17 +5,16 @@ on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
env:
# disable the demon, as it's causing some problems
GRADLE_OPTS: -Dorg.gradle.daemon=false
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to remove this?

Not sure how effective it is to use the daemon in CI.

Copy link
Contributor Author

@Sineaggi Sineaggi May 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe in modern gradle the daemon spawns regardless, in ci it will shut down at the end of the build.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm 99% sure that system property disables the daemon, but it's not a big deal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I was testing locally at least, these logs were printed.

To honour the JVM settings for this build a single-use Daemon process will be forked. See https://docs.gradle.org/6.9.2/userguide/gradle_daemon.html#sec:disabling_the_daemon.
Daemon will be stopped at the end of the build

I can add the flag back in.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, it's not a big deal. Let's just leave it as-is. Thanks!

build.gradle Show resolved Hide resolved
build.gradle Outdated
testCompile group: 'org.assertj', name: 'assertj-core', version: '3.6.2'
testAnnotationProcessor(getProject())
testAnnotationProcessor('com.squareup:javapoet:1.8.0')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this dependency for the third time? Would switching it from either compileOnly or testCompileOnly to compile / testCompile above would allow us to remove this declaration?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or can we at least remove the second instance of it, above (the testCompileOnly 'com.squareup:javapoet:1.8.0' one)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The testCompileOnly is no longer necessary, but since the annotation processor hasn't been repackaged by the shadow plugin yet, we'll need to keep the testAnnotationProcessor dependencies.

build.gradle Outdated Show resolved Hide resolved
build.gradle Outdated
dependencies {
compileOnly 'com.squareup:javapoet:1.8.0'

testCompileOnly 'com.squareup:javapoet:1.8.0'
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile group: 'junit', name: 'junit', version: '4.13.2'
testCompile group: 'org.assertj', name: 'assertj-core', version: '3.6.2'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here:

Suggested change
testCompile group: 'org.assertj', name: 'assertj-core', version: '3.6.2'
testCompile 'org.assertj:assertj-core:3.6.2'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

build.gradle Outdated Show resolved Hide resolved
}
}
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love these simplifications, thanks!

@@ -213,8 +212,20 @@ protected final String buildMethodName() {
}

protected final AnnotationSpec generatedAnnotation() {
Class<?> generatedAnnotation;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a huge fan of this variable name, mainly because it's the same as the method.

How about generatedAnnotationClass instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done with separate helper method below.

Comment on lines 215 to 226
Class<?> generatedAnnotation;
try {
// available since 9
generatedAnnotation = Class.forName("javax.annotation.processing.Generated");
} catch (ClassNotFoundException e) {
try {
generatedAnnotation = Class.forName("javax.annotation.Generated");
} catch (ClassNotFoundException ex) {
// Shouldn't be possible.
throw new IllegalStateException(ex);
}
}
Copy link
Owner

@skinny85 skinny85 May 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind actually extracting this to a separate helper method? Something like private Class<?> determineGeneratedAnnotationClass() throws Exception { ...?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 222 to 225
} catch (ClassNotFoundException ex) {
// Shouldn't be possible.
throw new IllegalStateException(ex);
}
Copy link
Owner

@skinny85 skinny85 May 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this dead code, can you instead change this method to have throws Exception (its caller already has it, so it shouldn't cause any problems), and remove this entire catch statement?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

steps:
- uses: actions/checkout@v2

- name: Set up JDK 1.8
- name: Set up JDK
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it legal to use dynamic expressions in step names? So this would become something like

Suggested change
- name: Set up JDK
- name: Set up JDK version ${{ matrix.version }}

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's find out!

Sineaggi and others added 5 commits May 14, 2022 09:50
Co-authored-by: Adam Ruka <adamruka85@gmail.com>
Co-authored-by: Adam Ruka <adamruka85@gmail.com>
Copy link
Owner

@skinny85 skinny85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks perfect, thanks @Sineaggi!

@skinny85 skinny85 merged commit 9474885 into skinny85:master May 14, 2022
@Sineaggi Sineaggi deleted the add-java-9-support branch May 14, 2022 16:58
@der-R
Copy link
Contributor

der-R commented Jan 3, 2023

Could you release a version with Java 9+ support as a maven artifact on the central maven repository? Would be really nice to be able to use it as a normal dependency without maintaining an own copy of the binary.

@skinny85
Copy link
Owner

skinny85 commented Jan 4, 2023

Yes! Apologies @der-R, I merged the PR, but completely blanked that I need to do a release!

I'll do it this week (this is a new laptop where I don't have all of the stupid PGP stuff for Maven Central set up, so it will take a while), and I'll let you know when it's done!

@skinny85
Copy link
Owner

skinny85 commented Jan 9, 2023

@der-R @Sineaggi Jilt version 1.2 has now been released with this fix (and also with #8). Thanks again for submitting these PRs, and apologies for the release taking so long!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use javax.annotation.processing.Generated on newer versions of java
3 participants