diff options
Diffstat (limited to 'project/QBarInterpreter/src/com/modulus/qbar/integration/QBWrappedClass.java')
| -rw-r--r-- | project/QBarInterpreter/src/com/modulus/qbar/integration/QBWrappedClass.java | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/project/QBarInterpreter/src/com/modulus/qbar/integration/QBWrappedClass.java b/project/QBarInterpreter/src/com/modulus/qbar/integration/QBWrappedClass.java new file mode 100644 index 0000000..01d6a9f --- /dev/null +++ b/project/QBarInterpreter/src/com/modulus/qbar/integration/QBWrappedClass.java @@ -0,0 +1,68 @@ +package com.modulus.qbar.integration; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.HashMap; +import java.util.Map; + +import com.modulus.qbar.core.QBFunction; +import com.modulus.qbar.core.QBNamespace; +import com.modulus.qbar.core.QBObject; +import com.modulus.qbar.core.QBStruct; + +public class QBWrappedClass extends QBStruct{ + private static final long serialVersionUID = -4889124451749124731L; + private static final Map<Class<?>, QBWrappedClass> wrappedClasses = new HashMap<Class<?>, QBWrappedClass>(); + + public static QBStruct wrapClass(Class<?> clazz, QBNamespace nmspce){ + QBWrappedClass ret = wrappedClasses.get(clazz); + + + if(ret == null){ + ret = new QBWrappedClass(clazz, nmspce); + wrappedClasses.put(clazz, ret); + } + + return ret; + } + + private QBWrappedClass(Class<?> toWrap, QBNamespace nmspce) { + super(""); + String name = toWrap.getName(); + this.setName(name.substring(name.lastIndexOf('.') + 1)); + + Method[] methods = toWrap.getMethods(); + + for( Method m : methods){ + int mod = m.getModifiers(); + int argc = m.getParameterTypes().length; + if(!Modifier.isStatic(mod)){ + argc += 1; + } + + QBFunction func = new QBWrappedMethod(toWrap, m.getName(), argc); + + + if(Modifier.isStatic(mod)){ + String methodName = m.getName(); + + if(methodName.startsWith("new$0x20")){ + methodName = methodName.substring(8); + methodName = "new$0x20" + methodName; + } + + nmspce.set(methodName, func); + } else { + this.set(m.getName(), func); + } + } + + Constructor<?>[] constructors = toWrap.getConstructors(); + if(constructors.length > 0){ + QBWrappedConstructor toAdd = new QBWrappedConstructor(constructors[0].getParameterTypes().length, toWrap); + nmspce.set( "new$0x20" + this.getName(), toAdd); + } + } + +} |