Skip to content

Commit

Permalink
Merge pull request #760 from beanshell/issue_747
Browse files Browse the repository at this point in the history
Undo instantiation of boxed priimitie object arrays with default values
  • Loading branch information
nickl- authored Aug 10, 2024
2 parents 986553b + f32fade commit 53b0436
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 28 deletions.
19 changes: 0 additions & 19 deletions src/main/java/bsh/BSHAllocationExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.concurrent.CompletionException;

/**
Expand Down Expand Up @@ -308,8 +307,6 @@ private Object arrayNewInstance(
try {
Object arr = Array.newInstance(
type, dimensionsNode.definedDimensions);
if ( !interpreter.getStrictJava() )
arrayFillDefaultValue(arr);
return arr;
} catch( NegativeArraySizeException e1 ) {
throw new TargetError( e1, this, callstack );
Expand All @@ -318,20 +315,4 @@ private Object arrayNewInstance(
+ e.getMessage(), this, callstack, e);
}
}

/** Fill boxed numeric types with default numbers instead of nulls.
* @param arr the array to fill. */
private void arrayFillDefaultValue(Object arr) {
if (null == arr)
return;
Class<?> clas = arr.getClass();
Class<?> comp = Types.arrayElementType(clas);
if ( !comp.isPrimitive() )
if ( Types.arrayDimensions(clas) > 1 )
for ( int i = 0; i < Array.getLength(arr); i++ )
arrayFillDefaultValue(Array.get(arr, i));
else
Arrays.fill((Object[]) arr, Primitive.unwrap(
Primitive.getDefaultValue(comp)));
}
}
17 changes: 8 additions & 9 deletions src/test/resources/test-scripts/array1.bsh
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,35 @@ assertThat("foo bar baz", containsString("bar"));

a = new Integer[5];
a[0] = 1;
assertArrayEquals( a, new Integer[] {1,0,0,0,0} );
assertArrayEquals( a, new Integer[] {1,null,null,null,null} );

a = new byte[5];
a[0] = 1;
assertArrayEquals( a, new byte[] {1,0,0,0,0} );

a = new Byte[5];
a[0] = 1;
assertArrayEquals( a, new Byte[] {1,0,0,0,0} );
assertArrayEquals( a, new Byte[] {1,null,null,null,null} );

a = new short[5];
a[0] = 1;
assertArrayEquals( a, new short[] {1,0,0,0,0} );

a = new Short[5];
a[0] = 1;
assertArrayEquals( a, new Short[] {1,0,0,0,0} );
assertArrayEquals( a, new Short[] {1,null,null,null,null} );

a = new long[5];
a[0] = 1;
assertArrayEquals( a, new long[] {1,0,0,0,0} );

a = new Long[5];
a[0] = 1;
assertArrayEquals( a, new Long[] {1,0,0,0,0} );
assertArrayEquals( a, new Long[] {1,null,null,null,null} );

a = new BigInteger[5];
a[0] = BigInteger.valueOf(1L);
assertArrayEquals( a, new BigInteger[] {1,0,0,0,0});
assertArrayEquals( a, new BigInteger[] {1,null,null,null,null});


a = new float[5];
Expand All @@ -49,19 +49,19 @@ assertArrayEquals( a, new float[] {1.0,0.0,0.0,0.0,0.0}, 0.0f );

a = new Float[5];
a[0] = 1.0;
assertArrayEquals( a, new Float[] {1.0,0.0,0.0,0.0,0.0} );
assertArrayEquals( a, new Float[] {1.0,null,null,null,null} );

a = new double[5];
a[0] = 1.0;
assertArrayEquals( a, new double[] {1.0,0.0,0.0,0.0,0.0}, 0.0 );

a = new Double[5];
a[0] = 1.0;
assertArrayEquals( a, new Double[] {1.0,0.0,0.0,0.0,0.0} );
assertArrayEquals( a, new Double[] {1.0,null,null,null,null} );

a = new BigDecimal[5];
a[0] = BigDecimal.valueOf(1.0);
assertArrayEquals( a, new BigDecimal[] {1.0,0.0,0.0,0.0,0.0});
assertArrayEquals( a, new BigDecimal[] {1.0,null,null,null,null});



Expand All @@ -86,4 +86,3 @@ for (int i=0; i<iarray.length; )
assertArrayEquals( iarray, new int[] {1, 2, 3, 4, 5, 6});

complete();

0 comments on commit 53b0436

Please sign in to comment.