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 5306aa35 Field not found -> search field in both super class and 
implemented interfaces (5x duplicated code to find field by name and type is 
refactored to a new method and now supports package-private) #181
5306aa35 is described below

commit 5306aa357a41b92aa29c394f7a4d6ef5af9118d7
Author: Gary David Gregory (Code signing key) <ggreg...@apache.org>
AuthorDate: Sun Apr 9 20:35:54 2023 -0400

    Field not found -> search field in both super class and implemented
    interfaces (5x duplicated code to find field by name and type is
    refactored to a new method and now supports package-private) #181
---
 .../java/org/apache/bcel/classfile/JavaClass.java  | 80 +++++++++++-----------
 .../bcel/verifier/statics/Pass3aVerifier.java      |  6 +-
 .../structurals/InstConstraintVisitor.java         |  4 +-
 3 files changed, 45 insertions(+), 45 deletions(-)

diff --git a/src/main/java/org/apache/bcel/classfile/JavaClass.java 
b/src/main/java/org/apache/bcel/classfile/JavaClass.java
index d95789a1..71e8e580 100644
--- a/src/main/java/org/apache/bcel/classfile/JavaClass.java
+++ b/src/main/java/org/apache/bcel/classfile/JavaClass.java
@@ -307,46 +307,6 @@ public class JavaClass extends AccessFlags implements 
Cloneable, Node, Comparabl
         }
     }
 
