Skip to content

Commit

Permalink
introduce @pos to maintain order, fix bug with setters
Browse files Browse the repository at this point in the history
  • Loading branch information
Revxrsal committed Jan 28, 2025
1 parent 7f7a9da commit 03009db
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "io.github.revxrsal"
version = "1.2"
version = "1.3"

repositories {
mavenCentral()
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/revxrsal/bubbles/annotation/Pos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package revxrsal.bubbles.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Sets the index of the blueprint field. Shortened for brevity. Lower values
* come first
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Pos {

/**
* The index value
*
* @return The index value
*/
int value();

}
29 changes: 21 additions & 8 deletions src/main/java/revxrsal/bubbles/blueprint/BlueprintProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import org.objectweb.asm.Type;
import revxrsal.bubbles.annotation.Blueprint;
import revxrsal.bubbles.annotation.Comment;
import revxrsal.bubbles.annotation.IgnoreMethod;
import revxrsal.bubbles.annotation.Key;
import revxrsal.bubbles.annotation.*;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
Expand Down Expand Up @@ -105,8 +102,10 @@ public boolean hasComments() {
throw new IllegalArgumentException("Class is not an interface: " + interfaceType.getName());
if (!interfaceType.isAnnotationPresent(Blueprint.class))
throw new IllegalArgumentException("Interface does not have @Blueprint on it!");
Map<String, BlueprintProperty> properties = new HashMap<>();
for (Method method : interfaceType.getMethods()) {
Map<String, BlueprintProperty> properties = new LinkedHashMap<>();
Method[] methods = interfaceType.getMethods();
sortByAnnotation(methods);
for (Method method : methods) {
if (Modifier.isStatic(method.getModifiers()))
continue;
if (method.isAnnotationPresent(IgnoreMethod.class)) {
Expand All @@ -126,6 +125,22 @@ public boolean hasComments() {
return Collections.unmodifiableMap(properties);
}

private static void sortByAnnotation(Method[] methods) {
Arrays.sort(methods, (o1, o2) -> {
Pos pos1 = o1.getAnnotation(Pos.class);
Pos pos2 = o2.getAnnotation(Pos.class);
if (pos1 == null && pos2 == null)
return 0; // Both methods are unannotated
if (pos1 == null)
return -1; // o1 is unannotated, so it comes first
if (pos2 == null)
return 1; // o2 is unannotated, so it comes first

// Both methods have the annotation, compare their values
return Integer.compare(pos1.value(), pos2.value());
});
}

private void setType(@Nullable Type type) {
if (this.type == null)
this.type = type;
Expand Down Expand Up @@ -155,8 +170,6 @@ private static void parse(
throw new IllegalArgumentException("Setter for property '" + key + "' has no parameters!");
if (method.getParameterCount() > 1)
throw new IllegalArgumentException("Setter for property '" + key + "' has more than 1 parameter!");
if (method.isDefault())
throw new IllegalArgumentException("");
existing.propClass = method.getParameterTypes()[0];
Type type = Type.getType(existing.propClass);
existing.setType(type);
Expand Down

0 comments on commit 03009db

Please # to comment.