Skip to content

Commit 641e226

Browse files
hansonrhansonr
authored andcommitted
Adds java.lang.reflect.Field, fixes Proxy, Class, and Method
1 parent 05e74b7 commit 641e226

File tree

11 files changed

+866
-641
lines changed

11 files changed

+866
-641
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2018_07_22__14_28_50
1+
20180723085606
2.73 KB
Binary file not shown.

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

Lines changed: 111 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ public String getName() {
646646

647647
// cache the name to reduce the number of calls into the VM
648648
private transient String name;
649+
private Field[] fields;
649650

650651
private String getName0() {
651652
String code = "";
@@ -1534,12 +1535,15 @@ public Class<?>[] getClasses() {
15341535
* @since JDK1.1
15351536
*/
15361537
public Field[] getFields() throws SecurityException {
1537-
return null;
1538-
// // be very careful not to change the stack depth of this
1539-
// // checkMemberAccess call for security reasons
1540-
// // see java.lang.SecurityManager.checkMemberAccess
1541-
//// checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
1542-
// return copyFields(privateGetPublicFields(null));
1538+
if (fields != null)
1539+
return fields;
1540+
fields = new Field[0];
1541+
int _static = Modifier.STATIC;
1542+
Object cl = /** @j2sNative this.$clazz$ || */null;
1543+
Object proto = /** @j2sNative cl.prototype || */null;
1544+
addFields(proto, this.fields, 0);
1545+
addFields(cl, this.fields, _static);
1546+
return fields;
15431547
}
15441548

15451549
/**
@@ -1686,10 +1690,13 @@ public Constructor<?>[] getConstructors() throws SecurityException {
16861690
* @since JDK1.1
16871691
*/
16881692
public Field getField(String name) throws NoSuchFieldException, SecurityException {
1689-
// TODO
1690-
{
1691-
return null;
1693+
getFields();
1694+
for (int i = fields.length; --i >= 0;) {
1695+
if (fields[i].jsName == name)
1696+
return fields[i];
16921697
}
1698+
throw new NoSuchFieldException("field " + name);
1699+
16931700
// // be very careful not to change the stack depth of this
16941701
// // checkMemberAccess call for security reasons
16951702
// // see java.lang.SecurityManager.checkMemberAccess
@@ -1701,6 +1708,46 @@ public Field getField(String name) throws NoSuchFieldException, SecurityExceptio
17011708
// return field;
17021709
}
17031710

1711+
private void addFields(Object c, Field[] f, int modifiers) {
1712+
String m = null;
1713+
/**
1714+
* @j2sNative
1715+
*
1716+
* for (m in c) {
1717+
* if (!modifiers && this.$clazz$[m])
1718+
* continue;
1719+
* if (this.excludeField$S(m)) continue;
1720+
* var o = c[m];
1721+
* switch (typeof o) {
1722+
* case "object": if (o.__CLASS_NAME__) continue;
1723+
* case "number": case "boolean": case "string":
1724+
*/
1725+
1726+
addField(f, m, modifiers);
1727+
1728+
/**
1729+
* @j2sNative
1730+
* break;
1731+
* }
1732+
* }
1733+
*/
1734+
1735+
}
1736+
1737+
private boolean excludeField(String name) {
1738+
return (name == "prototype" || name.startsWith("__"));
1739+
}
1740+
1741+
private void addField(Field[] fields, String m, int modifiers) {
1742+
1743+
@SuppressWarnings("unused")
1744+
Field f = new Field(this, m, modifiers);
1745+
/**
1746+
* @j2sNative
1747+
*
1748+
* fields.push(f);
1749+
*/
1750+
}
17041751
/**
17051752
* Returns a {@code Method} object that reflects the specified public member
17061753
* method of the class or interface represented by this {@code Class}
@@ -1781,15 +1828,32 @@ public Method getMethod(String name, Class<?>... paramTypes) throws NoSuchMethod
17811828
// see java.lang.SecurityManager.checkMemberAccess
17821829
// checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
17831830

1784-
/**
1785-
* @j2sNative
1786-
*
1787-
* return Clazz.new_(java.lang.reflect.Method.c$$Class$S$ClassA$Class$ClassA$I, [this, name,
1788-
* paramTypes, java.lang.Void, [], 0]);
1789-
*/
1790-
{
1791-
return null;
1831+
// note that we cannot check the method at this time, as it could be an interface,
1832+
// and interfaces will not have elaborated methods.
1833+
1834+
Method m = new Method(this, name, paramTypes, null, null, 0);
1835+
if (!isInterface()) {
1836+
Object o = null;
1837+
String qname = name + argumentTypesToString(paramTypes);
1838+
/**
1839+
* @j2sNative
1840+
*
1841+
* o = this.$clazz$;
1842+
* o = o[qname] || o.prototype && o.prototype[qname];
1843+
*/
1844+
if (o == null)
1845+
throw new NoSuchMethodException(getName() + "." + qname);
17921846
}
1847+
return m;
1848+
// /**
1849+
// * @j2sNative
1850+
// *
1851+
// * return Clazz.new_(Clazz.load('java.lang.reflect.Method').c$$Class$S$ClassA$Class$ClassA$I, [this, name,
1852+
// * paramTypes, java.lang.Void, [], 0]);
1853+
// */
1854+
// {
1855+
// return null;
1856+
// }
17931857
//
17941858
//
17951859
//
@@ -3094,6 +3158,20 @@ private static boolean arrayContentsEq(Object[] a1, Object[] a2) {
30943158
// return buf.toString();
30953159
// }
30963160

3161+
/**
3162+
* Java2Script style here
3163+
*
3164+
* @param parameterTypes
3165+
* @return
3166+
*/
3167+
public static String argumentTypesToString(Class<?>[] parameterTypes) {
3168+
String s = "";
3169+
if (parameterTypes != null)
3170+
for (int i = 0; i < parameterTypes.length; i++)
3171+
s += "$" + /** @j2sNative Clazz._getParamCode(parameterTypes[i]) || */null;
3172+
return s;
3173+
}
3174+
30973175
// /** use serialVersionUID from JDK 1.1 for interoperability */
30983176
private static final long serialVersionUID = 3206093459760846163L;
30993177
//
@@ -3452,4 +3530,20 @@ public boolean equals(Object o) {
34523530
*/
34533531
return false;
34543532
}
3533+
3534+
/**
3535+
* A SwingJS method for Constructor and Method
3536+
*
3537+
* @param parameterTypes
3538+
* @param args
3539+
* @return
3540+
*/
3541+
public static Object[] getArgumentArray(Class<?>[] types, Object[] args, boolean isProxy) {
3542+
Object[] a = new Object[args == null ? 0 : args.length];
3543+
if (args != null && (types != null || isProxy))
3544+
for (int i = args.length; --i >= 0;)
3545+
a[i] = (isProxy ? args[i] : /** @j2sNative (types[i].__PRIMITIVE && args[i].valueOf ? args[i].valueOf() : args[i]) || */ null);
3546+
return a;
3547+
}
3548+
34553549
}

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

Lines changed: 56 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,67 @@ public Constructor(Class<T> declaringClass, Class<?>[] parameterTypes, Class<?>[
4343
this.parameterTypes = parameterTypes;
4444
this.exceptionTypes = checkedExceptions;
4545
this.modifiers = modifiers;
46-
this.signature = "c$";
4746
// all of the SwingJS primitive classes run without parameterization
4847
if (";Integer;Long;Short;Byte;Float;Double;".indexOf(";" + declaringClass.getName() + ";") >= 0)
4948
parameterTypes = null;
5049
this.parameterTypes = parameterTypes;
51-
if (parameterTypes != null) {
52-
for (int i = 0; i < parameterTypes.length; i++) {
53-
String code = /** j2sNative Clazz._getParamCode(parameterTypes[i]) || */
54-
null;
55-
this.signature += "$" + code;
56-
}
50+
this.signature = "c$" + Class.argumentTypesToString(parameterTypes);
51+
constr = /** @j2sNative this.Class_.$clazz$[this.signature] || */ null;
52+
}
53+
54+
/**
55+
* Return a new instance of the declaring class, initialized by dynamically
56+
* invoking the modelled constructor. This reproduces the effect of
57+
* <code>new declaringClass(arg1, arg2, ... , argN)</code> This method performs
58+
* the following:
59+
* <ul>
60+
* <li>A new instance of the declaring class is created. If the declaring class
61+
* cannot be instantiated (i.e. abstract class, an interface, an array type, or
62+
* a base type) then an InstantiationException is thrown.</li>
63+
* <li>If this Constructor object is enforcing access control (see
64+
* AccessibleObject) and the modelled constructor is not accessible from the
65+
* current context, an IllegalAccessException is thrown.</li>
66+
* <li>If the number of arguments passed and the number of parameters do not
67+
* match, an IllegalArgumentException is thrown.</li>
68+
* <li>For each argument passed:
69+
* <ul>
70+
* <li>If the corresponding parameter type is a base type, the argument is
71+
* unwrapped. If the unwrapping fails, an IllegalArgumentException is
72+
* thrown.</li>
73+
* <li>If the resulting argument cannot be converted to the parameter type via a
74+
* widening conversion, an IllegalArgumentException is thrown.</li>
75+
* </ul>
76+
* <li>The modelled constructor is then invoked. If an exception is thrown
77+
* during the invocation, it is caught and wrapped in an
78+
* InvocationTargetException. This exception is then thrown. If the invocation
79+
* completes normally, the newly initialized object is returned.
80+
* </ul>
81+
*
82+
* @param args the arguments to the constructor
83+
* @return the new, initialized, object
84+
* @exception java.lang.InstantiationException if the class cannot be
85+
* instantiated
86+
* @exception java.lang.IllegalAccessException if the modelled constructor is
87+
* not accessible
88+
* @exception java.lang.IllegalArgumentException if an incorrect number of
89+
* arguments are passed, or an argument could not be converted by a
90+
* widening conversion
91+
* @exception java.lang.reflect.InvocationTargetException if an exception was
92+
* thrown by the invoked constructor
93+
* @see java.lang.reflect.AccessibleObject
94+
*
95+
*/
96+
@SuppressWarnings("unused")
97+
public T newInstance(Object... args)
98+
throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
99+
if (this.constr != null) {
100+
Object[] a = Class.getArgumentArray(parameterTypes, args, false);
101+
T instance = /** @j2sNative Clazz.new_(this.constr, a) || */ null;
102+
if (instance != null)
103+
return instance;
57104
}
58-
constr = /** @j2sNative this.Class_.$clazz$[this.signature] || */
59-
null;
105+
String message = "Constructor " + getDeclaringClass().getName() + "." + signature + " was not found";
106+
throw new IllegalArgumentException(message);
60107
}
61108

62109
public TypeVariable<Constructor<T>>[] getTypeParameters() {
@@ -243,72 +290,6 @@ public int hashCode() {
243290
return getDeclaringClass().getName().hashCode();
244291
}
245292

246-
/**
247-
* Return a new instance of the declaring class, initialized by dynamically
248-
* invoking the modelled constructor. This reproduces the effect of
249-
* <code>new declaringClass(arg1, arg2, ... , argN)</code> This method performs
250-
* the following:
251-
* <ul>
252-
* <li>A new instance of the declaring class is created. If the declaring class
253-
* cannot be instantiated (i.e. abstract class, an interface, an array type, or
254-
* a base type) then an InstantiationException is thrown.</li>
255-
* <li>If this Constructor object is enforcing access control (see
256-
* AccessibleObject) and the modelled constructor is not accessible from the
257-
* current context, an IllegalAccessException is thrown.</li>
258-
* <li>If the number of arguments passed and the number of parameters do not
259-
* match, an IllegalArgumentException is thrown.</li>
260-
* <li>For each argument passed:
261-
* <ul>
262-
* <li>If the corresponding parameter type is a base type, the argument is
263-
* unwrapped. If the unwrapping fails, an IllegalArgumentException is
264-
* thrown.</li>
265-
* <li>If the resulting argument cannot be converted to the parameter type via a
266-
* widening conversion, an IllegalArgumentException is thrown.</li>
267-
* </ul>
268-
* <li>The modelled constructor is then invoked. If an exception is thrown
269-
* during the invocation, it is caught and wrapped in an
270-
* InvocationTargetException. This exception is then thrown. If the invocation
271-
* completes normally, the newly initialized object is returned.
272-
* </ul>
273-
*
274-
* @param args the arguments to the constructor
275-
* @return the new, initialized, object
276-
* @exception java.lang.InstantiationException if the class cannot be
277-
* instantiated
278-
* @exception java.lang.IllegalAccessException if the modelled constructor is
279-
* not accessible
280-
* @exception java.lang.IllegalArgumentException if an incorrect number of
281-
* arguments are passed, or an argument could not be converted by a
282-
* widening conversion
283-
* @exception java.lang.reflect.InvocationTargetException if an exception was
284-
* thrown by the invoked constructor
285-
* @see java.lang.reflect.AccessibleObject
286-
*
287-
*/
288-
public T newInstance(Object... args)
289-
throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
290-
/**
291-
* @j2sNative
292-
*
293-
* var instance = null;
294-
* if (this.constr) {
295-
* var a = (args ? new Array(args.length) : []);
296-
* if (args) {
297-
* for (var i = args.length; --i >= 0;) {
298-
* a[i] = (this.parameterTypes[i].__PRIMITIVE ? args[i].valueOf() : args[i]);
299-
* }
300-
* }
301-
* var instance = Clazz.new_(this.constr, a);
302-
* }
303-
* if (instance == null)
304-
* newMethodNotFoundException(this.Class_.$clazz$, this.signature);
305-
* return instance;
306-
*/
307-
{
308-
return null;
309-
}
310-
}
311-
312293
/**
313294
* Answers a string containing a concise, human-readable description of the
314295
* receiver. The format of the string is modifiers (if any) declaring class name

0 commit comments

Comments
 (0)