Skip to content

Commit 2aba265

Browse files
author
jossonsmith
committed
Remove unnecessary dependency on class java.lang.StringBuffer.
Add partial support of java.lang.Character
1 parent fc58519 commit 2aba265

9 files changed

Lines changed: 326 additions & 12 deletions

File tree

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*******************************************************************************
2+
* Java2Script Pacemaker (http://j2s.sourceforge.net)
3+
*
4+
* Copyright (c) 2006 ognize.com and others.
5+
* All rights reserved. This program and the accompanying materials
6+
* are made available under the terms of the Eclipse Public License v1.0
7+
* which accompanies this distribution, and is available at
8+
* http://www.eclipse.org/legal/epl-v10.html
9+
*
10+
* Contributors:
11+
* ognize.com - initial API and implementation
12+
*******************************************************************************/
13+
14+
package java.lang;
15+
16+
import java.io.Serializable;
17+
18+
/**
19+
* @author josson smith
20+
*
21+
* 2006-8-5
22+
*/
23+
public class Character implements Serializable, Comparable {
24+
private static final long serialVersionUID = 3786198910865385080L;
25+
26+
private final char value;
27+
28+
/**
29+
* Constructs a new instance of the receiver which represents the char
30+
* valued argument.
31+
*
32+
* @param value
33+
* the char to store in the new instance.
34+
*/
35+
public Character(char value) {
36+
this.value = value;
37+
}
38+
39+
/**
40+
* Answers the char value which the receiver represents.
41+
*
42+
* @return char the value of the receiver
43+
*/
44+
public char charValue() {
45+
return value;
46+
}
47+
48+
/**
49+
* Compare the receiver to the specified Character to determine the relative
50+
* ordering.
51+
*
52+
* @param c
53+
* the Character
54+
* @return an int < 0 if this Character is less than the specified
55+
* Character, 0 if they are equal, and > 0 if this Character is
56+
* greater
57+
* @throws NullPointerException if <code>c</code> is <code>null</code>.
58+
* @since 1.2
59+
*/
60+
public int compareTo(Character c) {
61+
return value - c.value;
62+
}
63+
64+
public int compareTo(Object o) {
65+
return compareTo((Character) o);
66+
}
67+
68+
/**
69+
* Answers the lower case equivalent for the character when the character is
70+
* an upper case letter, otherwise answer the character.
71+
*
72+
* @param c
73+
* the character
74+
* @return if <code>c</code> is <b>not</b> a lower case character then
75+
* its lower case counterpart, otherwise just <code>c</code>
76+
*/
77+
public static char toLowerCase(char c) {
78+
return ("" + c).toLowerCase().charAt(0);
79+
}
80+
81+
/**
82+
* Answers the upper case equivalent for the character when the character is
83+
* a lower case letter, otherwise answer the character.
84+
*
85+
* @param c
86+
* the character
87+
* @return if <code>c</code> is <b>not</b> an upper case character then
88+
* its upper case counterpart, otherwise just <code>c</code>
89+
*/
90+
public static char toUpperCase(char c) {
91+
return ("" + c).toUpperCase().charAt(0);
92+
}
93+
94+
95+
/**
96+
* Answers whether the character is a digit.
97+
*
98+
* @param c
99+
* the character
100+
* @return true when the character is a digit, false otherwise
101+
*/
102+
public static boolean isDigit(char c) {
103+
// Optimized case for ASCII
104+
if ('0' <= c && c <= '9')
105+
return true;
106+
if (c < 1632)
107+
return false;
108+
//return getType(c) == DECIMAL_DIGIT_NUMBER;
109+
return false;
110+
}
111+
112+
/**
113+
* Answers whether the character is a whitespace character in Java.
114+
*
115+
* @param c
116+
* the character
117+
* @return <code>true</code> if the supplied <code>c</code> is a
118+
* whitespace character in Java, otherwise <code>false</code>.
119+
*/
120+
public static boolean isWhitespace(char c) {
121+
// Optimized case for ASCII
122+
if ((c >= 0x1c && c <= 0x20) || (c >= 0x9 && c <= 0xd))
123+
return true;
124+
if (c == 0x1680)
125+
return true;
126+
if (c < 0x2000 || c == 0x2007)
127+
return false;
128+
return c <= 0x200b || c == 0x2028 || c == 0x2029 || c == 0x3000;
129+
}
130+
131+
/**
132+
* Answers whether the character is a letter.
133+
*
134+
* @param c
135+
* the character
136+
* @return true when the character is a letter, false otherwise
137+
*/
138+
public static boolean isLetter(char c) {
139+
if (('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z'))
140+
return true;
141+
if (c < 128)
142+
return false;
143+
// int type = getType(c);
144+
// return type >= UPPERCASE_LETTER && type <= OTHER_LETTER;
145+
return false;
146+
}
147+
148+
/**
149+
* Answers whether the character is a letter or a digit.
150+
*
151+
* @param c
152+
* the character
153+
* @return true when the character is a letter or a digit, false otherwise
154+
*/
155+
public static boolean isLetterOrDigit(char c) {
156+
// int type = getType(c);
157+
// return (type >= UPPERCASE_LETTER && type <= OTHER_LETTER)
158+
// || type == DECIMAL_DIGIT_NUMBER;
159+
return isLetter(c) || isDigit(c);
160+
}
161+
162+
/**
163+
* Answers whether the character is a Unicode space character. A member of
164+
* one of the Unicode categories Space Separator, Line Separator, or
165+
* Paragraph Separator.
166+
*
167+
* @param c
168+
* the character
169+
* @return true when the character is a Unicode space character, false
170+
* otherwise
171+
*/
172+
public static boolean isSpaceChar(char c) {
173+
if (c == 0x20 || c == 0xa0 || c == 0x1680)
174+
return true;
175+
if (c < 0x2000)
176+
return false;
177+
return c <= 0x200b || c == 0x2028 || c == 0x2029 || c == 0x202f
178+
|| c == 0x3000;
179+
}
180+
181+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88
package java.lang.reflect;
99

10+
/*
1011
import java.security.AccessController;
1112
import sun.reflect.ReflectionFactory;
13+
*/
1214

1315
/**
1416
* The AccessibleObject class is the base class for Field, Method and
@@ -70,7 +72,7 @@ class AccessibleObject {
7072
*/
7173
public static void setAccessible(AccessibleObject[] array, boolean flag)
7274
throws SecurityException {
73-
SecurityManager sm = System.getSecurityManager();
75+
// SecurityManager sm = System.getSecurityManager();
7476
// if (sm != null) sm.checkPermission(ACCESS_PERMISSION);
7577
for (int i = 0; i < array.length; i++) {
7678
setAccessible0(array[i], flag);
@@ -103,7 +105,7 @@ public static void setAccessible(AccessibleObject[] array, boolean flag)
103105
* @see java.lang.RuntimePermission
104106
*/
105107
public void setAccessible(boolean flag) throws SecurityException {
106-
SecurityManager sm = System.getSecurityManager();
108+
// SecurityManager sm = System.getSecurityManager();
107109
// if (sm != null) sm.checkPermission(ACCESS_PERMISSION);
108110
setAccessible0(this, flag);
109111
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ public int hashCode() {
188188
*/
189189
public String toString() {
190190
try {
191+
/*
191192
StringBuffer sb = new StringBuffer();
192193
int mod = getModifiers();
193194
if (mod != 0) {
@@ -212,6 +213,37 @@ public String toString() {
212213
}
213214
}
214215
return sb.toString();
216+
*/
217+
String[] sb = new String[0];
218+
int mod = getModifiers();
219+
if (mod != 0) {
220+
sb[sb.length] = Modifier.toString(mod) + " ";
221+
}
222+
sb[sb.length] = Field.getTypeName(getDeclaringClass());
223+
sb[sb.length] = "(";
224+
Class[] params = parameterTypes; // avoid clone
225+
for (int j = 0; j < params.length; j++) {
226+
sb[sb.length] = Field.getTypeName(params[j]);
227+
if (j < (params.length - 1))
228+
sb[sb.length] = ",";
229+
}
230+
sb[sb.length] = ")";
231+
Class[] exceptions = exceptionTypes; // avoid clone
232+
if (exceptions.length > 0) {
233+
sb[sb.length] = " throws ";
234+
for (int k = 0; k < exceptions.length; k++) {
235+
sb[sb.length] = exceptions[k].getName();
236+
if (k < (exceptions.length - 1))
237+
sb[sb.length] = ",";
238+
}
239+
}
240+
/**
241+
* @j2sNativeSrc
242+
* return sb.join ('');
243+
* @j2sNative
244+
* return a.join ('');
245+
*/ {}
246+
return sb.toString();
215247
} catch (Exception e) {
216248
return "<" + e + ">";
217249
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,12 +840,26 @@ static String getTypeName(Class type) {
840840
dimensions++;
841841
cl = cl.getComponentType();
842842
}
843+
/*
843844
StringBuffer sb = new StringBuffer();
844845
sb.append(cl.getName());
845846
for (int i = 0; i < dimensions; i++) {
846847
sb.append("[]");
847848
}
848849
return sb.toString();
850+
*/
851+
String[] sb = new String[0];
852+
sb[sb.length] = cl.getName();
853+
for (int i = 0; i < dimensions; i++) {
854+
sb[sb.length] = "[]";
855+
}
856+
/**
857+
* @j2sNativeSrc
858+
* return sb.join ('');
859+
* @j2sNative
860+
* return d.join ('');
861+
*/ {}
862+
return sb.toString();
849863
} catch (Throwable e) { /*FALLTHRU*/ }
850864
}
851865
return type.getName();

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ public int hashCode() {
219219
*/
220220
public String toString() {
221221
try {
222+
/*
222223
StringBuffer sb = new StringBuffer();
223224
int mod = getModifiers();
224225
if (mod != 0) {
@@ -244,6 +245,38 @@ public String toString() {
244245
}
245246
}
246247
return sb.toString();
248+
*/
249+
String[] sb = new String[0];
250+
int mod = getModifiers();
251+
if (mod != 0) {
252+
sb[sb.length] = Modifier.toString(mod) + " ";
253+
}
254+
sb[sb.length] = Field.getTypeName(getReturnType()) + " ";
255+
sb[sb.length] = Field.getTypeName(getDeclaringClass()) + ".";
256+
sb[sb.length] = getName() + "(";
257+
Class[] params = parameterTypes; // avoid clone
258+
for (int j = 0; j < params.length; j++) {
259+
sb[sb.length] = Field.getTypeName(params[j]);
260+
if (j < (params.length - 1))
261+
sb[sb.length] = ",";
262+
}
263+
sb[sb.length] = ")";
264+
Class[] exceptions = exceptionTypes; // avoid clone
265+
if (exceptions.length > 0) {
266+
sb[sb.length] = " throws ";
267+
for (int k = 0; k < exceptions.length; k++) {
268+
sb[sb.length] = exceptions[k].getName();
269+
if (k < (exceptions.length - 1))
270+
sb[sb.length] = ",";
271+
}
272+
}
273+
/**
274+
* @j2sNativeSrc
275+
* return sb.join ('');
276+
* @j2sNative
277+
* return a.join ('');
278+
*/ {}
279+
return sb.toString();
247280
} catch (Exception e) {
248281
return "<" + e + ">";
249282
}

0 commit comments

Comments
 (0)