Skip to content

Commit 7f6442a

Browse files
author
sebastigurin
committed
my fixes in java.core
1 parent da74153 commit 7f6442a

File tree

7 files changed

+267
-4
lines changed

7 files changed

+267
-4
lines changed

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,43 @@ public static boolean isDigit(char c) {
193193
return false;
194194
}
195195

196+
//sgurin: added forDigit
197+
/**
198+
* Determines the character representation for a specific digit in
199+
* the specified radix. If the value of <code>radix</code> is not a
200+
* valid radix, or the value of <code>digit</code> is not a valid
201+
* digit in the specified radix, the null character
202+
* (<code>'&#92;u0000'</code>) is returned.
203+
* <p>
204+
* The <code>radix</code> argument is valid if it is greater than or
205+
* equal to <code>MIN_RADIX</code> and less than or equal to
206+
* <code>MAX_RADIX</code>. The <code>digit</code> argument is valid if
207+
* <code>0&nbsp;&lt;=digit&nbsp;&lt;&nbsp;radix</code>.
208+
* <p>
209+
* If the digit is less than 10, then
210+
* <code>'0'&nbsp;+ digit</code> is returned. Otherwise, the value
211+
* <code>'a'&nbsp;+ digit&nbsp;-&nbsp;10</code> is returned.
212+
*
213+
* @param digit the number to convert to a character.
214+
* @param radix the radix.
215+
* @return the <code>char</code> representation of the specified digit
216+
* in the specified radix.
217+
* @see java.lang.Character#MIN_RADIX
218+
* @see java.lang.Character#MAX_RADIX
219+
* @see java.lang.Character#digit(char, int)
220+
*/
221+
public static char forDigit(int digit, int radix) {
222+
if ((digit >= radix) || (digit < 0)) {
223+
return '\0';
224+
}
225+
if ((radix < Character.MIN_RADIX) || (radix > Character.MAX_RADIX)) {
226+
return '\0';
227+
}
228+
if (digit < 10) {
229+
return (char)('0' + digit);
230+
}
231+
return (char)('a' - 10 + digit);
232+
}
196233
/**
197234
* Answers whether the character is an upper case letter.
198235
*

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,6 +1885,71 @@ Clazz.decorateAsType = function (clazzFun, qClazzName, clazzParent,
18851885
return clazzFun;
18861886
};
18871887

1888+
/* sgurin: native exception detection mechanism. Only NullPointerException detected and wrapped to java excepions */
1889+
/** private utility method for creating a general regexp that can be used later
1890+
* for detecting a certain kind of native exceptions. use with error messages like "blabla IDENTIFIER blabla"
1891+
* @param msg String - the error message
1892+
* @param spliterName String, must be contained once in msg
1893+
* spliterRegex String, a string with the regexp literal for identifying the spitter in exception further error messages.
1894+
*/
1895+
Clazz._ex_reg=function (msg, spliterName, spliterRegex) {
1896+
if(!spliterRegex)
1897+
spliterRegex="[^\\s]+";
1898+
var idx = msg.indexOf (spliterName),
1899+
str = msg.substring (0, idx) + spliterRegex + msg.substring(idx + spliterName.length),
1900+
regexp = new RegExp("^"+str+"$");
1901+
return regexp;
1902+
};
1903+
// reproduce NullPointerException for knowing how to detect them, and create detector function Clazz._isNPEExceptionPredicate
1904+
var $$o$$ = null;
1905+
try {
1906+
$$o$$.hello ();
1907+
} catch (e) {
1908+
if(/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {// opera throws an exception with fixed messages like "Statement on line 23: Cannot convert undefined or null to Object Backtrace: Line....long text... "
1909+
var idx1 = e.message.indexOf(":"), idx2 = e.message.indexOf(":", idx1+2);
1910+
Clazz._NPEMsgFragment = e.message.substr(idx1+1, idx2-idx1-20);
1911+
Clazz._isNPEExceptionPredicate = function(e) {
1912+
return e.message.indexOf(Clazz._NPEMsgFragment)!=-1;
1913+
};
1914+
}
1915+
else if(navigator.userAgent.toLowerCase().indexOf("webkit")!=-1) { //webkit, google chrome prints the property name accessed.
1916+
Clazz._exceptionNPERegExp = Clazz._ex_reg(e.message, "hello");
1917+
Clazz._isNPEExceptionPredicate = function(e) {
1918+
return Clazz._exceptionNPERegExp.test(e.message);
1919+
};
1920+
}
1921+
else {// ie, firefox and others print the name of the object accessed:
1922+
Clazz._exceptionNPERegExp = Clazz._ex_reg(e.message, "$$o$$");
1923+
Clazz._isNPEExceptionPredicate = function(e) {
1924+
return Clazz._exceptionNPERegExp.test(e.message);
1925+
};
1926+
}
1927+
};
1928+
/**
1929+
* Implements Java's keyword "instanceof" in JavaScript's way **for exception objects**.
1930+
*
1931+
* calls Clazz.instanceOf if e is a Java exception. If not, try to detect known native
1932+
* exceptions, like native NullPointerExceptions and wrap it into a Java exception and
1933+
* call Clazz.instanceOf again. if the native exception can't be wrapped, false is returned.
1934+
*
1935+
* @param obj the object to be tested
1936+
* @param clazz the class to be checked
1937+
* @return whether the object is an instance of the class
1938+
* @author: sgurin
1939+
*/
1940+
Clazz.exceptionOf=function(e, clazz) {
1941+
if(e.__CLASS_NAME__)
1942+
return Clazz.instanceOf(e, clazz);
1943+
else if(Clazz._isNPEExceptionPredicate(e)) {
1944+
//wrap to a java npe
1945+
var jnpe=new java.lang.NullPointerException();
1946+
for(var i in jnpe){e[i]=jnpe[i];}
1947+
return Clazz.instanceOf(e, clazz);
1948+
}
1949+
else
1950+
return false;
1951+
};
1952+
18881953
Clazz.declarePackage ("java.io");
18891954
//Clazz.declarePackage ("java.lang");
18901955
Clazz.declarePackage ("java.lang.annotation"); // java.lang

sources/net.sf.j2s.java.core/src/java/lang/Double.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ Double.isInfinite = Double.prototype.isInfinite;
5353

5454
Clazz.defineMethod (Double, "parseDouble",
5555
function (s) {
56-
if (s == null) {
57-
throw new NumberFormatException ("null");
58-
}
56+
if(s==null) throw new NullPointerException();//sgurin . if s==null a NPE should be thrown and not NumberFormatException because isNaN(null)==true
5957
var doubleVal = parseFloat (s);
6058
if(isNaN(doubleVal)){
6159
throw new NumberFormatException ("Not a Number : " + s);

sources/net.sf.j2s.java.core/src/java/lang/Integer.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,88 @@ throw e;
124124
}
125125
return result;
126126
}, "~S");
127+
//sgurin compare and compareTo
128+
Integer.compare = Clazz.defineMethod (Integer, "compare",
129+
function (f1, f2) {
130+
if (f1 < f2) return -1;
131+
if (f1 > f2) return 1;
132+
return 0;
133+
}, "~N,~N");
134+
Integer.prototype.compareTo=function(anotherInt) {
135+
var otherValue = anotherInt;
136+
if(anotherInt.valueOf) otherValue=anotherInt.valueOf();
137+
return java.lang.Integer.compare(this.valueOf(), otherValue);
138+
}
139+
//sgurin bit related methods
140+
Integer.highestOneBit = Clazz.defineMethod (Integer, "highestOneBit",
141+
function (i) {
142+
i |= (i >> 1);
143+
i |= (i >> 2);
144+
i |= (i >> 4);
145+
i |= (i >> 8);
146+
i |= (i >> 16);
147+
return i - (i >>> 1);
148+
}, "~N");
149+
Integer.lowestOneBit = Clazz.defineMethod (Integer, "lowestOneBit",
150+
function (i) {return i & -i;}, "~N");
151+
Integer.numberOfLeadingZeros = Clazz.defineMethod (Integer, "numberOfLeadingZeros",
152+
function (i) {
153+
if (i == 0) return 32;
154+
var n = 1;
155+
if (i >>> 16 == 0) {n += 16;i <<= 16;}
156+
if (i >>> 24 == 0) {n += 8;i <<= 8;}
157+
if (i >>> 28 == 0) {n += 4;i <<= 4;}
158+
if (i >>> 30 == 0) {n += 2;i <<= 2;}
159+
n -= i >>> 31;
160+
return n;
161+
}, "~N");
162+
Integer.numberOfTrailingZeros = Clazz.defineMethod (Integer, "numberOfTrailingZeros",
163+
function (i) {
164+
var y;
165+
if (i == 0) return 32;
166+
var n = 31;
167+
y = i << 16;
168+
if (y != 0) {n = n - 16;i = y;}
169+
y = i << 8;
170+
if (y != 0) {n = n - 8;i = y;}
171+
y = i << 4;
172+
if (y != 0) {n = n - 4;i = y;}
173+
y = i << 2;
174+
if (y != 0) {n = n - 2;i = y;}
175+
return n - ((i << 1) >>> 31);
176+
}, "~N");
177+
Integer.reverse = Clazz.defineMethod (Integer, "reverse",
178+
function (i) {
179+
i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555;
180+
i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333;
181+
i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f;
182+
i = (i << 24) | ((i & 0xff00) << 8) | ((i >>> 8) & 0xff00) | (i >>> 24);
183+
return i;
184+
}, "~N");
185+
Integer.reverseBytes = Clazz.defineMethod (Integer, "reverseBytes",
186+
function (i) {
187+
return ((i >>> 24)) | ((i >> 8) & 0xFF00) | ((i << 8) & 0xFF0000) | ((i << 24));
188+
}, "~N");
189+
Integer.rotateLeft = Clazz.defineMethod (Integer, "rotateLeft",
190+
function (i, distance) {
191+
return (i << distance) | (i >>> -distance);
192+
}, "~N,~N");
193+
Integer.rotateRight = Clazz.defineMethod (Integer, "rotateRight",
194+
function (i, distance) {
195+
return (i >>> distance) | (i << -distance);
196+
}, "~N,~N");
197+
Integer.signum = Clazz.defineMethod (Integer, "signum",
198+
function (i) {
199+
return (i >> 31) | (-i >>> 31);
200+
}, "~N");
201+
Integer.bitCount = Clazz.defineMethod (Integer, "bitCount",
202+
function (i) {
203+
i = i - ((i >>> 1) & 0x55555555);
204+
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
205+
i = (i + (i >>> 4)) & 0x0f0f0f0f;
206+
i = i + (i >>> 8);
207+
i = i + (i >>> 16);
208+
return i & 0x3f;
209+
}, "~N");
127210
});
128211

