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

Enable Java preview features #1303

Closed
oskar2517 opened this issue Sep 18, 2023 · 17 comments
Closed

Enable Java preview features #1303

oskar2517 opened this issue Sep 18, 2023 · 17 comments
Labels
enhancement Issue/PR that involves features, improvements and other changes language PR / Issue deals (partly) with new and/or existing languages for JPlag minor Minor issue/feature/contribution/change

Comments

@oskar2517
Copy link

I am working with submissions that use pattern matching in switch statements, which is a language feature hidden behind the --enable-preview flag in Java 17. Is it possible to tell JPlag to enable preview features when parsing Java files? Currently, it fails with the following error message:

failed to parse 'MyClass.java' with reason: patterns in switch statements are a preview feature and are disabled by default.
  (use --enable-preview to enable patterns in switch statements)
        at de.jplag.ParsingException.wrappingExceptions(ParsingException.java:70)
        at de.jplag.java.JavacAdapter.parseFiles(JavacAdapter.java:59)
        at de.jplag.java.Parser.parse(Parser.java:25)
        at de.jplag.java.Language.parse(Language.java:47)
        at de.jplag.Submission.parse(Submission.java:249)
        at de.jplag.SubmissionSet.parseSubmissions(SubmissionSet.java:147)
        at de.jplag.SubmissionSet.parseAllSubmissions(SubmissionSet.java:103)
        at de.jplag.SubmissionSet.<init>(SubmissionSet.java:45)
        at de.jplag.SubmissionSetBuilder.buildSubmissionSet(SubmissionSetBuilder.java:78)
        at de.jplag.JPlag.run(JPlag.java:55)
        at de.jplag.cli.CLI.main(CLI.java:65)
@SirYwell
Copy link
Contributor

It's currently not possible to pass arguments to the language module directly, and I think this isn't something that can be added easily. Additionally, the token extraction does not cover preview features, so while passing the option would turn the error into a warning, plagiarism detection might not work as expected in such cases.

There are also breaking changes between the preview features in Java 17 and their finalized versions in Java 21, so supporting preview features likely causes additional work twice.

You can run JPlag with Java 21, from a quick test that seems to work, but the problem of the preview feature having slight syntactical and semantical differences will persist (it might result in different compilation errors).

Alternatively, you can probably either fork or extend the existing Java language module to suite your needs, I'm not sure if the JPlag maintainers are interested in such changes.

(@tsaglam this would be a case where a lexer-based language module is benefitial - with the aforementioned limitations)

@oskar2517
Copy link
Author

It's currently not possible to pass arguments to the language module directly, and I think this isn't something that can be added easily. Additionally, the token extraction does not cover preview features, so while passing the option would turn the error into a warning, plagiarism detection might not work as expected in such cases.

That means, plagiarism in switch expressions would likely not be detected?

@SirYwell
Copy link
Contributor

That means, plagiarism in switch expressions would likely not be detected?

All the code elements should be reached, so the code of each case will be handled just fine, but the pattern itself is not covered - whether this helps or hurts the detection is unclear.

@oskar2517
Copy link
Author

I think this is fine for now. Thank you.

I have forked the repository and added the required flags to enable preview features in the parser.

@tsaglam tsaglam added enhancement Issue/PR that involves features, improvements and other changes minor Minor issue/feature/contribution/change language PR / Issue deals (partly) with new and/or existing languages for JPlag labels Sep 28, 2023
@tsaglam
Copy link
Member

tsaglam commented Sep 28, 2023

We could probably consider supporting preview features with JPlag. I will take this into our internal discussions.

@tsaglam
Copy link
Member

tsaglam commented Dec 5, 2023

Closed by #1370.

@tsaglam tsaglam closed this as completed Dec 5, 2023
@euberdeveloper
Copy link

I still have this error:

2024-02-06-06:46:53_541 [main] [WARN] Submission - Failed to parse submission gr2 with error {}
de.jplag.ParsingException: failed to parse '/XXX/ConnectionHandler.java' with reason: patterns in switch statements are a preview feature and are disabled by default.
  (use --enable-preview to enable patterns in switch statements)
        at de.jplag.java.JavacAdapter.lambda$processErrors$1(JavacAdapter.java:81)
        at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
        at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179)
        at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625)
        at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
        at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
        at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:575)
        at java.base/java.util.stream.AbstractPipeline.evaluateToArrayNode(AbstractPipeline.java:260)
        at java.base/java.util.stream.ReferencePipeline.toArray(ReferencePipeline.java:616)
        at java.base/java.util.stream.ReferencePipeline.toArray(ReferencePipeline.java:622)
        at java.base/java.util.stream.ReferencePipeline.toList(ReferencePipeline.java:627)
        at de.jplag.java.JavacAdapter.processErrors(JavacAdapter.java:82)
        at de.jplag.java.JavacAdapter.parseFiles(JavacAdapter.java:57)
        at de.jplag.java.Parser.parse(Parser.java:25)
        at de.jplag.java.Language.parse(Language.java:47)
        at de.jplag.Submission.parse(Submission.java:249)
        at de.jplag.SubmissionSet.parseSubmissions(SubmissionSet.java:147)
        at de.jplag.SubmissionSet.parseAllSubmissions(SubmissionSet.java:103)
        at de.jplag.SubmissionSet.<init>(SubmissionSet.java:45)
        at de.jplag.SubmissionSetBuilder.buildSubmissionSet(SubmissionSetBuilder.java:78)
        at de.jplag.JPlag.run(JPlag.java:55)
        at de.jplag.cli.CLI.main(CLI.java:91)

I downloaded the latest jar in the Releases section

@koppor
Copy link

koppor commented Dec 1, 2024

There is an error message when using Java 23. Does this relate to this usse?

2024-12-02-12:04:54_836 [WARN] Submission - Failed to parse submission repo079:
failed to parse '<null>'error: invalid source release 21 with --enable-preview
  (preview language features are only supported for release 23)

@tsaglam
Copy link
Member

tsaglam commented Dec 2, 2024

@koppor What is your exact Java version? What source code did you parse (or which preview feature)?

@koppor
Copy link

koppor commented Dec 2, 2024

@koppor What is your exact Java version? What source code did you parse (or which preview feature)?

I used JDK23. When switching to JDK21, it worked fine.

@SirYwell
Copy link
Contributor

SirYwell commented Dec 2, 2024

The --enable-preview flag only works when combined with the --release X flag, where X matches the version of the running JVM. JPlag currently hard-codes that value to 21, so it won't work on any other version.

In theory it's possible to set the version dynamically, but that defeats the purpose of the flag as it means there might be incompatibilities (i.e. Java 21 and 22 can deal with string templates, but Java 23 can't).

@koppor
Copy link

koppor commented Dec 7, 2024

The --enable-preview flag only works when combined with the --release X flag, where X matches the version of the running JVM. JPlag currently hard-codes that value to 21, so it won't work on any other version.

I did not specify any flags. Just executed jplag with following command line

java -jar jplag.jar ../lecture/ -l java -bc ../lecture/example/ -s api/src

Thus, no --enable-preview. Just had JDK23 as JAVA_HOME - and in the PATH.

@misan
Copy link

misan commented Dec 10, 2024

Similar problem here with JDK22. It seems I have to downgrade to 21.

@misan
Copy link

misan commented Dec 10, 2024

Yes, I can confirm that going back to JDK21 from JDK22 made the error

[WARN] Submission - Failed to parse submission repo079:
failed to parse '<null>'error: invalid source release 21 with --enable-preview
  (preview language features are only supported for release 22)

go away and the same jplag-5.1.0-jar-with-dependencies.jar file to work ok.

@SirYwell
Copy link
Contributor

I did not specify any flags. Just executed jplag with following command line

java -jar jplag.jar ../lecture/ -l java -bc ../lecture/example/ -s api/src

Thus, no --enable-preview. Just had JDK23 as JAVA_HOME - and in the PATH.

Sorry @koppor, I should have clarified that this is a problem inside of JPlag. JPlag itself uses these flags when passing the code to the javac tool here

final CompilationTask task = javac.getTask(null, fileManager, listener,
List.of("-proc:none", "--enable-preview", "--release=" + JavaLanguage.JAVA_VERSION), null, javaFiles);

@misan
Copy link

misan commented Dec 11, 2024

I guess the intented behaviour is not for JPlag released JAR not to work and give cryptic error messages when run with JDK22 or JDK23 but to work fine with JDK21.

At least for me, it was a bit of a challenge to figure out that the errors I was getting were not because I was wrong with the command line options but because the compiled code was unhappy with my execution environment, as I haven't used JPlag for many years.

For me it is very convenient to just download the released JAR and run it in the command line. Perhaphs downloading the source and building it from scratch will fix it but who has time for that :-)

@tsaglam
Copy link
Member

tsaglam commented Dec 15, 2024

I did not have the chance to fully read all comments here as I am currently out of office, but the issues might be resolved with #1854. The next release will include this fix, which will most likely be released before the end of February. If your issues above are not addressed by #1854, we will take a look at them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Issue/PR that involves features, improvements and other changes language PR / Issue deals (partly) with new and/or existing languages for JPlag minor Minor issue/feature/contribution/change
Projects
None yet
Development

No branches or pull requests

6 participants