Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.4] Support ClassInfo signature at any position #162

Merged
merged 2 commits into from
Jan 5, 2022
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
<dependency>
<groupId>org.jboss.jandex</groupId>
<artifactId>typeannotation-test</artifactId>
<version>1.9</version>
<version>1.10</version>
<type>jar</type>
<scope>test</scope>
</dependency>
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/org/jboss/jandex/Indexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ private static void skipFully(InputStream s, long n) throws IOException {
private ArrayList<AnnotationInstance> elementAnnotations;
private IdentityHashMap<AnnotationTarget, Object> signaturePresent;
private List<Object> signatures;
private int classSignatureIndex;
private Map<DotName, InnerClassInfo> innerClasses;
private IdentityHashMap<AnnotationTarget, List<TypeAnnotationState>> typeAnnotations;
private List<MethodInfo> methods;
Expand Down Expand Up @@ -320,6 +321,7 @@ private void initClassFields() {
elementAnnotations = new ArrayList<AnnotationInstance>();
signaturePresent = new IdentityHashMap<AnnotationTarget, Object>();
signatures = new ArrayList<Object>();
classSignatureIndex = -1;
typeAnnotations = new IdentityHashMap<AnnotationTarget, List<TypeAnnotationState>>();

// in bytecode, record components are stored as class attributes,
Expand Down Expand Up @@ -1386,6 +1388,9 @@ private void processExceptions(DataInputStream data, MethodInfo target) throws I

private void processSignature(DataInputStream data, AnnotationTarget target) throws IOException {
String signature = decodeUtf8Entry(data.readUnsignedShort());
if (target instanceof ClassInfo) {
classSignatureIndex = signatures.size();
}
signatures.add(signature);
signatures.add(target);
signaturePresent.put(target, null);
Expand All @@ -1401,14 +1406,18 @@ private void parseClassSignature(String signature, ClassInfo clazz) {
private void applySignatures() {
int end = signatures.size();

// Class signature is always the last element and should be processed first
Object last = end > 1 ? signatures.get(end - 1) : null;
if (last instanceof ClassInfo) {
parseClassSignature((String)signatures.get(end - 2), (ClassInfo)last);
end -= 2;
// Class signature should be processed first to establish class type parameters
if (classSignatureIndex >= 0) {
String elementSignature = (String) signatures.get(classSignatureIndex);
Object element = signatures.get(classSignatureIndex + 1);
parseClassSignature(elementSignature, (ClassInfo) element);
}

for (int i = 0; i < end; i += 2) {
if (i == classSignatureIndex) {
continue;
}

String elementSignature = (String) signatures.get(i);
Object element = signatures.get(i + 1);

Expand Down Expand Up @@ -1995,6 +2004,7 @@ public ClassInfo index(InputStream stream) throws IOException {
elementAnnotations = null;
innerClasses = null;
signatures = null;
classSignatureIndex = -1;
signaturePresent = null;
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/test/java/org/jboss/jandex/test/RecordTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void testRecordComponentHasAnnotation() {
assertEquals("name", componentAnnos.get(0).target().asRecordComponent().name());
assertEquals("nameComponent", componentAnnos.get(0).value().asString());

assertEquals(2, rec.recordComponents().size());
assertEquals(3, rec.recordComponents().size());

RecordComponentInfo idComponent = rec.recordComponent("id");
assertNotNull(idComponent);
Expand Down Expand Up @@ -144,6 +144,16 @@ public void testComponentAccessorHasAnnotation() {
assertEquals("nameAccessor", nameAnnotations.get(1).value().asString());
}

@Test
public void testRecordSignatureProcessed() {
ClassInfo rec = index.getClassByName(DotName.createSimple("test.RecordExample"));
assertNotNull(rec);
assertTrue(rec.isRecord());

assertEquals(1, rec.typeParameters().size());
assertEquals("T", rec.typeParameters().get(0).identifier());
}

private Index buildIndex() throws IOException {
Indexer indexer = new Indexer();
indexer.index(getClass().getClassLoader().getResourceAsStream("test/RecordExample.class"));
Expand Down