Skip to content

Commit f230b00

Browse files
committed
BCELComparator now uses generics
Avoid NPEs in BCELComparator implementations
1 parent add006f commit f230b00

File tree

13 files changed

+250
-103
lines changed

13 files changed

+250
-103
lines changed

src/changes/changes.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ The <action> type attribute can be add,update,fix,remove.
6868
<!-- FIX -->
6969
<action type="fix" dev="ggregory" due-to="Gary Gregory">Replace internal use of StringBuffer with StringBuilder.</action>
7070
<action issue="BCEL-370" type="fix" dev="ggregory" due-to="Gary Gregory">CONSTANT_Dynamic is not handled in LDC #254.</action>
71+
<action type="fix" dev="ggregory" due-to="Gary Gregory">BCELComparator now uses generics.</action>
72+
<action type="fix" dev="ggregory" due-to="Gary Gregory">Avoid NullPointerException in ClassGen.BCELComparator#equals() and ClassGen.BCELComparator#hashCode().</action>
73+
<action type="fix" dev="ggregory" due-to="Gary Gregory">Avoid NullPointerException in Constant.BCELComparator#equals() and Constant.BCELComparator#hashCode().</action>
74+
<action type="fix" dev="ggregory" due-to="Gary Gregory">Avoid NullPointerException in Field.BCELComparator#equals() and Field.BCELComparator#hashCode().</action>
75+
<action type="fix" dev="ggregory" due-to="Gary Gregory">Avoid NullPointerException in FieldGen.BCELComparator#equals() and FieldGen.BCELComparator#hashCode().</action>
76+
<action type="fix" dev="ggregory" due-to="Gary Gregory">Avoid NullPointerException in JavaClass.BCELComparator#equals() and JavaClass.BCELComparator#hashCode().</action>
77+
<action type="fix" dev="ggregory" due-to="Gary Gregory">Avoid NullPointerException in Method.BCELComparator#equals() and Method.BCELComparator#hashCode().</action>
78+
<action type="fix" dev="ggregory" due-to="Gary Gregory">Avoid NullPointerException in MethodGen.BCELComparator#equals() and MethodGen.BCELComparator#hashCode().</action>
7179
<!-- UPDATE -->
7280
<action type="update" dev="ggregory" due-to="Dependabot">Bump GitHub various actions for CI builds.</action>
7381
<action type="update" dev="ggregory" due-to="Dependabot">Bump jna.version from 5.13.0 to 5.14.0 #250.</action>

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,23 @@
3030
*/
3131
public abstract class Constant implements Cloneable, Node {
3232

33-
private static BCELComparator bcelComparator = new BCELComparator() {
33+
private static BCELComparator<Constant> bcelComparator = new BCELComparator<Constant>() {
3434

3535
@Override
36-
public boolean equals(final Object o1, final Object o2) {
37-
final Constant THIS = (Constant) o1;
38-
final Constant THAT = (Constant) o2;
39-
return Objects.equals(THIS.toString(), THAT.toString());
36+
public boolean equals(final Constant a, final Constant b) {
37+
return a == b || a != null && b != null && Objects.equals(a.toString(), b.toString());
4038
}
4139

4240
@Override
43-
public int hashCode(final Object o) {
44-
final Constant THIS = (Constant) o;
45-
return THIS.toString().hashCode();
41+
public int hashCode(final Constant o) {
42+
return o != null ? Objects.hashCode(o.toString()) : 0;
4643
}
4744
};
4845

4946
/**
50-
* @return Comparison strategy object
47+
* @return Comparison strategy object.
5148
*/
52-
public static BCELComparator getComparator() {
49+
public static BCELComparator<Constant> getComparator() {
5350
return bcelComparator;
5451
}
5552

@@ -107,7 +104,7 @@ public static Constant readConstant(final DataInput dataInput) throws IOExceptio
107104
/**
108105
* @param comparator Comparison strategy object
109106
*/
110-
public static void setComparator(final BCELComparator comparator) {
107+
public static void setComparator(final BCELComparator<Constant> comparator) {
111108
bcelComparator = comparator;
112109
}
113110

@@ -168,7 +165,7 @@ public Constant copy() {
168165
*/
169166
@Override
170167
public boolean equals(final Object obj) {
171-
return bcelComparator.equals(this, obj);
168+
return obj instanceof Constant && bcelComparator.equals(this, (Constant) obj);
172169
}
173170

174171
/**

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,16 @@ public final class Field extends FieldOrMethod {
3737
*/
3838
public static final Field[] EMPTY_ARRAY = {};
3939

40-
private static BCELComparator bcelComparator = new BCELComparator() {
40+
private static BCELComparator<Field> bcelComparator = new BCELComparator<Field>() {
4141

4242
@Override
43-
public boolean equals(final Object o1, final Object o2) {
44-
final Field THIS = (Field) o1;
45-
final Field THAT = (Field) o2;
46-
return Objects.equals(THIS.getName(), THAT.getName()) && Objects.equals(THIS.getSignature(), THAT.getSignature());
43+
public boolean equals(final Field a, final Field b) {
44+
return a == b || a != null && b != null && Objects.equals(a.getName(), b.getName()) && Objects.equals(a.getSignature(), b.getSignature());
4745
}
4846

4947
@Override
50-
public int hashCode(final Object o) {
51-
final Field THIS = (Field) o;
52-
return THIS.getSignature().hashCode() ^ THIS.getName().hashCode();
48+
public int hashCode(final Field o) {
49+
return o != null ? Objects.hash(o.getSignature(), o.getName()) : 0;
5350
}
5451
};
5552

@@ -59,23 +56,23 @@ public int hashCode(final Object o) {
5956
static final Field[] EMPTY_FIELD_ARRAY = {};
6057

6158
/**
62-
* @return Comparison strategy object
59+
* @return Comparison strategy object.
6360
*/
64-
public static BCELComparator getComparator() {
61+
public static BCELComparator<Field> getComparator() {
6562
return bcelComparator;
6663
}
6764

6865
/**
69-
* @param comparator Comparison strategy object
66+
* @param comparator Comparison strategy object.
7067
*/
71-
public static void setComparator(final BCELComparator comparator) {
68+
public static void setComparator(final BCELComparator<Field> comparator) {
7269
bcelComparator = comparator;
7370
}
7471

7572
/**
7673
* Constructs object from file stream.
7774
*
78-
* @param file Input stream
75+
* @param file Input stream.
7976
*/
8077
Field(final DataInput file, final ConstantPool constantPool) throws IOException, ClassFormatException {
8178
super(file, constantPool);
@@ -128,7 +125,7 @@ public Field copy(final ConstantPool constantPool) {
128125
*/
129126
@Override
130127
public boolean equals(final Object obj) {
131-
return bcelComparator.equals(this, obj);
128+
return obj instanceof Field && bcelComparator.equals(this, (Field) obj);
132129
}
133130

134131
/**

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,17 @@ public class JavaClass extends AccessFlags implements Cloneable, Node, Comparabl
6565
public static final byte FILE = 2;
6666
public static final byte ZIP = 3;
6767
private static final boolean debug = Boolean.getBoolean("JavaClass.debug"); // Debugging on/off
68-
private static BCELComparator bcelComparator = new BCELComparator() {
68+
69+
private static BCELComparator<JavaClass> bcelComparator = new BCELComparator<JavaClass>() {
6970

7071
@Override
71-
public boolean equals(final Object o1, final Object o2) {
72-
final JavaClass THIS = (JavaClass) o1;
73-
final JavaClass THAT = (JavaClass) o2;
74-
return Objects.equals(THIS.getClassName(), THAT.getClassName());
72+
public boolean equals(final JavaClass a, final JavaClass b) {
73+
return a == b || a != null && b != null && Objects.equals(a.getClassName(), b.getClassName());
7574
}
7675

7776
@Override
78-
public int hashCode(final Object o) {
79-
final JavaClass THIS = (JavaClass) o;
80-
return THIS.getClassName().hashCode();
77+
public int hashCode(final JavaClass o) {
78+
return o != null ? Objects.hashCode(o.getClassName()) : 0;
8179
}
8280
};
8381

@@ -91,9 +89,9 @@ static void Debug(final String str) {
9189
}
9290

9391
/**
94-
* @return Comparison strategy object
92+
* @return Comparison strategy object.
9593
*/
96-
public static BCELComparator getComparator() {
94+
public static BCELComparator<JavaClass> getComparator() {
9795
return bcelComparator;
9896
}
9997

@@ -107,9 +105,9 @@ private static String indent(final Object obj) {
107105
}
108106

109107
/**
110-
* @param comparator Comparison strategy object
108+
* @param comparator Comparison strategy object.
111109
*/
112-
public static void setComparator(final BCELComparator comparator) {
110+
public static void setComparator(final BCELComparator<JavaClass> comparator) {
113111
bcelComparator = comparator;
114112
}
115113

@@ -391,7 +389,7 @@ public void dump(final String fileName) throws IOException {
391389
*/
392390
@Override
393391
public boolean equals(final Object obj) {
394-
return bcelComparator.equals(this, obj);
392+
return obj instanceof JavaClass && bcelComparator.equals(this, (JavaClass) obj);
395393
}
396394

397395
/**

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,16 @@ public final class Method extends FieldOrMethod {
3636
*/
3737
public static final Method[] EMPTY_ARRAY = {};
3838

39-
private static BCELComparator bcelComparator = new BCELComparator() {
39+
private static BCELComparator<Method> bcelComparator = new BCELComparator<Method>() {
4040

4141
@Override
42-
public boolean equals(final Object o1, final Object o2) {
43-
final Method THIS = (Method) o1;
44-
final Method THAT = (Method) o2;
45-
return Objects.equals(THIS.getName(), THAT.getName()) && Objects.equals(THIS.getSignature(), THAT.getSignature());
42+
public boolean equals(final Method a, final Method b) {
43+
return a == b || a != null && b != null && Objects.equals(a.getName(), b.getName()) && Objects.equals(a.getSignature(), b.getSignature());
4644
}
4745

4846
@Override
49-
public int hashCode(final Object o) {
50-
final Method THIS = (Method) o;
51-
return THIS.getSignature().hashCode() ^ THIS.getName().hashCode();
47+
public int hashCode(final Method o) {
48+
return o != null ? Objects.hash(o.getSignature(), o.getName()) : 0;
5249
}
5350
};
5451

@@ -58,20 +55,20 @@ public int hashCode(final Object o) {
5855
static final Method[] EMPTY_METHOD_ARRAY = {};
5956

6057
/**
61-
* @return Comparison strategy object
58+
* @return Comparison strategy object.
6259
*/
63-
public static BCELComparator getComparator() {
60+
public static BCELComparator<Method> getComparator() {
6461
return bcelComparator;
6562
}
6663

6764
/**
68-
* @param comparator Comparison strategy object
65+
* @param comparator Comparison strategy object.
6966
*/
70-
public static void setComparator(final BCELComparator comparator) {
67+
public static void setComparator(final BCELComparator<Method> comparator) {
7168
bcelComparator = comparator;
7269
}
7370

74-
// annotations defined on the parameters of a method
71+
/** Annotations defined on the parameters of a method. */
7572
private ParameterAnnotationEntry[] parameterAnnotationEntries;
7673

7774
/**
@@ -138,7 +135,7 @@ public Method copy(final ConstantPool constantPool) {
138135
*/
139136
@Override
140137
public boolean equals(final Object obj) {
141-
return bcelComparator.equals(this, obj);
138+
return obj instanceof Method && bcelComparator.equals(this, (Method) obj);
142139
}
143140

144141
/**

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,33 +44,30 @@
4444
*/
4545
public class ClassGen extends AccessFlags implements Cloneable {
4646

47-
private static BCELComparator bcelComparator = new BCELComparator() {
47+
private static BCELComparator<ClassGen> bcelComparator = new BCELComparator<ClassGen>() {
4848

4949
@Override
50-
public boolean equals(final Object o1, final Object o2) {
51-
final ClassGen THIS = (ClassGen) o1;
52-
final ClassGen THAT = (ClassGen) o2;
53-
return Objects.equals(THIS.getClassName(), THAT.getClassName());
50+
public boolean equals(final ClassGen a, final ClassGen b) {
51+
return a == b || a != null && b != null && Objects.equals(a.getClassName(), b.getClassName());
5452
}
5553

5654
@Override
57-
public int hashCode(final Object o) {
58-
final ClassGen THIS = (ClassGen) o;
59-
return THIS.getClassName().hashCode();
55+
public int hashCode(final ClassGen o) {
56+
return o != null ? Objects.hashCode(o.getClassName()) : 0;
6057
}
6158
};
6259

6360
/**
6461
* @return Comparison strategy object
6562
*/
66-
public static BCELComparator getComparator() {
63+
public static BCELComparator<ClassGen> getComparator() {
6764
return bcelComparator;
6865
}
6966

7067
/**
7168
* @param comparator Comparison strategy object
7269
*/
73-
public static void setComparator(final BCELComparator comparator) {
70+
public static void setComparator(final BCELComparator<ClassGen> comparator) {
7471
bcelComparator = comparator;
7572
}
7673

@@ -279,7 +276,7 @@ public Method containsMethod(final String name, final String signature) {
279276
*/
280277
@Override
281278
public boolean equals(final Object obj) {
282-
return bcelComparator.equals(this, obj);
279+
return obj instanceof ClassGen && bcelComparator.equals(this, (ClassGen) obj);
283280
}
284281

285282
// J5TODO: Should we make calling unpackAnnotations() lazy and put it in here?

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

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,33 +40,30 @@
4040
*/
4141
public class FieldGen extends FieldGenOrMethodGen {
4242

43-
private static BCELComparator bcelComparator = new BCELComparator() {
43+
private static BCELComparator<FieldGen> bcelComparator = new BCELComparator<FieldGen>() {
4444

4545
@Override
46-
public boolean equals(final Object o1, final Object o2) {
47-
final FieldGen THIS = (FieldGen) o1;
48-
final FieldGen THAT = (FieldGen) o2;
49-
return Objects.equals(THIS.getName(), THAT.getName()) && Objects.equals(THIS.getSignature(), THAT.getSignature());
46+
public boolean equals(final FieldGen a, final FieldGen b) {
47+
return a == b || a != null && b != null && Objects.equals(a.getName(), b.getName()) && Objects.equals(a.getSignature(), b.getSignature());
5048
}
5149

5250
@Override
53-
public int hashCode(final Object o) {
54-
final FieldGen THIS = (FieldGen) o;
55-
return THIS.getSignature().hashCode() ^ THIS.getName().hashCode();
51+
public int hashCode(final FieldGen o) {
52+
return o != null ? Objects.hash(o.getSignature(), o.getName()) : 0;
5653
}
5754
};
5855

5956
/**
60-
* @return Comparison strategy object
57+
* @return Comparison strategy object.
6158
*/
62-
public static BCELComparator getComparator() {
59+
public static BCELComparator<FieldGen> getComparator() {
6360
return bcelComparator;
6461
}
6562

6663
/**
67-
* @param comparator Comparison strategy object
64+
* @param comparator Comparison strategy object.
6865
*/
69-
public static void setComparator(final BCELComparator comparator) {
66+
public static void setComparator(final BCELComparator<FieldGen> comparator) {
7067
bcelComparator = comparator;
7168
}
7269

@@ -77,8 +74,8 @@ public static void setComparator(final BCELComparator comparator) {
7774
/**
7875
* Instantiate from existing field.
7976
*
80-
* @param field Field object
81-
* @param cp constant pool (must contain the same entries as the field's constant pool)
77+
* @param field Field object.
78+
* @param cp constant pool (must contain the same entries as the field's constant pool).
8279
*/
8380
public FieldGen(final Field field, final ConstantPoolGen cp) {
8481
this(field.getAccessFlags(), Type.getType(field.getSignature()), field.getName(), cp);
@@ -183,7 +180,7 @@ public FieldGen copy(final ConstantPoolGen cp) {
183180
*/
184181
@Override
185182
public boolean equals(final Object obj) {
186-
return bcelComparator.equals(this, obj);
183+
return obj instanceof FieldGen && bcelComparator.equals(this, (FieldGen) obj);
187184
}
188185

189186
/**

0 commit comments

Comments
 (0)