Skip to content

Formatting class with a variable/field called "sealed" causes IAE: No enum constant javax.lang.model.element.Modifier.SEALED with Java < 15 #1224

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

Closed
kwin opened this issue Mar 15, 2025 · 3 comments

Comments

@kwin
Copy link

kwin commented Mar 15, 2025

When trying to format the following class one runs into an IAE with Java < 15.

package org.test;

public class Test {
    void test() {
        String sealed = "a sealed test";
    }
}

The stacktrace looks like this

java.lang.IllegalArgumentException: No enum constant javax.lang.model.element.Modifier.SEALED
	at java.base/java.lang.Enum.valueOf(Enum.java:240)
	at [email protected]/javax.lang.model.element.Modifier.valueOf(Modifier.java:47)
	at com.google.googlejavaformat.java.ModifierOrderer.asModifier(ModifierOrderer.java:151)
	at com.google.googlejavaformat.java.ModifierOrderer.reorderModifiers(ModifierOrderer.java:79)
	at com.google.googlejavaformat.java.Formatter.getFormatReplacements(Formatter.java:285)
...

The reason is the unconditional call of Modifier.valueOf("sealed") in

which throws this exception on all Java versions below 15.

Although the newest version only supports Java 17 as runtime, this is an issue already with versions which are supposed to support Java 11 as runtime, e.g. < 1.24.0

@kwin
Copy link
Author

kwin commented Mar 15, 2025

This is related to #697 however this is about a case where the "sealed" modifier is incorrectly(!) detected. Notice that sealed is not a reserved keyword (https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html), so it may be used as variable/field name as well. According to https://docs.oracle.com/javase/specs/jls/se17/jls17.pdf, chapter 3.9 sealed is just a contextual keyword, so there needs to be some proper fallback.

@cushon
Copy link
Collaborator

cushon commented Mar 22, 2025

Thanks for the report, I agree this is a bug, and should be handled gracefully when running on older JDK versions.

@cushon
Copy link
Collaborator

cushon commented Mar 28, 2025

Although the newest version only supports Java 17 as runtime, this is an issue already with versions which are supposed to support Java 11 as runtime, e.g. < 1.24.0

I agree this is a bug, and should be handled gracefully when running on older JDK versions.

Sorry, I hadn't been thinking about the fact that the minimum supported JDK for the latest releases is JDK 17.

I agree this is a bug with < 1.24.0, but we are not doing update releases of old versions. I think the best option is to update to a newer version of the formatter.

@cushon cushon closed this as completed Mar 28, 2025
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

No branches or pull requests

2 participants