From 9aa23ef49d75f2832ad7f8ca7be69a2c8667eb92 Mon Sep 17 00:00:00 2001 From: bigguy345 Date: Sun, 28 Dec 2025 16:32:36 +0200 Subject: [PATCH] Added external type constructor validation --- .../util/script/interpreter/MethodInfo.java | 23 ++++++++++++- .../gui/util/script/interpreter/TypeInfo.java | 33 +++++++++++++++++-- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/MethodInfo.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/MethodInfo.java index d1f5ef72f..c8bee1855 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/MethodInfo.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/MethodInfo.java @@ -1,5 +1,7 @@ package noppes.npcs.client.gui.util.script.interpreter; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Collections; @@ -85,7 +87,7 @@ public final class MethodInfo { * Create a MethodInfo from reflection data. * Used when resolving method calls on known types. */ - public static MethodInfo fromReflection(java.lang.reflect.Method method, TypeInfo containingType) { + public static MethodInfo fromReflection(Method method, TypeInfo containingType) { String name = method.getName(); TypeInfo returnType = TypeInfo.fromClass(method.getReturnType()); int modifiers = method.getModifiers(); @@ -100,6 +102,25 @@ public final class MethodInfo { return new MethodInfo(name, returnType, containingType, params, -1, -1, -1, true, false, modifiers, null); } + /** + * Create a MethodInfo from a Constructor via reflection. + * Used when resolving constructor calls on external types. + */ + public static MethodInfo fromReflectionConstructor(Constructor constructor, TypeInfo containingType) { + String name = containingType.getSimpleName(); // Constructor name is the type name + TypeInfo returnType = containingType; // Constructor "returns" an instance of the type + int modifiers = constructor.getModifiers(); + + List params = new ArrayList<>(); + Class[] paramTypes = constructor.getParameterTypes(); + for (int i = 0; i < paramTypes.length; i++) { + TypeInfo paramType = TypeInfo.fromClass(paramTypes[i]); + params.add(FieldInfo.reflectionParam("arg" + i, paramType)); + } + + return new MethodInfo(name, returnType, containingType, params, -1, -1, -1, true, true, modifiers, null); + } + // Getters public String getName() { return name; } public TypeInfo getReturnType() { return returnType; } diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/TypeInfo.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/TypeInfo.java index 2184f42a6..6216fe9f0 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/TypeInfo.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/TypeInfo.java @@ -184,7 +184,13 @@ public class TypeInfo { * Override in ScriptTypeInfo for script-defined types. */ public boolean hasConstructors() { - return false; + if (javaClass == null) return false; + try { + return javaClass.getConstructors().length > 0; + } catch (Exception e) { + // Security or linkage error + return false; + } } /** @@ -192,7 +198,18 @@ public class TypeInfo { * Override in ScriptTypeInfo for script-defined types. */ public List getConstructors() { - return new ArrayList<>(); + List result = new ArrayList<>(); + if (javaClass == null) return result; + + try { + java.lang.reflect.Constructor[] constructors = javaClass.getConstructors(); + for (java.lang.reflect.Constructor ctor : constructors) { + result.add(MethodInfo.fromReflectionConstructor(ctor, this)); + } + } catch (Exception e) { + // Security or linkage error + } + return result; } /** @@ -200,6 +217,18 @@ public class TypeInfo { * Override in ScriptTypeInfo for script-defined types. */ public MethodInfo findConstructor(int argCount) { + if (javaClass == null) return null; + + try { + java.lang.reflect.Constructor[] constructors = javaClass.getConstructors(); + for (java.lang.reflect.Constructor ctor : constructors) { + if (ctor.getParameterCount() == argCount) { + return MethodInfo.fromReflectionConstructor(ctor, this); + } + } + } catch (Exception e) { + // Security or linkage error + } return null; }