This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-bcel.git
The following commit(s) were added to refs/heads/master by this push: new 0a2c18f0 Use JRE APIs 0a2c18f0 is described below commit 0a2c18f029601402b7d4f5c76e382c4764f69d11 Author: Gary David Gregory (Code signing key) <ggreg...@apache.org> AuthorDate: Mon Oct 3 22:37:57 2022 -0400 Use JRE APIs * Use Arrays.fill() * Better use of toArray() * Better parameter name * Better use of our own API --- .../java/org/apache/bcel/classfile/Attribute.java | 20 +- src/main/java/org/apache/bcel/classfile/Code.java | 13 +- .../org/apache/bcel/classfile/ExceptionTable.java | 5 +- .../org/apache/bcel/classfile/FieldOrMethod.java | 6 +- .../org/apache/bcel/classfile/InnerClasses.java | 5 +- .../java/org/apache/bcel/classfile/JavaClass.java | 29 ++- .../org/apache/bcel/classfile/LineNumberTable.java | 5 +- .../apache/bcel/classfile/LocalVariableTable.java | 5 +- .../bcel/classfile/LocalVariableTypeTable.java | 6 +- .../apache/bcel/classfile/MethodParameters.java | 5 +- .../java/org/apache/bcel/classfile/Module.java | 17 +- .../org/apache/bcel/classfile/ModulePackages.java | 5 +- .../org/apache/bcel/classfile/NestMembers.java | 5 +- .../java/org/apache/bcel/classfile/StackMap.java | 39 ++-- .../org/apache/bcel/classfile/StackMapEntry.java | 9 +- .../java/org/apache/bcel/classfile/Unknown.java | 16 +- .../apache/bcel/generic/AnnotationEntryGen.java | 6 +- .../java/org/apache/bcel/generic/ClassGen.java | 41 ++-- .../org/apache/bcel/generic/CodeExceptionGen.java | 2 + .../apache/bcel/generic/FieldGenOrMethodGen.java | 13 +- .../org/apache/bcel/generic/InstructionList.java | 4 +- .../org/apache/bcel/generic/LineNumberGen.java | 2 + .../java/org/apache/bcel/generic/MethodGen.java | 73 ++----- src/main/java/org/apache/bcel/generic/Type.java | 8 +- .../apache/bcel/util/ClassLoaderRepository.java | 2 +- src/main/java/org/apache/bcel/util/ClassPath.java | 6 +- src/main/java/org/apache/bcel/util/ClassSet.java | 16 +- .../java/org/apache/bcel/util/ClassVector.java | 4 +- .../org/apache/bcel/util/InstructionFinder.java | 9 +- .../org/apache/bcel/util/SyntheticRepository.java | 10 +- .../java/org/apache/bcel/verifier/Verifier.java | 20 +- .../org/apache/bcel/verifier/VerifierAppFrame.java | 11 +- .../org/apache/bcel/verifier/VerifierFactory.java | 28 ++- .../bcel/verifier/VerifierFactoryListModel.java | 3 +- .../org/apache/bcel/verifier/statics/IntList.java | 16 +- .../bcel/verifier/statics/LocalVariablesInfo.java | 6 +- .../verifier/structurals/ControlFlowGraph.java | 9 +- .../verifier/structurals/ExceptionHandlers.java | 5 +- .../verifier/structurals/ExecutionVisitor.java | 215 +++++++-------------- .../bcel/verifier/structurals/LocalVariables.java | 6 +- .../bcel/verifier/structurals/OperandStack.java | 4 +- .../bcel/verifier/structurals/Subroutines.java | 19 +- .../java/org/apache/bcel/AbstractTestCase.java | 2 +- .../GeneratingAnnotatedClassesTestCase.java | 2 +- .../org/apache/bcel/util/BCELifierTestCase.java | 6 +- .../bcel/verifier/VerifyBadClassesTestCase.java | 3 +- 46 files changed, 274 insertions(+), 467 deletions(-) diff --git a/src/main/java/org/apache/bcel/classfile/Attribute.java b/src/main/java/org/apache/bcel/classfile/Attribute.java index 1ecfc9b0..afb2f207 100644 --- a/src/main/java/org/apache/bcel/classfile/Attribute.java +++ b/src/main/java/org/apache/bcel/classfile/Attribute.java @@ -47,26 +47,26 @@ public abstract class Attribute implements Cloneable, Node { private static final boolean debug = Boolean.getBoolean(Attribute.class.getCanonicalName() + ".debug"); // Debugging on/off - private static final Map<String, Object> readers = new HashMap<>(); + private static final Map<String, Object> READERS = new HashMap<>(); /** * Empty array. * * @since 6.6.0 */ - public static final Attribute[] EMPTY_ATTRIBUTE_ARRAY = {}; + public static final Attribute[] EMPTY_ARRAY = {}; /** * Add an Attribute reader capable of parsing (user-defined) attributes named "name". You should not add readers for the * standard attributes such as "LineNumberTable", because those are handled internally. * * @param name the name of the attribute as stored in the class file - * @param r the reader object + * @param attributeReader the reader object * @deprecated (6.0) Use {@link #addAttributeReader(String, UnknownAttributeReader)} instead */ @java.lang.Deprecated - public static void addAttributeReader(final String name, final AttributeReader r) { - readers.put(name, r); + public static void addAttributeReader(final String name, final AttributeReader attributeReader) { + READERS.put(name, attributeReader); } /** @@ -74,10 +74,10 @@ public abstract class Attribute implements Cloneable, Node { * standard attributes such as "LineNumberTable", because those are handled internally. * * @param name the name of the attribute as stored in the class file - * @param r the reader object + * @param unknownAttributeReader the reader object */ - public static void addAttributeReader(final String name, final UnknownAttributeReader r) { - readers.put(name, r); + public static void addAttributeReader(final String name, final UnknownAttributeReader unknownAttributeReader) { + READERS.put(name, unknownAttributeReader); } protected static void println(final String msg) { @@ -120,7 +120,7 @@ public abstract class Attribute implements Cloneable, Node { // Call proper constructor, depending on `tag' switch (tag) { case Const.ATTR_UNKNOWN: - final Object r = readers.get(name); + final Object r = READERS.get(name); if (r instanceof UnknownAttributeReader) { return ((UnknownAttributeReader) r).createAttribute(name_index, length, file, constant_pool); } @@ -212,7 +212,7 @@ public abstract class Attribute implements Cloneable, Node { * @param name the name of the attribute as stored in the class file */ public static void removeAttributeReader(final String name) { - readers.remove(name); + READERS.remove(name); } /** diff --git a/src/main/java/org/apache/bcel/classfile/Code.java b/src/main/java/org/apache/bcel/classfile/Code.java index e0cba492..c2f9a844 100644 --- a/src/main/java/org/apache/bcel/classfile/Code.java +++ b/src/main/java/org/apache/bcel/classfile/Code.java @@ -20,6 +20,7 @@ package org.apache.bcel.classfile; import java.io.DataInput; import java.io.DataOutputStream; import java.io.IOException; +import java.util.Arrays; import org.apache.bcel.Const; import org.apache.commons.lang3.ArrayUtils; @@ -106,7 +107,7 @@ public final class Code extends Attribute { this.maxLocals = maxLocals; this.code = code != null ? code : ArrayUtils.EMPTY_BYTE_ARRAY; this.exceptionTable = exceptionTable != null ? exceptionTable : CodeException.EMPTY_CODE_EXCEPTION_ARRAY; - this.attributes = attributes != null ? attributes : EMPTY_ATTRIBUTE_ARRAY; + this.attributes = attributes != null ? attributes : EMPTY_ARRAY; super.setLength(calculateLength()); // Adjust length } @@ -149,13 +150,9 @@ public final class Code extends Attribute { } c.setConstantPool(constantPool); c.exceptionTable = new CodeException[exceptionTable.length]; - for (int i = 0; i < exceptionTable.length; i++) { - c.exceptionTable[i] = exceptionTable[i].copy(); - } + Arrays.setAll(c.exceptionTable, i -> exceptionTable[i].copy()); c.attributes = new Attribute[attributes.length]; - for (int i = 0; i < attributes.length; i++) { - c.attributes[i] = attributes[i].copy(constantPool); - } + Arrays.setAll(c.attributes, i -> attributes[i].copy(constantPool)); return c; } @@ -258,7 +255,7 @@ public final class Code extends Attribute { * @param attributes the attributes to set for this Code */ public void setAttributes(final Attribute[] attributes) { - this.attributes = attributes != null ? attributes : EMPTY_ATTRIBUTE_ARRAY; + this.attributes = attributes != null ? attributes : EMPTY_ARRAY; super.setLength(calculateLength()); // Adjust length } diff --git a/src/main/java/org/apache/bcel/classfile/ExceptionTable.java b/src/main/java/org/apache/bcel/classfile/ExceptionTable.java index 1bd66028..c06719c3 100644 --- a/src/main/java/org/apache/bcel/classfile/ExceptionTable.java +++ b/src/main/java/org/apache/bcel/classfile/ExceptionTable.java @@ -20,6 +20,7 @@ package org.apache.bcel.classfile; import java.io.DataInput; import java.io.DataOutputStream; import java.io.IOException; +import java.util.Arrays; import org.apache.bcel.Const; import org.apache.commons.lang3.ArrayUtils; @@ -125,9 +126,7 @@ public final class ExceptionTable extends Attribute { */ public String[] getExceptionNames() { final String[] names = new String[exceptionIndexTable.length]; - for (int i = 0; i < exceptionIndexTable.length; i++) { - names[i] = super.getConstantPool().getConstantString(exceptionIndexTable[i], Const.CONSTANT_Class).replace('/', '.'); - } + Arrays.setAll(names, i -> super.getConstantPool().getConstantString(exceptionIndexTable[i], Const.CONSTANT_Class).replace('/', '.')); return names; } diff --git a/src/main/java/org/apache/bcel/classfile/FieldOrMethod.java b/src/main/java/org/apache/bcel/classfile/FieldOrMethod.java index faf11c0d..dc1327b2 100644 --- a/src/main/java/org/apache/bcel/classfile/FieldOrMethod.java +++ b/src/main/java/org/apache/bcel/classfile/FieldOrMethod.java @@ -21,6 +21,7 @@ import java.io.DataInput; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; +import java.util.Arrays; import org.apache.bcel.Const; @@ -137,10 +138,7 @@ public abstract class FieldOrMethod extends AccessFlags implements Cloneable, No c.attributes = new Attribute[attributes.length]; c.attributes_count = attributes_count; // init deprecated field - for (int i = 0; i < attributes.length; i++) { - c.attributes[i] = attributes[i].copy(constant_pool); - } - + Arrays.setAll(c.attributes, i -> attributes[i].copy(constant_pool)); return c; } diff --git a/src/main/java/org/apache/bcel/classfile/InnerClasses.java b/src/main/java/org/apache/bcel/classfile/InnerClasses.java index 6fcc05de..b96962cd 100644 --- a/src/main/java/org/apache/bcel/classfile/InnerClasses.java +++ b/src/main/java/org/apache/bcel/classfile/InnerClasses.java @@ -20,6 +20,7 @@ package org.apache.bcel.classfile; import java.io.DataInput; import java.io.DataOutputStream; import java.io.IOException; +import java.util.Arrays; import java.util.Iterator; import java.util.stream.Stream; @@ -96,9 +97,7 @@ public final class InnerClasses extends Attribute implements Iterable<InnerClass // TODO this could be recoded to use a lower level constructor after creating a copy of the inner classes final InnerClasses c = (InnerClasses) clone(); c.innerClasses = new InnerClass[innerClasses.length]; - for (int i = 0; i < innerClasses.length; i++) { - c.innerClasses[i] = innerClasses[i].copy(); - } + Arrays.setAll(c.innerClasses, i -> innerClasses[i].copy()); c.setConstantPool(constantPool); return c; } diff --git a/src/main/java/org/apache/bcel/classfile/JavaClass.java b/src/main/java/org/apache/bcel/classfile/JavaClass.java index 2571cf4a..3d268e21 100644 --- a/src/main/java/org/apache/bcel/classfile/JavaClass.java +++ b/src/main/java/org/apache/bcel/classfile/JavaClass.java @@ -24,6 +24,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.Set; @@ -47,7 +48,12 @@ import org.apache.commons.lang3.ArrayUtils; */ public class JavaClass extends AccessFlags implements Cloneable, Node, Comparable<JavaClass> { - static final JavaClass[] EMPTY_ARRAY = {}; + /** + * Empty array. + * + * @since 6.6.0 + */ + public static final JavaClass[] EMPTY_ARRAY = {}; public static final byte HEAP = 1; public static final byte FILE = 2; @@ -176,7 +182,7 @@ public class JavaClass extends AccessFlags implements Cloneable, Node, Comparabl interfaces = ArrayUtils.EMPTY_INT_ARRAY; } if (attributes == null) { - attributes = Attribute.EMPTY_ATTRIBUTE_ARRAY; + attributes = Attribute.EMPTY_ARRAY; } if (fields == null) { fields = Field.EMPTY_FIELD_ARRAY; @@ -278,28 +284,21 @@ public class JavaClass extends AccessFlags implements Cloneable, Node, Comparabl * @return deep copy of this class */ public JavaClass copy() { - JavaClass c = null; try { - c = (JavaClass) clone(); + final JavaClass c = (JavaClass) clone(); c.constantPool = constantPool.copy(); c.interfaces = interfaces.clone(); c.interfaceNames = interfaceNames.clone(); c.fields = new Field[fields.length]; - for (int i = 0; i < fields.length; i++) { - c.fields[i] = fields[i].copy(c.constantPool); - } + Arrays.setAll(c.fields, i -> fields[i].copy(c.constantPool)); c.methods = new Method[methods.length]; - for (int i = 0; i < methods.length; i++) { - c.methods[i] = methods[i].copy(c.constantPool); - } + Arrays.setAll(c.methods, i -> methods[i].copy(c.constantPool)); c.attributes = new Attribute[attributes.length]; - for (int i = 0; i < attributes.length; i++) { - c.attributes[i] = attributes[i].copy(c.constantPool); - } + Arrays.setAll(c.attributes, i -> attributes[i].copy(c.constantPool)); + return c; } catch (final CloneNotSupportedException e) { - // TODO should this throw? + return null; } - return c; } /** diff --git a/src/main/java/org/apache/bcel/classfile/LineNumberTable.java b/src/main/java/org/apache/bcel/classfile/LineNumberTable.java index c350023f..e357dd82 100644 --- a/src/main/java/org/apache/bcel/classfile/LineNumberTable.java +++ b/src/main/java/org/apache/bcel/classfile/LineNumberTable.java @@ -20,6 +20,7 @@ package org.apache.bcel.classfile; import java.io.DataInput; import java.io.DataOutputStream; import java.io.IOException; +import java.util.Arrays; import java.util.Iterator; import java.util.stream.Stream; @@ -97,9 +98,7 @@ public final class LineNumberTable extends Attribute implements Iterable<LineNum // lineNumberTable to be made final final LineNumberTable c = (LineNumberTable) clone(); c.lineNumberTable = new LineNumber[lineNumberTable.length]; - for (int i = 0; i < lineNumberTable.length; i++) { - c.lineNumberTable[i] = lineNumberTable[i].copy(); - } + Arrays.setAll(c.lineNumberTable, i -> lineNumberTable[i].copy()); c.setConstantPool(constantPool); return c; } diff --git a/src/main/java/org/apache/bcel/classfile/LocalVariableTable.java b/src/main/java/org/apache/bcel/classfile/LocalVariableTable.java index 12154cf3..c87fc521 100644 --- a/src/main/java/org/apache/bcel/classfile/LocalVariableTable.java +++ b/src/main/java/org/apache/bcel/classfile/LocalVariableTable.java @@ -20,6 +20,7 @@ package org.apache.bcel.classfile; import java.io.DataInput; import java.io.DataOutputStream; import java.io.IOException; +import java.util.Arrays; import java.util.Iterator; import java.util.stream.Stream; @@ -91,9 +92,7 @@ public class LocalVariableTable extends Attribute implements Iterable<LocalVaria public Attribute copy(final ConstantPool constantPool) { final LocalVariableTable c = (LocalVariableTable) clone(); c.localVariableTable = new LocalVariable[localVariableTable.length]; - for (int i = 0; i < localVariableTable.length; i++) { - c.localVariableTable[i] = localVariableTable[i].copy(); - } + Arrays.setAll(c.localVariableTable, i -> localVariableTable[i].copy()); c.setConstantPool(constantPool); return c; } diff --git a/src/main/java/org/apache/bcel/classfile/LocalVariableTypeTable.java b/src/main/java/org/apache/bcel/classfile/LocalVariableTypeTable.java index 4c74d09f..0b6a1255 100644 --- a/src/main/java/org/apache/bcel/classfile/LocalVariableTypeTable.java +++ b/src/main/java/org/apache/bcel/classfile/LocalVariableTypeTable.java @@ -20,6 +20,7 @@ package org.apache.bcel.classfile; import java.io.DataInput; import java.io.DataOutputStream; import java.io.IOException; +import java.util.Arrays; import java.util.Iterator; import java.util.stream.Stream; @@ -93,10 +94,7 @@ public class LocalVariableTypeTable extends Attribute implements Iterable<LocalV final LocalVariableTypeTable c = (LocalVariableTypeTable) clone(); c.localVariableTypeTable = new LocalVariable[localVariableTypeTable.length]; - for (int i = 0; i < localVariableTypeTable.length; i++) { - c.localVariableTypeTable[i] = localVariableTypeTable[i].copy(); - } - + Arrays.setAll(c.localVariableTypeTable, i -> localVariableTypeTable[i].copy()); c.setConstantPool(constant_pool); return c; } diff --git a/src/main/java/org/apache/bcel/classfile/MethodParameters.java b/src/main/java/org/apache/bcel/classfile/MethodParameters.java index 7c710b0c..f8eaefbf 100644 --- a/src/main/java/org/apache/bcel/classfile/MethodParameters.java +++ b/src/main/java/org/apache/bcel/classfile/MethodParameters.java @@ -20,6 +20,7 @@ package org.apache.bcel.classfile; import java.io.DataInput; import java.io.DataOutputStream; import java.io.IOException; +import java.util.Arrays; import java.util.Iterator; import java.util.stream.Stream; @@ -61,9 +62,7 @@ public class MethodParameters extends Attribute implements Iterable<MethodParame final MethodParameters c = (MethodParameters) clone(); c.parameters = new MethodParameter[parameters.length]; - for (int i = 0; i < parameters.length; i++) { - c.parameters[i] = parameters[i].copy(); - } + Arrays.setAll(c.parameters, i -> parameters[i].copy()); c.setConstantPool(constantPool); return c; } diff --git a/src/main/java/org/apache/bcel/classfile/Module.java b/src/main/java/org/apache/bcel/classfile/Module.java index 1d1eb746..ae2b4e70 100644 --- a/src/main/java/org/apache/bcel/classfile/Module.java +++ b/src/main/java/org/apache/bcel/classfile/Module.java @@ -20,6 +20,7 @@ package org.apache.bcel.classfile; import java.io.DataInput; import java.io.DataOutputStream; import java.io.IOException; +import java.util.Arrays; import org.apache.bcel.Const; @@ -111,24 +112,16 @@ public final class Module extends Attribute { final Module c = (Module) clone(); c.requiresTable = new ModuleRequires[requiresTable.length]; - for (int i = 0; i < requiresTable.length; i++) { - c.requiresTable[i] = requiresTable[i].copy(); - } + Arrays.setAll(c.requiresTable, i -> requiresTable[i].copy()); c.exportsTable = new ModuleExports[exportsTable.length]; - for (int i = 0; i < exportsTable.length; i++) { - c.exportsTable[i] = exportsTable[i].copy(); - } + Arrays.setAll(c.exportsTable, i -> exportsTable[i].copy()); c.opensTable = new ModuleOpens[opensTable.length]; - for (int i = 0; i < opensTable.length; i++) { - c.opensTable[i] = opensTable[i].copy(); - } + Arrays.setAll(c.opensTable, i -> opensTable[i].copy()); c.providesTable = new ModuleProvides[providesTable.length]; - for (int i = 0; i < providesTable.length; i++) { - c.providesTable[i] = providesTable[i].copy(); - } + Arrays.setAll(c.providesTable, i -> providesTable[i].copy()); c.setConstantPool(constantPool); return c; diff --git a/src/main/java/org/apache/bcel/classfile/ModulePackages.java b/src/main/java/org/apache/bcel/classfile/ModulePackages.java index f14295f1..1624a9c8 100644 --- a/src/main/java/org/apache/bcel/classfile/ModulePackages.java +++ b/src/main/java/org/apache/bcel/classfile/ModulePackages.java @@ -20,6 +20,7 @@ package org.apache.bcel.classfile; import java.io.DataInput; import java.io.DataOutputStream; import java.io.IOException; +import java.util.Arrays; import org.apache.bcel.Const; import org.apache.commons.lang3.ArrayUtils; @@ -130,9 +131,7 @@ public final class ModulePackages extends Attribute { */ public String[] getPackageNames() { final String[] names = new String[packageIndexTable.length]; - for (int i = 0; i < packageIndexTable.length; i++) { - names[i] = super.getConstantPool().getConstantString(packageIndexTable[i], Const.CONSTANT_Package).replace('/', '.'); - } + Arrays.setAll(names, i -> super.getConstantPool().getConstantString(packageIndexTable[i], Const.CONSTANT_Package).replace('/', '.')); return names; } diff --git a/src/main/java/org/apache/bcel/classfile/NestMembers.java b/src/main/java/org/apache/bcel/classfile/NestMembers.java index 66367b17..9fc38ccb 100644 --- a/src/main/java/org/apache/bcel/classfile/NestMembers.java +++ b/src/main/java/org/apache/bcel/classfile/NestMembers.java @@ -20,6 +20,7 @@ package org.apache.bcel.classfile; import java.io.DataInput; import java.io.DataOutputStream; import java.io.IOException; +import java.util.Arrays; import org.apache.bcel.Const; import org.apache.commons.lang3.ArrayUtils; @@ -124,9 +125,7 @@ public final class NestMembers extends Attribute { */ public String[] getClassNames() { final String[] names = new String[classes.length]; - for (int i = 0; i < classes.length; i++) { - names[i] = super.getConstantPool().getConstantString(classes[i], Const.CONSTANT_Class).replace('/', '.'); - } + Arrays.setAll(names, i -> super.getConstantPool().getConstantString(classes[i], Const.CONSTANT_Class).replace('/', '.')); return names; } diff --git a/src/main/java/org/apache/bcel/classfile/StackMap.java b/src/main/java/org/apache/bcel/classfile/StackMap.java index b6632aef..a152cbb7 100644 --- a/src/main/java/org/apache/bcel/classfile/StackMap.java +++ b/src/main/java/org/apache/bcel/classfile/StackMap.java @@ -20,6 +20,7 @@ package org.apache.bcel.classfile; import java.io.DataInput; import java.io.DataOutputStream; import java.io.IOException; +import java.util.Arrays; import org.apache.bcel.Const; @@ -35,7 +36,7 @@ import org.apache.bcel.Const; */ public final class StackMap extends Attribute { - private StackMapEntry[] map; // Table of stack map entries + private StackMapEntry[] table; // Table of stack map entries /** * Construct object from input stream. @@ -49,9 +50,9 @@ public final class StackMap extends Attribute { StackMap(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool) throws IOException { this(name_index, length, (StackMapEntry[]) null, constant_pool); final int map_length = input.readUnsignedShort(); - map = new StackMapEntry[map_length]; + table = new StackMapEntry[map_length]; for (int i = 0; i < map_length; i++) { - map[i] = new StackMapEntry(input, constant_pool); + table[i] = new StackMapEntry(input, constant_pool); } } @@ -66,7 +67,7 @@ public final class StackMap extends Attribute { */ public StackMap(final int name_index, final int length, final StackMapEntry[] map, final ConstantPool constant_pool) { super(Const.ATTR_STACK_MAP, name_index, length, constant_pool); - this.map = map; + this.table = map; } /** @@ -86,10 +87,8 @@ public final class StackMap extends Attribute { @Override public Attribute copy(final ConstantPool constantPool) { final StackMap c = (StackMap) clone(); - c.map = new StackMapEntry[map.length]; - for (int i = 0; i < map.length; i++) { - c.map[i] = map[i].copy(); - } + c.table = new StackMapEntry[table.length]; + Arrays.setAll(c.table, i -> table[i].copy()); c.setConstantPool(constantPool); return c; } @@ -103,30 +102,30 @@ public final class StackMap extends Attribute { @Override public void dump(final DataOutputStream file) throws IOException { super.dump(file); - file.writeShort(map.length); - for (final StackMapEntry entry : map) { + file.writeShort(table.length); + for (final StackMapEntry entry : table) { entry.dump(file); } } public int getMapLength() { - return map == null ? 0 : map.length; + return table == null ? 0 : table.length; } /** * @return Array of stack map entries */ public StackMapEntry[] getStackMap() { - return map; + return table; } /** - * @param map Array of stack map entries + * @param table Array of stack map entries */ - public void setStackMap(final StackMapEntry[] map) { - this.map = map; + public void setStackMap(final StackMapEntry[] table) { + this.table = table; int len = 2; // Length of 'number_of_entries' field prior to the array of stack maps - for (final StackMapEntry element : map) { + for (final StackMapEntry element : table) { len += element.getMapEntrySize(); } setLength(len); @@ -139,10 +138,10 @@ public final class StackMap extends Attribute { public String toString() { final StringBuilder buf = new StringBuilder("StackMap("); int running_offset = -1; // no +1 on first entry - for (int i = 0; i < map.length; i++) { - running_offset = map[i].getByteCodeOffset() + running_offset + 1; - buf.append(String.format("%n@%03d %s", running_offset, map[i])); - if (i < map.length - 1) { + for (int i = 0; i < table.length; i++) { + running_offset = table[i].getByteCodeOffset() + running_offset + 1; + buf.append(String.format("%n@%03d %s", running_offset, table[i])); + if (i < table.length - 1) { buf.append(", "); } } diff --git a/src/main/java/org/apache/bcel/classfile/StackMapEntry.java b/src/main/java/org/apache/bcel/classfile/StackMapEntry.java index fbea575e..5145e4a5 100644 --- a/src/main/java/org/apache/bcel/classfile/StackMapEntry.java +++ b/src/main/java/org/apache/bcel/classfile/StackMapEntry.java @@ -20,6 +20,7 @@ package org.apache.bcel.classfile; import java.io.DataInput; import java.io.DataOutputStream; import java.io.IOException; +import java.util.Arrays; import org.apache.bcel.Const; @@ -152,13 +153,9 @@ public final class StackMapEntry implements Node, Cloneable { } e.typesOfLocals = new StackMapType[typesOfLocals.length]; - for (int i = 0; i < typesOfLocals.length; i++) { - e.typesOfLocals[i] = typesOfLocals[i].copy(); - } + Arrays.setAll(e.typesOfLocals, i -> typesOfLocals[i].copy()); e.typesOfStackItems = new StackMapType[typesOfStackItems.length]; - for (int i = 0; i < typesOfStackItems.length; i++) { - e.typesOfStackItems[i] = typesOfStackItems[i].copy(); - } + Arrays.setAll(e.typesOfStackItems, i -> typesOfStackItems[i].copy()); return e; } diff --git a/src/main/java/org/apache/bcel/classfile/Unknown.java b/src/main/java/org/apache/bcel/classfile/Unknown.java index c2affe40..82d8f630 100644 --- a/src/main/java/org/apache/bcel/classfile/Unknown.java +++ b/src/main/java/org/apache/bcel/classfile/Unknown.java @@ -37,16 +37,20 @@ import org.apache.bcel.Const; */ public final class Unknown extends Attribute { - private static final Map<String, Unknown> unknownAttributes = new HashMap<>(); + private static final Unknown[] EMPTY_ARRAY = {}; + + private static final Map<String, Unknown> UNKNOWN_ATTRIBUTES = new HashMap<>(); /** * @return array of unknown attributes, but just one for each kind. */ static Unknown[] getUnknownAttributes() { - final Unknown[] unknowns = new Unknown[unknownAttributes.size()]; - unknownAttributes.values().toArray(unknowns); - unknownAttributes.clear(); - return unknowns; + try { + return UNKNOWN_ATTRIBUTES.values().toArray(EMPTY_ARRAY); + } finally { + // TODO Does this really make sense? + UNKNOWN_ATTRIBUTES.clear(); + } } private byte[] bytes; @@ -65,7 +69,7 @@ public final class Unknown extends Attribute { super(Const.ATTR_UNKNOWN, name_index, length, constant_pool); this.bytes = bytes; name = ((ConstantUtf8) constant_pool.getConstant(name_index, Const.CONSTANT_Utf8)).getBytes(); - unknownAttributes.put(name, this); + UNKNOWN_ATTRIBUTES.put(name, this); } /** diff --git a/src/main/java/org/apache/bcel/generic/AnnotationEntryGen.java b/src/main/java/org/apache/bcel/generic/AnnotationEntryGen.java index 4d7883c7..21de0148 100644 --- a/src/main/java/org/apache/bcel/generic/AnnotationEntryGen.java +++ b/src/main/java/org/apache/bcel/generic/AnnotationEntryGen.java @@ -50,7 +50,7 @@ public class AnnotationEntryGen { */ static Attribute[] getAnnotationAttributes(final ConstantPoolGen cp, final AnnotationEntryGen[] annotationEntryGens) { if (annotationEntryGens.length == 0) { - return Attribute.EMPTY_ATTRIBUTE_ARRAY; + return Attribute.EMPTY_ARRAY; } try { @@ -106,7 +106,7 @@ public class AnnotationEntryGen { new RuntimeInvisibleAnnotations(riaIndex, riaData.length, new DataInputStream(new ByteArrayInputStream(riaData)), cp.getConstantPool())); } - return newAttributes.toArray(Attribute.EMPTY_ATTRIBUTE_ARRAY); + return newAttributes.toArray(Attribute.EMPTY_ARRAY); } catch (final IOException e) { System.err.println("IOException whilst processing annotations"); e.printStackTrace(); @@ -187,7 +187,7 @@ public class AnnotationEntryGen { newAttributes.add(new RuntimeInvisibleParameterAnnotations(riaIndex, riaData.length, new DataInputStream(new ByteArrayInputStream(riaData)), cp.getConstantPool())); } - return newAttributes.toArray(Attribute.EMPTY_ATTRIBUTE_ARRAY); + return newAttributes.toArray(Attribute.EMPTY_ARRAY); } catch (final IOException e) { System.err.println("IOException whilst processing parameter annotations"); e.printStackTrace(); diff --git a/src/main/java/org/apache/bcel/generic/ClassGen.java b/src/main/java/org/apache/bcel/generic/ClassGen.java index dc0de419..7d4ac553 100644 --- a/src/main/java/org/apache/bcel/generic/ClassGen.java +++ b/src/main/java/org/apache/bcel/generic/ClassGen.java @@ -18,6 +18,8 @@ package org.apache.bcel.generic; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Objects; @@ -33,6 +35,7 @@ import org.apache.bcel.classfile.RuntimeInvisibleAnnotations; import org.apache.bcel.classfile.RuntimeVisibleAnnotations; import org.apache.bcel.classfile.SourceFile; import org.apache.bcel.util.BCELComparator; +import org.apache.commons.lang3.ArrayUtils; /** * Template class for building up a java class. May be initialized with an existing java class (file). @@ -112,26 +115,15 @@ public class ClassGen extends AccessFlags implements Cloneable { final Attribute[] attributes = clazz.getAttributes(); // J5TODO: Could make unpacking lazy, done on first reference final AnnotationEntryGen[] annotations = unpackAnnotations(attributes); - final Method[] methods = clazz.getMethods(); - final Field[] fields = clazz.getFields(); - final String[] interfaces = clazz.getInterfaceNames(); - for (final String interface1 : interfaces) { - addInterface(interface1); - } + Collections.addAll(interfaceList, clazz.getInterfaceNames()); for (final Attribute attribute : attributes) { if (!(attribute instanceof Annotations)) { addAttribute(attribute); } } - for (final AnnotationEntryGen annotation : annotations) { - addAnnotationEntry(annotation); - } - for (final Method method : methods) { - addMethod(method); - } - for (final Field field : fields) { - addField(field); - } + Collections.addAll(annotationList, annotations); + Collections.addAll(methodList, clazz.getMethods()); + Collections.addAll(fieldList, clazz.getFields()); } /** @@ -171,9 +163,7 @@ public class ClassGen extends AccessFlags implements Cloneable { classNameIndex = cp.addClass(className); superclassNameIndex = cp.addClass(superClassName); if (interfaces != null) { - for (final String interface1 : interfaces) { - addInterface(interface1); - } + Collections.addAll(interfaceList, interfaces); } } @@ -298,7 +288,7 @@ public class ClassGen extends AccessFlags implements Cloneable { } public Attribute[] getAttributes() { - return attributeList.toArray(Attribute.EMPTY_ATTRIBUTE_ARRAY); + return attributeList.toArray(Attribute.EMPTY_ARRAY); } public String getClassName() { @@ -322,18 +312,13 @@ public class ClassGen extends AccessFlags implements Cloneable { } public String[] getInterfaceNames() { - final int size = interfaceList.size(); - final String[] interfaces = new String[size]; - interfaceList.toArray(interfaces); - return interfaces; + return interfaceList.toArray(ArrayUtils.EMPTY_STRING_ARRAY); } public int[] getInterfaces() { final int size = interfaceList.size(); final int[] interfaces = new int[size]; - for (int i = 0; i < size; i++) { - interfaces[i] = cp.addClass(interfaceList.get(i)); - } + Arrays.setAll(interfaces, i -> cp.addClass(interfaceList.get(i))); return interfaces; } @@ -504,9 +489,7 @@ public class ClassGen extends AccessFlags implements Cloneable { public void setMethods(final Method[] methods) { methodList.clear(); - for (final Method method : methods) { - addMethod(method); - } + Collections.addAll(methodList, methods); } /** diff --git a/src/main/java/org/apache/bcel/generic/CodeExceptionGen.java b/src/main/java/org/apache/bcel/generic/CodeExceptionGen.java index 72b42b30..0d06c438 100644 --- a/src/main/java/org/apache/bcel/generic/CodeExceptionGen.java +++ b/src/main/java/org/apache/bcel/generic/CodeExceptionGen.java @@ -32,6 +32,8 @@ import org.apache.bcel.classfile.CodeException; */ public final class CodeExceptionGen implements InstructionTargeter, Cloneable { + static final CodeExceptionGen[] EMPTY_ARRAY = {}; + private InstructionHandle startPc; private InstructionHandle endPc; private InstructionHandle handlerPc; diff --git a/src/main/java/org/apache/bcel/generic/FieldGenOrMethodGen.java b/src/main/java/org/apache/bcel/generic/FieldGenOrMethodGen.java index 1997914e..c0b3c9cb 100644 --- a/src/main/java/org/apache/bcel/generic/FieldGenOrMethodGen.java +++ b/src/main/java/org/apache/bcel/generic/FieldGenOrMethodGen.java @@ -18,6 +18,7 @@ package org.apache.bcel.generic; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import org.apache.bcel.Const; @@ -63,6 +64,10 @@ public abstract class FieldGenOrMethodGen extends AccessFlags implements NamedAn super(access_flags); } + protected void addAll(final Attribute[] attrs) { + Collections.addAll(attributeList, attrs); + } + /** * @since 6.0 */ @@ -90,18 +95,14 @@ public abstract class FieldGenOrMethodGen extends AccessFlags implements NamedAn } public AnnotationEntryGen[] getAnnotationEntries() { - final AnnotationEntryGen[] annotations = new AnnotationEntryGen[annotationList.size()]; - annotationList.toArray(annotations); - return annotations; + return annotationList.toArray(AnnotationEntryGen.EMPTY_ARRAY); } /** * @return all attributes of this method. */ public Attribute[] getAttributes() { - final Attribute[] attributes = new Attribute[attributeList.size()]; - attributeList.toArray(attributes); - return attributes; + return attributeList.toArray(Attribute.EMPTY_ARRAY); } public ConstantPoolGen getConstantPool() { diff --git a/src/main/java/org/apache/bcel/generic/InstructionList.java b/src/main/java/org/apache/bcel/generic/InstructionList.java index b6e887ac..b31926a7 100644 --- a/src/main/java/org/apache/bcel/generic/InstructionList.java +++ b/src/main/java/org/apache/bcel/generic/InstructionList.java @@ -1043,9 +1043,7 @@ public class InstructionList implements Iterable<InstructionHandle> { } buf.append("}"); if (!target_vec.isEmpty()) { - final InstructionHandle[] targeted = new InstructionHandle[target_vec.size()]; - target_vec.toArray(targeted); - throw new TargetLostException(targeted, buf.toString()); + throw new TargetLostException(target_vec.toArray(InstructionHandle.EMPTY_ARRAY), buf.toString()); } } diff --git a/src/main/java/org/apache/bcel/generic/LineNumberGen.java b/src/main/java/org/apache/bcel/generic/LineNumberGen.java index 253e2f5a..afcde841 100644 --- a/src/main/java/org/apache/bcel/generic/LineNumberGen.java +++ b/src/main/java/org/apache/bcel/generic/LineNumberGen.java @@ -30,6 +30,8 @@ import org.apache.bcel.classfile.LineNumber; */ public class LineNumberGen implements InstructionTargeter, Cloneable { + static final LineNumberGen[] EMPTY_ARRAY = {}; + private InstructionHandle ih; private int srcLine; diff --git a/src/main/java/org/apache/bcel/generic/MethodGen.java b/src/main/java/org/apache/bcel/generic/MethodGen.java index 1ee37b78..c4ef9180 100644 --- a/src/main/java/org/apache/bcel/generic/MethodGen.java +++ b/src/main/java/org/apache/bcel/generic/MethodGen.java @@ -19,6 +19,7 @@ package org.apache.bcel.generic; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Comparator; import java.util.Hashtable; import java.util.List; @@ -43,6 +44,7 @@ import org.apache.bcel.classfile.ParameterAnnotations; import org.apache.bcel.classfile.RuntimeVisibleParameterAnnotations; import org.apache.bcel.classfile.Utility; import org.apache.bcel.util.BCELComparator; +import org.apache.commons.lang3.ArrayUtils; /** * Template class for building up a method. This is done by defining exception handlers, adding thrown exceptions, local @@ -102,14 +104,14 @@ public class MethodGen extends FieldGenOrMethodGen { @Override public boolean equals(final Object o1, final Object o2) { - final MethodGen THIS = (MethodGen) o1; - final MethodGen THAT = (MethodGen) o2; + final FieldGenOrMethodGen THIS = (FieldGenOrMethodGen) o1; + final FieldGenOrMethodGen THAT = (FieldGenOrMethodGen) o2; return Objects.equals(THIS.getName(), THAT.getName()) && Objects.equals(THIS.getSignature(), THAT.getSignature()); } @Override public int hashCode(final Object o) { - final MethodGen THIS = (MethodGen) o; + final FieldGenOrMethodGen THIS = (FieldGenOrMethodGen) o; return THIS.getSignature().hashCode() ^ THIS.getName().hashCode(); } }; @@ -360,10 +362,7 @@ public class MethodGen extends FieldGenOrMethodGen { } } } else if (a instanceof ExceptionTable) { - final String[] names = ((ExceptionTable) a).getExceptionNames(); - for (final String name2 : names) { - addException(name2); - } + Collections.addAll(throwsList, ((ExceptionTable) a).getExceptionNames()); } else if (a instanceof Annotations) { final Annotations runtimeAnnotations = (Annotations) a; runtimeAnnotations.forEach(element -> addAnnotationEntry(new AnnotationEntryGen(element, cp, false))); @@ -377,10 +376,7 @@ public class MethodGen extends FieldGenOrMethodGen { * @since 6.0 */ public void addAnnotationsAsAttribute(final ConstantPoolGen cp) { - final Attribute[] attrs = AnnotationEntryGen.getAnnotationAttributes(cp, super.getAnnotationEntries()); - for (final Attribute attr : attrs) { - addAttribute(attr); - } + addAll(AnnotationEntryGen.getAnnotationAttributes(cp, super.getAnnotationEntries())); } /** @@ -534,28 +530,22 @@ public class MethodGen extends FieldGenOrMethodGen { } final Attribute[] attrs = AnnotationEntryGen.getParameterAnnotationAttributes(cp, paramAnnotations); if (attrs != null) { - for (final Attribute attr : attrs) { - addAttribute(attr); - } + addAll(attrs); } } private Attribute[] addRuntimeAnnotationsAsAttribute(final ConstantPoolGen cp) { final Attribute[] attrs = AnnotationEntryGen.getAnnotationAttributes(cp, super.getAnnotationEntries()); - for (final Attribute attr : attrs) { - addAttribute(attr); - } + addAll(attrs); return attrs; } private Attribute[] addRuntimeParameterAnnotationsAsAttribute(final ConstantPoolGen cp) { if (!hasParameterAnnotations) { - return Attribute.EMPTY_ATTRIBUTE_ARRAY; + return Attribute.EMPTY_ARRAY; } final Attribute[] attrs = AnnotationEntryGen.getParameterAnnotationAttributes(cp, paramAnnotations); - for (final Attribute attr : attrs) { - addAttribute(attr); - } + addAll(attrs); return attrs; } @@ -607,9 +597,7 @@ public class MethodGen extends FieldGenOrMethodGen { @SuppressWarnings("unchecked") // OK final List<AnnotationEntryGen>[] parmList = new List[argTypes.length]; paramAnnotations = parmList; - for (int j = 0; j < argTypes.length; j++) { - paramAnnotations[j] = new ArrayList<>(); - } + Arrays.setAll(paramAnnotations, i -> new ArrayList<>()); } hasParameterAnnotations = true; final ParameterAnnotations rpa = (ParameterAnnotations) attribute; @@ -691,9 +679,7 @@ public class MethodGen extends FieldGenOrMethodGen { * @return all attributes of this method. */ public Attribute[] getCodeAttributes() { - final Attribute[] attributes = new Attribute[codeAttrsList.size()]; - codeAttrsList.toArray(attributes); - return attributes; + return codeAttrsList.toArray(Attribute.EMPTY_ARRAY); } /** @@ -702,10 +688,7 @@ public class MethodGen extends FieldGenOrMethodGen { private CodeException[] getCodeExceptions() { final int size = exceptionList.size(); final CodeException[] c_exc = new CodeException[size]; - for (int i = 0; i < size; i++) { - final CodeExceptionGen c = exceptionList.get(i); - c_exc[i] = c.getCodeException(super.getConstantPool()); - } + Arrays.setAll(c_exc, i -> exceptionList.get(i).getCodeException(super.getConstantPool())); return c_exc; } @@ -713,18 +696,14 @@ public class MethodGen extends FieldGenOrMethodGen { * @return array of declared exception handlers */ public CodeExceptionGen[] getExceptionHandlers() { - final CodeExceptionGen[] cg = new CodeExceptionGen[exceptionList.size()]; - exceptionList.toArray(cg); - return cg; + return exceptionList.toArray(CodeExceptionGen.EMPTY_ARRAY); } /* * @return array of thrown exceptions */ public String[] getExceptions() { - final String[] e = new String[throwsList.size()]; - throwsList.toArray(e); - return e; + return throwsList.toArray(ArrayUtils.EMPTY_STRING_ARRAY); } /** @@ -733,9 +712,7 @@ public class MethodGen extends FieldGenOrMethodGen { private ExceptionTable getExceptionTable(final ConstantPoolGen cp) { final int size = throwsList.size(); final int[] ex = new int[size]; - for (int i = 0; i < size; i++) { - ex[i] = cp.addClass(throwsList.get(i)); - } + Arrays.setAll(ex, i -> cp.addClass(throwsList.get(i))); return new ExceptionTable(cp.addUtf8("Exceptions"), 2 + 2 * size, ex, cp.getConstantPool()); } @@ -747,9 +724,7 @@ public class MethodGen extends FieldGenOrMethodGen { * @return array of line numbers */ public LineNumberGen[] getLineNumbers() { - final LineNumberGen[] lg = new LineNumberGen[lineNumberList.size()]; - lineNumberList.toArray(lg); - return lg; + return lineNumberList.toArray(LineNumberGen.EMPTY_ARRAY); } /** @@ -758,9 +733,7 @@ public class MethodGen extends FieldGenOrMethodGen { public LineNumberTable getLineNumberTable(final ConstantPoolGen cp) { final int size = lineNumberList.size(); final LineNumber[] ln = new LineNumber[size]; - for (int i = 0; i < size; i++) { - ln[i] = lineNumberList.get(i).getLineNumber(); - } + Arrays.setAll(ln, i -> lineNumberList.get(i).getLineNumber()); return new LineNumberTable(cp.addUtf8("LineNumberTable"), 2 + ln.length * 4, ln, cp.getConstantPool()); } @@ -795,9 +768,7 @@ public class MethodGen extends FieldGenOrMethodGen { final LocalVariableGen[] lg = getLocalVariables(); final int size = lg.length; final LocalVariable[] lv = new LocalVariable[size]; - for (int i = 0; i < size; i++) { - lv[i] = lg[i].getLocalVariable(cp); - } + Arrays.setAll(lv, i -> lg[i].getLocalVariable(cp)); return new LocalVariableTable(cp.addUtf8("LocalVariableTable"), 2 + lv.length * 10, lv, cp.getConstantPool()); } @@ -1006,9 +977,7 @@ public class MethodGen extends FieldGenOrMethodGen { * Remove all local variables. */ public void removeLocalVariables() { - for (final LocalVariableGen lv : variableList) { - lv.dispose(); - } + variableList.forEach(LocalVariableGen::dispose); variableList.clear(); } diff --git a/src/main/java/org/apache/bcel/generic/Type.java b/src/main/java/org/apache/bcel/generic/Type.java index 3a07b79d..b633a44e 100644 --- a/src/main/java/org/apache/bcel/generic/Type.java +++ b/src/main/java/org/apache/bcel/generic/Type.java @@ -18,6 +18,7 @@ package org.apache.bcel.generic; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Objects; @@ -81,7 +82,6 @@ public abstract class Type { public static Type[] getArgumentTypes(final String signature) { final List<Type> vec = new ArrayList<>(); int index; - Type[] types; try { // Skip any type arguments to read argument declarations between `(' and `)' index = signature.indexOf('(') + 1; @@ -96,7 +96,7 @@ public abstract class Type { } catch (final StringIndexOutOfBoundsException e) { // Should never occur throw new ClassFormatException("Invalid method signature: " + signature, e); } - types = new Type[vec.size()]; + Type[] types = new Type[vec.size()]; vec.toArray(types); return types; } @@ -260,9 +260,7 @@ public abstract class Type { */ public static Type[] getTypes(final Class<?>[] classes) { final Type[] ret = new Type[classes.length]; - for (int i = 0; i < ret.length; i++) { - ret[i] = getType(classes[i]); - } + Arrays.setAll(ret, i -> getType(classes[i])); return ret; } diff --git a/src/main/java/org/apache/bcel/util/ClassLoaderRepository.java b/src/main/java/org/apache/bcel/util/ClassLoaderRepository.java index b315fd9e..c5c9d223 100644 --- a/src/main/java/org/apache/bcel/util/ClassLoaderRepository.java +++ b/src/main/java/org/apache/bcel/util/ClassLoaderRepository.java @@ -55,7 +55,7 @@ public class ClassLoaderRepository implements Repository { */ @Override public JavaClass findClass(final String className) { - return loadedClasses.getOrDefault(className, null); + return loadedClasses.get(className); } /* diff --git a/src/main/java/org/apache/bcel/util/ClassPath.java b/src/main/java/org/apache/bcel/util/ClassPath.java index b782d585..4e6d8f62 100644 --- a/src/main/java/org/apache/bcel/util/ClassPath.java +++ b/src/main/java/org/apache/bcel/util/ClassPath.java @@ -354,11 +354,7 @@ public class ClassPath implements Closeable { public JrtModules(final String path) throws IOException { this.modularRuntimeImage = new ModularRuntimeImage(); - final List<Path> list = modularRuntimeImage.list(path); - this.modules = new JrtModule[list.size()]; - for (int i = 0; i < modules.length; i++) { - modules[i] = new JrtModule(list.get(i)); - } + this.modules = modularRuntimeImage.list(path).stream().map(JrtModule::new).toArray(JrtModule[]::new); } @Override diff --git a/src/main/java/org/apache/bcel/util/ClassSet.java b/src/main/java/org/apache/bcel/util/ClassSet.java index 2c38db93..341355c9 100644 --- a/src/main/java/org/apache/bcel/util/ClassSet.java +++ b/src/main/java/org/apache/bcel/util/ClassSet.java @@ -17,7 +17,6 @@ */ package org.apache.bcel.util; -import java.util.Collection; import java.util.HashMap; import java.util.Map; @@ -25,8 +24,7 @@ import org.apache.bcel.classfile.JavaClass; import org.apache.commons.lang3.ArrayUtils; /** - * Utility class implementing a (typesafe) set of JavaClass objects. Since JavaClass has no equals() method, the name of - * the class is used for comparison. + * Utility class implementing a (type-safe) set of JavaClass objects. Since JavaClass has no equals() method, the name of the class is used for comparison. * * @see ClassStack */ @@ -35,12 +33,7 @@ public class ClassSet { private final Map<String, JavaClass> map = new HashMap<>(); public boolean add(final JavaClass clazz) { - boolean result = false; - if (!map.containsKey(clazz.getClassName())) { - result = true; - map.put(clazz.getClassName(), clazz); - } - return result; + return map.putIfAbsent(clazz.getClassName(), clazz) != null; } public boolean empty() { @@ -56,9 +49,6 @@ public class ClassSet { } public JavaClass[] toArray() { - final Collection<JavaClass> values = map.values(); - final JavaClass[] classes = new JavaClass[values.size()]; - values.toArray(classes); - return classes; + return map.values().toArray(JavaClass.EMPTY_ARRAY); } } diff --git a/src/main/java/org/apache/bcel/util/ClassVector.java b/src/main/java/org/apache/bcel/util/ClassVector.java index eb24e6b4..459d0e36 100644 --- a/src/main/java/org/apache/bcel/util/ClassVector.java +++ b/src/main/java/org/apache/bcel/util/ClassVector.java @@ -49,8 +49,6 @@ public class ClassVector implements Serializable { } public JavaClass[] toArray() { - final JavaClass[] classes = new JavaClass[vec.size()]; - vec.toArray(classes); - return classes; + return vec.toArray(JavaClass.EMPTY_ARRAY); } } diff --git a/src/main/java/org/apache/bcel/util/InstructionFinder.java b/src/main/java/org/apache/bcel/util/InstructionFinder.java index 04447ab1..0296cb6f 100644 --- a/src/main/java/org/apache/bcel/util/InstructionFinder.java +++ b/src/main/java/org/apache/bcel/util/InstructionFinder.java @@ -79,6 +79,7 @@ public class InstructionFinder { private static final int OFFSET = 32767; // char + OFFSET is outside of LATIN-1 private static final int NO_OPCODES = 256; // Potential number, some are not used private static final Map<String, String> map = new HashMap<>(); + // Initialize pattern map static { map.put("arithmeticinstruction", @@ -142,14 +143,12 @@ public class InstructionFinder { map.put("fstore", precompile(Const.FSTORE_0, Const.FSTORE_3, Const.FSTORE)); map.put("astore", precompile(Const.ASTORE_0, Const.ASTORE_3, Const.ASTORE)); // Compile strings - for (final Map.Entry<String, String> entry : map.entrySet()) { - final String key = entry.getKey(); - final String value = entry.getValue(); + map.forEach((key, value) -> { final char ch = value.charAt(1); // Omit already precompiled patterns if (ch < OFFSET) { map.put(key, compilePattern(value)); // precompile all patterns } - } + }); // Add instruction alias to match anything final StringBuilder buf = new StringBuilder("("); for (short i = 0; i < NO_OPCODES; i++) { @@ -216,7 +215,7 @@ public class InstructionFinder { } for (short i = 0; i < NO_OPCODES; i++) { if (pattern.equals(Const.getOpcodeName(i))) { - return "" + makeChar(i); + return String.valueOf(makeChar(i)); } } throw new IllegalArgumentException("Instruction unknown: " + pattern); diff --git a/src/main/java/org/apache/bcel/util/SyntheticRepository.java b/src/main/java/org/apache/bcel/util/SyntheticRepository.java index faac9cbd..998690a1 100644 --- a/src/main/java/org/apache/bcel/util/SyntheticRepository.java +++ b/src/main/java/org/apache/bcel/util/SyntheticRepository.java @@ -30,24 +30,18 @@ import java.util.Map; * </p> * * @see org.apache.bcel.Repository - * */ public class SyntheticRepository extends MemorySensitiveClassPathRepository { // private static final String DEFAULT_PATH = ClassPath.getClassPath(); - private static final Map<ClassPath, SyntheticRepository> instances = new HashMap<>(); // CLASSPATH X REPOSITORY + private static final Map<ClassPath, SyntheticRepository> MAP = new HashMap<>(); // CLASSPATH X REPOSITORY public static SyntheticRepository getInstance() { return getInstance(ClassPath.SYSTEM_CLASS_PATH); } public static SyntheticRepository getInstance(final ClassPath classPath) { - SyntheticRepository rep = instances.get(classPath); - if (rep == null) { - rep = new SyntheticRepository(classPath); - instances.put(classPath, rep); - } - return rep; + return MAP.computeIfAbsent(classPath, SyntheticRepository::new); } private SyntheticRepository(final ClassPath path) { diff --git a/src/main/java/org/apache/bcel/verifier/Verifier.java b/src/main/java/org/apache/bcel/verifier/Verifier.java index 86181695..98930cc2 100644 --- a/src/main/java/org/apache/bcel/verifier/Verifier.java +++ b/src/main/java/org/apache/bcel/verifier/Verifier.java @@ -43,6 +43,8 @@ import org.apache.commons.lang3.ArrayUtils; */ public class Verifier { + static final Verifier[] EMPTY_ARRAY = {}; + /** * Verifies class files. This is a simple demonstration of how the API of BCEL's class file verifier "JustIce" may be * used. You should supply command-line arguments which are fully qualified namea of the classes to verify. These class @@ -143,8 +145,7 @@ public class Verifier { /** Returns the VerificationResult for the given pass. */ public VerificationResult doPass3a(final int method_no) { final String key = Integer.toString(method_no); - Pass3aVerifier p3av; - p3av = p3avs.get(key); + Pass3aVerifier p3av = p3avs.get(key); if (p3avs.get(key) == null) { p3av = new Pass3aVerifier(this, method_no); p3avs.put(key, p3av); @@ -155,8 +156,7 @@ public class Verifier { /** Returns the VerificationResult for the given pass. */ public VerificationResult doPass3b(final int method_no) { final String key = Integer.toString(method_no); - Pass3bVerifier p3bv; - p3bv = p3bvs.get(key); + Pass3bVerifier p3bv = p3bvs.get(key); if (p3bvs.get(key) == null) { p3bv = new Pass3bVerifier(this, method_no); p3bvs.put(key, p3bv); @@ -194,28 +194,24 @@ public class Verifier { public String[] getMessages() throws ClassNotFoundException { final List<String> messages = new ArrayList<>(); if (p1v != null) { - final String[] p1m = p1v.getMessages(); - for (final String element : p1m) { + for (final String element : p1v.getMessages()) { messages.add("Pass 1: " + element); } } if (p2v != null) { - final String[] p2m = p2v.getMessages(); - for (final String element : p2m) { + for (final String element : p2v.getMessages()) { messages.add("Pass 2: " + element); } } for (final Pass3aVerifier pv : p3avs.values()) { - final String[] p3am = pv.getMessages(); final int meth = pv.getMethodNo(); - for (final String element : p3am) { + for (final String element : pv.getMessages()) { messages.add("Pass 3a, method " + meth + " ('" + org.apache.bcel.Repository.lookupClass(classname).getMethods()[meth] + "'): " + element); } } for (final Pass3bVerifier pv : p3bvs.values()) { - final String[] p3bm = pv.getMessages(); final int meth = pv.getMethodNo(); - for (final String element : p3bm) { + for (final String element : pv.getMessages()) { messages.add("Pass 3b, method " + meth + " ('" + org.apache.bcel.Repository.lookupClass(classname).getMethods()[meth] + "'): " + element); } } diff --git a/src/main/java/org/apache/bcel/verifier/VerifierAppFrame.java b/src/main/java/org/apache/bcel/verifier/VerifierAppFrame.java index 67dd89b9..8669fc38 100644 --- a/src/main/java/org/apache/bcel/verifier/VerifierAppFrame.java +++ b/src/main/java/org/apache/bcel/verifier/VerifierAppFrame.java @@ -25,6 +25,7 @@ import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.InputEvent; import java.awt.event.WindowEvent; +import java.util.Arrays; import javax.swing.BorderFactory; import javax.swing.JFrame; @@ -327,13 +328,11 @@ public class VerifierAppFrame extends JFrame { /* * boolean all3aok = true; boolean all3bok = true; String all3amsg = ""; String all3bmsg = ""; */ - final String[] methodnames = new String[jc.getMethods().length]; - for (int i = 0; i < jc.getMethods().length; i++) { - methodnames[i] = jc.getMethods()[i].toString().replace('\n', ' ').replace('\t', ' '); - } - pass3aJList.setListData(methodnames); + final String[] methodNames = new String[jc.getMethods().length]; + Arrays.setAll(methodNames, i -> jc.getMethods()[i].toString().replace('\n', ' ').replace('\t', ' ')); + pass3aJList.setListData(methodNames); pass3aJList.setSelectionInterval(0, jc.getMethods().length - 1); - pass3bJList.setListData(methodnames); + pass3bJList.setListData(methodNames); pass3bJList.setSelectionInterval(0, jc.getMethods().length - 1); } } diff --git a/src/main/java/org/apache/bcel/verifier/VerifierFactory.java b/src/main/java/org/apache/bcel/verifier/VerifierFactory.java index 4ea01d3a..c7d94024 100644 --- a/src/main/java/org/apache/bcel/verifier/VerifierFactory.java +++ b/src/main/java/org/apache/bcel/verifier/VerifierFactory.java @@ -34,24 +34,25 @@ public class VerifierFactory { /** * The HashMap that holds the data about the already-constructed Verifier instances. */ - private static final Map<String, Verifier> hashMap = new HashMap<>(); + private static final Map<String, Verifier> MAP = new HashMap<>(); + /** * The VerifierFactoryObserver instances that observe the VerifierFactory. */ - private static final List<VerifierFactoryObserver> observers = new Vector<>(); + private static final List<VerifierFactoryObserver> OBSVERVERS = new Vector<>(); /** * Adds the VerifierFactoryObserver o to the list of observers. */ public static void attach(final VerifierFactoryObserver o) { - observers.add(o); + OBSVERVERS.add(o); } /** * Removes the VerifierFactoryObserver o from the list of observers. */ public static void detach(final VerifierFactoryObserver o) { - observers.remove(o); + OBSVERVERS.remove(o); } /** @@ -61,13 +62,11 @@ public class VerifierFactory { * @return the (only) verifier responsible for the class with the given name. */ public static Verifier getVerifier(final String fullyQualifiedClassName) { - Verifier v = hashMap.get(fullyQualifiedClassName); - if (v == null) { - v = new Verifier(fullyQualifiedClassName); - hashMap.put(fullyQualifiedClassName, v); - notify(fullyQualifiedClassName); - } - return v; + return MAP.computeIfAbsent(fullyQualifiedClassName, k -> { + Verifier v = new Verifier(k); + notify(k); + return v; + }); } /** @@ -75,8 +74,7 @@ public class VerifierFactory { * create other Verifier instances and if you want to verify the transitive hull of referenced class files. */ public static Verifier[] getVerifiers() { - final Verifier[] vs = new Verifier[hashMap.size()]; - return hashMap.values().toArray(vs); // Because vs is big enough, vs is used to store the values into and returned! + return MAP.values().toArray(Verifier.EMPTY_ARRAY); } /** @@ -84,9 +82,7 @@ public class VerifierFactory { */ private static void notify(final String fullyQualifiedClassName) { // notify the observers - for (final VerifierFactoryObserver vfo : observers) { - vfo.update(fullyQualifiedClassName); - } + OBSVERVERS.forEach(vfo -> vfo.update(fullyQualifiedClassName)); } /** diff --git a/src/main/java/org/apache/bcel/verifier/VerifierFactoryListModel.java b/src/main/java/org/apache/bcel/verifier/VerifierFactoryListModel.java index ed08d7c1..dcc95940 100644 --- a/src/main/java/org/apache/bcel/verifier/VerifierFactoryListModel.java +++ b/src/main/java/org/apache/bcel/verifier/VerifierFactoryListModel.java @@ -71,8 +71,7 @@ public class VerifierFactoryListModel implements VerifierFactoryObserver, ListMo cache.add(verifier.getClassName()); } for (final ListDataListener listener : listeners) { - final ListDataEvent e = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, num_of_verifiers - 1); - listener.contentsChanged(e); + listener.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, num_of_verifiers - 1)); } } diff --git a/src/main/java/org/apache/bcel/verifier/statics/IntList.java b/src/main/java/org/apache/bcel/verifier/statics/IntList.java index 0be1fc7c..de9b0443 100644 --- a/src/main/java/org/apache/bcel/verifier/statics/IntList.java +++ b/src/main/java/org/apache/bcel/verifier/statics/IntList.java @@ -25,28 +25,22 @@ import java.util.List; * */ public class IntList { + /** The int are stored as Integer objects here. */ - private final List<Integer> theList; + private final List<Integer> list; /** This constructor creates an empty list. */ IntList() { - theList = new ArrayList<>(); + list = new ArrayList<>(); } /** Adds an element to the list. */ void add(final int i) { - theList.add(Integer.valueOf(i)); + list.add(Integer.valueOf(i)); } /** Checks if the specified int is already in the list. */ boolean contains(final int i) { - final Integer[] ints = new Integer[theList.size()]; - theList.toArray(ints); - for (final Integer k : ints) { - if (i == k.intValue()) { - return true; - } - } - return false; + return list.contains(i); } } diff --git a/src/main/java/org/apache/bcel/verifier/statics/LocalVariablesInfo.java b/src/main/java/org/apache/bcel/verifier/statics/LocalVariablesInfo.java index 648760d0..9f6278a1 100644 --- a/src/main/java/org/apache/bcel/verifier/statics/LocalVariablesInfo.java +++ b/src/main/java/org/apache/bcel/verifier/statics/LocalVariablesInfo.java @@ -17,6 +17,8 @@ */ package org.apache.bcel.verifier.statics; +import java.util.Arrays; + import org.apache.bcel.generic.Type; import org.apache.bcel.verifier.exc.AssertionViolatedException; import org.apache.bcel.verifier.exc.LocalVariableInfoInconsistentException; @@ -32,9 +34,7 @@ public class LocalVariablesInfo { /** The constructor. */ LocalVariablesInfo(final int max_locals) { localVariableInfos = new LocalVariableInfo[max_locals]; - for (int i = 0; i < max_locals; i++) { - localVariableInfos[i] = new LocalVariableInfo(); - } + Arrays.setAll(localVariableInfos, i -> new LocalVariableInfo()); } /** diff --git a/src/main/java/org/apache/bcel/verifier/structurals/ControlFlowGraph.java b/src/main/java/org/apache/bcel/verifier/structurals/ControlFlowGraph.java index f1c29ae2..e479e221 100644 --- a/src/main/java/org/apache/bcel/verifier/structurals/ControlFlowGraph.java +++ b/src/main/java/org/apache/bcel/verifier/structurals/ControlFlowGraph.java @@ -18,6 +18,7 @@ package org.apache.bcel.verifier.structurals; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -113,9 +114,7 @@ public class ControlFlowGraph { final InstructionHandle[] jsrs = s.getEnteringJsrInstructions(); final InstructionHandle[] ret = new InstructionHandle[jsrs.length]; - for (int i = 0; i < jsrs.length; i++) { - ret[i] = jsrs[i].getNext(); - } + Arrays.setAll(ret, i -> jsrs[i].getNext()); return ret; } @@ -421,9 +420,7 @@ public class ControlFlowGraph { */ public InstructionContext[] contextsOf(final InstructionHandle[] insts) { final InstructionContext[] ret = new InstructionContext[insts.length]; - for (int i = 0; i < insts.length; i++) { - ret[i] = contextOf(insts[i]); - } + Arrays.setAll(ret, i -> contextOf(insts[i])); return ret; } diff --git a/src/main/java/org/apache/bcel/verifier/structurals/ExceptionHandlers.java b/src/main/java/org/apache/bcel/verifier/structurals/ExceptionHandlers.java index 1978b38a..06a98e8f 100644 --- a/src/main/java/org/apache/bcel/verifier/structurals/ExceptionHandlers.java +++ b/src/main/java/org/apache/bcel/verifier/structurals/ExceptionHandlers.java @@ -50,9 +50,8 @@ public class ExceptionHandlers { for (final CodeExceptionGen ceg : cegs) { final ExceptionHandler eh = new ExceptionHandler(ceg.getCatchType(), ceg.getHandlerPC()); for (InstructionHandle ih = ceg.getStartPC(); ih != ceg.getEndPC().getNext(); ih = ih.getNext()) { - Set<ExceptionHandler> hs; - hs = exceptionHandlers.computeIfAbsent(ih, k -> new HashSet<>()); - hs.add(eh); + exceptionHandlers.computeIfAbsent(ih, k -> new HashSet<>()).add(eh); + exceptionHandlers.computeIfAbsent(ih, k -> new HashSet<>()).add(eh); } } } diff --git a/src/main/java/org/apache/bcel/verifier/structurals/ExecutionVisitor.java b/src/main/java/org/apache/bcel/verifier/structurals/ExecutionVisitor.java index 458512c1..d297076e 100644 --- a/src/main/java/org/apache/bcel/verifier/structurals/ExecutionVisitor.java +++ b/src/main/java/org/apache/bcel/verifier/structurals/ExecutionVisitor.java @@ -139,9 +139,7 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitAASTORE(final AASTORE o) { - stack().pop(); - stack().pop(); - stack().pop(); + stack().pop(3); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @@ -198,17 +196,14 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitBALOAD(final BALOAD o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.INT); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitBASTORE(final BASTORE o) { - stack().pop(); - stack().pop(); - stack().pop(); + stack().pop(3); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @@ -220,17 +215,14 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitCALOAD(final CALOAD o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.INT); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitCASTORE(final CASTORE o) { - stack().pop(); - stack().pop(); - stack().pop(); + stack().pop(3); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @@ -269,40 +261,34 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitDADD(final DADD o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.DOUBLE); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitDALOAD(final DALOAD o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.DOUBLE); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitDASTORE(final DASTORE o) { - stack().pop(); - stack().pop(); - stack().pop(); + stack().pop(3); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitDCMPG(final DCMPG o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.INT); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitDCMPL(final DCMPL o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.INT); } @@ -315,8 +301,7 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitDDIV(final DDIV o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.DOUBLE); } @@ -329,8 +314,7 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitDMUL(final DMUL o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.DOUBLE); } @@ -344,8 +328,7 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitDREM(final DREM o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.DOUBLE); } @@ -365,8 +348,7 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitDSUB(final DSUB o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.DOUBLE); } @@ -495,40 +477,34 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitFADD(final FADD o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.FLOAT); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitFALOAD(final FALOAD o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.FLOAT); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitFASTORE(final FASTORE o) { - stack().pop(); - stack().pop(); - stack().pop(); + stack().pop(3); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitFCMPG(final FCMPG o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.INT); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitFCMPL(final FCMPL o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.INT); } @@ -541,8 +517,7 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitFDIV(final FDIV o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.FLOAT); } @@ -555,8 +530,7 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitFMUL(final FMUL o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.FLOAT); } @@ -570,8 +544,7 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitFREM(final FREM o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.FLOAT); } @@ -590,8 +563,7 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitFSUB(final FSUB o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.FLOAT); } @@ -673,33 +645,28 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitIADD(final IADD o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.INT); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitIALOAD(final IALOAD o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.INT); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitIAND(final IAND o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.INT); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitIASTORE(final IASTORE o) { - stack().pop(); - stack().pop(); - stack().pop(); + stack().pop(3); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @@ -711,65 +678,56 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitIDIV(final IDIV o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.INT); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitIF_ACMPEQ(final IF_ACMPEQ o) { - stack().pop(); - stack().pop(); + stack().pop(2); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitIF_ACMPNE(final IF_ACMPNE o) { - stack().pop(); - stack().pop(); + stack().pop(2); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitIF_ICMPEQ(final IF_ICMPEQ o) { - stack().pop(); - stack().pop(); + stack().pop(2); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitIF_ICMPGE(final IF_ICMPGE o) { - stack().pop(); - stack().pop(); + stack().pop(2); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitIF_ICMPGT(final IF_ICMPGT o) { - stack().pop(); - stack().pop(); + stack().pop(2); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitIF_ICMPLE(final IF_ICMPLE o) { - stack().pop(); - stack().pop(); + stack().pop(2); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitIF_ICMPLT(final IF_ICMPLT o) { - stack().pop(); - stack().pop(); + stack().pop(2); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitIF_ICMPNE(final IF_ICMPNE o) { - stack().pop(); - stack().pop(); + stack().pop(2); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @@ -835,8 +793,7 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitIMUL(final IMUL o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.INT); } @@ -861,9 +818,7 @@ public class ExecutionVisitor extends EmptyVisitor { */ @Override public void visitINVOKEDYNAMIC(final INVOKEDYNAMIC o) { - for (int i = 0; i < o.getArgumentTypes(cpg).length; i++) { - stack().pop(); - } + stack().pop(o.getArgumentTypes(cpg).length); // We are sure the invoked method will xRETURN eventually // We simulate xRETURNs functionality here because we // don't really "jump into" and simulate the invoked @@ -881,9 +836,7 @@ public class ExecutionVisitor extends EmptyVisitor { @Override public void visitINVOKEINTERFACE(final INVOKEINTERFACE o) { stack().pop(); // objectref - for (int i = 0; i < o.getArgumentTypes(cpg).length; i++) { - stack().pop(); - } + stack().pop(o.getArgumentTypes(cpg).length); // We are sure the invoked method will xRETURN eventually // We simulate xRETURNs functionality here because we // don't really "jump into" and simulate the invoked @@ -909,9 +862,7 @@ public class ExecutionVisitor extends EmptyVisitor { locals().initializeObject(t); } stack().pop(); // objectref - for (int i = 0; i < o.getArgumentTypes(cpg).length; i++) { - stack().pop(); - } + stack().pop(o.getArgumentTypes(cpg).length); // We are sure the invoked method will xRETURN eventually // We simulate xRETURNs functionality here because we // don't really "jump into" and simulate the invoked @@ -928,9 +879,7 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitINVOKESTATIC(final INVOKESTATIC o) { - for (int i = 0; i < o.getArgumentTypes(cpg).length; i++) { - stack().pop(); - } + stack().pop(o.getArgumentTypes(cpg).length); // We are sure the invoked method will xRETURN eventually // We simulate xRETURNs functionality here because we // don't really "jump into" and simulate the invoked @@ -948,9 +897,7 @@ public class ExecutionVisitor extends EmptyVisitor { @Override public void visitINVOKEVIRTUAL(final INVOKEVIRTUAL o) { stack().pop(); // objectref - for (int i = 0; i < o.getArgumentTypes(cpg).length; i++) { - stack().pop(); - } + stack().pop(o.getArgumentTypes(cpg).length); // We are sure the invoked method will xRETURN eventually // We simulate xRETURNs functionality here because we // don't really "jump into" and simulate the invoked @@ -967,16 +914,14 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitIOR(final IOR o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.INT); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitIREM(final IREM o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.INT); } @@ -989,16 +934,14 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitISHL(final ISHL o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.INT); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitISHR(final ISHR o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.INT); } @@ -1011,24 +954,21 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitISUB(final ISUB o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.INT); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitIUSHR(final IUSHR o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.INT); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitIXOR(final IXOR o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.INT); } @@ -1069,40 +1009,34 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitLADD(final LADD o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.LONG); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitLALOAD(final LALOAD o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.LONG); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitLAND(final LAND o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.LONG); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitLASTORE(final LASTORE o) { - stack().pop(); - stack().pop(); - stack().pop(); + stack().pop(3); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitLCMP(final LCMP o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.INT); } @@ -1162,8 +1096,7 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitLDIV(final LDIV o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.LONG); } @@ -1176,8 +1109,7 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitLMUL(final LMUL o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.LONG); } @@ -1197,16 +1129,14 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitLOR(final LOR o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.LONG); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitLREM(final LREM o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.LONG); } @@ -1219,16 +1149,14 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitLSHL(final LSHL o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.LONG); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitLSHR(final LSHR o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.LONG); } @@ -1242,24 +1170,21 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitLSUB(final LSUB o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.LONG); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitLUSHR(final LUSHR o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.LONG); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitLXOR(final LXOR o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.LONG); } @@ -1278,9 +1203,7 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitMULTIANEWARRAY(final MULTIANEWARRAY o) { - for (int i = 0; i < o.getDimensions(); i++) { - stack().pop(); - } + stack().pop(o.getDimensions()); stack().push(o.getType(cpg)); } @@ -1320,8 +1243,7 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitPUTFIELD(final PUTFIELD o) { - stack().pop(); - stack().pop(); + stack().pop(2); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @@ -1346,17 +1268,14 @@ public class ExecutionVisitor extends EmptyVisitor { /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitSALOAD(final SALOAD o) { - stack().pop(); - stack().pop(); + stack().pop(2); stack().push(Type.INT); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ @Override public void visitSASTORE(final SASTORE o) { - stack().pop(); - stack().pop(); - stack().pop(); + stack().pop(3); } /** Symbolically executes the corresponding Java Virtual Machine instruction. */ diff --git a/src/main/java/org/apache/bcel/verifier/structurals/LocalVariables.java b/src/main/java/org/apache/bcel/verifier/structurals/LocalVariables.java index 2902bd4a..eaea3472 100644 --- a/src/main/java/org/apache/bcel/verifier/structurals/LocalVariables.java +++ b/src/main/java/org/apache/bcel/verifier/structurals/LocalVariables.java @@ -17,6 +17,8 @@ */ package org.apache.bcel.verifier.structurals; +import java.util.Arrays; + import org.apache.bcel.generic.ReferenceType; import org.apache.bcel.generic.Type; import org.apache.bcel.verifier.exc.AssertionViolatedException; @@ -37,9 +39,7 @@ public class LocalVariables implements Cloneable { */ public LocalVariables(final int localVariableCount) { locals = new Type[localVariableCount]; - for (int i = 0; i < localVariableCount; i++) { - locals[i] = Type.UNKNOWN; - } + Arrays.fill(locals, Type.UNKNOWN); } /** diff --git a/src/main/java/org/apache/bcel/verifier/structurals/OperandStack.java b/src/main/java/org/apache/bcel/verifier/structurals/OperandStack.java index cfb3cce7..968511d6 100644 --- a/src/main/java/org/apache/bcel/verifier/structurals/OperandStack.java +++ b/src/main/java/org/apache/bcel/verifier/structurals/OperandStack.java @@ -192,8 +192,8 @@ public class OperandStack implements Cloneable { /** * Pops i elements off the stack. ALWAYS RETURNS "null"!!! */ - public Type pop(final int i) { - for (int j = 0; j < i; j++) { + public Type pop(final int count) { + for (int j = 0; j < count; j++) { pop(); } return null; diff --git a/src/main/java/org/apache/bcel/verifier/structurals/Subroutines.java b/src/main/java/org/apache/bcel/verifier/structurals/Subroutines.java index de6a3e63..c0c82aab 100644 --- a/src/main/java/org/apache/bcel/verifier/structurals/Subroutines.java +++ b/src/main/java/org/apache/bcel/verifier/structurals/Subroutines.java @@ -72,6 +72,8 @@ public class Subroutines { */ private static final int UNSET = -1; + private final SubroutineImpl[] EMPTY_ARRAY = {}; + /** * The Local Variable slot where the first instruction of this subroutine (an ASTORE) stores the JsrInstruction's * ReturnAddress in and the RET of this subroutine operates on. @@ -102,14 +104,14 @@ public class Subroutines { * * @see #getRecursivelyAccessedLocalsIndices() */ - private void _getRecursivelyAccessedLocalsIndicesHelper(final Set<Integer> s, final Subroutine[] subs) { + private void _getRecursivelyAccessedLocalsIndicesHelper(final Set<Integer> set, final Subroutine[] subs) { for (final Subroutine sub : subs) { final int[] lvs = sub.getAccessedLocalsIndices(); for (final int lv : lvs) { - s.add(Integer.valueOf(lv)); + set.add(Integer.valueOf(lv)); } if (sub.subSubs().length != 0) { - _getRecursivelyAccessedLocalsIndicesHelper(s, sub.subSubs()); + _getRecursivelyAccessedLocalsIndicesHelper(set, sub.subSubs()); } } } @@ -205,8 +207,7 @@ public class Subroutines { if (this == getTopLevel()) { throw new AssertionViolatedException("getLeavingRET() called on top level pseudo-subroutine."); } - final InstructionHandle[] jsrs = new InstructionHandle[theJSRs.size()]; - return theJSRs.toArray(jsrs); + return theJSRs.toArray(InstructionHandle.EMPTY_ARRAY); } /* @@ -214,8 +215,7 @@ public class Subroutines { */ @Override public InstructionHandle[] getInstructions() { - final InstructionHandle[] ret = new InstructionHandle[instructions.size()]; - return instructions.toArray(ret); + return instructions.toArray(InstructionHandle.EMPTY_ARRAY); } /* @@ -299,8 +299,7 @@ public class Subroutines { h.add(getSubroutine(targ)); } } - final Subroutine[] ret = new Subroutine[h.size()]; - return h.toArray(ret); + return h.toArray(EMPTY_ARRAY); } /** @@ -461,7 +460,7 @@ public class Subroutines { // we don't want to assign an instruction to two or more Subroutine objects. final Set<InstructionHandle> instructions_assigned = new HashSet<>(); - // Graph colouring. Key: InstructionHandle, Value: ColourConstants enum . + // Graph coloring. Key: InstructionHandle, Value: ColourConstants enum . final Map<InstructionHandle, ColourConstants> colors = new HashMap<>(); final List<InstructionHandle> qList = new ArrayList<>(); diff --git a/src/test/java/org/apache/bcel/AbstractTestCase.java b/src/test/java/org/apache/bcel/AbstractTestCase.java index f7766f95..f11bcf12 100644 --- a/src/test/java/org/apache/bcel/AbstractTestCase.java +++ b/src/test/java/org/apache/bcel/AbstractTestCase.java @@ -128,7 +128,7 @@ public abstract class AbstractTestCase { chosenAttrsList.add(element); } } - return chosenAttrsList.toArray(new Attribute[] {}); + return chosenAttrsList.toArray(Attribute.EMPTY_ARRAY); } protected Method getMethod(final JavaClass cl, final String methodname) { diff --git a/src/test/java/org/apache/bcel/generic/GeneratingAnnotatedClassesTestCase.java b/src/test/java/org/apache/bcel/generic/GeneratingAnnotatedClassesTestCase.java index 9daef63d..7fa44d8d 100644 --- a/src/test/java/org/apache/bcel/generic/GeneratingAnnotatedClassesTestCase.java +++ b/src/test/java/org/apache/bcel/generic/GeneratingAnnotatedClassesTestCase.java @@ -386,7 +386,7 @@ public class GeneratingAnnotatedClassesTestCase extends AbstractTestCase { final Method[] m = cg2.getMethods(); i = m[0].getAnnotationEntries().length; assertEquals(1, i, "Wrong number of annotations on the main 'Method'"); - final MethodGen mg = new MethodGen(m[0], cg2.getClassName(), cg2.getConstantPool()); + final FieldGenOrMethodGen mg = new MethodGen(m[0], cg2.getClassName(), cg2.getConstantPool()); // Check it finally when the Method is changed to a MethodGen i = mg.getAnnotationEntries().length; assertEquals(1, i, "Wrong number of annotations on the main 'MethodGen'"); diff --git a/src/test/java/org/apache/bcel/util/BCELifierTestCase.java b/src/test/java/org/apache/bcel/util/BCELifierTestCase.java index c9155f02..351fcc09 100644 --- a/src/test/java/org/apache/bcel/util/BCELifierTestCase.java +++ b/src/test/java/org/apache/bcel/util/BCELifierTestCase.java @@ -61,12 +61,12 @@ public class BCELifierTestCase { } } - private void testClassOnPath(final String javaClass) throws Exception { + private void testClassOnPath(final String javaClassFileName) throws Exception { // Get javap of the input class - final String initial = exec(null, "javap", "-p", "-c", javaClass); + final String initial = exec(null, "javap", "-p", "-c", javaClassFileName); final File workDir = new File("target"); - final File infile = new File(javaClass); + final File infile = new File(javaClassFileName); final JavaClass java_class = BCELifier.getJavaClass(infile.getName().replace(".class", "")); assertNotNull(java_class); final File outfile = new File(workDir, infile.getName().replace(".class", "Creator.java")); diff --git a/src/test/java/org/apache/bcel/verifier/VerifyBadClassesTestCase.java b/src/test/java/org/apache/bcel/verifier/VerifyBadClassesTestCase.java index 0f968389..e28dde32 100644 --- a/src/test/java/org/apache/bcel/verifier/VerifyBadClassesTestCase.java +++ b/src/test/java/org/apache/bcel/verifier/VerifyBadClassesTestCase.java @@ -33,6 +33,7 @@ import org.apache.commons.exec.DefaultExecutor; import org.apache.commons.exec.ExecuteException; import org.apache.commons.exec.ExecuteWatchdog; import org.apache.commons.exec.PumpStreamHandler; +import org.apache.commons.lang3.ArrayUtils; import org.junit.jupiter.api.Test; /** @@ -69,7 +70,7 @@ public class VerifyBadClassesTestCase { /** The process timeout in milliseconds. Defaults to 30 seconds. */ final long timeout = 30 * 1000; - final String[] args = command.toArray(new String[0]); + final String[] args = command.toArray(ArrayUtils.EMPTY_STRING_ARRAY); final CommandLine cmdLine = new CommandLine(args[0]); // constructor requires executable name cmdLine.addArguments(Arrays.copyOfRange(args, 1, args.length));