Search code examples
javaenumsjava-17

"The code for the static initializer is exceeding the 65535 bytes limit" : Message in a bottle


Let me explain my problem.

Context :

  • Migration JDK 8 > JDK 17 of a 2.5M LOC client/server application
  • We have a huge enumeration (>7500 elements) that contains all security rights
  • This enumeration is used by an annotation which target can be a type or a method, ex :
@Access(Right.R_A30110)
public void method(...) {...}

If I work with JDK 8 No problem ! It compiles and runs perfectly.

With JDK 17 Error message The code for the static initializer is exceeding the 65535 bytes limit !

a) First, I don't understand why I have this error message with JDK 17. I've read a lot of pages, I understand the error message but I should have the same with JDK 8 ??

b) I need to solve the problem :) What I tried (or wanted to try) :

  • split the big enumeration into small parts => not possible because inheritance is not allowed for Enums
  • use the pre-java 5 strategy : create a class and a set of constants (something like R_A30110=new Right("R_A30110"); => not possible because the I'll have the error invalid type for the annotation attribute 'value' : only primitive type, String, Class, annotation, enumeration are permitted or 1-dimensional arrays thereof => The risk is that solution may lead to the same problem (65535 bytes limitation...=

Do you have any idea I missed ?

Thank you !


Solution

  • Let's put aside the concerns mentioned in the comments to this question: although they are correct, they raise different issues.

    You are using the enum because you want to have already the compiler warning you when using an access right that does not exist. Otherwise you could also use a string as the value for the annotation …

    But because you are using an annotation, you can use a string and get the compiler to warn you if the given access right does not exist: write an annotation processor that checks the @Access annotation value (now a string) against a list of valid ones at compile time. If not matching, the annotation processor throws an exception and stops the compilation, otherwise it does nothing.

    Only drawback is that the IDE won't help you when applying the value.

    But with a little bit more effort you can extend the annotation processor to do some additional checks, like whether the given access right is feasible at the given location, or if the same access right is applied more than once (or never) or … think about.