sources/net.sf.j2s.java.core/src/java/lang/Long.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,76 @@ throw e;
124124
}
125125
return result;
126126
}, "~S");
127+
//sgurin compare and compareTo. the same implementation as in Integer
128+
Long.compare = Clazz.defineMethod (Long, "compare",
129+
function (f1, f2) {
130+
if (f1 < f2) return -1;
131+
if (f1 > f2) return 1;
132+
return 0;
133+
}, "~N,~N");
134+
Long.prototype.compareTo=function(anotherInt) {
135+
var otherValue = anotherInt;
136+
if(anotherInt.valueOf) otherValue=anotherInt.valueOf();
137+
return java.lang.Long.compare(this.valueOf(), otherValue);
138+
}
139+
//sgurin bitwise related static methods
140+
Long.bitCount = Clazz.defineMethod (Long, "bitCount", function (i) {
141+
i = i - ((i >>> 1) & 0x5555555555555555);
142+
i = (i & 0x3333333333333333) + ((i >>> 2) & 0x3333333333333333);
143+
i = (i + (i >>> 4)) & 0x0f0f0f0f0f0f0f0f;
144+
i = i + (i >>> 8);
145+
i = i + (i >>> 16);
146+
i = i + (i >>> 32);
147+
return i & 0x7f;
148+
}, "~N");
149+
Long.rotateLeft = Clazz.defineMethod (Long, "rotateLeft", function (i, distance) {
150+
return (i << distance) | (i >>> -distance);
151+
}, "~N,~N");
152+
Long.rotateRight = Clazz.defineMethod (Long, "rotateRight", function (i, distance) {
153+
return (i >>> distance) | (i << -distance);
154+
}, "~N,~N");
155+
Long.highestOneBit = Clazz.defineMethod (Long, "highestOneBit", function (i) {
156+
i |= (i >> 1);i |= (i >> 2);i |= (i >> 4);
157+
i |= (i >> 8);i |= (i >> 16);i |= (i >> 32);
158+
return i - (i >>> 1);
159+
}, "~N");
160+
Long.lowestOneBit = Clazz.defineMethod (Long, "lowestOneBit", function (i) {
161+
return i & -i;}, "~N");
162+
Long.numberOfLeadingZeros = Clazz.defineMethod (Long, "numberOfLeadingZeros", function (i) {
163+
if (i == 0) return 64;
164+
var n = 1;var x = (i >>> 32);
165+
if (x == 0) {n += 32;x = i;}
166+
if (x >>> 16 == 0) {n += 16;x <<= 16;}
167+
if (x >>> 24 == 0) {n += 8;x <<= 8;}
168+
if (x >>> 28 == 0) {n += 4;x <<= 4;}
169+
if (x >>> 30 == 0) {n += 2;x <<= 2;}
170+
n -= x >>> 31;return n;}, "~N");
171+
Long.numberOfTrailingZeros = Clazz.defineMethod (Long, "numberOfTrailingZeros", function (i) {
172+
var x;var y;
173+
if (i == 0) return 64;
174+
var n = 63;y = i;
175+
if (y != 0) {n = n - 32;x = y;}
176+
else x = (i >>> 32);
177+
y = x << 16;
178+
if (y != 0) {n = n - 16;x = y;}
179+
y = x << 8;
180+
if (y != 0) {n = n - 8;x = y;
181+
}y = x << 4;
182+
if (y != 0) {n = n - 4;x = y;
183+
}y = x << 2;
184+
if (y != 0) {n = n - 2;x = y;
185+
}return n - ((x << 1) >>> 31);}, "~N");
186+
Long.signum = Clazz.defineMethod (Long, "signum", function (i) {
187+
return ((i >> 63) | (-i >>> 63));}, "~N");
188+
Long.reverseBytes = Clazz.defineMethod (Long, "reverseBytes", function (i) {
189+
i = (i & 0x00ff00ff00ff00ff) << 8 | (i >>> 8) & 0x00ff00ff00ff00ff;
190+
return (i << 48) | ((i & 0xffff0000) << 16) | ((i >>> 16) & 0xffff0000) | (i >>> 48);}, "~N");
191+
Long.reverse = Clazz.defineMethod (Long, "reverse", function (i) {
192+
i = (i & 0x5555555555555555) << 1 | (i >>> 1) & 0x5555555555555555;
193+
i = (i & 0x3333333333333333) << 2 | (i >>> 2) & 0x3333333333333333;
194+
i = (i & 0x0f0f0f0f0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f0f0f0f0f;
195+
i = (i & 0x00ff00ff00ff00ff) << 8 | (i >>> 8) & 0x00ff00ff00ff00ff;
196+
i = (i << 48) | ((i & 0xffff0000) << 16) | ((i >>> 16) & 0xffff0000) | (i >>> 48);
197+
return i;}, "~N");
127198
});
128199

sources/net.sf.j2s.java.core/src/java/lang/Number.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ function () {
4444
return this.valueOf();
4545
});
4646

47+
//sgurin : added this because if not, a native number in native code will print as [Object Number] instead printing the number value...
48+
Clazz.defineMethod (Number, "toString",
49+
function () {
50+
return this.valueOf()+"";
51+
});
52+
4753
Clazz.defineMethod (Number, "hashCode",
4854
function () {
4955
return this.valueOf ();

sources/net.sf.j2s.java.core/src/java/lang/String.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,10 @@ String.valueOf = function (o) {
308308
return o.join ('');
309309
} else {
310310
var off = arguments[1];
311-
var len = arguments[2];
311+
var len = arguments[2];
312+
//sgurin : check for IndexOutOfBoundsException
313+
if(off<0 || len<0 || off+len>arguments[0].length)
314+
throw new java.lang.IndexOutOfBoundsException();
312315
var oo = new Array (len);
313316
for (var i = 0; i < len; i++) {
314317
oo[i] = o[off + i];

0 commit comments

Comments
 (0)