0 Replies Latest reply: May 11, 2006 7:44 PM by Yanic Inghelbrecht RSS

rel 3.1 - Problem with CtMethod#equals

Yanic Inghelbrecht Newbie

(release 3.1)

The implementation of class CtMethod regards a seed and its redefinitions as equals. This can cause problems when their CtMethod objects are stored in a Collection or used as keys in a Map. It would be more useful if the implementation would (also) pass the following testcase :

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.NotFoundException;
import junit.framework.TestCase;

public class CtMethodTest extends TestCase {
 public void testEquals() throws NotFoundException {
 ClassPool cp=ClassPool.getDefault();
 CtClass object=cp.get("java.lang.Object");
 CtClass string=cp.get("java.lang.String");
 CtMethod seed=object.getDeclaredMethod("equals");
 CtMethod redefinition=string.getDeclaredMethod("equals");

 assertFalse("declaring class different", seed.getDeclaringClass().equals(redefinition.getDeclaringClass()));
 assertFalse("CtMethod object different", seed.equals(redefinition));
 }
}


The identity of a CtMethod object is based on getStringRep :

 final String getStringRep() {
 if (cachedStringRep == null)
 cachedStringRep = methodInfo.getName()
 + Descriptor.getParamDescriptor(methodInfo.getDescriptor());
 return cachedStringRep;
 }


A possible (quick) fix for this issue adds getDeclaringClass to the identity, for example by changing getStringRep to :

 final String getStringRep() {
 if (cachedStringRep == null)
 cachedStringRep = this.getDeclaringClass().getName() + "#" + methodInfo.getName()
 + Descriptor.getParamDescriptor(methodInfo.getDescriptor());
 return cachedStringRep;
 }


Can one of the developers please confirm that the implementation of equals either disregards the declaring class on purpose or that it will be fixed in a next release?

Btw, thank you for Javassist. I think it's great!!!