Skip to content

Commit 8decacc

Browse files
committed
// TODO: resource loading using ResourceBundle.getBundle throws error
when looking for format java.class // TODO: need real Class object Foo.class, not just Foo itself -- see Test_Class.java // BH 8/30/2017 -- all i/o working, including printf and FileOutputStream
1 parent bd12d77 commit 8decacc

File tree

5 files changed

+123
-75
lines changed

5 files changed

+123
-75
lines changed

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

Lines changed: 72 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@
5858
import org.eclipse.jdt.core.dom.TypeLiteral;
5959
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
6060

61-
// TODO: resource loading
61+
// TODO: resource loading using ResourceBundle.getBundle throws error when looking for format java.class
62+
// TODO: need real Class object Foo.class, not just Foo itself -- see Test_Class.java
6263

64+
// BH 8/30/2017 -- all i/o working, including printf and FileOutputStream
6365
// BH 8/19/2017 -- String must implement CharSequence, so all .length() -> .length$()
6466
// BH 8/19/2017 -- varargs logic fixed for missing argument
6567
// BH 8/18/2017 -- array instanceof, reflection, componentType fixes
@@ -213,47 +215,59 @@ public boolean visit(AnonymousClassDeclaration node) {
213215

214216
ITypeBinding binding = node.resolveBinding();
215217
ASTTypeVisitor typeVisitor = ((ASTTypeVisitor) getAdaptable(ASTTypeVisitor.class));
216-
String anonClassName = getNameForBinding(binding);
217-
String fullClassName = anonClassName;
218-
String shortClassName = anonClassName.substring(anonClassName.lastIndexOf('.') + 1);
218+
String fullClassName = getNameForBinding(binding);
219+
int idx = fullClassName.lastIndexOf('.');
220+
String shortClassName = fullClassName.substring(idx + 1);
219221
String className = typeVisitor.getClassName();
222+
buffer.append("(");
223+
220224
// BH: add the anonymous class definition inline, not as a static,
221225
// and not requiring finalization of variables
222-
buffer.append("(");
223226
addAnonymousFunctionWrapper(true);
224-
buffer.append("var C$ = Clazz.decorateAsClass (function () {\r\n");
225-
buffer.append("Clazz.newInstance$ (this, arguments");
226-
if (!(node.getParent() instanceof EnumConstantDeclaration))
227-
buffer.append("[0], true");
228-
buffer.append(");\r\n}, ");
229227

230-
int idx = fullClassName.lastIndexOf('.');
231-
if (idx >= 0) {
232-
buffer.append(assureQualifiedName(shortenPackageName(fullClassName)));
228+
// add decorateAsClass
229+
buffer.append("var C$ = Clazz.decorateAsClass (");
230+
{
231+
// arg1 is the class definition function, C$, which is called in Clazz.$new().
232+
buffer.append("function () {\r\n");
233+
{
234+
// add the appropriate newInstance$ call
235+
buffer.append("Clazz.newInstance$ (this, arguments");
236+
if (!(node.getParent() instanceof EnumConstantDeclaration))
237+
buffer.append("[0], true");
238+
buffer.append(");\r\n}, ");
239+
}
240+
241+
// arg2 is the package name or null
242+
buffer.append(idx >= 0 ? assureQualifiedName(shortenPackageName(fullClassName)) : "null");
243+
244+
// arg3 is the full class name in quotes
233245
buffer.append(", \"" + fullClassName.substring(idx + 1) + "\"");
234-
} else {
235-
buffer.append("null, \"" + fullClassName + "\"");
236-
}
237246

238-
ITypeBinding superclass = binding.getSuperclass();
239-
if (superclass != null) {
240-
String superclassName = superclass.getQualifiedName();
241-
superclassName = assureQualifiedName(removeJavaLang(superclassName));
242-
if (superclassName != null && superclassName.length() != 0 && !"Object".equals(superclassName)) {
243-
buffer.append(", ");
244-
buffer.append(superclassName);
245-
} else {
246-
ITypeBinding[] declaredTypes = binding.getInterfaces();
247-
if (declaredTypes != null && declaredTypes.length > 0) {
248-
superclassName = declaredTypes[0].getQualifiedName();
249-
if (superclassName != null && superclassName.length() > 0) {
250-
superclassName = assureQualifiedName(removeJavaLang(superclassName));
251-
buffer.append(", null, ");
252-
buffer.append(superclassName);
247+
// arg4 is the superclass
248+
// arg5 is the superinterface
249+
// (only one or the other is ever non-null)
250+
if (binding.getSuperclass() != null) {
251+
String superclassName = getSuperclassName(binding);
252+
if (superclassName != null) {
253+
// superclass, not superinterface
254+
buffer.append(", ");
255+
buffer.append(superclassName);
256+
} else {
257+
// no superclass other than Object -- look for
258+
// superInterface
259+
ITypeBinding[] declaredTypes = binding.getInterfaces();
260+
if (declaredTypes != null && declaredTypes.length > 0) {
261+
String superinterfaceName = declaredTypes[0].getQualifiedName();
262+
if (superinterfaceName != null && superinterfaceName.length() > 0) {
263+
buffer.append(", null, ");
264+
buffer.append(assureQualifiedName(removeJavaLang(superinterfaceName)));
265+
}
253266
}
254267
}
255268
}
256269
}
270+
// close decorateAsClass
257271
buffer.append(");\r\n");
258272

259273
String oldClassName = className;
@@ -271,8 +285,6 @@ public boolean visit(AnonymousClassDeclaration node) {
271285
}
272286
}
273287

274-
// addDefaultConstructor();
275-
276288
for (Iterator<?> iter = bodyDeclarations.iterator(); iter.hasNext();) {
277289
BodyDeclaration element = (BodyDeclaration) iter.next();
278290
if (element instanceof FieldDeclaration && isStatic(element.getModifiers()))
@@ -281,9 +293,9 @@ public boolean visit(AnonymousClassDeclaration node) {
281293

282294
typeVisitor.setClassName(oldClassName);
283295
buffer.append(staticFieldDefBuffer);
284-
285296
staticFieldDefBuffer = oldStaticDefBuffer;
286297

298+
// close the anonymous function wrapper
287299
addAnonymousFunctionWrapper(false);
288300
buffer.append(")");
289301

@@ -1187,8 +1199,8 @@ public boolean visit(MethodDeclaration node) {
11871199
// as it is too risky to do this -- lose all initialization.
11881200
@SuppressWarnings("unchecked")
11891201
List<ASTNode> statements = node.getBody().statements();
1190-
ASTNode firstStatement = statements.get(0);
1191-
if (statements.size() == 0 || !(firstStatement instanceof SuperConstructorInvocation)
1202+
ASTNode firstStatement;
1203+
if (statements.size() == 0 || !((firstStatement = statements.get(0)) instanceof SuperConstructorInvocation)
11921204
&& !(firstStatement instanceof ConstructorInvocation)) {
11931205
buffer.append("{\r\n");
11941206
IMethodBinding binding = node.resolveBinding();
@@ -1844,7 +1856,10 @@ public boolean visit(SuperFieldAccess node) {
18441856
public boolean visit(SuperMethodInvocation node) {
18451857
IMethodBinding mBinding = node.resolveMethodBinding();
18461858
String name = getJ2SName(node.getName()) + getJ2SParamQualifier(null, mBinding);
1847-
buffer.append("C$.superClazz.prototype.").append(name).append(".apply(this, arguments)");
1859+
// BH if this is a call to super.clone() and there is no superclass, or the superclass is Object,
1860+
// then we need to invoke Clazz.clone(this) directly instead of calling C$.superClazz.clone()
1861+
buffer.append("clone".equals(name) && getSuperclassName(mBinding.getDeclaringClass()) == null
1862+
? "Clazz.clone(this)" : "C$.superClazz.prototype." + name + ".apply(this, arguments)");
18481863
return false;
18491864
}
18501865

@@ -2009,21 +2024,8 @@ public void endVisit(TypeDeclaration node) {
20092024

20102025
Type superClassType = null;
20112026
if (!isInterface) {
2012-
// add superclass name
2027+
buffer.append(", ").append("" + getSuperclassName(node.resolveBinding()));
20132028
superClassType = node.getSuperclassType();
2014-
String superClassName = "null";
2015-
ITypeBinding typeBinding = node.resolveBinding();
2016-
if (typeBinding != null) {
2017-
ITypeBinding superclass = typeBinding.getSuperclass();
2018-
if (superclass != null) {
2019-
String clazzName = superclass.getQualifiedName();
2020-
clazzName = assureQualifiedName(removeJavaLang(clazzName));
2021-
if (clazzName != null && clazzName.length() != 0 && !"Object".equals(clazzName)) {
2022-
superClassName = clazzName;
2023-
}
2024-
}
2025-
}
2026-
buffer.append(", ").append(superClassName);
20272029
}
20282030
// Add superinterfaces
20292031
buffer.append(", ");
@@ -2244,6 +2246,25 @@ public void endVisit(TypeDeclaration node) {
22442246
super.endVisit(node);
22452247
}
22462248

2249+
/**
2250+
* return the superclass name, provided it is not Object or ""
2251+
*
2252+
* @param typeBinding
2253+
* @return superclass name or null
2254+
*/
2255+
private String getSuperclassName(ITypeBinding typeBinding) {
2256+
if (typeBinding != null) {
2257+
ITypeBinding superclass = typeBinding.getSuperclass();
2258+
if (superclass != null) {
2259+
String clazzName = superclass.getQualifiedName();
2260+
clazzName = assureQualifiedName(removeJavaLang(clazzName));
2261+
if (clazzName != null && clazzName.length() != 0 && !"Object".equals(clazzName))
2262+
return clazzName;
2263+
}
2264+
}
2265+
return null;
2266+
}
2267+
22472268
private void appendDefaultValue(Type type) {
22482269
if (type.isPrimitiveType()) {
22492270
PrimitiveType pType = (PrimitiveType) type;

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,6 @@ public HashSet<String> getDefinedBasePackages() {
376376
public boolean visit(PackageDeclaration node) {
377377
ASTPackageVisitor packageVisitor = ((ASTPackageVisitor) getAdaptable(ASTPackageVisitor.class));
378378
String name = "" + node.getName();
379-
System.err.println("declaring package " + name);
380379
packageVisitor.setPackageName(name);
381380
addPackage(name);
382381
return false;
@@ -394,9 +393,7 @@ private void addPackage(String name) {
394393
for (int i = defaultPackageNamesDefined.length; --i >= 0;)
395394
definedBasePackageNames.add(defaultPackageNamesDefined[i]);
396395
}
397-
if (definedBasePackageNames.add(name)) {
398-
System.err.println("adding package " + name);
399-
}
396+
definedBasePackageNames.add(name);
400397
}
401398

402399
// sgurin - fix for bug

sources/net.sf.j2s.core/src/net/sf/j2s/core/compiler/Java2ScriptCompiler.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class Java2ScriptCompiler implements IExtendedCompiler {
3737

3838
// BH: added "true".equals(getProperty(props, "j2s.compiler.allow.compression")) to ensure compression only occurs when desired
3939
static final int JSL_LEVEL = AST.JLS8; // BH can we go to JSL 8?
40+
private static boolean showJ2SSettings = true; // BH adding this
4041

4142
public void process(ICompilationUnit sourceUnit, IContainer binaryFolder) {
4243
final IProject project = binaryFolder.getProject();
@@ -145,7 +146,6 @@ public void process(ICompilationUnit sourceUnit, IContainer binaryFolder) {
145146

146147
DependencyASTVisitor dvisitor = null;
147148
String visitorID = getProperty(props, "j2s.compiler.visitor");
148-
System.err.println("j2s.compiler.visitor = " + visitorID);
149149
IExtendedVisitor extVisitor = null;
150150
if ("ASTScriptVisitor".equals(visitorID)) {
151151
dvisitor = new DependencyASTVisitor();
@@ -191,7 +191,6 @@ public void process(ICompilationUnit sourceUnit, IContainer binaryFolder) {
191191
// J2SDependencyCompiler.outputJavaScript(dvisitor, root, binFolder);
192192

193193
ASTScriptVisitor visitor = null;
194-
System.err.println("using visitor " + visitorID);
195194
if ("ASTScriptVisitor".equals(visitorID)) {
196195
visitor = new ASTScriptVisitor();
197196
} else if ("SWTScriptVisitor".equals(visitorID)) {
@@ -201,7 +200,9 @@ public void process(ICompilationUnit sourceUnit, IContainer binaryFolder) {
201200
visitor = extVisitor.getScriptVisitor();
202201
}
203202
if (visitor == null) {
204-
visitor = new SWTScriptVisitor();
203+
visitor = new ASTScriptVisitor();
204+
// BH default to ASTScriptVisitor, not SWTScriptVisitor
205+
// visitor = new SWTScriptVisitor();
205206
}
206207
}
207208
visitor.setPackageNames(dvisitor.getDefinedBasePackages());
@@ -306,7 +307,8 @@ public void process(ICompilationUnit sourceUnit, IContainer binaryFolder) {
306307

307308
private static String getProperty(Properties props, String key) {
308309
String val = props.getProperty(key);
309-
System.err.println(key + " = " + val);
310+
if (showJ2SSettings)
311+
System.err.println(key + " = " + val);
310312
return val;
311313
}
312314

@@ -477,6 +479,7 @@ public static void outputJavaScript(ASTScriptVisitor visitor, DependencyASTVisit
477479
}
478480
}
479481

482+
showJ2SSettings = false;
480483
// if (visitor instanceof SWTScriptVisitor) {
481484
// SWTScriptVisitor swtVisitor = (SWTScriptVisitor) visitor;
482485
// String removedJS = swtVisitor.getBufferRemoved().toString();

sources/net.sf.j2s.core/test/src/test/Test_Bugs.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,6 @@ public static void main(String[] args) {
8787
// class A init2
8888
// class A init
8989

90-
try {
91-
Class<?> cl;
92-
cl = Class.forName("test.Test_Bugs");
93-
cl.getConstructor(String.class, String.class).newInstance(
94-
new Object[] { "test1", "test2" });
95-
cl.getConstructor(Object[].class).newInstance(
96-
new Object[] { new Object[] { "test1", "test2" } });
97-
cl.getConstructor(String.class, String.class).newInstance("test1",
98-
"test2");
99-
cl.getConstructor().newInstance();
100-
cl.newInstance();
101-
} catch (Exception e) {
102-
// TODO Auto-generated catch block
103-
e.printStackTrace();
104-
}
10590

10691
System.out.println(getFont("f"));
10792
System.out.println(getFont("f", "y"));

sources/net.sf.j2s.core/test/src/test/Test_Class.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,30 @@
22

33
@SuppressWarnings("rawtypes")
44
class Test_Class {
5+
6+
public Test_Class() {
7+
System.out.println("defaultConstructor");
8+
}
9+
10+
public Test_Class(Object... ab) {
11+
System.out.println("a==test3 " + (ab[0] == "test3"));
12+
System.out.println("b==test4 " + (ab[1] == "test4"));
13+
14+
}
15+
public Test_Class(String a, String b) {
16+
System.out.println("a==test1 " + (a == "test1"));
17+
System.out.println("b==test2 " + (b == "test2"));
18+
}
19+
20+
public static Test_Class newInstance() {
21+
System.out.println("this is static Test_Class.newInstance()");
22+
return null;
23+
}
24+
25+
public static Test_Class newInstance(Object... objects) {
26+
System.out.println("this is static Test_Class.newInstance(Object... objects");
27+
return null;
28+
}
529

630
Class B() {
731
return null;
@@ -11,11 +35,29 @@ static class C extends Test_Class {
1135

1236
}
1337

14-
Class D() {
38+
Class C() {
1539
return C.class;
1640
}
1741

1842
public static void main(String[] args) {
43+
44+
try {
45+
Class<?> cl;
46+
cl = Class.forName("test.Test_Class");
47+
cl.getConstructor(String.class, String.class).newInstance(
48+
new Object[] { "test1", "test2" });
49+
cl.getConstructor(Object[].class).newInstance(
50+
new Object[] { new Object[] { "test3", "test4" } });
51+
cl.getConstructor(String.class, String.class).newInstance("test1",
52+
"test2");
53+
Test_Class c = (Test_Class) cl.getConstructor().newInstance();
54+
cl = c.C();
55+
cl.newInstance(); // fails in JavaScript - runs the static method instead
56+
} catch (Exception e) {
57+
// TODO Auto-generated catch block
58+
e.printStackTrace();
59+
}
60+
1961
}
2062

2163
}

0 commit comments

Comments
 (0)