mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-12 20:07:32 +00:00
Added external type constructor validation
This commit is contained in:
1 parent
c1e4e824ab
commit
9aa23ef49d
2 files changed
+53
-3
No files matched your search
@@ -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<FieldInfo> 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; }
|
||||
|
||||
@@ -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<MethodInfo> getConstructors() {
|
||||
return new ArrayList<>();
|
||||
List<MethodInfo> 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;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user