Skip to content

Commit 57466b2

Browse files
committed
allowing for inner-class call to private method in outer class
1 parent c281a05 commit 57466b2

File tree

1 file changed

+44
-103
lines changed

1 file changed

+44
-103
lines changed

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

Lines changed: 44 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ private static final String getPrimitiveTYPE(String name) {
126126

127127
protected AbstractTypeDeclaration rootTypeNode;
128128

129+
private String b$name;
130+
129131
public boolean isMethodOverloadingSupported() {
130132
return methodOverloadingSupported;
131133
}
@@ -1228,7 +1230,7 @@ public boolean visit(MethodDeclaration node) {
12281230
System.err.println("native: " + key);
12291231
}
12301232
visitNativeJavadoc(node.getJavadoc(), null);
1231-
buffer.append("}");
1233+
buffer.append("}\r\n");
12321234
}
12331235
List<ASTFinalVariable> normalVars = ((ASTVariableVisitor) getAdaptable(
12341236
ASTVariableVisitor.class)).normalVars;
@@ -1368,8 +1370,8 @@ public boolean visit(MethodInvocation node) {
13681370
expression.accept(this);
13691371
buffer.append(".");
13701372
}
1371-
1372-
String className = mBinding.getDeclaringClass().getQualifiedName();
1373+
ITypeBinding cl = mBinding.getDeclaringClass();
1374+
String className = cl.getQualifiedName();
13731375
String methodName = node.getName().getIdentifier();
13741376
String j2sParams = getJ2SParamQualifier(className, mBinding);
13751377

@@ -1394,18 +1396,51 @@ public boolean visit(MethodInvocation node) {
13941396
}
13951397
}
13961398
}
1399+
1400+
// record whether this.b$[.....] was used, and if so and it is private, we need to use it again
1401+
b$name = null;
13971402
if (!isSpecialMethod) {
13981403
node.getName().accept(this);
13991404
buffer.append(j2sParams);
14001405
}
1401-
buffer.append(isPrivateAndNotStatic ? ".apply(this, [" : " (");
1406+
if (isPrivateAndNotStatic) {
1407+
// A call to a private outer-class method from an inner class requires
1408+
// this.b$["....."], not just "this"
1409+
// There is probably a nicer way to do this...
1410+
buffer.append(".apply(this");
1411+
if (b$name != null)
1412+
buffer.append(b$name);
1413+
buffer.append(", [");
1414+
} else {
1415+
buffer.append("(");
1416+
}
14021417
addMethodParameterList(node.arguments(), mBinding, false, null, null);
14031418
if (isPrivateAndNotStatic)
14041419
buffer.append("]");
14051420
buffer.append(")");
14061421
return false;
14071422
}
14081423

