Skip to content

Commit 403faef

Browse files
committed
adds Clazz.assert(func_tf, func_msg)
1 parent faafdf1 commit 403faef

File tree

1 file changed

+41
-53
lines changed

1 file changed

+41
-53
lines changed

sources/net.sf.j2s.core/test/dev/js/j2sSwingJS.js

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
// TODO: Check String.contentEquals -- CharSequence? StringBuffer.shareValue??
1515

16+
// BH 9/7/2017 10:53:30 AM adds Clazz.assert; deprecates all Clazz.floatToInt, floatToByte, etc.
1617
// BH 8/30/2017 6:40:11 PM adds $newClass()
1718
// BH 8/26/2017 9:45:55 AM fix for URL.getContent()
1819
// BH 8/24/2017 1:54:01 AM fix for static Character.toUpperCase, .toLowerCase
@@ -162,8 +163,31 @@ Clazz.makeConstructor = function(){debugger}
162163

163164
Clazz.defineMethod = function(){debugger}
164165

165-
Clazz.popup = Clazz.assert = Clazz.log = Clazz.error = window.alert;
166+
Clazz.popup = Clazz.log = Clazz.error = window.alert;
166167

168+
Clazz._assertEnabled = true;
169+
Clazz._assertFunction = null;
170+
Clazz.assert = function(clazz, tf, msg) {
171+
if (!Clazz._assertEnabled)return;
172+
var ok = true;
173+
try {
174+
ok = tf.apply(clazz)
175+
if (!ok)
176+
msg = msg.apply(clazz);
177+
} catch (e) {
178+
ok = false;
179+
}
180+
if (!ok) {
181+
if (Clazz._assertFunction) {
182+
return Clazz._assertFunction(msg || Clazz.getStackTrace());
183+
}
184+
Clazz.loadClass("java.lang.AssertionError");
185+
if (msg == null)
186+
throw new AssertionError();
187+
else
188+
throw Clazz.$new(AssertionError.construct$S, [msg]);
189+
}
190+
}
167191
//J2S._debugCode = false;
168192

