From 5054f64fb40c34065cd0c52f0d375a978258714f Mon Sep 17 00:00:00 2001 From: nbauma109 Date: Sun, 1 Feb 2026 16:42:40 +0100 Subject: [PATCH 1/4] Add getAttribute(final byte tag) to RecordComponentInfo --- .../bcel/classfile/RecordComponentInfo.java | 19 +++++++++++++++++++ .../org/apache/bcel/classfile/RecordTest.java | 9 ++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/bcel/classfile/RecordComponentInfo.java b/src/main/java/org/apache/bcel/classfile/RecordComponentInfo.java index 2aabce87e2..3006d3adc3 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 void dump(final DataOutputStream file) throws IOException { } } + /** + * Gets attribute for given tag. + * + * @param 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 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 9588e4e761..8804d91ca1 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 @@ 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; @@ -111,11 +113,16 @@ void testRecordToString() throws ClassNotFoundException, ClassFormatException, I + " RuntimeVisibleAnnotations:\n" + " @Ljavax/annotation/Nonnull;", recordAttribute.toString()); final RecordComponentInfo firstComponent = recordAttribute.getComponents()[0]; + final RecordComponentInfo secondComponent = recordAttribute.getComponents()[1]; assertEquals(5, firstComponent.getIndex()); assertEquals(6, firstComponent.getDescriptorIndex()); assertEquals(0, firstComponent.getAttributes().length); assertEquals(recordAttribute.getConstantPool(), firstComponent.getConstantPool()); assertEquals("RecordComponentInfo(aNumber,I,0):", firstComponent.toString()); + 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)); } - + } From eb3a9a240bc5a3c23728382af0855ba02f83bf47 Mon Sep 17 00:00:00 2001 From: nbauma109 Date: Sun, 1 Feb 2026 16:59:53 +0100 Subject: [PATCH 2/4] fix checkstyle issues --- src/test/java/org/apache/bcel/classfile/RecordTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/apache/bcel/classfile/RecordTest.java b/src/test/java/org/apache/bcel/classfile/RecordTest.java index 8804d91ca1..d4eb8d9e4e 100644 --- a/src/test/java/org/apache/bcel/classfile/RecordTest.java +++ b/src/test/java/org/apache/bcel/classfile/RecordTest.java @@ -119,10 +119,10 @@ void testRecordToString() throws ClassNotFoundException, ClassFormatException, I assertEquals(0, firstComponent.getAttributes().length); assertEquals(recordAttribute.getConstantPool(), firstComponent.getConstantPool()); assertEquals("RecordComponentInfo(aNumber,I,0):", firstComponent.toString()); - RuntimeVisibleAnnotations ann = secondComponent.getAttribute(Const.ATTR_RUNTIME_VISIBLE_ANNOTATIONS); + 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)); } - + } From f9a0d66dfe58bf7d0b61e1fbcb19ed3b16a72847 Mon Sep 17 00:00:00 2001 From: nbauma109 Date: Sun, 1 Feb 2026 20:40:07 +0100 Subject: [PATCH 3/4] Make a separate record test testRecordComponentGetAttribute --- src/test/java/org/apache/bcel/classfile/RecordTest.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/apache/bcel/classfile/RecordTest.java b/src/test/java/org/apache/bcel/classfile/RecordTest.java index d4eb8d9e4e..afdd7fe32e 100644 --- a/src/test/java/org/apache/bcel/classfile/RecordTest.java +++ b/src/test/java/org/apache/bcel/classfile/RecordTest.java @@ -113,16 +113,21 @@ void testRecordToString() throws ClassNotFoundException, ClassFormatException, I + " RuntimeVisibleAnnotations:\n" + " @Ljavax/annotation/Nonnull;", recordAttribute.toString()); final RecordComponentInfo firstComponent = recordAttribute.getComponents()[0]; - final RecordComponentInfo secondComponent = recordAttribute.getComponents()[1]; assertEquals(5, firstComponent.getIndex()); assertEquals(6, firstComponent.getDescriptorIndex()); assertEquals(0, firstComponent.getAttributes().length); assertEquals(recordAttribute.getConstantPool(), firstComponent.getConstantPool()); 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)); } - } From 57c7c6e011f50e594f396819449172197452d63e Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sun, 1 Feb 2026 14:48:29 -0500 Subject: [PATCH 4/4] Javadoc --- .../java/org/apache/bcel/classfile/RecordComponentInfo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/apache/bcel/classfile/RecordComponentInfo.java b/src/main/java/org/apache/bcel/classfile/RecordComponentInfo.java index 3006d3adc3..4a1c349aa0 100644 --- a/src/main/java/org/apache/bcel/classfile/RecordComponentInfo.java +++ b/src/main/java/org/apache/bcel/classfile/RecordComponentInfo.java @@ -78,7 +78,7 @@ public void dump(final DataOutputStream file) throws IOException { } /** - * Gets attribute for given tag. + * Gets the attribute for the given tag if present, or null if absent. * * @param the attribute type. * @param tag the attribute tag.