siom79 / japicmp

Comparison of two versions of a jar archive
https://siom79.github.io/japicmp
Apache License 2.0
701 stars 107 forks source link

Type parameter changes in Java is not detected #337

Closed DhanJaya closed 1 year ago

DhanJaya commented 1 year ago

The type parameter change(generics) in Java is not detected. For an example if a method signature

public void test1(List<String> arg1){
        System.out.println(arg1);
    }

Changes to

 public void test1(List<Integer> arg1){
        System.out.println(arg1);
    }

JAPICmp tool output:

WARNING: You are using the option '--ignore-missing-classes', i.e. superclasses and interfaces that could not be found on the classpath are ignored. Hence changes caused by these superclasses and interfaces are not reflected in the output.
===  UNCHANGED CLASS: PUBLIC org.test.pkg1.TestClass1  (not serializable)
    ===  CLASS FILE FORMAT VERSION: 60.0 <- 60.0
    ===  UNCHANGED SUPERCLASS: java.lang.Object (<- java.lang.Object)
    ===  UNCHANGED CONSTRUCTOR: PUBLIC TestClass1()
    ===  UNCHANGED METHOD: PUBLIC STATIC void main(java.lang.String[])
    ===  UNCHANGED METHOD: PUBLIC void test1(java.util.List)
siom79 commented 1 year ago

You may be surprised, but the compiler erases the generic types. Hence, the class file does no longer have the information what type you used in the source file. And indeed, you can invoke this method at runtime with a list of different types.

DhanJaya commented 1 year ago

Thank you for your response. I went through the class files that were created for the jar files. The generic type was mentioned under the LocalVariableTypeTable and the Signature for the method. Class file before modification:  test1 (Ljava/util/List;)V arg1 Ljava/util/List; LocalVariableTypeTable $Ljava/util/List<Ljava/lang/String;>; Signature '(Ljava/util/List<Ljava/lang/String;>;) Class file after modification: test1 (Ljava/util/List;)V arg1 Ljava/util/List; LocalVariableTypeTable %Ljava/util/List<Ljava/lang/Integer;>; Signature ((Ljava/util/List<Ljava/lang/Integer;>;)

Could these information be used by the tool to extract the type change of the parameters?

siom79 commented 1 year ago

Yes, it should be possible to evaluate the information inside the LocalVariableTypeTable. I have started a PoC implementation and will let you know, once I have a releasable result.

siom79 commented 1 year ago

Added support for generic types. Will be released with 0.17.0