Skip to content

Commit 04bb8ff

Browse files
authored
Merge pull request #58 from BobHanson/master
adds java.lang.reflect.Field
2 parents 4e1f159 + 3b9f587 commit 04bb8ff

File tree

15 files changed

+1840
-670
lines changed

15 files changed

+1840
-670
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+
20180724064133

sources/net.sf.j2s.core/dist/timestamp

Lines changed: 0 additions & 1 deletion
This file was deleted.
6.23 KB
Binary file not shown.

sources/net.sf.j2s.java.core/src/java/awt/Font.java

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,7 @@ public String getFamily() {
11611161
return (family == null ? family = JSToolkit.getFontFamily(this) : family);
11621162
// return getFamily_NoClientCode();
11631163
}
1164+
11641165
// // NOTE: This method is called by privileged threads.
11651166
// // We implement this functionality in a package-private
11661167
// // method to insure that it cannot be overridden by client
@@ -1196,17 +1197,50 @@ public String getFamily() {
11961197
// return getFont2D().getFamilyName(l);
11971198
// }
11981199
//
1199-
// /**
1200-
// * Returns the postscript name of this <code>Font</code>.
1201-
// * Use <code>getFamily</code> to get the family name of the font.
1202-
// * Use <code>getFontName</code> to get the font face name of the font.
1203-
// * @return a <code>String</code> representing the postscript name of
1204-
// * this <code>Font</code>.
1205-
// * @since 1.2
1206-
// */
1207-
// public String getPSName() {
1208-
// return getFont2D().getPostscriptName();
1209-
// }
1200+
/**
1201+
* Returns the postscript name of this <code>Font</code>. Use
1202+
* <code>getFamily</code> to get the family name of the font. Use
1203+
* <code>getFontName</code> to get the font face name of the font.
1204+
*
1205+
* @return a <code>String</code> representing the postscript name of this
1206+
* <code>Font</code>.
1207+
* @since 1.2
1208+
*/
1209+
public String getPSName() {
1210+
1211+
// Serif Serif.plain Serif.bold Serif.italic
1212+
// SansSerif SansSerif.plain SansSerif.bold SansSerif.italic
1213+
// TimesRoman Serif.plain Serif.bold Serif.italic
1214+
// Helvetica SansSerif.plain SansSerif.bold SansSerif.italic
1215+
// Courier Monospaced.plain Monospaced.bold Monospaced.italic
1216+
// Monospaced Monospaced.plain Monospaced.bold Monospaced.italic
1217+
// Dialog Dialog.plain Dialog.bold Dialog.italic
1218+
// DialogInput DialogInput.plain DialogInput.bold DialogInput.italic
1219+
1220+
switch (name) {
1221+
case "TimesRoman":
1222+
name = SERIF;
1223+
break;
1224+
case "Helvetica":
1225+
name = SANS_SERIF;
1226+
break;
1227+
case "Courier":
1228+
name = MONOSPACED;
1229+
break;
1230+
default:
1231+
case MONOSPACED:
1232+
case SERIF:
1233+
case SANS_SERIF:
1234+
case DIALOG:
1235+
case DIALOG_INPUT:
1236+
break;
1237+
}
1238+
return name + (isPlain()? ".plain"
1239+
: "."
1240+
+ (isBold() ? "bold" : "")
1241+
+ (isItalic() ? "italic" : "")
1242+
);
1243+
}
12101244

12111245
/**
12121246
* Returns the logical name of this <code>Font</code>.

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
}

0 commit comments

Comments
 (0)