1424+
private boolean addThisOrSyntheticReference(Expression node) {
1425+
/*
1426+
* only need callbacks wrapper in inner classes or anonymous
1427+
* classes.
1428+
*/
1429+
buffer.append("this");
1430+
Name qualifier = (node instanceof ThisExpression ? ((ThisExpression) node).getQualifier() : null);
1431+
if (qualifier != null) {
1432+
ASTNode xparent = getXparent(node);
1433+
if (xparent != null && xparent.getParent() != null // CompilationUnit
1434+
&& xparent.getParent().getParent() != null) {
1435+
buffer.append(".b$[\"");
1436+
qualifier.accept(this);
1437+
buffer.append("\"]");
1438+
return false;
1439+
}
1440+
}
1441+
return false;
1442+
}
1443+
14091444
public boolean visit(SimpleName node) {
14101445
String constValue = checkConstantValue(node);
14111446
if (constValue != null) {
@@ -1687,87 +1722,11 @@ private void appendFieldName(ASTNode parent, ITypeBinding declaringClass, boolea
16871722
parent = parent.getParent();
16881723
}
16891724

1690-
// Start SwingJS modded 6/9/17- Reverted b$
16911725
if (!isThis) {
1692-
buffer.append("this.b$[\"");
1693-
buffer.append(removeJavaLang(name));
1694-
buffer.append("\"].");
1695-
}
1696-
1697-
// if (!isThis) {
1698-
// buffer.append("this.callbacks[\"");
1699-
// //buffer.append(shortenQualifiedName(name));
1700-
// StringBuilder dollarBuilder = new StringBuilder();
1701-
// ArrayList<String> levels = new ArrayList<String>();
1702-
// ArrayList<String> classes = new ArrayList<String>();
1703-
// if (originalType != null) {
1704-
// levels.add(originalType.getBinaryName());
1705-
// classes.add(originalType.getSuperclass().getBinaryName());
1706-
// ITypeBinding thisDeclaringClass = originalType.getDeclaringClass();
1707-
// while (thisDeclaringClass != null) {
1708-
// levels.add(thisDeclaringClass.getBinaryName());
1709-
// classes.add(thisDeclaringClass.getSuperclass().getBinaryName());
1710-
// dollarBuilder.append("$");
1711-
// thisDeclaringClass = thisDeclaringClass.getDeclaringClass();
1712-
// }
1713-
// }
1714-
// String binaryName = declaringClass.getBinaryName();
1715-
// int idx = levels.indexOf(binaryName);
1716-
// if (idx == -1) {
1717-
// idx = classes.indexOf(binaryName);
1718-
// if (idx == -1) {
1719-
// // Check each super class
1720-
// int index = 0;
1721-
// ITypeBinding superClass = originalType.getSuperclass();
1722-
// while (superClass != null) {
1723-
// String superName = superClass.getBinaryName();
1724-
// if ("java.lang.Object".equals(superName)) {
1725-
// break;
1726-
// }
1727-
// if (binaryName.equals(superName)) {
1728-
// idx = index;
1729-
// //break;
1730-
// }
1731-
// superClass = superClass.getSuperclass();
1732-
// }
1733-
// ITypeBinding thisDeclaringClass = originalType.getDeclaringClass();
1734-
// while (thisDeclaringClass != null) {
1735-
// index++;
1736-
// superClass = thisDeclaringClass.getSuperclass();
1737-
// while (superClass != null) {
1738-
// String superName = superClass.getBinaryName();
1739-
// if ("java.lang.Object".equals(superName)) {
1740-
// break;
1741-
// }
1742-
// if (binaryName.equals(superName)) {
1743-
// idx = index;
1744-
// //break;
1745-
// }
1746-
// superClass = superClass.getSuperclass();
1747-
// }
1748-
// thisDeclaringClass = thisDeclaringClass.getDeclaringClass();
1749-
// }
1750-
// }
1751-
// }
1752-
// if (idx != -1) {
1753-
// for (int i = idx + 1; i < levels.size(); i++) {
1754-
// if (dollarBuilder.length() > 0) {
1755-
// dollarBuilder.deleteCharAt(0);
1756-
// }
1757-
// }
1758-
// } else {
1759-
// declaringClass = declaringClass.getDeclaringClass();
1760-
// while (declaringClass != null) {
1761-
// if (dollarBuilder.length() > 0) {
1762-
// dollarBuilder.deleteCharAt(0);
1763-
// }
1764-
// declaringClass = declaringClass.getDeclaringClass();
1765-
// }
1766-
// }
1767-
// buffer.append(dollarBuilder);
1768-
// buffer.append("\"].");
1769-
// }
1770-
1726+
buffer.append("this");
1727+
buffer.append(b$name = ".b$[\"" + removeJavaLang(name) + "\"]");
1728+
buffer.append(".");
1729+
}
17711730
}
17721731

17731732
public boolean visit(SimpleType node) {
@@ -1875,25 +1834,7 @@ public boolean visit(SuperMethodInvocation node) {
18751834
}
18761835

18771836
public boolean visit(ThisExpression node) {
1878-
Name qualifier = node.getQualifier();
1879-
if (qualifier != null) {
1880-
ASTNode xparent = getXparent(node);
1881-
if (xparent == null || xparent.getParent() == null // CompilationUnit
1882-
|| xparent.getParent().getParent() == null) {
1883-
buffer.append("this");
1884-
} else {
1885-
/*
1886-
* only need callbacks wrapper in inner classes or anonymous
1887-
* classes.
1888-
*/
1889-
buffer.append("this.b$[\"");
1890-
// Start SwingJS modded 6/9/17- Reverted dollarBuilder etc.
1891-
qualifier.accept(this);
1892-
buffer.append("\"]");
1893-
}
1894-
} else {
1895-
buffer.append("this");
1896-
}
1837+
addThisOrSyntheticReference(node);
18971838
return false;
18981839
}
18991840

0 commit comments

Comments
 (0)