From f328b5356a8493aa21560f05029cb7b0e1414859 Mon Sep 17 00:00:00 2001 From: ZZZank <3410764033@qq.com> Date: Tue, 13 Aug 2024 20:44:33 +0800 Subject: [PATCH] misc --- .../zzzank/probejs/lang/java/clazz/Clazz.java | 47 +++++++++++-------- .../zzzank/probejs/mixins/MixinConsole.java | 6 +-- .../zzzank/probejs/utils/CollectUtils.java | 18 +++++++ .../java/zzzank/probejs/utils/NameUtils.java | 1 - .../probejs/utils/registry/RegistryInfo.java | 8 +++- 5 files changed, 56 insertions(+), 24 deletions(-) diff --git a/src/main/java/zzzank/probejs/lang/java/clazz/Clazz.java b/src/main/java/zzzank/probejs/lang/java/clazz/Clazz.java index 69659d3d..a6a657b6 100644 --- a/src/main/java/zzzank/probejs/lang/java/clazz/Clazz.java +++ b/src/main/java/zzzank/probejs/lang/java/clazz/Clazz.java @@ -52,7 +52,8 @@ public Clazz(Class clazz) { )) .collect(Collectors.toList()); this.fields = Arrays.stream(ReflectUtils.fieldsSafe(original)) - .filter(f -> !names.contains(RemapperBridge.remapField(original, f)) && !f.isAnnotationPresent(HideFromJS.class)) + .filter(f -> !names.contains(RemapperBridge.remapField(original, f)) + && !f.isAnnotationPresent(HideFromJS.class)) .map(f -> new FieldInfo(original, f)) .collect(Collectors.toList()); @@ -106,29 +107,37 @@ private static boolean hasIdenticalParentMethod(Method method, Class clazz) { /** * getGenericTypeReplacementForParentInterfaceMethodsJustBecauseJavaDoNotKnowToReplaceThemWithGenericArgumentsOfThisClass */ - private static Map, Type> getGenericTypeReplacementForParentInterfaceMethods(Class thisClass, Method thatMethod) { + private static Map, Type> getGenericTypeReplacementForParentInterfaceMethods( + Class thisClass, + Method thatMethod + ) { Class targetClass = thatMethod.getDeclaringClass(); Map, Type> replacement = new HashMap<>(); - if (Arrays.stream(thisClass.getInterfaces()).noneMatch(c -> c.equals(targetClass))) { - Class superInterface = Arrays.stream(thisClass.getInterfaces()).filter(targetClass::isAssignableFrom).findFirst().orElse(null); - if (superInterface == null) { - return Collections.emptyMap(); - } - Map, Type> parentType = getGenericTypeReplacementForParentInterfaceMethods(superInterface, thatMethod); - Map, Type> parentReplacement = getInterfaceRemap(thisClass, superInterface); - - for (Map.Entry, Type> entry : parentType.entrySet()) { - TypeVariable variable = entry.getKey(); - Type type = entry.getValue(); - - replacement.put(variable, - type instanceof TypeVariable typeVariable ? parentReplacement.getOrDefault(typeVariable, typeVariable) : type - ); - } - } else { + if (Arrays.asList(thisClass.getInterfaces()).contains(targetClass)) { return getInterfaceRemap(thisClass, targetClass); } + val superInterface = Arrays + .stream(thisClass.getInterfaces()) + .filter(targetClass::isAssignableFrom) + .findFirst() + .orElse(null); + if (superInterface == null) { + return Collections.emptyMap(); + } + val parentType = getGenericTypeReplacementForParentInterfaceMethods(superInterface, thatMethod); + val parentReplacement = getInterfaceRemap(thisClass, superInterface); + + for (val entry : parentType.entrySet()) { + val variable = entry.getKey(); + val type = entry.getValue(); + + replacement.put(variable, + type instanceof TypeVariable typeVariable + ? parentReplacement.getOrDefault(typeVariable, typeVariable) + : type + ); + } return replacement; } diff --git a/src/main/java/zzzank/probejs/mixins/MixinConsole.java b/src/main/java/zzzank/probejs/mixins/MixinConsole.java index 37e13cd4..e51f5620 100644 --- a/src/main/java/zzzank/probejs/mixins/MixinConsole.java +++ b/src/main/java/zzzank/probejs/mixins/MixinConsole.java @@ -3,6 +3,7 @@ import dev.latvian.kubejs.script.ScriptType; import dev.latvian.kubejs.util.ConsoleJS; import dev.latvian.mods.rhino.RhinoException; +import lombok.val; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -14,7 +15,6 @@ import zzzank.probejs.utils.FileUtils; import zzzank.probejs.utils.JsonUtils; -import java.nio.file.Path; import java.util.regex.Pattern; @Mixin(value = ConsoleJS.class, remap = false) @@ -34,11 +34,11 @@ public void reportError(String message, Throwable error, Pattern exitPattern, Ca return; } if (error instanceof RhinoException rhinoException) { - Path path = FileUtils.parseSourcePath(rhinoException.sourceName()); + val path = FileUtils.parseSourcePath(rhinoException.sourceName()); if (path == null) { return; } - LintingWarning warning = new LintingWarning( + val warning = new LintingWarning( path, LintingWarning.Level.ERROR, rhinoException.lineNumber(), diff --git a/src/main/java/zzzank/probejs/utils/CollectUtils.java b/src/main/java/zzzank/probejs/utils/CollectUtils.java index 46e8431d..e92fe0cb 100644 --- a/src/main/java/zzzank/probejs/utils/CollectUtils.java +++ b/src/main/java/zzzank/probejs/utils/CollectUtils.java @@ -86,4 +86,22 @@ static T anyIn(Iterable iterable) { static T anyIn(Stream stream) { return stream.findAny().orElse(null); } + + static Iterator enumToItr(Enumeration enumeration) { + return new Iterator() { + @Override + public boolean hasNext() { + return enumeration.hasMoreElements(); + } + + @Override + public T next() { + return enumeration.nextElement(); + } + }; + } + + static Iterable enumToIterable(Enumeration enumeration) { + return () -> enumToItr(enumeration); + } } diff --git a/src/main/java/zzzank/probejs/utils/NameUtils.java b/src/main/java/zzzank/probejs/utils/NameUtils.java index 9d61ee18..8bf388a2 100644 --- a/src/main/java/zzzank/probejs/utils/NameUtils.java +++ b/src/main/java/zzzank/probejs/utils/NameUtils.java @@ -14,7 +14,6 @@ public class NameUtils { public static final Pattern MATCH_CONST_REQUIRE = Pattern.compile("^const \\{(.+)} = require\\((.+)\\)"); public static final Pattern MATCH_ANY_REQUIRE = Pattern.compile("^.+ \\{(.+)} = require\\((.+)\\)"); - public static String[] extractAlphabets(String input) { return input.split("[^a-zA-Z]+"); } diff --git a/src/main/java/zzzank/probejs/utils/registry/RegistryInfo.java b/src/main/java/zzzank/probejs/utils/registry/RegistryInfo.java index 33a4fd7d..7368af1a 100644 --- a/src/main/java/zzzank/probejs/utils/registry/RegistryInfo.java +++ b/src/main/java/zzzank/probejs/utils/registry/RegistryInfo.java @@ -7,11 +7,12 @@ import net.minecraft.tags.StaticTags; import net.minecraftforge.registries.ForgeRegistry; import net.minecraftforge.registries.IForgeRegistryEntry; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.Set; -public class RegistryInfo { +public class RegistryInfo implements Comparable { public final Registry raw; public final ForgeRegistry> forgeRaw; @@ -42,4 +43,9 @@ public RegistryInfo(Registry registry) { this.names = raw.keySet(); this.tagHelper = StaticTags.get(this.id); } + + @Override + public int compareTo(@NotNull RegistryInfo o) { + return resKey.compareTo(o.resKey); + } } \ No newline at end of file