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 a67d64ae Add getAttribute(final byte tag) to RecordComponentInfo (#494)
a67d64ae is described below

commit a67d64ae04ebbdc11418f1a052d024dec97528e1
Author: nbauma109 <[email protected]>
AuthorDate: Sun Feb 1 21:57:10 2026 +0100

    Add getAttribute(final byte tag) to RecordComponentInfo (#494)
    
    * Add getAttribute(final byte tag) to RecordComponentInfo
    
    * fix checkstyle issues
    
    * Make a separate record test testRecordComponentGetAttribute
    
    * Javadoc
    
    ---------
    
    Co-authored-by: nbauma109 <[email protected]>
    Co-authored-by: Gary Gregory <[email protected]>
---
 .../apache/bcel/classfile/RecordComponentInfo.java    | 19 +++++++++++++++++++
 .../java/org/apache/bcel/classfile/RecordTest.java    | 12 ++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/src/main/java/org/apache/bcel/classfile/RecordComponentInfo.java 
b/src/main/java/org/apache/bcel/classfile/RecordComponentInfo.java
index 2aabce87..4a1c349a 100644
--- a/src/main/java/org/apache/bcel/classfile/RecordComponentInfo.java
+++ b/src/main/java/org/apache/bcel/classfile/RecordComponentInfo.java
@@ -77,6 +77,25 @@ public class RecordComponentInfo implements Node {
         }
     }
 
+    /**
+     * Gets the attribute for the given tag if present, or null if absent.
+     *
+     * @param <T> the attribute type.
+     * @param tag the attribute tag.
+     * @return Attribute for given tag, null if not found.
+     * Refer to {@link org.apache.bcel.Const#ATTR_UNKNOWN} constants named 
ATTR_* for possible values.
+     * @since 6.13.0
+     */
+    @SuppressWarnings("unchecked")
+    public final <T extends Attribute> T getAttribute(final byte tag) {
+        for (final Attribute attribute : getAttributes()) {
+            if (attribute.getTag() == tag) {
+                return (T) attribute;
+            }
+        }
+        return null;
+    }
+
     /**
      * Gets all attributes.
      *
diff --git a/src/test/java/org/apache/bcel/classfile/RecordTest.java 
b/src/test/java/org/apache/bcel/classfile/RecordTest.java
index 9588e4e7..afdd7fe3 100644
--- a/src/test/java/org/apache/bcel/classfile/RecordTest.java
+++ b/src/test/java/org/apache/bcel/classfile/RecordTest.java
@@ -21,12 +21,14 @@ package org.apache.bcel.classfile;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.io.File;
 import java.io.IOException;
 
 import org.apache.bcel.AbstractTest;
+import org.apache.bcel.Const;
 import org.apache.bcel.util.SyntheticRepository;
 import org.apache.bcel.visitors.CountingVisitor;
 import org.junit.jupiter.api.Test;
@@ -118,4 +120,14 @@ class RecordTest extends AbstractTest {
         assertEquals("RecordComponentInfo(aNumber,I,0):", 
firstComponent.toString());
     }
 
+    @Test
+    void testRecordComponentGetAttribute() throws ClassFormatException, 
IOException {
+        final JavaClass clazz = new 
ClassParser("src/test/resources/record/SimpleRecord.class").parse();
+        final Record recordAttribute = (Record) findAttribute("Record", 
clazz)[0];
+        final RecordComponentInfo secondComponent = 
recordAttribute.getComponents()[1];
+        final RuntimeVisibleAnnotations ann = 
secondComponent.getAttribute(Const.ATTR_RUNTIME_VISIBLE_ANNOTATIONS);
+        assertEquals("RuntimeVisibleAnnotations:\n"
+                + "  @Ljavax/annotation/Nonnull;", ann.toString());
+        
assertNull(secondComponent.getAttribute(Const.ATTR_RUNTIME_INVISIBLE_ANNOTATIONS));
+    }
 }

Reply via email to