Skip to content

Commit

Permalink
Merge pull request #420 from Ladicek/replace-unsorted-with-in-declara…
Browse files Browse the repository at this point in the history
…tion-order

replace ClassInfo.unsorted* with *InDeclarationOrder
  • Loading branch information
Ladicek authored Aug 8, 2024
2 parents 6b0f3e3 + a4f679d commit 333ceef
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 39 deletions.
50 changes: 37 additions & 13 deletions core/src/main/java/org/jboss/jandex/ClassInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -704,9 +704,9 @@ public final List<AnnotationInstance> classAnnotationsWithRepeatable(DotName nam
* This list may be empty, but is never {@code null}.
* <p>
* Note that the result <em>doesn't</em> have any guaranteed order. If you need
* declaration order, use {@link #unsortedMethods()}.
* declaration order, use {@link #methodsInDeclarationOrder()}.
*
* @return the list of methods declared in this class
* @return immutable list of methods
*/
public final List<MethodInfo> methods() {
return new MethodInfoGenerator(this, methods, EMPTY_POSITIONS);
Expand All @@ -721,13 +721,21 @@ public final List<MethodInfo> methods() {
* methods may be present; if there's more, order is undefined. This also assumes that the bytecode
* order corresponds to declaration order, which is not guaranteed, but practically always holds.
*
* @return a list of methods
* @since 2.4
* @return immutable list of methods, in declaration order
* @since 3.2.2
*/
public final List<MethodInfo> unsortedMethods() {
public final List<MethodInfo> methodsInDeclarationOrder() {
return new MethodInfoGenerator(this, methods, methodPositions);
}

/**
* @deprecated use {@link #methodsInDeclarationOrder()}
*/
@Deprecated
public final List<MethodInfo> unsortedMethods() {
return methodsInDeclarationOrder();
}

/**
* Returns a list of all constructors declared in this class (which have the special name {@code <init>}).
* It does not include inherited constructors. These must be discovered by traversing the class hierarchy.
Expand Down Expand Up @@ -881,9 +889,9 @@ public final FieldInfo field(String name) {
* empty, but is never {@code null}.
* <p>
* Note that the result <em>doesn't</em> have any guaranteed order. If you need
* declaration order, use {@link #unsortedFields()}.
* declaration order, use {@link #fieldsInDeclarationOrder()}.
*
* @return a list of fields
* @return immutable list of fields
*/
public final List<FieldInfo> fields() {
return new FieldInfoGenerator(this, fields, EMPTY_POSITIONS);
Expand All @@ -898,13 +906,21 @@ public final List<FieldInfo> fields() {
* fields may be present; if there's more, order is undefined. This also assumes that the bytecode
* order corresponds to declaration order, which is not guaranteed, but practically always holds.
*
* @return a list of fields
* @since 2.4
* @return immutable list of fields, in declaration order
* @since 3.2.2
*/
public final List<FieldInfo> unsortedFields() {
public final List<FieldInfo> fieldsInDeclarationOrder() {
return new FieldInfoGenerator(this, fields, fieldPositions);
}

/**
* @deprecated use {@link #fieldsInDeclarationOrder()}
*/
@Deprecated
public final List<FieldInfo> unsortedFields() {
return fieldsInDeclarationOrder();
}

final FieldInternal[] fieldArray() {
return fields;
}
Expand Down Expand Up @@ -961,17 +977,25 @@ public final List<RecordComponentInfo> recordComponents() {
* assumes that the bytecode order corresponds to declaration order, which is not guaranteed,
* but practically always holds.
*
* @return immutable list of record components
* @since 2.4
* @return immutable list of record components, in declaration order
* @since 3.2.2
*/
public final List<RecordComponentInfo> unsortedRecordComponents() {
public final List<RecordComponentInfo> recordComponentsInDeclarationOrder() {
if (extra == null || extra.recordComponents == null) {
return Collections.emptyList();
}

return new RecordComponentInfoGenerator(this, extra.recordComponents, extra.recordComponentPositions);
}

/**
* @deprecated use {@link #recordComponentsInDeclarationOrder()}
*/
@Deprecated
public final List<RecordComponentInfo> unsortedRecordComponents() {
return recordComponentsInDeclarationOrder();
}

final RecordComponentInternal[] recordComponentArray() {
return extra != null && extra.recordComponents != null ? extra.recordComponents : RecordComponentInternal.EMPTY_ARRAY;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,69 +40,61 @@ public class ClassInfoMemberPositionTestCase {

@BeforeEach
public void setUp() throws IOException {
Indexer indexer = new Indexer();
String prefix = "org/jboss/jandex/test/ClassInfoMemberPositionTestCase$";
indexer.index(getClass().getClassLoader().getResourceAsStream(prefix + "TestEntity.class"));
indexer.index(getClass().getClassLoader().getResourceAsStream(prefix + "MaxSizeTestEntity.class"));
indexer.index(getClass().getClassLoader().getResourceAsStream(prefix + "OverMaxSizeTestEntity.class"));
this.index = indexer.complete();
this.index = Index.of(TestEntity.class, MaxSizeTestEntity.class, OverMaxSizeTestEntity.class);
}

@Test
public void testMembersUnsorted() {
assertOriginalPositions(index);
public void testMembersInDeclarationOrder() throws IOException {
assertDeclarationOrder(index);
assertDeclarationOrder(IndexingUtil.roundtrip(index));
}

@Test
public void testMembersUnsortedAfterRoundtrip() throws IOException {
assertOriginalPositions(IndexingUtil.roundtrip(index));
}

@Test
public void testMaxMembersUnsortedAndSorted() {
public void testMaxMembers() {
ClassInfo clazz = index.getClassByName(DotName.createSimple(MaxSizeTestEntity.class.getName()));
assertNotNull(clazz);

List<FieldInfo> unsortedFields = clazz.unsortedFields();
List<FieldInfo> fieldsInDeclarationOrder = clazz.fieldsInDeclarationOrder();
for (int i = 0; i < 256; i++) {
assertEquals(String.format(Locale.ROOT, "f%03d", 255 - i), unsortedFields.get(i).name());
assertEquals(String.format(Locale.ROOT, "f%03d", 255 - i), fieldsInDeclarationOrder.get(i).name());
}

List<FieldInfo> sortedFields = clazz.fields();
List<FieldInfo> fields = clazz.fields();
for (int i = 0; i < 256; i++) {
assertEquals(String.format(Locale.ROOT, "f%03d", i), sortedFields.get(i).name());
assertEquals(String.format(Locale.ROOT, "f%03d", i), fields.get(i).name());
}
}

@Test
public void testOverMaxMembersUnsortedAndSorted() {
public void testOverMaxMembers() {
ClassInfo clazz = index.getClassByName(DotName.createSimple(OverMaxSizeTestEntity.class.getName()));
assertNotNull(clazz);

List<FieldInfo> unsortedFields = clazz.unsortedFields();
// actually _not_ in declaration order, because of too many fields!
List<FieldInfo> fieldsInDeclarationOrder = clazz.fieldsInDeclarationOrder();
for (int i = 0; i < 257; i++) {
assertEquals(String.format(Locale.ROOT, "f%03d", i), unsortedFields.get(i).name());
assertEquals(String.format(Locale.ROOT, "f%03d", i), fieldsInDeclarationOrder.get(i).name());
}

List<FieldInfo> sortedFields = clazz.fields();
List<FieldInfo> fields = clazz.fields();
for (int i = 0; i < 257; i++) {
assertEquals(String.format(Locale.ROOT, "f%03d", i), sortedFields.get(i).name());
assertEquals(String.format(Locale.ROOT, "f%03d", i), fields.get(i).name());
}
}

private static void assertOriginalPositions(Index index) {
private static void assertDeclarationOrder(Index index) {
ClassInfo clazz = index.getClassByName(DotName.createSimple(TestEntity.class.getName()));
assertNotNull(clazz);

List<FieldInfo> fields = clazz.unsortedFields();
List<FieldInfo> fields = clazz.fieldsInDeclarationOrder();
int f = 0;
assertEquals("z", fields.get(f++).name());
assertEquals("omega", fields.get(f++).name());
assertEquals("y", fields.get(f++).name());
assertEquals("x", fields.get(f++).name());
assertEquals("alpha", fields.get(f++).name());

List<MethodInfo> methods = clazz.unsortedMethods();
List<MethodInfo> methods = clazz.methodsInDeclarationOrder();
int m = 0;
assertEquals("c", methods.get(m++).name());
assertEquals("<init>", methods.get(m++).name());
Expand Down

0 comments on commit 333ceef

Please sign in to comment.