Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main/java/org/apache/bcel/classfile/RecordComponentInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,25 @@ public void dump(final DataOutputStream file) throws IOException {
}
}

/**
* 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.
*
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/org/apache/bcel/classfile/RecordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -118,4 +120,14 @@ void testRecordToString() throws ClassNotFoundException, ClassFormatException, I
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));
}
}
Loading