Skip to content

Commit e30871a

Browse files
committed
Simplify array copy
1 parent a3efc6f commit e30871a

File tree

9 files changed

+22
-28
lines changed

9 files changed

+22
-28
lines changed

src/examples/Package.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import org.apache.bcel.classfile.ConstantUtf8;
3535
import org.apache.bcel.classfile.JavaClass;
3636
import org.apache.bcel.util.ClassPath;
37-
import org.apache.commons.lang3.ArraySorter;
3837

3938
/**
4039
* Package the client. Creates a jar file in the current directory that contains a minimal set of classes needed to run

src/main/java/org/apache/bcel/ExceptionConst.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package org.apache.bcel;
1818

19+
import org.apache.commons.lang3.ArrayUtils;
20+
1921
/**
2022
* Exception constants.
2123
*
@@ -120,13 +122,7 @@ public static Class<?>[] createExceptions(final EXCS type, final Class<?>... ext
120122

121123
// helper method to merge exception class arrays
122124
private static Class<?>[] mergeExceptions(final Class<?>[] input, final Class<?>... extraClasses) {
123-
final int extraLen = extraClasses == null ? 0 : extraClasses.length;
124-
final Class<?>[] excs = new Class<?>[input.length + extraLen];
125-
System.arraycopy(input, 0, excs, 0, input.length);
126-
if (extraLen > 0) {
127-
System.arraycopy(extraClasses, 0, excs, input.length, extraLen);
128-
}
129-
return excs;
125+
return ArrayUtils.addAll(input, extraClasses);
130126
}
131127

132128
}

src/main/java/org/apache/bcel/classfile/Unknown.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.DataInput;
2020
import java.io.DataOutputStream;
2121
import java.io.IOException;
22+
import java.util.Arrays;
2223
import java.util.HashMap;
2324
import java.util.Map;
2425

@@ -165,9 +166,9 @@ public String toString() {
165166
return "(Unknown attribute " + name + ")";
166167
}
167168
String hex;
168-
if (super.getLength() > 10) {
169-
final byte[] tmp = new byte[10];
170-
System.arraycopy(bytes, 0, tmp, 0, 10);
169+
final int limit = 10;
170+
if (super.getLength() > limit) {
171+
final byte[] tmp = Arrays.copyOf(bytes, limit);
171172
hex = Utility.toHexString(tmp) + "... (truncated)";
172173
} else {
173174
hex = Utility.toHexString(bytes);

src/main/java/org/apache/bcel/classfile/Utility.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,7 @@ public static byte[] decode(final String s, final boolean uncompress) throws IOE
671671
while ((b = gis.read()) >= 0) {
672672
tmp[count++] = (byte) b;
673673
}
674-
bytes = new byte[count];
675-
System.arraycopy(tmp, 0, bytes, 0, count);
674+
bytes = Arrays.copyOf(tmp, count);
676675
}
677676
return bytes;
678677
}

src/main/java/org/apache/bcel/generic/ConstantPoolGen.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.bcel.generic;
1818

19+
import java.util.Arrays;
1920
import java.util.HashMap;
2021
import java.util.Map;
2122

@@ -215,6 +216,8 @@ public ConstantPoolGen(final Constant[] cs) {
215216

216217
/**
217218
* Initialize with given constant pool.
219+
*
220+
* @param cp the constant pool.
218221
*/
219222
public ConstantPoolGen(final ConstantPool cp) {
220223
this(cp.getConstantPool());
@@ -563,9 +566,7 @@ public ConstantPool getConstantPool() {
563566
* @return constant pool with proper length
564567
*/
565568
public ConstantPool getFinalConstantPool() {
566-
final Constant[] cs = new Constant[index];
567-
System.arraycopy(constants, 0, cs, 0, index);
568-
return new ConstantPool(cs);
569+
return new ConstantPool(Arrays.copyOf(constants, index));
569570
}
570571

571572
private int getIndex(final Map<String, Integer> map, final String key) {

src/main/java/org/apache/bcel/generic/InstructionList.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.DataOutputStream;
2121
import java.io.IOException;
2222
import java.util.ArrayList;
23+
import java.util.Arrays;
2324
import java.util.HashMap;
2425
import java.util.Iterator;
2526
import java.util.List;
@@ -135,8 +136,7 @@ public InstructionList(final byte[] code) {
135136
} catch (final IOException e) {
136137
throw new ClassGenException(e.toString(), e);
137138
}
138-
bytePositions = new int[count]; // Trim to proper size
139-
System.arraycopy(pos, 0, bytePositions, 0, count);
139+
bytePositions = Arrays.copyOf(pos, count); // Trim to proper size
140140
/*
141141
* Pass 2: Look for BranchInstruction and update their targets, i.e., convert offsets to instruction handles.
142142
*/

src/main/java/org/apache/bcel/generic/SWITCH.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package org.apache.bcel.generic;
1818

19+
import java.util.Arrays;
20+
1921
/**
2022
* SWITCH - Branch depending on int value, generates either LOOKUPSWITCH or TABLESWITCH instruction, depending on
2123
* whether the match values (int[]) can be sorted with no gaps between the numbers.
@@ -78,10 +80,8 @@ private void fillup(final int maxGap, final InstructionHandle target) {
7880
tVec[count] = targets[i];
7981
count++;
8082
}
81-
match = new int[count];
82-
targets = new InstructionHandle[count];
83-
System.arraycopy(mVec, 0, match, 0, count);
84-
System.arraycopy(tVec, 0, targets, 0, count);
83+
match = Arrays.copyOf(mVec, count);
84+
targets = Arrays.copyOf(tVec, count);
8585
}
8686

8787
public Instruction getInstruction() {

src/main/java/org/apache/bcel/util/InstructionFinder.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.bcel.util;
1818

1919
import java.util.ArrayList;
20+
import java.util.Arrays;
2021
import java.util.HashMap;
2122
import java.util.Iterator;
2223
import java.util.List;
@@ -257,9 +258,7 @@ public final InstructionList getInstructionList() {
257258
* @return the matched piece of code as an array of instruction (handles)
258259
*/
259260
private InstructionHandle[] getMatch(final int matchedFrom, final int matchLength) {
260-
final InstructionHandle[] match = new InstructionHandle[matchLength];
261-
System.arraycopy(handles, matchedFrom, match, 0, matchLength);
262-
return match;
261+
return Arrays.copyOfRange(handles, matchedFrom, matchedFrom + matchLength);
263262
}
264263

265264
/**

src/test/java/org/apache/bcel/PerformanceTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.File;
2424
import java.io.IOException;
2525
import java.io.InputStream;
26+
import java.util.Arrays;
2627
import java.util.Enumeration;
2728
import java.util.jar.JarEntry;
2829
import java.util.jar.JarFile;
@@ -49,9 +50,7 @@ private static byte[] read(final InputStream is) throws IOException {
4950
final int n = is.read(b, len, b.length - len);
5051
if (n == -1) {
5152
if (len < b.length) {
52-
final byte[] c = new byte[len];
53-
System.arraycopy(b, 0, c, 0, len);
54-
b = c;
53+
b = Arrays.copyOf(b, len);
5554
}
5655
return b;
5756
}

0 commit comments

Comments
 (0)