-    /**
-     * Finds a visible field by name and type in this class and its super 
classes.
-     * @param fieldName the field name to find
-     * @param fieldType the field type to find
-     * @return field matching given name and type, null if field is not found 
or not accessible from this class.
-     * @throws ClassNotFoundException
-     * @since 6.8.0
-     */
-    public Field findFieldByNameAndType(final String fieldName, final Type 
fieldType) throws ClassNotFoundException {
-        for (final Field field : fields) {
-            if (field.getName().equals(fieldName)) {
-                final Type fType = Type.getType(field.getSignature());
-                /*
-                 * TODO: Check if assignment compatibility is sufficient. What 
does Sun do?
-                 */
-                if (fType.equals(fieldType)) {
-                    return field;
-                }
-            }
-        }
-
-        final JavaClass superclass = getSuperClass();
-        if (superclass != null && 
!"java.lang.Object".equals(superclass.getClassName())) {
-            final Field f = superclass.findFieldByNameAndType(fieldName, 
fieldType);
-            if (f != null && (f.isPublic() || f.isProtected() || 
!f.isPrivate() && packageName.equals(superclass.getPackageName()))) {
-                return f;
-            }
-        }
-        JavaClass[] implementedInterfaces = getInterfaces();
-        if (implementedInterfaces != null) {
-            for (JavaClass implementedInterface : implementedInterfaces) {
-                final Field f = 
implementedInterface.findFieldByNameAndType(fieldName, fieldType);
-                if (f != null) {
-                    return f;
-                }
-            }
-        }
-        return null;
-    }
-
     /**
      * Dump Java class to output stream in binary format.
      *
@@ -434,6 +394,46 @@ public class JavaClass extends AccessFlags implements 
Cloneable, Node, Comparabl
         return bcelComparator.equals(this, obj);
     }
 
+    /**
+     * Finds a visible field by name and type in this class and its super 
classes.
+     * @param fieldName the field name to find
+     * @param fieldType the field type to find
+     * @return field matching given name and type, null if field is not found 
or not accessible from this class.
+     * @throws ClassNotFoundException
+     * @since 6.8.0
+     */
+    public Field findField(final String fieldName, final Type fieldType) 
throws ClassNotFoundException {
+        for (final Field field : fields) {
+            if (field.getName().equals(fieldName)) {
+                final Type fType = Type.getType(field.getSignature());
+                /*
+                 * TODO: Check if assignment compatibility is sufficient. What 
does Sun do?
+                 */
+                if (fType.equals(fieldType)) {
+                    return field;
+                }
+            }
+        }
+
+        final JavaClass superclass = getSuperClass();
+        if (superclass != null && 
!"java.lang.Object".equals(superclass.getClassName())) {
+            final Field f = superclass.findField(fieldName, fieldType);
+            if (f != null && (f.isPublic() || f.isProtected() || 
!f.isPrivate() && packageName.equals(superclass.getPackageName()))) {
+                return f;
+            }
+        }
+        JavaClass[] implementedInterfaces = getInterfaces();
+        if (implementedInterfaces != null) {
+            for (JavaClass implementedInterface : implementedInterfaces) {
+                final Field f = implementedInterface.findField(fieldName, 
fieldType);
+                if (f != null) {
+                    return f;
+                }
+            }
+        }
+        return null;
+    }
+
     /**
      * Get all interfaces implemented by this JavaClass (transitively).
      *
diff --git a/src/main/java/org/apache/bcel/verifier/statics/Pass3aVerifier.java 
b/src/main/java/org/apache/bcel/verifier/statics/Pass3aVerifier.java
index fcb2375a..61e555d7 100644
--- a/src/main/java/org/apache/bcel/verifier/statics/Pass3aVerifier.java
+++ b/src/main/java/org/apache/bcel/verifier/statics/Pass3aVerifier.java
@@ -322,7 +322,7 @@ public final class Pass3aVerifier extends PassVerifier {
                 final String fieldName = o.getFieldName(constantPoolGen);
 
                 final JavaClass jc = 
Repository.lookupClass(getObjectType(o).getClassName());
-                final Field f = jc.findFieldByNameAndType(fieldName, 
o.getType(constantPoolGen));
+                final Field f = jc.findField(fieldName, 
o.getType(constantPoolGen));
                 if (f == null) {
                     constraintViolated(o, "Referenced field '" + fieldName + 
"' does not exist in class '" + jc.getClassName() + "'.");
                 }
@@ -366,7 +366,7 @@ public final class Pass3aVerifier extends PassVerifier {
             try {
                 final String fieldName = o.getFieldName(constantPoolGen);
                 final JavaClass jc = 
Repository.lookupClass(getObjectType(o).getClassName());
-                final Field f = jc.findFieldByNameAndType(fieldName, 
o.getType(constantPoolGen));
+                final Field f = jc.findField(fieldName, 
o.getType(constantPoolGen));
                 if (f == null) {
                     throw new AssertionViolatedException("Field '" + fieldName 
+ "' not found in " + jc.getClassName());
                 }
@@ -807,7 +807,7 @@ public final class Pass3aVerifier extends PassVerifier {
             try {
                 final String fieldName = o.getFieldName(constantPoolGen);
                 final JavaClass jc = 
Repository.lookupClass(getObjectType(o).getClassName());
-                final Field f = jc.findFieldByNameAndType(fieldName, 
o.getType(constantPoolGen));
+                final Field f = jc.findField(fieldName, 
o.getType(constantPoolGen));
                 if (f == null) {
                     throw new AssertionViolatedException("Field '" + fieldName 
+ "' not found in " + jc.getClassName());
                 }
diff --git 
a/src/main/java/org/apache/bcel/verifier/structurals/InstConstraintVisitor.java 
b/src/main/java/org/apache/bcel/verifier/structurals/InstConstraintVisitor.java
index dae00b07..ec6d6743 100644
--- 
a/src/main/java/org/apache/bcel/verifier/structurals/InstConstraintVisitor.java
+++ 
b/src/main/java/org/apache/bcel/verifier/structurals/InstConstraintVisitor.java
@@ -918,7 +918,7 @@ public class InstConstraintVisitor extends EmptyVisitor {
     private Field visitFieldInstructionInternals(final FieldInstruction o) 
throws ClassNotFoundException {
         final String fieldName = o.getFieldName(cpg);
         final JavaClass jc = 
Repository.lookupClass(getObjectType(o).getClassName());
-        final Field f = jc.findFieldByNameAndType(fieldName, o.getType(cpg));
+        final Field f = jc.findField(fieldName, o.getType(cpg));
         if (f == null) {
             throw new AssertionViolatedException("Field '" + fieldName + "' 
not found in " + jc.getClassName());
         }
@@ -1040,7 +1040,7 @@ public class InstConstraintVisitor extends EmptyVisitor {
             final String fieldName = o.getFieldName(cpg);
 
             final JavaClass jc = 
Repository.lookupClass(getObjectType(o).getClassName());
-            final Field f = jc.findFieldByNameAndType(fieldName, 
o.getType(cpg));
+            final Field f = jc.findField(fieldName, o.getType(cpg));
             if (f == null) {
                 throw new AssertionViolatedException("Field '" + fieldName + 
"' not found in " + jc.getClassName());
             }

Reply via email to