mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-12 20:07:32 +00:00
Processed external/java class enums properly
This commit is contained in:
1 parent
fd160e980a
commit
5553ece3e6
3 files changed
+110
-11
No files matched your search
+67
-7
@@ -22,9 +22,9 @@ public class EnumConstantInfo {
|
||||
|
||||
private final FieldInfo fieldInfo;
|
||||
private final MethodCallInfo constructorCall; // Can be null if no args provided
|
||||
private final ScriptTypeInfo enumType;
|
||||
private final TypeInfo enumType;
|
||||
|
||||
private EnumConstantInfo(FieldInfo fieldInfo, MethodCallInfo constructorCall, ScriptTypeInfo enumType) {
|
||||
public EnumConstantInfo(FieldInfo fieldInfo, MethodCallInfo constructorCall, TypeInfo enumType) {
|
||||
this.fieldInfo = fieldInfo;
|
||||
this.constructorCall = constructorCall;
|
||||
this.enumType = enumType;
|
||||
@@ -128,10 +128,9 @@ public class EnumConstantInfo {
|
||||
constantName,
|
||||
enumType,
|
||||
absPos,
|
||||
args,
|
||||
enumType,
|
||||
initStart,
|
||||
initEnd
|
||||
initEnd,
|
||||
null
|
||||
);
|
||||
|
||||
EnumConstantInfo constantInfo = new EnumConstantInfo(fieldInfo, constructorCall, enumType);
|
||||
@@ -193,7 +192,68 @@ public class EnumConstantInfo {
|
||||
callInfo.validate();
|
||||
return callInfo;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create EnumConstantInfo from a reflection-based enum constant.
|
||||
* Used for enum constants from external libraries (via reflection).
|
||||
*
|
||||
* @param constantName The name of the enum constant
|
||||
* @param enumType The enum type this constant belongs to
|
||||
* @return EnumConstantInfo representing this constant, or null if not found
|
||||
*/
|
||||
public static EnumConstantInfo fromReflection(String constantName, TypeInfo enumType, Field javaField) {
|
||||
if (enumType == null || enumType.getJavaClass() == null || !enumType.getJavaClass().isEnum()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
Class<?> enumClass = enumType.getJavaClass();
|
||||
|
||||
|
||||
// Create FieldInfo from the reflection field
|
||||
FieldInfo fieldInfo = FieldInfo.enumConstant(
|
||||
constantName,
|
||||
enumType,
|
||||
-1, // No declaration offset for reflection-based constants
|
||||
-1,
|
||||
-1,
|
||||
javaField
|
||||
);
|
||||
|
||||
// Create a MethodCallInfo for the constructor validation
|
||||
// Get constructors from the Java enum class itself
|
||||
Constructor<?>[] javaConstructors = enumClass.getDeclaredConstructors();
|
||||
Constructor<?> matchedConstructor = null;
|
||||
|
||||
// Find the primary constructor (usually the first one)
|
||||
if (javaConstructors.length > 0) {
|
||||
matchedConstructor = javaConstructors[0];
|
||||
}
|
||||
|
||||
// Create MethodCallInfo for the constructor call
|
||||
// For reflection-based enums, we don't have actual position info, so use -1
|
||||
MethodCallInfo constructorCall = null;
|
||||
if (matchedConstructor != null) {
|
||||
constructorCall = new MethodCallInfo(
|
||||
constantName, // Method name
|
||||
-1, // No position info for reflection
|
||||
-1, // No paren position
|
||||
-1, // No open paren position
|
||||
-1, // No close paren position
|
||||
new ArrayList<>(), // Empty arguments (reflection doesn't give us this)
|
||||
enumType, // Receiver is the enum type
|
||||
null // No MethodInfo for reflection-based constructor
|
||||
);
|
||||
constructorCall.setConstructor(true);
|
||||
}
|
||||
EnumConstantInfo enumInfo = new EnumConstantInfo(fieldInfo, constructorCall, enumType);
|
||||
fieldInfo.setEnumConstantInfo(enumInfo);
|
||||
return enumInfo;
|
||||
} catch (SecurityException e) {
|
||||
// Constant not found or access denied
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find where enum constants section ends.
|
||||
@@ -259,7 +319,7 @@ public class EnumConstantInfo {
|
||||
return constructorCall;
|
||||
}
|
||||
|
||||
public ScriptTypeInfo getEnumType() {
|
||||
public TypeInfo getEnumType() {
|
||||
return enumType;
|
||||
}
|
||||
|
||||
|
||||
@@ -131,6 +131,15 @@ public final class FieldInfo {
|
||||
public static FieldInfo fromReflection(Field field, TypeInfo containingType) {
|
||||
String name = field.getName();
|
||||
TypeInfo type = TypeInfo.fromClass(field.getType());
|
||||
|
||||
// Check if this is an enum constant
|
||||
if (field.isEnumConstant()) {
|
||||
// Create enum constant FieldInfo
|
||||
EnumConstantInfo constantInfo = EnumConstantInfo.fromReflection(name, containingType,field);
|
||||
if (constantInfo != null)
|
||||
return constantInfo.getFieldInfo();
|
||||
}
|
||||
|
||||
return new FieldInfo(name, Scope.GLOBAL, type, -1, true, null, null, -1, -1, field.getModifiers(), field);
|
||||
}
|
||||
|
||||
@@ -139,16 +148,14 @@ public final class FieldInfo {
|
||||
* @param name The constant name (e.g., "NORTH")
|
||||
* @param type The enum type itself
|
||||
* @param declOffset The declaration position
|
||||
* @param args The constructor arguments (or null if no args)
|
||||
* @param containingEnum The enum type this constant belongs to
|
||||
* @param initStart The position of '(' if args present, else -1
|
||||
* @param initEnd The position after ')' if args present, else -1
|
||||
*/
|
||||
public static FieldInfo enumConstant(String name, TypeInfo type, int declOffset, String args, TypeInfo containingEnum, int initStart, int initEnd) {
|
||||
public static FieldInfo enumConstant(String name, TypeInfo type, int declOffset, int initStart, int initEnd,Field javaField) {
|
||||
// Enum constants are implicitly public static final
|
||||
int modifiers = Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL;
|
||||
return new FieldInfo(name, Scope.ENUM_CONSTANT, type, declOffset, true, null, null, initStart, initEnd,
|
||||
modifiers, null);
|
||||
modifiers, javaField);
|
||||
}
|
||||
|
||||
// ==================== ENUM HANDLING ====================
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package noppes.npcs.client.gui.util.script.interpreter.type;
|
||||
|
||||
import noppes.npcs.client.gui.util.script.interpreter.field.EnumConstantInfo;
|
||||
import noppes.npcs.client.gui.util.script.interpreter.field.FieldInfo;
|
||||
import noppes.npcs.client.gui.util.script.interpreter.method.MethodInfo;
|
||||
import noppes.npcs.client.gui.util.script.interpreter.token.TokenType;
|
||||
@@ -283,6 +284,37 @@ public class TypeInfo {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean hasEnumConstant(String constantName) {
|
||||
if (javaClass == null || !javaClass.isEnum()) return false;
|
||||
try {
|
||||
Object[] constants = javaClass.getEnumConstants();
|
||||
for (Object constant : constants) {
|
||||
if (constant.toString().equals(constantName))
|
||||
return true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// Security or linkage error
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an enum constant by name.
|
||||
*/
|
||||
public EnumConstantInfo getEnumConstant(String constantName) {
|
||||
if (javaClass == null || !javaClass.isEnum()) return null;
|
||||
try {
|
||||
Object[] constants = javaClass.getEnumConstants();
|
||||
for (Object constant : constants) {
|
||||
if (constant.toString().equals(constantName))
|
||||
return EnumConstantInfo.fromReflection(constantName, this, null);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// Security or linkage error
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get MethodInfo for a method by name. Returns null if not found.
|
||||
* Creates a synthetic MethodInfo based on reflection data.
|
||||
|
||||
Reference in new issue
Block a user