Skip to content

Commit b5f4a6a

Browse files
committed
Add createArrayWithType intrinsic
This is replaced by java_replace_intrinsics with a direct implementation that copies the input array's classid, and enables implementing functions such as ArrayList.toArray and Arrays.copyOf.
1 parent ca0c926 commit b5f4a6a

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/main/java/java/lang/Class.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,4 +606,19 @@ public Field getField(String name) throws NoSuchFieldException, SecurityExceptio
606606
public Method getMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException {
607607
return new Method(this, name, parameterTypes);
608608
}
609+
610+
/**
611+
* Returns the {@code Class} representing the component type of an
612+
* array. If this class does not represent an array class this method
613+
* returns null.
614+
*
615+
* @return the {@code Class} representing the component type of this
616+
* class if this class is an array
617+
* @see java.lang.reflect.Array
618+
* @since 1.1
619+
*/
620+
public Class<?> getComponentType() {
621+
CProver.notModelled();
622+
return CProver.nondetWithoutNullForNotModelled();
623+
}
609624
}

src/main/java/org/cprover/CProver.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.BufferedInputStream;
44
import java.io.PrintStream;
5+
import java.lang.reflect.Array;
56

67
public final class CProver
78
{
@@ -350,4 +351,18 @@ public static float doubleToFloat(double d)
350351
CProver.assume(d == (double) converted);
351352
return converted;
352353
}
354+
355+
/**
356+
* Instantiates (but does not populate) an array with type matching a given array,
357+
* but with a potentially different length. Used by ArrayList.toArray, for example,
358+
* whose internal array is an Object[] but must provide that array as a user-supplied
359+
* T[], copying the runtime type of whatever array the user provided as a template.
360+
*
361+
* The implementation given here is correct, but JBMC cannot currently understand these
362+
* reflective methods and therefore replaces this function with its own implementation.
363+
*/
364+
public static <T> T[] createArrayWithType(int length, T[] type) {
365+
Class typeClass = type.getClass();
366+
return (T[])Array.newInstance(typeClass.getComponentType(), length);
367+
}
353368
}

0 commit comments

Comments
 (0)