Skip to content

Commit 1f2f00d

Browse files
committed
fix for superclass being an inner class
1 parent 0629480 commit 1f2f00d

2 files changed

Lines changed: 63 additions & 35 deletions

File tree

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/ASTKeywordVisitor.java

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,18 +1737,23 @@ private IVariableBinding getLeftVariableBinding(Expression left, IBinding leftTy
17371737
}
17381738

17391739
/**
1740-
* Proved access to C$.$clinit$ when a static method is called or a static field is accessed.
1740+
* Proved access to C$.$clinit$ when a static method is called or a static
1741+
* field is accessed.
17411742
*
17421743
* @param methodQualifier
17431744
* SimpleName qualifier in qualifier.methodName()
17441745
* @param className
1745-
* @param doEscape set true except for static nonprivate field names
1746-
* @param doCache generally true, but not for initial class definitions or for some nonstatic references
1747-
* @param doAppend true to use buffer.append;
1748-
* @return name wrapped if necessary by nested Class.load() calls
1746+
* @param doEscape
1747+
* set true except for static nonprivate field names
1748+
* @param doCache
1749+
* generally true, but not for initial class definitions or for
1750+
* some nonstatic references
1751+
* @param doAppend
1752+
* true to use buffer.append;
1753+
* @return name wrapped if necessary by nested Class.load() calls
17491754
*/
1750-
protected String getQualifiedStaticName(Name methodQualifier, String className, boolean doEscape,
1751-
boolean doCache, boolean doAppend) {
1755+
protected String getQualifiedStaticName(Name methodQualifier, String className, boolean doEscape, boolean doCache,
1756+
boolean doAppend) {
17521757
// BH: The idea here is to load these on demand.
17531758
// It will require synchronous loading,
17541759
// but it will ensure that a class is only
@@ -1759,7 +1764,7 @@ protected String getQualifiedStaticName(Name methodQualifier, String className,
17591764
if (!doEscape) {
17601765
if (methodQualifier != null) {
17611766
methodQualifier.accept(this);
1762-
return null;
1767+
return null;
17631768
}
17641769
s = className;
17651770
doCache = false;
@@ -1774,38 +1779,37 @@ protected String getQualifiedStaticName(Name methodQualifier, String className,
17741779
if (s == null) {
17751780
if (methodQualifier != null)
17761781
className = fixNameNoC$(null, className);
1777-
s = "Clazz.load(";
1778-
String[] parts = className.split("\\.");
1779-
int nclass = 0;
1780-
for (int i = 0; i < parts.length; i++) {
1781-
String part = parts[i];
1782-
if (i == 0) {
1783-
if (part.equals("C$")) {
1784-
s += part;
1785-
continue;
1786-
}
1787-
if (part.equals("P$")) {
1788-
// can't do this with Clazz.load
1789-
part = getPackageName();
1790-
}
1791-
}
1792-
s += (i == 0 ? "'" : ".");
1793-
s += part;
1794-
if (Character.isUpperCase(part.charAt(0))) {
1795-
if (nclass > 0)
1796-
s = "Clazz.load(" + s;
1797-
if (++nclass == 1 && s.indexOf("'") >= 0)
1798-
s += "'";
1799-
s += ")";
1800-
}
1801-
}
1782+
s = getNestedClazzLoads(className);
18021783
if (n != null)
18031784
s = "(I$[" + n + "] || (I$[" + n + "]=" + s + "))";
18041785
}
18051786
if (doAppend)
18061787
buffer.append(s);
18071788
return s;
1789+
}
18081790

1791+
/**
1792+
* Nest loads of inner classes pkg.Foo.Bar as Clazz.load(Clazz.load('pkg.Foo').Bar)
1793+
*
1794+
* @param className
1795+
* @return
1796+
*/
1797+
private String getNestedClazzLoads(String className) {
1798+
String[] parts = className.split("\\.");
1799+
String s = parts[0];
1800+
if (s.equals("P$")) {
1801+
// can't do this with Clazz.load
1802+
s = getPackageName();
1803+
}
1804+
int i = 1;
1805+
// loop through packages and outer Class
1806+
while (i < parts.length && (i == 1 || !Character.isUpperCase(parts[i - 1].charAt(0))))
1807+
s += "." + parts[i++];
1808+
String ret = "Clazz.load('" + s + "')";
1809+
// add inner classes
1810+
while (i < parts.length)
1811+
ret += "Clazz.load(" + ret + "." + parts[i++] + ")";
1812+
return ret;
18091813
}
18101814

18111815
private boolean haveDirectStaticAccess(Expression exp) {

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/ASTScriptVisitor.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,8 +1245,8 @@ private boolean addClassOrInterface(ASTNode node, ITypeBinding binding, List<?>
12451245
if (isAnonymous)
12461246
getQualifiedStaticName(null, superclassName, true, false, true);
12471247
else
1248-
buffer.append("'" + superclassName + "'"); // taken care of by
1249-
// loading
1248+
buffer.append(getInnerClassList(superclassName));
1249+
12501250
}
12511251

12521252
// arg5: superinterface(s) if not null
@@ -1494,6 +1494,30 @@ private boolean addClassOrInterface(ASTNode node, ITypeBinding binding, List<?>
14941494
return false;
14951495
}
14961496

1497+
/**
1498+
* For Clazz.newClazz$ we want an array if there is an inner class
1499+
* so that the outer class is guarranteed to be loaded first.
1500+
*
1501+
* @param className
1502+
* @return
1503+
*/
1504+
private String getInnerClassList(String className) {
1505+
1506+
String[] parts = className.split("\\.");
1507+
String s = parts[0];
1508+
int i = 1;
1509+
// loop through packages and outer Class
1510+
while (i < parts.length && !Character.isUpperCase(parts[i - 1].charAt(0))){
1511+
s += "." + parts[i++];
1512+
}
1513+
String ret = "'" + s + "'";
1514+
// add inner classes
1515+
while (i < parts.length){
1516+
ret += ",'" + s + "." + parts[i++] + "'";
1517+
}
1518+
return (ret.indexOf(",") >= 0 ? "[" + ret + "]" : ret);
1519+
}
1520+
14971521
/**
14981522
* If there is no Foo() or Foo(xxx... array),
14991523
* then we need to provide our own constructor.

0 commit comments

Comments
 (0)