169193
Clazz.$new = function(c, args, cl) {
@@ -185,6 +209,7 @@ Clazz.$new = function(c, args, cl) {
185209
return f;
186210
}
187211

212+
188213
Clazz.$newClass = function(cl) {
189214
// $Class$ is the java.lang.Class object wrapper
190215
// $clazz$ is the unwrapped JavaScript object
@@ -1552,51 +1577,14 @@ java.lang.ClassLoader = {
15521577
__CLASS_NAME__ : "ClassLoader"
15531578
};
15541579

1555-
/******************************************************************************
1556-
* Copyright (c) 2007 java2script.org and others.
1557-
* All rights reserved. This program and the accompanying materials
1558-
* are made available under the terms of the Eclipse Public License v1.0
1559-
* which accompanies this distribution, and is available at
1560-
* http://www.eclipse.org/legal/epl-v10.html
1561-
*
1562-
* Contributors:
1563-
* Zhou Renjian - initial API and implementation
1564-
*****************************************************************************/
1565-
/*******
1566-
* @author zhou renjian
1567-
* @create March 10, 2006
1568-
*******/
1569-
1570-
/**
1571-
* Once ClassExt.js is part of Class.js.
1572-
* In order to make the Class.js as small as possible, part of its content
1573-
* is moved into this ClassExt.js.
1574-
*
1575-
* See also http://j2s.sourceforge.net/j2sclazz/
1576-
*/
1577-
1578-
/**
1579-
* Clazz.MethodNotFoundException is used to notify the developer about calling
1580-
* methods with incorrect parameters.
1581-
*/
1582-
15831580
//////// (int) conversions //////////
15841581

1585-
Clazz.floatToInt = function (x) {
1582+
// deprecated
1583+
Clazz.doubleToInt = Clazz.floatToInt = function (x) {
15861584
// asm.js-style conversion
15871585
return x|0;
15881586
};
15891587

1590-
Clazz.floatToByte = Clazz.floatToShort = Clazz.floatToLong = Clazz.floatToInt;
1591-
Clazz.doubleToByte = Clazz.doubleToShort = Clazz.doubleToLong = Clazz.doubleToInt = Clazz.floatToInt;
1592-
1593-
Clazz.floatToChar = function (x) {
1594-
return String.fromCharCode (isNaN(x) ? 0 : x < 0 ? Math.ceil(x) : Math.floor(x));
1595-
};
1596-
1597-
Clazz.doubleToChar = Clazz.floatToChar;
1598-
1599-
16001588

16011589
///////////////////////////////// Array additions //////////////////////////////
16021590
//
@@ -4080,18 +4068,19 @@ Number.compare = function(a,b) { return (a < b ? -1 : a == b ? 0 : 1) };
40804068
m$(Number,"shortValue",
40814069
function(){
40824070
var x = Math.round(this)&0xffff;
4083-
return (this < 0 && x > 0 ? x - 0x10000 : x);
4071+
return (this < 0 && x > 0 || x == 0xffff ? x - 0x10000 : x);
40844072
});
40854073

40864074
m$(Number,"byteValue",
40874075
function(){
40884076
var x = Math.round(this)&0xff;
4089-
return (this < 0 && x > 0 ? x - 0x100 : x);
4077+
return (this < 0 && x > 0 || x == 0xff ? x - 0x100 : x);
40904078
});
40914079

40924080
m$(Number,"intValue",
40934081
function(){
4094-
return Math.round(this)&0xffffffff;
4082+
var x = Math.round(this)&0xffffffff;
4083+
return (this < 0 && x > 0 || x == 0xffffffff ? x - 0x100000000 : x);
40954084
});
40964085

40974086
m$(Number,"longValue",
@@ -4155,6 +4144,7 @@ m$(Integer, "construct", function(v){
41554144
v == null && (v = 0);
41564145
if (typeof v != "number")
41574146
v = Integer.parseIntRadix(v, 10);
4147+
v = v.intValue();
41584148
this.valueOf=function(){return v;};
41594149
}, 1);
41604150

@@ -4450,9 +4440,8 @@ m$(Byte, "construct", function(v){
44504440
if (typeof v != "number")
44514441
v = Integer.parseIntRadix(v, 10);
44524442
v = v.byteValue();
4453-
this.valueOf=function(){
4454-
return v;
4455-
};
4443+
this.valueOf=function(){return v;};
4444+
this.byteValue = function(){return v};
44564445
}, 1);
44574446

44584447
Byte.toString=Byte.prototype.toString=function(){
@@ -5465,6 +5454,11 @@ if (typeof arguments[0] != "object")this.construct(arguments[0]);
54655454
Clazz._setDeclared("java.lang.Character", java.lang.Character);
54665455
setJ2STypeclass(Character, "char", "C");
54675456

5457+
m$(c$,"getName",
5458+
function(){
5459+
return "?";
5460+
}, 1);
5461+
54685462
m$(c$,"construct",
54695463
function(value){
54705464
this.value=value;
@@ -5474,6 +5468,7 @@ m$(c$,"charValue",
54745468
function(){
54755469
return this.value;
54765470
});
5471+
54775472
m$(c$,"hashCode",
54785473
function(){
54795474
return(this.value).charCodeAt(0);
@@ -5929,7 +5924,6 @@ declareType(java.lang,"LinkageError",Error);
59295924
declareType(java.lang,"VirtualMachineError",Error);
59305925
declareType(java.lang,"IncompatibleClassChangeError",LinkageError);
59315926

5932-
59335927
declareType(java.lang,"AbstractMethodError",IncompatibleClassChangeError);
59345928
declareType(java.lang,"ArithmeticException",RuntimeException);
59355929
declareType(java.lang,"ArrayStoreException",RuntimeException);
@@ -5964,12 +5958,6 @@ declareType(java.lang,"UnsupportedClassVersionError",ClassFormatError);
59645958
declareType(java.lang,"UnsupportedOperationException",RuntimeException);
59655959
declareType(java.lang,"VerifyError",LinkageError);
59665960

5967-
5968-
var c$=declareType(java.lang,"ArrayIndexOutOfBoundsException",IndexOutOfBoundsException);
5969-
m$(c$, "construct$I", function() {
5970-
c$.superClazz.construct$S.apply(this, ["Array index out of range: "+index]);
5971-
}, 1);
5972-
59735961
declareType(java.lang,"ClassCastException",RuntimeException);
59745962
c$=Clazz.decorateAsClass(java.lang,"ClassNotFoundException",function(){this.ex=null;},Exception);
59755963
m$(c$, "construct$S$Throwable", function(detailMessage,exception){

0 commit comments

Comments
 (0)