Skip to content

Commit e48a6c2

Browse files
committed
HHH-18976 Use dedicated methods in ArrayHelper for copy of well-known types
1 parent 8bd671a commit e48a6c2

File tree

6 files changed

+37
-15
lines changed

6 files changed

+37
-15
lines changed

hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/tracker/SimpleCollectionTracker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void add(String name, int size) {
3232
return;
3333
}
3434
}
35-
names = Arrays.copyOf( names, names.length + 1 );
35+
names = ArrayHelper.copyOf( names, names.length + 1 );
3636
names[names.length - 1] = name;
3737
sizes = Arrays.copyOf( sizes, sizes.length + 1 );
3838
sizes[sizes.length - 1] = size;

hibernate-core/src/main/java/org/hibernate/dialect/JsonHelper.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.time.OffsetDateTime;
1212
import java.time.format.DateTimeFormatter;
1313
import java.util.AbstractCollection;
14-
import java.util.Arrays;
1514
import java.util.Collection;
1615
import java.util.Iterator;
1716
import java.util.NoSuchElementException;
@@ -1546,13 +1545,12 @@ private static class CustomArrayList extends AbstractCollection<Object> implemen
15461545
Object[] array = ArrayHelper.EMPTY_OBJECT_ARRAY;
15471546
int size;
15481547

1549-
@AllowReflection
15501548
public void ensureCapacity(int minCapacity) {
15511549
int oldCapacity = array.length;
15521550
if ( minCapacity > oldCapacity ) {
15531551
int newCapacity = oldCapacity + ( oldCapacity >> 1 );
15541552
newCapacity = Math.max( Math.max( newCapacity, minCapacity ), 10 );
1555-
array = Arrays.copyOf( array, newCapacity );
1553+
array = ArrayHelper.copyOf( array, newCapacity );
15561554
}
15571555
}
15581556

@@ -1609,9 +1607,8 @@ public Object next() {
16091607
}
16101608

16111609
@Override
1612-
@AllowReflection
16131610
public Object[] toArray() {
1614-
return Arrays.copyOf( array, size );
1611+
return ArrayHelper.copyOf( array, size );
16151612
}
16161613

16171614
@Override

hibernate-core/src/main/java/org/hibernate/internal/util/collections/ArrayHelper.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,4 +532,34 @@ public static <T> T[] newInstance(Class<T> elementType, int length) {
532532
return (T[]) Array.newInstance( elementType, length );
533533
}
534534

535+
/**
536+
* Alternative to {@link Arrays#copyOf(Object[], int)} that is specific to String[],
537+
* but does not require reflection.
538+
*
539+
* @param original the array to be copied
540+
* @param newLength the length of the copy to be returned
541+
* @return a copy of the original array, truncated or padded with nulls
542+
* to obtain the specified length
543+
*/
544+
public static String[] copyOf(String[] original, int newLength) {
545+
String[] copy = new String[newLength];
546+
System.arraycopy( original, 0, copy, 0, Math.min( original.length, newLength ) );
547+
return copy;
548+
}
549+
550+
/**
551+
* Alternative to {@link Arrays#copyOf(Object[], int)} that is specific to Object[] (no support for generics),
552+
* but does not require reflection.
553+
*
554+
* @param original the array to be copied
555+
* @param newLength the length of the copy to be returned
556+
* @return a copy of the original array, truncated or padded with nulls
557+
* to obtain the specified length
558+
*/
559+
public static Object[] copyOf(Object[] original, int newLength) {
560+
Object[] copy = new Object[newLength];
561+
System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
562+
return copy;
563+
}
564+
535565
}

hibernate-core/src/main/java/org/hibernate/internal/util/collections/StandardStack.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
*/
55
package org.hibernate.internal.util.collections;
66

7-
import org.hibernate.internal.build.AllowReflection;
87

9-
import java.util.Arrays;
108
import java.util.NoSuchElementException;
119
import java.util.function.BiFunction;
1210
import java.util.function.Consumer;
@@ -144,11 +142,10 @@ public <X, Y> X findCurrentFirstWithParameter(Y parameter, BiFunction<T, Y, X> b
144142
return null;
145143
}
146144

147-
@AllowReflection
148145
private void grow() {
149146
final int oldCapacity = elements.length;
150147
final int jump = ( oldCapacity < 64 ) ? ( oldCapacity + 2 ) : ( oldCapacity >> 1 );
151-
elements = Arrays.copyOf( elements, oldCapacity + jump );
148+
elements = ArrayHelper.copyOf( elements, oldCapacity + jump );
152149
}
153150

154151
}

hibernate-core/src/main/java/org/hibernate/persister/entity/mutation/TableSet.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
*/
55
package org.hibernate.persister.entity.mutation;
66

7-
import java.util.Arrays;
87
import java.util.BitSet;
98

10-
import org.hibernate.internal.build.AllowReflection;
9+
import org.hibernate.internal.util.collections.ArrayHelper;
1110
import org.hibernate.sql.model.TableMapping;
1211

1312
/**
@@ -69,14 +68,13 @@ private boolean addForChecks(final TableMapping tableMapping) {
6968
}
7069

7170
//Meant for assertions only
72-
@AllowReflection
7371
private void ensureCapacity(final int position) {
7472
final int increments = 3; //Needs to be at least 1.
7573
if ( checks == null ) {
7674
checks = new Object[position + increments];
7775
}
7876
else if ( checks.length <= position ) {
79-
checks = Arrays.copyOf( checks, position + increments );
77+
checks = ArrayHelper.copyOf( checks, position + increments );
8078
}
8179
}
8280

rules/forbidden-apis.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ org.hibernate.internal.util.collections.ArrayHelper#newInstance(java.lang.Class,
1515
org.hibernate.internal.util.collections.ArrayHelper#filledArray(java.lang.Object, java.lang.Class, int)
1616
org.hibernate.internal.util.collections.ArrayHelper#join(java.lang.Object[], java.lang.Object[])
1717

18-
@defaultMessage This forbidden method requires reflection and may not work in natively compiled applications. If you really must use this forbidden method, annotate the calling method with @AllowReflection.
18+
@defaultMessage Use a type-specific 'ArrayHelper.copyOf' instead if possible. This forbidden method requires reflection and may not work in natively compiled applications. If you really must use this forbidden method, annotate the calling method with @AllowReflection.
1919

2020
java.util.Arrays#copyOf(java.lang.Object[], int)
2121
java.util.Arrays#copyOf(java.lang.Object[], int, java.lang.Class)

0 commit comments

Comments
 (0)