Repository navigation
Expand file tree
/
Copy pathPrimitive.java
More file actions
627 lines (554 loc) · 24.8 KB
/
Copy pathPrimitive.java
File metadata and controls
627 lines (554 loc) · 24.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*****************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you under the Apache License, Version 2.0 (the *
* "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, *
* software distributed under the License is distributed on an *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
* KIND, either express or implied. See the License for the *
* specific language governing permissions and limitations *
* under the License. *
* *
* *
* This file is part of the BeanShell Java Scripting distribution. *
* Documentation and updates may be found at http://www.beanshell.org/ *
* Patrick Niemeyer (pat@pat.net) *
* Author of Learning Java, O'Reilly & Associates *
* *
*****************************************************************************/
package bsh;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
/**
* <p>Wrapper for primitive types in Bsh. This is package public because it
* is used in the implementation of some bsh commands.</p>
*
* <p>See the note in LHS.java about wrapping objects.</p>
*
* @author Pat Niemeyer
* @author Daniel Leuck
*/
public final class Primitive implements Serializable {
/*
Note: this class is final because we may test == Primitive.class in places.
If we need to change that search for those tests.
*/
/** default serial version id */
private static final long serialVersionUID = 1L;
static final Map<Class<?>, Class<?>> wrapperMap = new HashMap<>();
static {
wrapperMap.put( Boolean.TYPE, Boolean.class );
wrapperMap.put( Byte.TYPE, Byte.class );
wrapperMap.put( Short.TYPE, Short.class );
wrapperMap.put( Character.TYPE, Character.class );
wrapperMap.put( Integer.TYPE, Integer.class );
wrapperMap.put( Long.TYPE, Long.class );
wrapperMap.put( Float.TYPE, Float.class );
wrapperMap.put( Double.TYPE, Double.class );
wrapperMap.put( Boolean.class, Boolean.TYPE );
wrapperMap.put( Byte.class, Byte.TYPE );
wrapperMap.put( Short.class, Short.TYPE );
wrapperMap.put( Character.class, Character.TYPE );
wrapperMap.put( Integer.class, Integer.TYPE );
wrapperMap.put( Long.class, Long.TYPE );
wrapperMap.put( Float.class, Float.TYPE );
wrapperMap.put( Double.class, Double.TYPE );
wrapperMap.put( BigInteger.class, BigInteger.class );
wrapperMap.put( BigDecimal.class, BigDecimal.class );
}
/** The primitive value stored in its java.lang wrapper class */
private Object value;
public static final Primitive TRUE = new Primitive(true);
public static final Primitive FALSE = new Primitive(false);
/** Default zero value constants */
public static final Primitive ZERO_CHAR = new Primitive('\000');
public static final Primitive ZERO_BYTE = new Primitive((byte) 0);
public static final Primitive ZERO_SHORT = new Primitive((short) 0);
public static final Primitive ZERO_INT = new Primitive(0);
public static final Primitive ZERO_LONG = new Primitive(0L);
public static final Primitive ZERO_FLOAT = new Primitive(0f);
public static final Primitive ZERO_DOUBLE = new Primitive(0d);
public static final Primitive ZERO_BIG_INTEGER = new Primitive(BigInteger.ZERO);
public static final Primitive ZERO_BIG_DECIMAL = new Primitive(BigDecimal.ZERO);
private enum Special { NULL_VALUE, VOID_TYPE }
/*
NULL means "no value".
This ia a placeholder for primitive null value.
*/
public static final Primitive NULL = new Primitive(Special.NULL_VALUE);
/**
VOID means "no type".
Strictly speaking, this makes no sense here. But for practical
reasons we'll consider the lack of a type to be a special value.
*/
public static final Primitive VOID = new Primitive(Special.VOID_TYPE);
private Object readResolve() throws ObjectStreamException
{
if (value == Special.NULL_VALUE)
return Primitive.NULL;
return this;
}
// private to prevent invocation with param that isn't a primitive-wrapper
private Primitive( Object value )
{
if ( value == null )
throw new InterpreterError(
"Use Primitve.NULL instead of Primitive(null)");
this.value = value;
}
public Primitive(boolean value) { this(value ? Boolean.TRUE : Boolean.FALSE); }
public Primitive(byte value) { this(Byte.valueOf(value)); }
public Primitive(short value) { this(Short.valueOf(value)); }
public Primitive(char value) { this(Character.valueOf(value)); }
public Primitive(int value) { this(Integer.valueOf(value)); }
public Primitive(long value) { this(Long.valueOf(value)); }
public Primitive(float value) { this(Float.valueOf(value)); }
public Primitive(double value) { this(Double.valueOf(value)); }
public Primitive(BigInteger value) { this((Object) value); }
public Primitive(BigDecimal value) {
this((Object) (null != value && value.scale() == 0 ? value.setScale(1) : value));
}
/**
Return the primitive value stored in its java.lang wrapper class
*/
public Object getValue()
{
if ( value == Special.NULL_VALUE )
return null;
if ( value == Special.VOID_TYPE )
throw new InterpreterError("attempt to unwrap void type");
return value;
}
public String toString()
{
if(value == Special.NULL_VALUE)
return "null";
if(value == Special.VOID_TYPE)
return "void";
return value.toString();
}
/**
Get the corresponding Java primitive TYPE class for this Primitive.
@return the primitive TYPE class type of the value or Void.TYPE for
Primitive.VOID or null value for type of Primitive.NULL
*/
public Class<?> getType()
{
if ( this == Primitive.VOID )
return Void.TYPE;
// NULL return null as type... we currently use null type to indicate
// loose typing throughout bsh.
if ( this == Primitive.NULL )
return null;
return unboxType( value.getClass() );
}
private static final BigInteger INTEGER_MAX = BigInteger.valueOf(Integer.MAX_VALUE);
private static final BigInteger INTEGER_MIN = BigInteger.valueOf(Integer.MIN_VALUE);
static final BigInteger LONG_MAX = BigInteger.valueOf(Long.MAX_VALUE);
static final BigInteger LONG_MIN = BigInteger.valueOf(Long.MIN_VALUE);
public static Primitive shrinkWrap(Object number) {
if (!(number instanceof Number))
throw new InterpreterError("Can only shrink wrap Number types");
Number value = (Number) number;
if (value instanceof Float)
return new Primitive(value.floatValue());
if ( Types.isFloatingpoint(number) ) {
if ( !Double.isInfinite(value.doubleValue()) )
return new Primitive(value.doubleValue());
return new Primitive((BigDecimal) number);
}
BigInteger bi = number instanceof BigInteger
? (BigInteger) number : BigInteger.valueOf(value.longValue());
if ( bi.compareTo(INTEGER_MIN) >= 0 && bi.compareTo(INTEGER_MAX) <= 0 )
return new Primitive(bi.intValue());
if ( bi.compareTo(LONG_MIN) >= 0 && bi.compareTo(LONG_MAX) <= 0 )
return new Primitive(bi.longValue());
return new Primitive(bi);
}
/**
Determine if this primitive is a numeric type.
i.e. not boolean, null, or void (but including char)
*/
public boolean isNumber() {
return !(value instanceof Boolean)
&& this != NULL && this != VOID
&& Types.isNumeric(value);
}
public Number numberValue() {
Object value = this.value;
// Promote character to Number type for these purposes
if (value instanceof Character)
value = Integer.valueOf(((Character)value).charValue());
if (value instanceof Number)
return (Number) value;
// if (value instanceof Boolean)
// return (Boolean) value ? 1 : 0;
throw new InterpreterError("Primitive not a number");
}
/**
Primitives compare equal with other Primitives containing an equal
wrapped value.
*/
public boolean equals( Object obj )
{
if ( !( obj instanceof Primitive ) )
if ( wrapperMap.containsKey(obj.getClass()) )
obj = new Primitive(obj);
else
return false;
Primitive pobj = (Primitive) obj;
if ( pobj.isNumber() && this.isNumber() ) {
if ( this.getType() == BigDecimal.class )
return this.value.equals(castNumber(BigDecimal.class, pobj.numberValue()));
if ( pobj.getType() == BigDecimal.class )
return pobj.value.equals(castNumber(BigDecimal.class, this.numberValue()));
if ( Types.isFloatingpoint(this.value) || Types.isFloatingpoint(pobj.value) )
return this.numberValue().doubleValue() == pobj.numberValue().doubleValue();
if ( this.getType() == BigInteger.class )
return this.value.equals(castNumber(BigInteger.class, pobj.numberValue()));
if ( pobj.getType() == BigInteger.class )
return pobj.value.equals(castNumber(BigInteger.class, this.numberValue()));
return this.numberValue().longValue() == pobj.numberValue().longValue();
}
return this.value.equals( pobj.value );
}
/**
The hash of the Primitive is tied to the hash of the wrapped value but
shifted so that they are not the same.
*/
public int hashCode()
{
return this.value.hashCode() * 21; // arbitrary
}
/**
Unwrap primitive values and map voids to nulls.
Non Primitive types remain unchanged.
@param obj object type which may be bsh.Primitive
@return corresponding "normal" Java type, "unwrapping"
any bsh.Primitive types to their wrapper types.
*/
public static Object unwrap( Object obj )
{
// map voids to nulls for the outside world
if (obj == Primitive.VOID)
return null;
// unwrap primitives
if (obj instanceof Primitive)
return((Primitive)obj).getValue();
return obj;
}
/*
Unwrap Primitive wrappers to their java.lang wrapper values.
e.g. Primitive(42) becomes Integer(42)
@see #unwrap( Object )
*/
public static Object [] unwrap( Object[] args )
{
if ( args == null )
return null;
Object [] oa = new Object[ args.length ];
for(int i=0; i<args.length; i++)
oa[i] = unwrap( args[i] );
return oa;
}
/*
*/
public static Object [] wrap( Object[] args, Class<?> [] paramTypes )
{
if ( args == null )
return null;
Object [] oa = new Object[ args.length ];
for(int i=0; i<args.length; i++)
oa[i] = wrap( args[i], paramTypes[i] );
return oa;
}
/**
Wrap primitive values (as indicated by type param) and nulls in the
Primitive class. Values not primitive or null are left unchanged.
Primitive values are represented by their wrapped values in param value.
<p/>
The value null is mapped to Primitive.NULL.
Any value specified with type Void.TYPE is mapped to Primitive.VOID.
*/
public static Object wrap(
Object value, Class<?> type )
{
if ( type == Void.TYPE )
return Primitive.VOID;
if ( value == null )
return Primitive.NULL;
if (Types.isPrimitive(type))
if ( value instanceof Boolean )
return ((Boolean) value).booleanValue()
? Primitive.TRUE : Primitive.FALSE;
else if ( isWrapperType( value.getClass() ) )
return new Primitive( value );
return value;
}
/**
Get the appropriate default value per JLS 4.5.4
*/
public static Primitive getDefaultValue( Class<?> type )
{
if ( type == null )
return Primitive.NULL;
if ( Boolean.TYPE == type || Boolean.class == type )
return Primitive.FALSE;
if ( Character.TYPE == type || Character.class == type )
return Primitive.ZERO_CHAR;
if ( Byte.TYPE == type || Byte.class == type )
return Primitive.ZERO_BYTE;
if ( Short.TYPE == type || Short.class == type )
return Primitive.ZERO_SHORT;
if ( Integer.TYPE == type || Integer.class == type )
return Primitive.ZERO_INT;
if ( Long.TYPE == type || Long.class == type )
return Primitive.ZERO_LONG;
if ( Float.TYPE == type || Float.class == type )
return Primitive.ZERO_FLOAT;
if ( Double.TYPE == type || Double.class == type )
return Primitive.ZERO_DOUBLE;
if ( BigInteger.class == type )
return Primitive.ZERO_BIG_INTEGER;
if ( BigDecimal.class == type )
return Primitive.ZERO_BIG_DECIMAL;
return Primitive.NULL;
}
/**
Get the corresponding java.lang wrapper class for the primitive TYPE
class.
e.g. Integer.TYPE -> Integer.class
*/
public static Class<?> boxType( Class<?> primitiveType )
{
Class<?> c = wrapperMap.get( primitiveType );
if ( c != null && !c.isPrimitive() )
return c;
throw new InterpreterError(
"Not a primitive type: "+ primitiveType );
}
/**
Get the corresponding primitive TYPE class for the java.lang wrapper
class type.
e.g. Integer.class -> Integer.TYPE
*/
public static Class<?> unboxType( Class<?> wrapperType )
{
Class<?> c = wrapperMap.get( wrapperType );
if ( c != null && (c.isPrimitive() || c == wrapperType) )
return c;
throw new InterpreterError(
"Not a primitive wrapper type: "+wrapperType );
}
/**
Cast this bsh.Primitive value to a new bsh.Primitive value
This is usually a numeric type cast. Other cases include:
A boolean can be cast to boolen
null can be cast to any object type and remains null
Attempting to cast a void causes an exception
@param toType is the java object or primitive TYPE class
*/
public Primitive castToType( Class<?> toType, int operation )
throws UtilEvalError
{
return castPrimitive(
toType, getType()/*fromType*/, this/*fromValue*/,
false/*checkOnly*/, operation );
}
/*
Cast or check a cast of a primitive type to another type.
Normally both types are primitive (e.g. numeric), but a null value
(no type) may be cast to any type.
<p/>
@param toType is the target type of the cast. It is normally a
java primitive TYPE, but in the case of a null cast can be any object
type.
@param fromType is the java primitive TYPE type of the primitive to be
cast or null, to indicate that the fromValue was null or void.
@param fromValue is, optionally, the value to be converted. If
checkOnly is true fromValue must be null. If checkOnly is false,
fromValue must be non-null (Primitive.NULL is of course valid).
*/
static Primitive castPrimitive( Class<?> toType, Class<?> fromType,
Primitive fromValue, boolean checkOnly, int operation )
throws UtilEvalError {
// can't cast void to anything
if ( fromType == Void.TYPE )
if ( checkOnly )
return Types.INVALID_CAST;
else
throw Types.castError( StringUtil.typeString(toType),
"void value", operation );
// Do numeric cast
if ( !checkOnly && fromValue.isNumber() && Types.isNumeric(toType) )
return new Primitive( castNumber(toType, fromValue.numberValue()) );
if ( toType.isPrimitive() ) {
// Cast null value to primitive default value
if ( fromType == null && !Primitive.VOID.equals(fromValue) )
return checkOnly ? Types.VALID_CAST : getDefaultValue(toType);
if (toType == Boolean.TYPE)
return checkOnly ? Types.VALID_CAST : new Primitive( castWrapper(toType, fromValue) );
} else {
// Trying to cast primitive to an object type
// Primitive.NULL can be cast to any object type
if ( fromType == null )
return checkOnly ? Types.VALID_CAST :
Primitive.NULL;
if ( checkOnly )
return Types.INVALID_CAST;
throw Types.castError(
"object type " + toType.getName(), "primitive value", operation);
}
// can only cast boolean to boolean
if ( checkOnly && fromType == Boolean.TYPE ) {
if ( toType != Boolean.TYPE )
return Types.INVALID_CAST;
return Types.VALID_CAST;
}
// Only allow legal Java assignment unless we're a CAST operation
if ( operation == Types.ASSIGNMENT
&& !Types.isJavaAssignable( toType, fromType ) ) {
if ( checkOnly )
return Types.INVALID_CAST;
}
return checkOnly ? Types.VALID_CAST :
new Primitive( castWrapper(toType, fromValue) );
}
public static boolean isWrapperType( Class<?> type )
{
return null != type && wrapperMap.containsKey( type ) && !type.isPrimitive();
}
/**
Cast a primitive value represented by its java.lang wrapper type to the
specified java.lang wrapper type. e.g. Byte(5) to Integer(5) or
Integer(5) to Byte(5)
@param toType is the java TYPE type
@param value is the value in java.lang wrapper.
value may not be null.
*/
static Object castWrapper( Class<?> toType, Object value ) {
if ( Primitive.VOID.equals(value) )
return value;
value = Primitive.unwrap(value);
if ( !(Primitive.isWrapperType(toType) || toType.isPrimitive()) )
throw new InterpreterError("invalid type in castWrapper: "+toType);
// first promote char to Number type to avoid duplicating code
if ( value instanceof Character )
value = Integer.valueOf(((Character)value).charValue());
if ( toType == Boolean.TYPE ) {
if ( value instanceof Boolean )
return value;
throw new InterpreterError("cannot cast "+((value==null)?"null":value.getClass().getSimpleName())+" to boolean");
// else if ( value instanceof String )
// return !"".equals(String.valueOf(value));
// else if ( value instanceof Number )
// return ((Number) value).intValue() != 0;
// else
// return value != null;
}
if ( value == null && toType.isPrimitive() )
value = Primitive.unwrap(getDefaultValue(toType));
// throw new InterpreterError("cannot convert null to primitive");
if ( value instanceof String )
// try {
// value = Double.parseDouble(String.valueOf(value));
// } catch (NumberFormatException nfe) {
throw new InterpreterError("cannot cast string \""+value+"\" to number");
// }
// if ( value instanceof Boolean )
// value = (Boolean) value ? 1 : 0;
if ( !(value instanceof Number) )
throw new InterpreterError("bad type in cast "+StringUtil.typeValueString(value));
return castNumber(toType, (Number) value);
}
static Object castNumber(Class<?> toType, Number number) {
if (toType.isInstance(number) || toType == unboxType(number.getClass()))
return number;
if ((toType == Byte.class || toType == Byte.TYPE)
&& number.shortValue() <= 0xff && number.shortValue() >= Byte.MIN_VALUE)
return number.byteValue();
if ((toType == Short.class || toType == Short.TYPE)
&& number.intValue() <= Short.MAX_VALUE && number.intValue() >= Short.MIN_VALUE)
return number.shortValue();
if ((toType == Character.class || toType == Character.TYPE)
&& number.intValue() <= (int) Character.MAX_VALUE && number.intValue() >= (int) Character.MIN_VALUE)
return (char) number.intValue();
if ((toType == Integer.class || toType == Integer.TYPE)
&& number.longValue() <= Integer.MAX_VALUE && number.longValue() >= Integer.MIN_VALUE)
if (number instanceof Byte)
return Byte.toUnsignedInt(number.byteValue());
else
return number.intValue();
if ((toType == Float.class || toType == Float.TYPE)
&& !Float.isInfinite(number.floatValue()))
return number.floatValue();
if ((toType == Double.class || toType == Double.TYPE)
&& !Double.isInfinite(number.doubleValue()))
return number.doubleValue();
if ( number.doubleValue() == 0 ) {
if (toType == Long.class || toType == Long.TYPE)
return 0L;
if (toType == BigInteger.class)
return BigInteger.ZERO;
if (toType == BigDecimal.class)
return BigDecimal.ZERO.setScale(1);
} else if ( number.doubleValue() == 1 ) {
if (toType == Long.class || toType == Long.TYPE)
return 1L;
if (toType == BigInteger.class)
return BigInteger.ONE;
if (toType == BigDecimal.class)
return BigDecimal.ONE.setScale(1);
} else if (toType == BigDecimal.class) {
if ( number instanceof BigInteger )
return new BigDecimal((BigInteger) number).setScale(1);
if ( Types.isFloatingpoint(number) )
return BigDecimal.valueOf(number.doubleValue());
return new BigDecimal(number.longValue()).setScale(1);
} else {
BigInteger bi = null;
if ( number instanceof BigInteger )
bi = (BigInteger) number;
else if (number instanceof BigDecimal)
bi = ((BigDecimal) number).toBigInteger();
else if (Types.isFloatingpoint(number))
bi = BigDecimal.valueOf(number.doubleValue()).toBigInteger();
else
bi = BigInteger.valueOf(number.longValue());
if ((toType == Long.class || toType == Long.TYPE)
&& bi.compareTo(LONG_MIN) >= 0 && bi.compareTo(LONG_MAX) <= 0)
return number.longValue();
if ( toType == BigInteger.class )
return bi;
}
throw new InterpreterError("cannot assign number "+number+" to type "+toType.getSimpleName());
}
static Object castNumberStrictJava(Class<?> toType, Number number) {
if (toType == Byte.class || toType == Byte.TYPE)
return number.byteValue();
if (toType == Short.class || toType == Short.TYPE)
return number.shortValue();
if (toType == Character.class || toType == Character.TYPE)
return (char) number.intValue();
if (toType == Integer.class || toType == Integer.TYPE)
return number.intValue();
if (toType == Long.class || toType == Long.TYPE)
return number.longValue();
if (toType == Float.class || toType == Float.TYPE)
return number.floatValue();
if (toType == Double.class || toType == Double.TYPE)
return number.doubleValue();
return castNumber(toType, number);
}
}