Skip to content

Commit a67d64a

Browse files
nbauma109nbauma109garydgregory
authored
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 <nbauma109@github.com> Co-authored-by: Gary Gregory <garydgregory@users.noreply.github.com>
1 parent 952c64b commit a67d64a

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,25 @@ public void dump(final DataOutputStream file) throws IOException {
7777
}
7878
}
7979

80+
/**
81+
* Gets the attribute for the given tag if present, or null if absent.
82+
*
83+
* @param <T> the attribute type.
84+
* @param tag the attribute tag.
85+
* @return Attribute for given tag, null if not found.
86+
* Refer to {@link org.apache.bcel.Const#ATTR_UNKNOWN} constants named ATTR_* for possible values.
87+
* @since 6.13.0
88+
*/
89+
@SuppressWarnings("unchecked")
90+
public final <T extends Attribute> T getAttribute(final byte tag) {
91+
for (final Attribute attribute : getAttributes()) {
92+
if (attribute.getTag() == tag) {
93+
return (T) attribute;
94+
}
95+
}
96+
return null;
97+
}
98+
8099
/**
81100
* Gets all attributes.
82101
*

src/test/java/org/apache/bcel/classfile/RecordTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
import static org.junit.jupiter.api.Assertions.assertEquals;
2222
import static org.junit.jupiter.api.Assertions.assertFalse;
2323
import static org.junit.jupiter.api.Assertions.assertNotNull;
24+
import static org.junit.jupiter.api.Assertions.assertNull;
2425
import static org.junit.jupiter.api.Assertions.assertTrue;
2526

2627
import java.io.File;
2728
import java.io.IOException;
2829

2930
import org.apache.bcel.AbstractTest;
31+
import org.apache.bcel.Const;
3032
import org.apache.bcel.util.SyntheticRepository;
3133
import org.apache.bcel.visitors.CountingVisitor;
3234
import org.junit.jupiter.api.Test;
@@ -118,4 +120,14 @@ void testRecordToString() throws ClassNotFoundException, ClassFormatException, I
118120
assertEquals("RecordComponentInfo(aNumber,I,0):", firstComponent.toString());
119121
}
120122

123+
@Test
124+
void testRecordComponentGetAttribute() throws ClassFormatException, IOException {
125+
final JavaClass clazz = new ClassParser("src/test/resources/record/SimpleRecord.class").parse();
126+
final Record recordAttribute = (Record) findAttribute("Record", clazz)[0];
127+
final RecordComponentInfo secondComponent = recordAttribute.getComponents()[1];
128+
final RuntimeVisibleAnnotations ann = secondComponent.getAttribute(Const.ATTR_RUNTIME_VISIBLE_ANNOTATIONS);
129+
assertEquals("RuntimeVisibleAnnotations:\n"
130+
+ " @Ljavax/annotation/Nonnull;", ann.toString());
131+
assertNull(secondComponent.getAttribute(Const.ATTR_RUNTIME_INVISIBLE_ANNOTATIONS));
132+
}
121133
}

0 commit comments

Comments
 (0)