Skip to content

Commit 8add3ef

Browse files
committed
3.2.6 upgrade and fixes for reflection, primarily
1 parent 48ecc93 commit 8add3ef

39 files changed

+13437
-5487
lines changed
196 KB
Binary file not shown.
206 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20191221191910
1+
20191222231808
196 KB
Binary file not shown.
206 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20191221191910
1+
20191222231808

sources/net.sf.j2s.core/src/net/sf/j2s/core/Java2ScriptVisitor.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,8 +1883,7 @@ private void appendClinit() {
18831883
* 'l' (local),
18841884
* 'm' (LambdaExpression),
18851885
* '@' (_at_interface AnnotationType),
1886-
* or 'c' (standard
1887-
* class)
1886+
* or 'c' (standard class)
18881887
* @return localName
18891888
*/
18901889
@SuppressWarnings({ "null", "unchecked" })
@@ -1959,6 +1958,7 @@ private boolean addClassOrInterface(ASTNode node, ITypeBinding binding, List<?>
19591958
}
19601959
ITypeBinding oldBinding = null;
19611960
String oldShortClassName = null, this$0Name0 = null, finalShortClassName, finalPackageName;
1961+
List<ClassAnnotation> oldAnnotations = null;
19621962
if (isTopLevel) {
19631963
String javaName = binding.getName();
19641964
appendElementKey(javaName);
@@ -1969,6 +1969,8 @@ private boolean addClassOrInterface(ASTNode node, ITypeBinding binding, List<?>
19691969
if (isAnonymous) {
19701970
oldShortClassName = class_shortName;
19711971
oldBinding = class_typeBinding;
1972+
oldAnnotations = class_annotations;
1973+
class_annotations = null;
19721974
// anonymous classes reference their package, not their outer class in
19731975
// Clazz.newClass, so clazz.$this$0 is not assigned.
19741976
this$0Name0 = this$0Name;
@@ -2039,6 +2041,7 @@ private boolean addClassOrInterface(ASTNode node, ITypeBinding binding, List<?>
20392041
buffer.append(", ");
20402042
List<IMethodBinding> unqualifiedMethods = getUnqualifiedMethods(binding, null);
20412043
List<AbstractTypeDeclaration> innerClasses = new ArrayList<>();
2044+
String innerTypes = "";
20422045
if (isAnonymous) {
20432046
if (!(parent instanceof EnumConstantDeclaration))
20442047
func = "function(){Clazz.newInstance(this, arguments[0],1,C$);}";
@@ -2058,7 +2061,11 @@ private boolean addClassOrInterface(ASTNode node, ITypeBinding binding, List<?>
20582061
} else {
20592062
for (Iterator<?> iter = bodyDeclarations.iterator(); iter.hasNext();) {
20602063
BodyDeclaration bd = (BodyDeclaration) iter.next();
2061-
if (bd instanceof TypeDeclaration || bd instanceof EnumDeclaration || bd instanceof AnnotationTypeDeclaration) {
2064+
if (bd instanceof AbstractTypeDeclaration) {
2065+
//TypeDeclaration || EnumDeclaration || AnnotationTypeDeclaration)
2066+
ITypeBinding b = ((AbstractTypeDeclaration)bd).resolveBinding();
2067+
String s = j2sNonPrimitiveName(b, false);
2068+
innerTypes += ",['" + s.substring(s.lastIndexOf(".") + 1) + "'," + b.getModifiers() + "]";
20622069
innerClasses.add((AbstractTypeDeclaration) bd);
20632070
}
20642071
}
@@ -2181,6 +2188,10 @@ private boolean addClassOrInterface(ASTNode node, ITypeBinding binding, List<?>
21812188

21822189
buffer.append(");\n");
21832190

2191+
if (innerTypes.length() > 0) {
2192+
buffer.append("C$.$classes$=[" + innerTypes.substring(1) + "];\n");
2193+
}
2194+
21842195
// add the Java8 compatibility local variable $o$
21852196

21862197
// also add the local var p$ short for C$.prototype if we have any
@@ -2434,6 +2445,7 @@ && checkAnnotations(element, CHECK_J2S_IGNORE_AND_ANNOTATIONS)) {
24342445
buffer.append(")");
24352446
this$0Name = this$0Name0;
24362447
setClassAndBinding(oldShortClassName, oldBinding);
2448+
class_annotations = oldAnnotations;
24372449
}
24382450
}
24392451
return isStatic;
196 KB
Binary file not shown.

sources/net.sf.j2s.java.core/doc/Differences.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ ever be overridden by subclasses. As in java.lang.Thread.java:
3232
----------------------------------
3333

3434

35+
updated 12/22/19 -- additional issues
3536
updated 11/03/19 -- adds information about File.exists() and points to src/javajs/async
3637
updated 10/26/19 -- adds information about File.createTempFile()
3738
updated 8/16/19 -- minor typos and added summary paragraph
@@ -1052,3 +1053,18 @@ may or may not be present with the JAR or class files. Use class files at your o
10521053

10531054
Bob Hanson
10541055
2019.08.16
1056+
1057+
1058+
Additional Issues
1059+
-----------------
1060+
1061+
Annotation is working for classes, methods, and fields (12/22/19). Method reflection, however,
1062+
is limited. Interfaces do not expose their methods, as the transpiler does not actually transpile
1063+
the interfaces themselves. And method reflection only includes annotated methods.
1064+
1065+
java.util.concurrent is not fully elaborated. This package is rewritten to not actually use the
1066+
memory handling capabilities of concurrency, which JavaScript does not have access to.
1067+
1068+
System.getProperties() just returns a minimal set of properties.
1069+
1070+

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

Lines changed: 91 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.util.Arrays;
4343
import java.util.Enumeration;
4444
import java.util.HashMap;
45+
import java.util.HashSet;
4546
import java.util.LinkedHashMap;
4647
import java.util.List;
4748
import java.util.Map;
@@ -450,6 +451,8 @@ public boolean isInstance(Object obj) {
450451
*/
451452
@SuppressWarnings("unused")
452453
public boolean isAssignableFrom(Class<?> cls) {
454+
if (cls == null)
455+
return false;
453456
Object a = cls.$clazz$;
454457
Object me = $clazz$;
455458
return /** @j2sNative Clazz.instanceOf(a, me) || */ false;
@@ -689,6 +692,8 @@ public String getName() {
689692
// cache the name to reduce the number of calls into the VM
690693
private transient String name;
691694
private Field[] fields;
695+
private Class<?>[] implementz;
696+
private int modifiers = -1;
692697

693698
private String getName0() {
694699
String code = "";
@@ -928,23 +933,22 @@ public Class<? super T> getSuperclass() {
928933
// }
929934

930935
/**
931-
* Determines the interfaces implemented by the class or interface
932-
* represented by this object.
936+
* Determines the interfaces implemented by the class or interface represented
937+
* by this object.
933938
*
934939
* <p>
935-
* If this object represents a class, the return value is an array
936-
* containing objects representing all interfaces implemented by the class.
937-
* The order of the interface objects in the array corresponds to the order
938-
* of the interface names in the {@code implements} clause of the
939-
* declaration of the class represented by this object. For example, given
940-
* the declaration: <blockquote> {@code class Shimmer implements FloorWax,
941-
* DessertTopping { ... }} </blockquote> suppose the value of {@code s} is
942-
* an instance of {@code Shimmer}; the value of the expression: <blockquote>
943-
* {@code s.getClass().getInterfaces()[0]} </blockquote> is the
944-
* {@code Class} object that represents interface {@code FloorWax}; and the
945-
* value of: <blockquote> {@code s.getClass().getInterfaces()[1]}
946-
* </blockquote> is the {@code Class} object that represents interface
947-
* {@code DessertTopping}.
940+
* If this object represents a class, the return value is an array containing
941+
* objects representing all interfaces implemented by the class. The order of
942+
* the interface objects in the array corresponds to the order of the interface
943+
* names in the {@code implements} clause of the declaration of the class
944+
* represented by this object. For example, given the declaration: <blockquote>
945+
* {@code class Shimmer implements FloorWax, DessertTopping { ... }}
946+
* </blockquote> suppose the value of {@code s} is an instance of
947+
* {@code Shimmer}; the value of the expression: <blockquote>
948+
* {@code s.getClass().getInterfaces()[0]} </blockquote> is the {@code Class}
949+
* object that represents interface {@code FloorWax}; and the value of:
950+
* <blockquote> {@code s.getClass().getInterfaces()[1]} </blockquote> is the
951+
* {@code Class} object that represents interface {@code DessertTopping}.
948952
*
949953
* <p>
950954
* If this object represents an interface, the array contains objects
@@ -954,19 +958,33 @@ public Class<? super T> getSuperclass() {
954958
* represented by this object.
955959
*
956960
* <p>
957-
* If this object represents a class or interface that implements no
958-
* interfaces, the method returns an array of length 0.
961+
* If this object represents a class or interface that implements no interfaces,
962+
* the method returns an array of length 0.
959963
*
960964
* <p>
961965
* If this object represents a primitive type or void, the method returns an
962966
* array of length 0.
963967
*
964968
* @return an array of interfaces implemented by this class.
965969
*/
970+
@SuppressWarnings("unused")
966971
public Class<?>[] getInterfaces() {
967-
JSUtil.notImplemented(null);
968-
return new Class<?>[0];
969-
}
972+
if (implementz == null) {
973+
Class<?>[] a = new Class<?>[0];
974+
Object me = $clazz$;
975+
Class<?>[] list = /** @j2sNative me.implementz || */null;
976+
if (list != null) {
977+
for (int i = 0, n = list.length; i < n; i++) {
978+
/**
979+
* @j2sNative a.push(Clazz.getClass(list[i]));
980+
*/
981+
}
982+
}
983+
implementz = a;
984+
}
985+
return implementz;
986+
}
987+
970988

971989
// /**
972990
// * Returns the {@code Type}s representing the interfaces directly
@@ -1065,11 +1083,18 @@ public Class<?> getComponentType() {
10651083
* @see java.lang.reflect.Modifier
10661084
* @since JDK1.1
10671085
*/
1068-
@SuppressWarnings("unused")
10691086
public int getModifiers() {
1070-
return Modifier.PUBLIC
1087+
return (modifiers >= 0 ? modifiers : Modifier.PUBLIC
10711088
| (isEnum() ? ENUM : isInterface() ? Modifier.INTERFACE : 0)
1072-
| (isAnnotation() ? ANNOTATION : 0);
1089+
| (isAnnotation() ? ANNOTATION : 0));
1090+
}
1091+
1092+
/**
1093+
* From AnnotationParser.JSAnnotationObject
1094+
* @param m
1095+
*/
1096+
public void _setModifiers(int m) {
1097+
modifiers = m;
10731098
}
10741099

10751100
/**
@@ -1542,33 +1567,36 @@ private boolean isLocalOrAnonymousClass() {
15421567
* @since JDK1.1
15431568
*/
15441569
public Class<?>[] getClasses() {
1545-
return null;
1546-
// // be very careful not to change the stack depth of this
1547-
// // checkMemberAccess call for security reasons
1548-
// // see java.lang.SecurityManager.checkMemberAccess
1549-
//// checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
1550-
//
1551-
// // Privileged so this implementation can look at DECLARED classes,
1552-
// // something the caller might not have privilege to do. The code here
1553-
// // is allowed to look at DECLARED classes because (1) it does not hand
1554-
// // out anything other than public members and (2) public member access
1555-
// // has already been ok'd by the SecurityManager.
1556-
//
1557-
// Class[] result = (Class[]) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
1570+
// be very careful not to change the stack depth of this
1571+
// checkMemberAccess call for security reasons
1572+
// see java.lang.SecurityManager.checkMemberAccess
1573+
// checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
1574+
1575+
// Privileged so this implementation can look at DECLARED classes,
1576+
// something the caller might not have privilege to do. The code here
1577+
// is allowed to look at DECLARED classes because (1) it does not hand
1578+
// out anything other than public members and (2) public member access
1579+
// has already been ok'd by the SecurityManager.
1580+
1581+
// Class[] result;
1582+
// (Class[]) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
15581583
// public Object run() {
1559-
// java.util.List<Class> list = new java.util.ArrayList();
1560-
// Class currentClass = Class.this;
1561-
// while (currentClass != null) {
1562-
// Class[] members = currentClass.getDeclaredClasses();
1563-
// for (int i = 0; i < members.length; i++) {
1564-
// if (Modifier.isPublic(members[i].getModifiers())) {
1565-
// list.add(members[i]);
1566-
// }
1567-
// }
1568-
// currentClass = currentClass.getSuperclass();
1569-
// }
1584+
java.util.List<Class> list = new java.util.ArrayList();
1585+
Class currentClass = this;
1586+
while (currentClass != null) {
1587+
Class[] members = currentClass.getDeclaredClasses();
1588+
for (int i = 0; i < members.length; i++) {
1589+
//if (Modifier.isPublic(members[i].getModifiers())) {
1590+
// public only
1591+
list.add(members[i]);
1592+
//}
1593+
}
1594+
currentClass = currentClass.getSuperclass();
1595+
}
15701596
// Class[] empty = {};
1571-
// return list.toArray(empty);
1597+
// result =
1598+
1599+
return list.toArray(new Class[list.size()]);
15721600
// }
15731601
// });
15741602
//
@@ -2044,12 +2072,11 @@ public Constructor<T> getConstructor(Class<?>... parameterTypes) throws NoSuchMe
20442072
* @since JDK1.1
20452073
*/
20462074
public Class<?>[] getDeclaredClasses() throws SecurityException {
2047-
return getClasses();
20482075
// // be very careful not to change the stack depth of this
20492076
// // checkMemberAccess call for security reasons
20502077
// // see java.lang.SecurityManager.checkMemberAccess
20512078
//// checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
2052-
// return getDeclaredClasses0();
2079+
return getDeclaredClasses0();
20532080
}
20542081

20552082
/**
@@ -2179,6 +2206,9 @@ public Method[] getDeclaredMethods() throws SecurityException {
21792206
*/
21802207
public Constructor<?>[] getDeclaredConstructors() throws SecurityException {
21812208
return getConstructors();
2209+
2210+
// TODO
2211+
21822212
// // be very careful not to change the stack depth of this
21832213
// // checkMemberAccess call for security reasons
21842214
// // see java.lang.SecurityManager.checkMemberAccess
@@ -3021,12 +3051,11 @@ private Method[] privateGetPublicMethods() {
30213051
* @j2sNative
30223052
*
30233053
*
3024-
* var p = this.$clazz$.prototype;
3054+
* var p = this.$clazz$.prototype;
30253055
*
3026-
* for (attr in p) { o = p[attr]; if (o.exName && typeof
3027-
* o == "function" && o.exName && !o.__CLASS_NAME__ &&
3028-
* o != this.$clazz$[attr] && o.exClazz == this.$clazz$)
3029-
* { // there are polynormical methods.
3056+
* for (attr in p) { o = p[attr]; if (typeof o == "function" &&
3057+
* o.exName && !o.__CLASS_NAME__ && o != this.$clazz$[attr] &&
3058+
* o.exClazz == this.$clazz$) { // there are polynormical methods.
30303059
*/
30313060

30323061
Method m = new Method(this, attr, NO_PARAMETERS, Void.class, NO_PARAMETERS, Modifier.PUBLIC);
@@ -3277,7 +3306,10 @@ private static boolean arrayContentsEq(Object[] a1, Object[] a2) {
32773306
//
32783307
// private native Constructor[] getDeclaredConstructors0(boolean publicOnly);
32793308
//
3280-
// private native Class[] getDeclaredClasses0();
3309+
private //native
3310+
Class[] getDeclaredClasses0() {
3311+
return AnnotationParser.JSAnnotationObject.getDeclaredClasses(this.$clazz$);
3312+
}
32813313
//
32823314
// private static String argumentTypesToString(Class[] argTypes) {
32833315
// StringBuilder buf = new StringBuilder();
@@ -3705,10 +3737,12 @@ public boolean equals(Object o) {
37053737
/**
37063738
* @j2sNative
37073739
*
3708-
* return o.__CLASS_NAME__ == "java.lang.Class" && o.$clazz$ == this.$clazz$;
3740+
* return o && o.__CLASS_NAME__ == "java.lang.Class" && o.$clazz$ == this.$clazz$;
37093741
*
37103742
*/
3743+
{
37113744
return false;
3745+
}
37123746
}
37133747

37143748
/**

0 commit comments

Comments
 (0)