Skip to content

Commit

Permalink
misc
Browse files Browse the repository at this point in the history
  • Loading branch information
ZZZank committed Aug 13, 2024
1 parent 5393346 commit f328b53
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 24 deletions.
47 changes: 28 additions & 19 deletions src/main/java/zzzank/probejs/lang/java/clazz/Clazz.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -106,29 +107,37 @@ private static boolean hasIdenticalParentMethod(Method method, Class<?> clazz) {
/**
* getGenericTypeReplacementForParentInterfaceMethodsJustBecauseJavaDoNotKnowToReplaceThemWithGenericArgumentsOfThisClass
*/
private static Map<TypeVariable<?>, Type> getGenericTypeReplacementForParentInterfaceMethods(Class<?> thisClass, Method thatMethod) {
private static Map<TypeVariable<?>, Type> getGenericTypeReplacementForParentInterfaceMethods(
Class<?> thisClass,
Method thatMethod
) {
Class<?> targetClass = thatMethod.getDeclaringClass();

Map<TypeVariable<?>, 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<TypeVariable<?>, Type> parentType = getGenericTypeReplacementForParentInterfaceMethods(superInterface, thatMethod);
Map<TypeVariable<?>, Type> parentReplacement = getInterfaceRemap(thisClass, superInterface);

for (Map.Entry<TypeVariable<?>, 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;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/zzzank/probejs/mixins/MixinConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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(),
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/zzzank/probejs/utils/CollectUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,22 @@ static <T> T anyIn(Iterable<T> iterable) {
static <T> T anyIn(Stream<T> stream) {
return stream.findAny().orElse(null);
}

static <T> Iterator<T> enumToItr(Enumeration<T> enumeration) {
return new Iterator<T>() {
@Override
public boolean hasNext() {
return enumeration.hasMoreElements();
}

@Override
public T next() {
return enumeration.nextElement();
}
};
}

static <T> Iterable<T> enumToIterable(Enumeration<T> enumeration) {
return () -> enumToItr(enumeration);
}
}
1 change: 0 additions & 1 deletion src/main/java/zzzank/probejs/utils/NameUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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]+");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<RegistryInfo> {

public final Registry<?> raw;
public final ForgeRegistry<? extends IForgeRegistryEntry<?>> forgeRaw;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit f328b53

Please sign in to comment.