Skip to content

Commit

Permalink
Refactor common code
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Oct 29, 2022
1 parent 8267a86 commit 6b17bbe
Showing 1 changed file with 5 additions and 30 deletions.
35 changes: 5 additions & 30 deletions src/main/java/org/apache/bcel/generic/ReferenceType.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,36 +64,7 @@ public ReferenceType firstCommonSuperclass(final ReferenceType t) throws ClassNo
return Type.OBJECT;
// TODO: Is there a proof of OBJECT being the direct ancestor of every ArrayType?
}
if (this instanceof ObjectType && ((ObjectType) this).referencesInterface() || t instanceof ObjectType && ((ObjectType) t).referencesInterface()) {
return Type.OBJECT;
// TODO: The above line is correct comparing to the vmspec2. But one could
// make class file verification a bit stronger here by using the notion of
// superinterfaces or even castability or assignment compatibility.
}
// this and t are ObjectTypes, see above.
final ObjectType thiz = (ObjectType) this;
final ObjectType other = (ObjectType) t;
final JavaClass[] thizSups = Repository.getSuperClasses(thiz.getClassName());
final JavaClass[] otherSups = Repository.getSuperClasses(other.getClassName());
if (thizSups == null || otherSups == null) {
return null;
}
// Waaahh...
final JavaClass[] thisSups = new JavaClass[thizSups.length + 1];
final JavaClass[] tSups = new JavaClass[otherSups.length + 1];
System.arraycopy(thizSups, 0, thisSups, 1, thizSups.length);
System.arraycopy(otherSups, 0, tSups, 1, otherSups.length);
thisSups[0] = Repository.lookupClass(thiz.getClassName());
tSups[0] = Repository.lookupClass(other.getClassName());
for (final JavaClass tSup : tSups) {
for (final JavaClass thisSup : thisSups) {
if (thisSup.equals(tSup)) {
return ObjectType.getInstance(thisSup.getClassName());
}
}
}
// Huh? Did you ask for Type.OBJECT's superclass??
return null;
return getFirstCommonSuperclassInternal(t);
}

/**
Expand Down Expand Up @@ -134,6 +105,10 @@ public ReferenceType getFirstCommonSuperclass(final ReferenceType t) throws Clas
return Type.OBJECT;
// TODO: Is there a proof of OBJECT being the direct ancestor of every ArrayType?
}
return getFirstCommonSuperclassInternal(t);
}

private ReferenceType getFirstCommonSuperclassInternal(final ReferenceType t) throws ClassNotFoundException {
if (this instanceof ObjectType && ((ObjectType) this).referencesInterfaceExact()
|| t instanceof ObjectType && ((ObjectType) t).referencesInterfaceExact()) {
return Type.OBJECT;
Expand Down

0 comments on commit 6b17bbe

Please sign in to comment.