Skip to content

Commit afcdcb2

Browse files
committed
fixed wrongly mapped variables
1 parent f39b01a commit afcdcb2

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

src/main/java/com/prunoideae/probejs/formatter/NameResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public static String getNameSafe(String kw) {
123123
}
124124

125125
public static void init() {
126-
putResolvedName(Object.class, "any");
126+
putResolvedName(Object.class, "object");
127127
putResolvedName(String.class, "string");
128128
putResolvedName(Character.class, "string");
129129
putResolvedName(Character.TYPE, "string");

src/main/java/com/prunoideae/probejs/formatter/SpecialTypes.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
package com.prunoideae.probejs.formatter;
22

33
import com.prunoideae.probejs.formatter.formatter.FormatterType;
4-
import com.prunoideae.probejs.formatter.formatter.IFormatter;
4+
import com.prunoideae.probejs.info.ClassInfo;
55
import com.prunoideae.probejs.info.MethodInfo;
66
import com.prunoideae.probejs.info.type.ITypeInfo;
77
import com.prunoideae.probejs.info.type.TypeInfoClass;
88
import com.prunoideae.probejs.info.type.TypeInfoParameterized;
99
import com.prunoideae.probejs.info.type.TypeInfoVariable;
1010
import dev.latvian.mods.kubejs.item.ingredient.IngredientJS;
11-
import dev.latvian.mods.kubejs.util.BuilderBase;
1211

1312
import java.lang.reflect.Method;
1413
import java.lang.reflect.Modifier;
15-
import java.util.ArrayList;
16-
import java.util.HashSet;
17-
import java.util.List;
18-
import java.util.Set;
19-
import java.util.function.*;
14+
import java.lang.reflect.TypeVariable;
15+
import java.util.*;
2016

2117
public class SpecialTypes {
2218

@@ -30,22 +26,25 @@ private FormatterLambda(MethodInfo info) {
3026
}
3127

3228
public String format(ITypeInfo typeInfo) {
33-
List<ITypeInfo> paramTypes = new ArrayList<>();
29+
Map<String, ITypeInfo> variableMap = new HashMap<>();
3430
if (typeInfo instanceof TypeInfoParameterized parameterized) {
35-
paramTypes.addAll(parameterized.getParamTypes());
31+
List<ITypeInfo> concreteTypes = new ArrayList<>(parameterized.getParamTypes());
32+
for (ITypeInfo variable : info.getFrom().getParameters()) {
33+
variableMap.put(variable.getTypeName(), concreteTypes.isEmpty() ? new TypeInfoClass(Object.class) : concreteTypes.remove(0));
34+
}
3635
}
3736

3837
List<String> formattedParam = new ArrayList<>();
3938
for (MethodInfo.ParamInfo param : info.getParams()) {
4039
ITypeInfo resolvedType = param.getType();
4140
if (resolvedType instanceof TypeInfoVariable) {
42-
resolvedType = paramTypes.isEmpty() ? new TypeInfoClass(Object.class) : paramTypes.remove(0);
41+
resolvedType = variableMap.getOrDefault(resolvedType.getTypeName(), new TypeInfoClass(Object.class));
4342
}
4443
formattedParam.add("%s: %s".formatted(param.getName(), new FormatterType(resolvedType).format(0, 0)));
4544
}
4645
ITypeInfo resolvedReturn = info.getReturnType();
4746
if (resolvedReturn instanceof TypeInfoVariable) {
48-
resolvedReturn = paramTypes.isEmpty() ? new TypeInfoClass(Object.class) : paramTypes.remove(0);
47+
resolvedReturn = variableMap.getOrDefault(resolvedReturn.getTypeName(), new TypeInfoClass(Object.class));
4948
}
5049
return "(%s) => %s".formatted(String.join(", ", formattedParam), new FormatterType(resolvedReturn).format(0, 0));
5150
}
@@ -55,9 +54,10 @@ public static void processFunctionalInterfaces(Set<Class<?>> globalClasses) {
5554
for (Class<?> clazz : globalClasses) {
5655
if (clazz.isInterface() && clazz.getAnnotation(FunctionalInterface.class) != null && !skippedSpecials.contains(clazz)) {
5756
//Functional interfaces has one and only one abstract method
58-
for (Method method : clazz.getMethods()) {
59-
if (Modifier.isAbstract(method.getModifiers())) {
60-
FormatterLambda formatter = new FormatterLambda(new MethodInfo(method, method.getDeclaringClass()));
57+
ClassInfo info = ClassInfo.getOrCache(clazz);
58+
for (MethodInfo method : info.getMethodInfo()) {
59+
if (method.isAbstract()) {
60+
FormatterLambda formatter = new FormatterLambda(method);
6161
NameResolver.putTypeFormatter(clazz, formatter::format);
6262
break;
6363
}

src/main/java/com/prunoideae/probejs/info/MethodInfo.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class MethodInfo {
1818
private final String name;
1919
private final boolean shouldHide;
2020
private final int modifiers;
21+
private final Class<?> from;
2122
private ITypeInfo returnType;
2223
private List<ParamInfo> params;
2324
private List<ITypeInfo> typeVariables;
@@ -31,6 +32,7 @@ private static String getRemappedOrDefault(Method method, Class<?> from) {
3132
public MethodInfo(Method method, Class<?> from) {
3233
this.name = getRemappedOrDefault(method, from);
3334
this.shouldHide = method.getAnnotation(HideFromJS.class) != null;
35+
this.from = from;
3436
this.modifiers = method.getModifiers();
3537
this.returnType = InfoTypeResolver.resolveType(method.getGenericReturnType());
3638
this.params = Arrays.stream(method.getParameters()).map(ParamInfo::new).collect(Collectors.toList());
@@ -49,6 +51,10 @@ public boolean isStatic() {
4951
return Modifier.isStatic(modifiers);
5052
}
5153

54+
public boolean isAbstract() {
55+
return Modifier.isAbstract(modifiers);
56+
}
57+
5258
public ITypeInfo getReturnType() {
5359
return returnType;
5460
}
@@ -61,6 +67,10 @@ public List<ITypeInfo> getTypeVariables() {
6167
return typeVariables;
6268
}
6369

70+
public ClassInfo getFrom() {
71+
return ClassInfo.getOrCache(from);
72+
}
73+
6474
public void setParams(List<ParamInfo> params) {
6575
this.params = params;
6676
}

0 commit comments

Comments
 (0)