From a4f679d1173c3a7fef55da53fa6052a3a2c29dc1 Mon Sep 17 00:00:00 2001 From: Ladislav Thon Date: Thu, 8 Aug 2024 15:03:45 +0200 Subject: [PATCH] replace ClassInfo.unsorted* with *InDeclarationOrder --- .../main/java/org/jboss/jandex/ClassInfo.java | 50 ++++++++++++++----- .../test/ClassInfoMemberPositionTestCase.java | 44 +++++++--------- 2 files changed, 55 insertions(+), 39 deletions(-) diff --git a/core/src/main/java/org/jboss/jandex/ClassInfo.java b/core/src/main/java/org/jboss/jandex/ClassInfo.java index ff2e22e2..f1335491 100644 --- a/core/src/main/java/org/jboss/jandex/ClassInfo.java +++ b/core/src/main/java/org/jboss/jandex/ClassInfo.java @@ -704,9 +704,9 @@ public final List classAnnotationsWithRepeatable(DotName nam * This list may be empty, but is never {@code null}. *

* Note that the result doesn't 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 methods() { return new MethodInfoGenerator(this, methods, EMPTY_POSITIONS); @@ -721,13 +721,21 @@ public final List 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 unsortedMethods() { + public final List methodsInDeclarationOrder() { return new MethodInfoGenerator(this, methods, methodPositions); } + /** + * @deprecated use {@link #methodsInDeclarationOrder()} + */ + @Deprecated + public final List unsortedMethods() { + return methodsInDeclarationOrder(); + } + /** * Returns a list of all constructors declared in this class (which have the special name {@code }). * It does not include inherited constructors. These must be discovered by traversing the class hierarchy. @@ -881,9 +889,9 @@ public final FieldInfo field(String name) { * empty, but is never {@code null}. *

* Note that the result doesn't 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 fields() { return new FieldInfoGenerator(this, fields, EMPTY_POSITIONS); @@ -898,13 +906,21 @@ public final List 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 unsortedFields() { + public final List fieldsInDeclarationOrder() { return new FieldInfoGenerator(this, fields, fieldPositions); } + /** + * @deprecated use {@link #fieldsInDeclarationOrder()} + */ + @Deprecated + public final List unsortedFields() { + return fieldsInDeclarationOrder(); + } + final FieldInternal[] fieldArray() { return fields; } @@ -961,10 +977,10 @@ public final List 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 unsortedRecordComponents() { + public final List recordComponentsInDeclarationOrder() { if (extra == null || extra.recordComponents == null) { return Collections.emptyList(); } @@ -972,6 +988,14 @@ public final List unsortedRecordComponents() { return new RecordComponentInfoGenerator(this, extra.recordComponents, extra.recordComponentPositions); } + /** + * @deprecated use {@link #recordComponentsInDeclarationOrder()} + */ + @Deprecated + public final List unsortedRecordComponents() { + return recordComponentsInDeclarationOrder(); + } + final RecordComponentInternal[] recordComponentArray() { return extra != null && extra.recordComponents != null ? extra.recordComponents : RecordComponentInternal.EMPTY_ARRAY; } diff --git a/core/src/test/java/org/jboss/jandex/test/ClassInfoMemberPositionTestCase.java b/core/src/test/java/org/jboss/jandex/test/ClassInfoMemberPositionTestCase.java index f32fcf84..fe5cb6d8 100644 --- a/core/src/test/java/org/jboss/jandex/test/ClassInfoMemberPositionTestCase.java +++ b/core/src/test/java/org/jboss/jandex/test/ClassInfoMemberPositionTestCase.java @@ -40,61 +40,53 @@ 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 unsortedFields = clazz.unsortedFields(); + List 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 sortedFields = clazz.fields(); + List 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 unsortedFields = clazz.unsortedFields(); + // actually _not_ in declaration order, because of too many fields! + List 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 sortedFields = clazz.fields(); + List 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 fields = clazz.unsortedFields(); + List fields = clazz.fieldsInDeclarationOrder(); int f = 0; assertEquals("z", fields.get(f++).name()); assertEquals("omega", fields.get(f++).name()); @@ -102,7 +94,7 @@ private static void assertOriginalPositions(Index index) { assertEquals("x", fields.get(f++).name()); assertEquals("alpha", fields.get(f++).name()); - List methods = clazz.unsortedMethods(); + List methods = clazz.methodsInDeclarationOrder(); int m = 0; assertEquals("c", methods.get(m++).name()); assertEquals("", methods.get(m++).name());