Skip to content

Commit

Permalink
Slightly optimize type generator; add explanation comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadelessFox committed Jan 2, 2025
1 parent 116a089 commit 8e12e83
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,14 @@ private void collectDeclaredAttrs(
var serializable = cls.isAnnotationPresent(Serializable.class);
var start = output.size();
for (Method method : cls.getDeclaredMethods()) {
if (!Modifier.isAbstract(method.getModifiers()) || method.getDeclaringClass() == TypedObject.class) {
if (!Modifier.isAbstract(method.getModifiers())) {
// We'll look for the overloaded version of it
continue;
}
if (method.getDeclaringClass() == TypedObject.class) {
// We're not interested in methods from that class
continue;
}
Category category = method.getDeclaredAnnotation(Category.class);
if (category != null) {
collectCategoryAttrs(method.getReturnType(), category.name(), lookup, output, offset, serializable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,27 +446,34 @@ private static void collectAttrs(
for (Class<?> base : cls.getInterfaces()) {
collectAttrs(base, output);
}
output.addAll(collectDeclaredAttrs(cls));
collectDeclaredAttrs(cls, output);
}

@NotNull
private static List<AttrInfo> collectDeclaredAttrs(@NotNull Class<?> cls) throws ReflectiveOperationException {
List<AttrInfo> attrs = new ArrayList<>();
private static void collectDeclaredAttrs(
@NotNull Class<?> cls,
@NotNull List<AttrInfo> output
) throws ReflectiveOperationException {
var start = output.size();
for (Method method : cls.getDeclaredMethods()) {
if (!Modifier.isAbstract(method.getModifiers()) || method.getDeclaringClass() == TypedObject.class) {
if (!Modifier.isAbstract(method.getModifiers())) {
// We'll look for the overloaded version of it
continue;
}
if (method.getDeclaringClass() == TypedObject.class) {
// We're not interested in methods from that class
continue;
}
Category category = method.getDeclaredAnnotation(Category.class);
if (category != null) {
collectCategoryAttrs(new CategoryInfo(category.name(), method.getReturnType(), method), attrs);
collectCategoryAttrs(new CategoryInfo(category.name(), method.getReturnType(), method), output);
} else {
collectAttr(null, method, attrs);
collectAttr(null, method, output);
}
}
// Position is relative to the enclosing compound type, even inside categories.
attrs.sort(Comparator.comparingInt(AttrInfo::position));
return attrs;
output
.subList(start, output.size())
.sort(Comparator.comparingInt(AttrInfo::position));
}

private static void collectCategoryAttrs(
Expand Down Expand Up @@ -502,7 +509,8 @@ private record CategoryInfo(
@NotNull String name,
@NotNull Class<?> type,
@NotNull Method getter
) {}
) {
}

private record AttrInfo(
@NotNull String name,
Expand Down

0 comments on commit 8e12e83

Please sign in to comment.