Skip to content

Commit 13a7075

Browse files
committed
j2s runtime refactoring 1/2
1 parent b020b0c commit 13a7075

29 files changed

+46
-103
lines changed

sources/net.sf.j2s.core/src/net/sf/j2s/core/adapters/MethodAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ public static String translate(String className, String methodName) {
130130
private static void init() {
131131
pmMap = new HashMap<String, String>();
132132
methodSet = new HashSet<String>();
133-
register("java.lang.Class", "forName", "Clazz._4Name");
134-
register("java.lang.reflect.Array", "newInstance", "Clazz.newArray$");
133+
register("java.lang.Class", "forName", "Clazz.forName");
134+
register("java.lang.reflect.Array", "newInstance", "Clazz.array");
135135
// register("java.lang.String", "length", "length"); // BH no -- we need String to implement CharSequence. Both will be length$()
136136
// register("java.lang.CharSequence", "length", "length");//sgurin: fix for bug: CharSequence cs = "123"; cs.length();
137137
register("java.lang.String", "replace", "~replace");

sources/net.sf.j2s.core/src/net/sf/j2s/core/adapters/VariableAdapter.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,6 @@ public String getIndexedVarName(String name, int i) {
128128

129129
/**
130130
* Generated final variable list for anonymous class creation.
131-
* <ol>
132-
* <li>Generate "null" if there are no referenced final variales inside
133-
* anonymous class</li>
134-
* <li>Generate "Clazz.cloneFinals (...)" if there are referenced final
135-
* variable</li>
136-
* </ol>
137131
*
138132
* @param list
139133
* @param seperator

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,10 @@ public void setBuffer(StringBuffer buffer) {
304304

305305
protected Map<Class<?>, AbstractPluginAdapter> adapterMap = new HashMap<Class<?>, AbstractPluginAdapter>();
306306

307-
public AbstractPluginAdapter getAdaptable(Class<?> clazz) {
307+
public AbstractPluginAdapter getAdaptable(Class<?> thisClass) {
308308
try {
309-
AbstractPluginAdapter adapter = adapterMap.get(clazz);
310-
return (adapter == null ? (adapter = registerPluginVisitor((AbstractPluginAdapter) clazz.newInstance()))
309+
AbstractPluginAdapter adapter = adapterMap.get(thisClass);
310+
return (adapter == null ? (adapter = registerPluginVisitor((AbstractPluginAdapter) thisClass.newInstance()))
311311
: adapter);
312312
} catch (@SuppressWarnings("unused") InstantiationException | IllegalAccessException e) {
313313
return null;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,11 @@ private TagElement getTag(List<?> tags, String j2sKey) {
135135
* for example, for classes only:
136136
*
137137
* @j2sPrefix /-* this is from <@>j2sPrefix added outside of just before
138-
* Clazz.decorateAsClass() *-/
138+
* Clazz.newClass() *-/
139139
*
140140
*
141141
* @j2sSuffix /-* this is from <@>j2sSuffix added just after
142-
* Clazz.decorateAsClass() *-/
142+
* Clazz.newClass() *-/
143143
* @param isExtended if this is j2sXHTML or j2sXCSS
144144
*
145145
* @return true if javadoc of this sort was found and added to the buffer

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

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public boolean visit(ArrayCreation node) {
238238
ArrayInitializer inode = node.getInitializer();
239239
ITypeBinding binding = node.resolveTypeBinding();
240240
if (inode == null) {
241-
buffer.append(j2sGetArrayClass(binding, 0));
241+
buffer.append(clazzArray(binding, 0));
242242
buffer.append(", [");
243243
List<ASTNode> dim = node.dimensions();
244244
visitList(dim, ", ");
@@ -253,7 +253,7 @@ public boolean visit(ArrayCreation node) {
253253

254254
public boolean visit(ArrayInitializer node) {
255255
// as in: public String[] d = {"1", "2"};
256-
buffer.append(j2sGetArrayClass(node.resolveTypeBinding(), -1));
256+
buffer.append(clazzArray(node.resolveTypeBinding(), -1));
257257
buffer.append(", [");
258258
@SuppressWarnings("unchecked")
259259
List<ASTNode> expressions = node.expressions();
@@ -703,7 +703,7 @@ public boolean visit(InstanceofExpression node) {
703703
node.getLeftOperand().accept(this);
704704
buffer.append(", ");
705705
if (right instanceof ArrayType) {
706-
buffer.append(j2sGetArrayClass(binding, 1));
706+
buffer.append(clazzArray(binding, 1));
707707
} else {
708708
buffer.append("\"" + removeBrackets(binding.getQualifiedName()) + "\"");
709709
// right.accept(this);
@@ -1069,7 +1069,8 @@ public boolean visit(TypeLiteral node) {
10691069
// adds Integer.TYPE, Float.TYPE, etc.
10701070
buffer.append(getPrimitiveTYPE(binding.getName()));
10711071
} else if (type instanceof ArrayType) {
1072-
buffer.append(j2sGetArrayClass(binding, 1));
1072+
// int[][].class --> Clazz.array(Integer.TYPE, -2);
1073+
buffer.append(clazzArray(binding, 1));
10731074
} else {
10741075
// BH we are creating a new Class object around this class
10751076
// if it is an interface, then we explicitly add .$methodList$
@@ -1210,21 +1211,18 @@ protected static boolean isStatic(IBinding b) {
12101211
}
12111212

12121213
/**
1213-
* Add the Clazz.arrayClass$(class, ndim) call to create a faux class with
1214-
* the correct _paramType and __NDIM
12151214
*
12161215
* @param type
12171216
* @param dimFlag
1218-
* -1 : initialized depth; n > 0 uninitialized depth as
1219-
* Clazz.arrayClass$; 0: not necessary
1217+
* -1 : initialized depth; n > 0 uninitialized depth; 0: not necessary
12201218
* @return JavaScript for array creation
12211219
*/
1222-
String j2sGetArrayClass(ITypeBinding type, int dimFlag) {
1220+
private String clazzArray(ITypeBinding type, int dimFlag) {
12231221
ITypeBinding ebinding = type.getElementType();
12241222
String params = (ebinding.isPrimitive() ? getPrimitiveTYPE(ebinding.getName())
12251223
: getQualifiedStaticName(null, ebinding.getQualifiedName(), true, true, false))
1226-
+ (dimFlag == 0 ? "" : ", " + dimFlag * type.getDimensions());
1227-
return (dimFlag > 0 ? "Clazz.arrayClass$(" + params + ")" : " Clazz.newArray$(" + params);
1224+
+ (dimFlag == 0 ? "" : ", " + Math.abs(dimFlag) * type.getDimensions() * -1);
1225+
return "Clazz.array(" + params + (dimFlag > 0 ? ")" : "");
12281226
}
12291227

12301228
public static void setNoQualifiedNamePackages(String names) {
@@ -1816,8 +1814,8 @@ private boolean checkSimpleBooleanOperator(String op) {
18161814
* @return
18171815
*/
18181816
protected boolean checkStaticBinding(IVariableBinding varBinding) {
1819-
ITypeBinding declaring;
1820-
return (isStatic(varBinding) && (declaring = varBinding.getDeclaringClass()) != null
1817+
// ITypeBinding declaring;
1818+
return (isStatic(varBinding) && varBinding.getDeclaringClass() != null
18211819
//&& (!allowExtensions || !ExtendedAdapter.isHTMLClass(declaring.getQualifiedName(), false))
18221820
);
18231821
}
@@ -2190,9 +2188,7 @@ protected String getMethodNameOrArrayForDeclaration(MethodDeclaration node, IMet
21902188
* @param nodeName
21912189
* @param binding
21922190
* @param genericTypes
2193-
* TODO
21942191
* @param isMethodInvoc
2195-
* TODO
21962192
*
21972193
* @return a fully j2s-qualified method name
21982194
*/
@@ -2466,7 +2462,7 @@ static void debugListAllOverrides(ITypeBinding binding) {
24662462
*
24672463
* @param name
24682464
* @param checkPackages
2469-
* @param addName TODO
2465+
* @param addName
24702466
* @return
24712467
*/
24722468
protected String getValidFieldName$Qualifier(String name, boolean checkPackages, boolean addName) {

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

Lines changed: 21 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,6 @@
9090
// BH 8/13/2017 -- includes native code calls in System.err
9191
// BH 7/31/2017 -- extensively reworked for fully qualified method names and no SAEM
9292

93-
// DONE: type def, including inner classes and anonymous classes
94-
// DONE: fully encapsulated C$ variable
95-
// DONE: proper <init> processing
96-
// DONE: non-final variables for anonymous class definition
97-
// DONE: array handling in instanceof and reflection
98-
// DONE: String + double/float/Double/Float --> new Double/Float().toString()
99-
// TODO: Q: Good assumption that generic parameterization can be ignored? put<K,V> vs put<K>?
10093
/*
10194
*
10295
@@ -110,42 +103,6 @@ interface Editable {
110103
class EditDialog extends Dialog implements AdjustmentListener, ActionListener, ItemListener {
111104
...
112105
113-
TODO #16 when an inner public class is called by another class using instanceOf, that inner class becomes an optional load.
114-
but optional loads must still be loaded, and unless declared in package.js, J2S will look for xxx.xxx.Outer/Inner.js
115-
because the inner classes are not fully declared.
116-
117-
Solution is to switch to requiring the outer class, not the inner class:
118-
119-
@J2SRequireImport(NumberFormat.class)
120-
@J2SIgnoreImport(NumberFormat.Field.class)
121-
public class NumberFormatter extends InternationalFormatter...
122-
123-
124-
125-
TODO #14 in java.awt.image.Raster, we have a static block that
126-
creates new Objects. In that case, we need to add the annotation:
127-
128-
@J2SRequireImport({ jsjava.awt.image.SinglePixelPackedSampleModel.class, jssun.awt.image.IntegerInterleavedRaster.class, jssun.awt.image.ByteInterleavedRaster.class })
129-
130-
131-
TODO #12 Inner classes must not call other inner classes defined after them in a file.
132-
This showed up in java.awt.geom.Path2D.Float.CopyIterator, which extends
133-
java.awt.geom.Path2D.Iterator. Since the Iterator is in the code after CopyIterator,
134-
the reference to java.awt.geom.Path2D.Iterator in
135-
136-
c$ = Clazz.decorateAsClass (function () {
137-
this.floatCoords = null;
138-
Clazz.instantialize (this, arguments);
139-
}, java.awt.geom.Path2D.Float, "CopyIterator", java.awt.geom.Path2D.Iterator);
140-
141-
is null, and then CopyIterator does not extend Iterator.
142-
143-
TODO #4 @J2SRequireImport({jsjava.util.PropertyResourceBundle.class})
144-
145-
is required for public abstract class ResourceBundle because the inner class
146-
ResourceBundle.Control requires it, but for some reason it is not included in the
147-
MUST list in the Clazz.load() call.
148-
149106
150107
*/
151108

@@ -247,7 +204,7 @@ public boolean visit(PackageDeclaration node) {
247204
if (isBasePackage()) {
248205
buffer.append(name);
249206
} else {
250-
buffer.append("Clazz.newPackage$(\"").append(name).append("\")");
207+
buffer.append("Clazz.newPackage(\"").append(name).append("\")");
251208
}
252209
buffer.append(",I$=[];\r\n");
253210
return false;
@@ -370,22 +327,22 @@ public boolean visit(ClassInstanceCreation node) {
370327
(constructorBinding == null ? null : constructorBinding.getMethodDeclaration()), null);
371328
return false;
372329
}
330+
String className = TypeAdapter.getTypeStringName(node.getType());
331+
String fqName = getShortenedQualifiedName(className);
332+
if ("Object".equals(fqName)) {
333+
buffer.append(" Clazz.new()");
334+
return false;
335+
}
373336
String prefix = null, postfix = null;
374337
IMethodBinding methodDeclaration = null;
375338
IMethodBinding constructorBinding = node.resolveConstructorBinding();
376-
String className = TypeAdapter.getTypeStringName(node.getType());
377-
String fqName = getShortenedQualifiedName(className);
378339
if (constructorBinding != null) {
379340
methodDeclaration = constructorBinding.getMethodDeclaration();
380341
}
381342
boolean isDefault = false;
382343
if ("String".equals(fqName)) {
383344
// special treatment for String -- see j2sSwingJS.js
384345
buffer.append(" String.instantialize(");
385-
} else if ("Object".equals(fqName)) {
386-
// For discussion, please visit
387-
// http://groups.google.com/group/java2script/browse_thread/thread/3d6deb9c3c0a0cda
388-
buffer.append(" new Clazz._O("); // BH removing window.JavaObject
389346
} else if (noConstructorNames.indexOf(fqName) >= 0) {
390347
// look out for java.lang.Integer and the like -- just pass it
391348
// directly
@@ -608,7 +565,7 @@ public boolean visit(MethodDeclaration node) {
608565
// names here
609566
if (name.equals("'main'"))
610567
addApplication();
611-
buffer.append("\r\nClazz.newMethod$(C$, ").append(name).append(", function (");
568+
buffer.append("\r\nClazz.newMeth(C$, ").append(name).append(", function (");
612569
@SuppressWarnings("unchecked")
613570
List<ASTNode> parameters = node.parameters();
614571
visitList(parameters, ", ");
@@ -721,7 +678,7 @@ public boolean visit(MethodInvocation node) {
721678
if (MethodAdapter.isMethodRegistered(methodName)) {
722679
String j2sName = MethodAdapter.translate(className, methodName);
723680
if (j2sName != null) {
724-
// Array.newInstance --> Clazz.newArray$
681+
// Array.newInstance --> Clazz.array
725682
if (j2sName.startsWith("Clazz.")) {
726683
buffer.setLength(pt);
727684
buffer.append(j2sName);
@@ -1057,7 +1014,7 @@ private void addCallInit() {
10571014
}
10581015

10591016
/**
1060-
* Add Clazz.newInterface$(...) or Clazz.newClass$(...) for all classes and
1017+
* Add Clazz.newInterface(...) or Clazz.newClass(...) for all classes and
10611018
* interfaces, including Enum and anonymous.
10621019
*
10631020
* If this is an inner class, then iterate, just adding its definition to
@@ -1128,7 +1085,7 @@ private boolean addClassOrInterface(ASTNode node, ITypeBinding binding, List<?>
11281085

11291086
// begin the class or interface definition
11301087

1131-
buffer.append("var C$=" + (isInterface ? "Clazz.newInterface$(" : "Clazz.newClass$("));
1088+
buffer.append("var C$=" + (isInterface ? "Clazz.newInterface(" : "Clazz.newClass("));
11321089

11331090
// arg1 is the package name
11341091
// arg2 is the full class name in quotes
@@ -1170,7 +1127,7 @@ private boolean addClassOrInterface(ASTNode node, ITypeBinding binding, List<?>
11701127
buffer.append(", ");
11711128
if (isAnonymous) {
11721129
if (!(parent instanceof EnumConstantDeclaration))
1173-
func = "function(){Clazz.newInstance$(this, arguments[0],1,C$);}";
1130+
func = "function(){Clazz.newInstance(this, arguments[0],1,C$);}";
11741131
superclassName = "" + getSuperclassNameQualified(binding);
11751132
ITypeBinding[] declaredTypes = binding.getInterfaces();
11761133
if (declaredTypes != null && declaredTypes.length > 0) {
@@ -1210,7 +1167,7 @@ private boolean addClassOrInterface(ASTNode node, ITypeBinding binding, List<?>
12101167
// directly by the
12111168
// user using new Foo()
12121169
if (!isInterface) {
1213-
buffer.append("Clazz.newInstance$(this, arguments")
1170+
buffer.append("Clazz.newInstance(this, arguments")
12141171
.append(isTopLevel ? ",0" : "[0]," + !isStatic(binding)).append(",C$);\r\n");
12151172
}
12161173
buffer.append("}");
@@ -1387,7 +1344,7 @@ private boolean addClassOrInterface(ASTNode node, ITypeBinding binding, List<?>
13871344
init0Buffer = new StringBuffer();
13881345

13891346
int len = buffer.length();
1390-
buffer.append("\r\nClazz.newMethod$(C$, '$init$', function () {\r\n");
1347+
buffer.append("\r\nClazz.newMeth(C$, '$init$', function () {\r\n");
13911348
// we include all field definitions here and all nonstatic
13921349
// initializers
13931350
for (Iterator<?> iter = bodyDeclarations.iterator(); iter.hasNext();) {
@@ -1405,7 +1362,7 @@ private boolean addClassOrInterface(ASTNode node, ITypeBinding binding, List<?>
14051362
if (init0Buffer.length() > 0) {
14061363
String buf = buffer.substring(len);
14071364
buffer.setLength(len);
1408-
buffer.append("\r\nClazz.newMethod$(C$, '$init0$', function () {\r\n");
1365+
buffer.append("\r\nClazz.newMeth(C$, '$init0$', function () {\r\n");
14091366
buffer.append("var c;if((c = C$.superClazz) && (c = c.$init0$))c.apply(this);\r\n");
14101367
buffer.append(init0Buffer);
14111368
buffer.append("}, 1);\r\n");
@@ -1486,7 +1443,7 @@ private boolean addClassOrInterface(ASTNode node, ITypeBinding binding, List<?>
14861443
}
14871444

14881445
/**
1489-
* For Clazz.newClazz$ we want an array if there is an inner class
1446+
* For Clazz.newClass$ we want an array if there is an inner class
14901447
* so that the outer class is guarranteed to be loaded first.
14911448
*
14921449
* @param className
@@ -1518,7 +1475,7 @@ private void addDefaultConstructor() {
15181475
if (haveDefaultConstructor) {
15191476
haveDefaultConstructor = false;
15201477
} else {
1521-
buffer.append("\r\nClazz.newMethod$(C$);\r\n");
1478+
buffer.append("\r\nClazz.newMeth(C$);\r\n");
15221479
}
15231480
}
15241481

@@ -1540,7 +1497,7 @@ private void addEnumConstants(List<?> constants) {
15401497
anonName = getAnonymousName(anonDeclare.resolveBinding());
15411498
buffer.append("\r\n");
15421499
}
1543-
buffer.append("Clazz.newEnumConst$(vals, ").append(getJ2SQualifiedName("C$.c$", null, binding, null, false))
1500+
buffer.append("Clazz.newEnumConst(vals, ").append(getJ2SQualifiedName("C$.c$", null, binding, null, false))
15441501
.append(", \"");
15451502
enumConst.getName().accept(this);
15461503
buffer.append("\", " + i);
@@ -1549,10 +1506,10 @@ private void addEnumConstants(List<?> constants) {
15491506
buffer.append(", ").append(anonName);
15501507
buffer.append(");\r\n");
15511508
}
1552-
buffer.append("Clazz.newMethod$(C$, 'values', function() { return vals }, 1);\r\n");
1509+
buffer.append("Clazz.newMeth(C$, 'values', function() { return vals }, 1);\r\n");
15531510
// this next just ensures we have the valueOf() method in Enum if it
15541511
// is not already there.
1555-
buffer.append("Clazz.newMethod$(Enum, 'valueOf$Class$S', function(cl, name) { return cl[name] }, 1);\r\n");
1512+
buffer.append("Clazz.newMeth(Enum, 'valueOf$Class$S', function(cl, name) { return cl[name] }, 1);\r\n");
15561513
}
15571514

15581515
/**
@@ -1602,7 +1559,7 @@ private boolean addFieldDeclaration(FieldDeclaration field, int mode) {
16021559
buffer.append(";\r\n");
16031560
//
16041561
// $clinit$ -- statics; once only
1605-
// $init0$ -- from within Clazz.newInstance$, before any
1562+
// $init0$ -- from within Clazz.newInstance, before any
16061563
// constructors
16071564
// $init$ -- from the constructor, just after any super()
16081565
// call or whenever there is no this() call

sources/net.sf.j2s.java.core/src/java/lang/Class.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ private static Class<?> forName0(String name, boolean initialize, ClassLoader lo
257257
/**
258258
* @j2sNative
259259
*
260-
* return Clazz._4Name(name);
260+
* return Clazz.forName(name, initialize, loader);
261261
*/
262262
{
263263
return null;

sources/net.sf.j2s.java.core/src/java/lang/reflect/Constructor.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,6 @@ public int hashCode() {
273273
* if an exception was thrown by the invoked constructor
274274
* @see java.lang.reflect.AccessibleObject
275275
*
276-
* @j2sNative
277-
* var instance = new this.clazz (Clazz.inheritArgs);
278-
* Clazz.instantialize (instance, args);
279-
* return instance;
280276
*/
281277
public T newInstance(Object... args) throws InstantiationException,
282278
IllegalAccessException, IllegalArgumentException,

sources/net.sf.j2s.java.core/src/java/lang/reflect/Proxy.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -678,14 +678,14 @@ private static Class defineClass0(ClassLoader loader, String name, Class<?>[] in
678678
/**
679679
* @j2sNative
680680
*
681-
* cl = Clazz.getClass(Clazz.newClass$(java.lang.reflect, name, null, 'java.lang.reflect.Proxy'));
681+
* cl = Clazz.getClass(Clazz.newClass(java.lang.reflect, name, null, 'java.lang.reflect.Proxy'));
682682
*
683683
* var cl$ = cl.$clazz$;
684684
*
685685
* cl$.$clinit$ = function() {Clazz.load(cl$, 1);};
686-
* Clazz.newMethod$(cl$, '$init$', function () {}, 1);
687-
* Clazz.newMethod$(cl$, '$init0$', function () {}, 1);
688-
* Clazz.newMethod$(cl$, "c$$reflect_InvocationHandler", function(h) {
686+
* Clazz.newMeth(cl$, '$init$', function () {}, 1);
687+
* Clazz.newMeth(cl$, '$init0$', function () {}, 1);
688+
* Clazz.newMeth(cl$, "c$$reflect_InvocationHandler", function(h) {
689689
* cl$.superClazz.c$$reflect_InvocationHandler.apply(this, [h]);
690690
* cl$.$init$.apply(this);
691691
* }, 1);

0 commit comments

Comments
 (0)