Skip to content

Commit

Permalink
Merge pull request #466 from shifujun/pr-fix-lookupMethod
Browse files Browse the repository at this point in the history
Fix MemberResolver.lookupMethod bug when super class has more precise…
  • Loading branch information
chibash committed Dec 9, 2023
2 parents 1582943 + c04c375 commit 310fb8f
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 11 deletions.
25 changes: 16 additions & 9 deletions src/main/javassist/compiler/MemberResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@ else if (maybe == null || maybe.notmatch > res)

if (onlyExact)
maybe = null;
else
if (maybe != null)
return maybe;
//else maybe super class has more precise match

int mod = clazz.getModifiers();
boolean isIntf = Modifier.isInterface(mod);
Expand All @@ -143,8 +141,11 @@ else if (maybe == null || maybe.notmatch > res)
if (pclazz != null) {
Method r = lookupMethod(pclazz, methodName, argTypes,
argDims, argClassNames, onlyExact);
if (r != null)
return r;
if (r != null) {
if (maybe == null || maybe.notmatch > r.notmatch) {
maybe = r;
}
}
}
}
}
Expand All @@ -156,8 +157,11 @@ else if (maybe == null || maybe.notmatch > res)
Method r = lookupMethod(intf, methodName,
argTypes, argDims, argClassNames,
onlyExact);
if (r != null)
return r;
if (r != null) {
if (maybe == null || maybe.notmatch > r.notmatch) {
maybe = r;
}
}
}

if (isIntf) {
Expand All @@ -166,8 +170,11 @@ else if (maybe == null || maybe.notmatch > res)
if (pclazz != null) {
Method r = lookupMethod(pclazz, methodName, argTypes,
argDims, argClassNames, onlyExact);
if (r != null)
return r;
if (r != null) {
if (maybe == null || maybe.notmatch > r.notmatch) {
maybe = r;
}
}
}
}
}
Expand Down
25 changes: 23 additions & 2 deletions src/test/javassist/JvstTest5.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.TypeVariable;

import javassist.bytecode.AccessFlag;
Expand Down Expand Up @@ -166,7 +167,7 @@ public void testJIRA256() throws Exception {
CtClass cc = sloader.makeClass("test5.JIRA256");
ClassFile ccFile = cc.getClassFile();
ConstPool constpool = ccFile.getConstPool();

AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
javassist.bytecode.annotation.Annotation entityAnno
= new javassist.bytecode.annotation.Annotation("test5.Entity", constpool);
Expand All @@ -181,7 +182,7 @@ public void testJIRA256() throws Exception {
assertTrue(o.getClass().getName().equals("test5.JIRA256"));

java.lang.annotation.Annotation[] annotations = o.getClass().getDeclaredAnnotations();
assertEquals(1, annotations.length);
assertEquals(1, annotations.length);
}

public void testJIRA250() throws Exception {
Expand Down Expand Up @@ -625,4 +626,24 @@ public void testGithubIssue462Java21WithoutParameters() throws IOException {
assertEquals(1, attr.size());
assertNull(attr.parameterName(0));
}

public void testSuperCall() throws Exception {
String javacResult = new BearKeeper().javacResult();
assertEquals("Man feed(Bear)", javacResult);

CtClass cc = sloader.get("javassist.BearKeeper");
CtMethod cm = CtMethod.make(
"public String javassistResult() {return super.feed(new javassist.Bear());}",
cc);
cc.addMethod(cm);
cc.setModifiers(Modifier.PUBLIC);
cc.writeFile();
Object obj = make(cc.getName());
Method m = obj.getClass().getMethod("javassistResult");
Object javassistResult = m.invoke(obj);

//before this fix
//expected:<Man feed(Bear)> but was:<Keeper feed(Animal)>
assertEquals(javacResult, javassistResult);
}
}
38 changes: 38 additions & 0 deletions src/test/javassist/SuperCallCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package javassist;

class Animal {
}

class Bear extends Animal {
}


/**
* Base class has a method with precise type.
*/
class Man {
String feed(Bear bear) {
return "Man feed(Bear)";
}
}

/**
* Derived class has a method which has same name with base class's and more imprecise type.
*/
class Keeper extends Man {
String feed(Animal animal) {
return "Keeper feed(Animal)";
}
}

/**
* Derived class has a method which call super method with precise type.
*/
class BearKeeper extends Keeper {
public BearKeeper() {
}

String javacResult() {
return super.feed(new Bear());
}
}

0 comments on commit 310fb8f

Please sign in to comment.