mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-12 18:57:32 +00:00
Added 'constructor()' method and 'class' field to Java.type()
This commit is contained in:
1 parent
5ad6a429cc
commit
458162901e
1 file changed
+85
@@ -1,5 +1,13 @@
|
||||
package noppes.npcs.client.gui.util.script.interpreter.type;
|
||||
|
||||
import noppes.npcs.client.gui.util.script.interpreter.field.FieldInfo;
|
||||
import noppes.npcs.client.gui.util.script.interpreter.method.MethodInfo;
|
||||
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Represents a reference to a Java Class itself (not an instance).
|
||||
* Used for patterns like: var File = Java.type("java.io.File");
|
||||
@@ -7,12 +15,15 @@ package noppes.npcs.client.gui.util.script.interpreter.type;
|
||||
* When you have a ClassTypeInfo:
|
||||
* - Accessing members should show ONLY static members (File.listRoots())
|
||||
* - Using 'new' should return an instance of the wrapped class (new File("path"))
|
||||
* - Calling .constructor(...) should return an instance of the wrapped class
|
||||
*
|
||||
* This is distinct from a regular TypeInfo which represents an instance of that type.
|
||||
*/
|
||||
public class ClassTypeInfo extends TypeInfo {
|
||||
|
||||
private final TypeInfo instanceType; // The type you get when you do 'new' on this class
|
||||
private List<MethodInfo> constructorMethod;
|
||||
private List<FieldInfo> classField;
|
||||
|
||||
/**
|
||||
* Create a ClassTypeInfo wrapping a Java class.
|
||||
@@ -66,6 +77,80 @@ public class ClassTypeInfo extends TypeInfo {
|
||||
return instanceType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MethodInfo> getSyntheticMethods() {
|
||||
List<MethodInfo> base = super.getSyntheticMethods();
|
||||
|
||||
// if (constructorMethod == null) {
|
||||
constructorMethod = buildConstructorMethod();
|
||||
//}
|
||||
|
||||
if (constructorMethod.isEmpty()) {
|
||||
return base;
|
||||
}
|
||||
|
||||
List<MethodInfo> merged = new ArrayList<>(base.size() + constructorMethod.size());
|
||||
merged.addAll(base);
|
||||
merged.addAll(constructorMethod);
|
||||
return merged;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FieldInfo> getSyntheticFields() {
|
||||
List<FieldInfo> base = super.getSyntheticFields();
|
||||
|
||||
if (classField == null) {
|
||||
classField = buildClassField();
|
||||
}
|
||||
|
||||
if (classField.isEmpty()) {
|
||||
return base;
|
||||
}
|
||||
|
||||
List<FieldInfo> merged = new ArrayList<>(base.size() + classField.size());
|
||||
merged.addAll(base);
|
||||
merged.addAll(classField);
|
||||
return merged;
|
||||
}
|
||||
|
||||
private List<MethodInfo> buildConstructorMethod() {
|
||||
if (instanceType == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
FieldInfo argsParam = FieldInfo.parameter("args", TypeInfo.OBJECT, -1, null);
|
||||
argsParam.setVarArg(true);
|
||||
|
||||
List<FieldInfo> params = new ArrayList<>();
|
||||
params.add(argsParam);
|
||||
|
||||
MethodInfo ctor = MethodInfo.external(
|
||||
"constructor",
|
||||
instanceType,
|
||||
this,
|
||||
params,
|
||||
Modifier.PUBLIC | Modifier.STATIC,
|
||||
"Creates a new instance of " + instanceType.getSimpleName() + ".\n\n" +
|
||||
"Equivalent to using the 'new' operator on this class reference."
|
||||
);
|
||||
|
||||
return Collections.singletonList(ctor);
|
||||
}
|
||||
|
||||
private List<FieldInfo> buildClassField() {
|
||||
if (getJavaClass() == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
try {
|
||||
TypeInfo classType = TypeInfo.fromClass(Class.class).parameterize(instanceType);
|
||||
FieldInfo classField = FieldInfo.external("class", classType,
|
||||
"The Class object for " + getSimpleName(), Modifier.PUBLIC | Modifier.STATIC);
|
||||
return Collections.singletonList(classField);
|
||||
} catch (Exception e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Class<" + getSimpleName() + ">";
|
||||
|
||||
Reference in new issue
Block a user