Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawbacks of javac -parameters flag

Tags:

java-8

javac

I want to try some frameworks features that required names of parameter in runtime, so I need to compile my app with -parameters and it will store the names of parameter in JVM byte code.

Which drawbacks except the size of jar/war exist of this parameter usage?

like image 557
VladS Avatar asked May 19 '17 10:05

VladS


People also ask

Is Java 17 or 18 better?

While JDK 17 was a long-term support (LTS) release that will receive at least eight years of support from Oracle, JDK 18 will be a short-term feature release that is supported for six months.

Should I move to Java 17?

Java 17. Java 17 is the most recent version of Java offering LTS. Released in September 2021, Java 17 offers performance and security enhancements that make it worth the migration. It also has several cool development features worth checking out.

How much faster is Java 17?

On average, for OptaPlanner use cases, these benchmarks indicate that: Java 17 is 8.66% faster than Java 11 and 2.41% faster than Java 16 for G1GC (default). Java 17 is 6.54% faster than Java 11 and 0.37% faster than Java 16 for ParallelGC.

Is JDK 17 backwards compatible with JDK 11?

JDK 8, 11, and 17 are all reasonable choices both for compiling and running Scala code. Since the JVM is normally backwards compatible, it is usually safe to use a newer JVM for running your code than the one it was compiled on, especially if you are not using JVM features designated “experimental” or “unsafe”.


2 Answers

The addition of parameter names to the class file format is covered by JEP 118, which was delivered in Java 8. There was a little bit of discussion about why inclusion of parameter names was made optional in OpenJDK email threads here and here. Briefly, the stated reasons to make parameter names optional are concerns about class file size, compatibility surface, and exposure of sensitive information.

The issue of compatibility surface deserves some additional discussion. One of the threads linked above says that changing a parameter name is a binary compatible change. This is true, but only in the strict context of the JVM's notion of binary compatibility. That is, changing a parameter name of a method will never change whether or not that method can be linked by the JVM. But the statement doesn't hold for compatibility in general.

Historically, parameter names have been treated like local variable names. (They are, after all, local in scope.) You could change them at will and nothing outside the method will be affected. But if you enable reflective access to parameter names, suddenly you can't change a name without thinking about what other parts of the program might be using it. Worse, there's nothing that can tell you unless you have strict test cases for all uses of parameter names, or you have a really good static analyzer that can find these cases (I'm not aware of one).

The comments linked to a question about using Jackson (a JSON processing library) which has a feature that maps method parameter names to JSON property names. This may be quite convenient, but it also means that if you change a parameter name, JSON binding might break. Worse, if the program is generating JSON structures based on Java method parameter names, then changing a method parameter name might silently change a data format or wire protocol. Clearly, in such an environment, using this feature reliably means that you have to have very good tests, plus comments sprinkled around the code indicating what parameter names mustn't be changed.

like image 116
Stuart Marks Avatar answered Oct 06 '22 22:10

Stuart Marks


The only thing is the size of the .class that will change, since the bytecode will now contain more information:

public class DeleteMe3 {
    public static void main(String[] args) {
    }

    private static void go(String s) {
    }
}

For example this will contain information about paramter names like so:

private static void go(java.lang.String);
   descriptor: (Ljava/lang/String;)V
   flags: ACC_PRIVATE, ACC_STATIC
   Code:
     stack=0, locals=1, args_size=1
       0: return
     LineNumberTable:
       line 11: 0
   MethodParameters:
     Name                           Flags
     s

This MethodParameters will simply not be present without -parameters.

There might be frameworks that don't play nice with this. Here is one Spring Data JPA Issue. I know about it because we hit it some time ago and had to upgrade (I have not been faced with any other from there on).

like image 37
Eugene Avatar answered Oct 06 '22 22:10

Eugene