Skip to content

Commit

Permalink
Created factory to generate components from prototypes
Browse files Browse the repository at this point in the history
  • Loading branch information
ironchefpython committed May 3, 2012
1 parent d0b8874 commit 362a665
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 220 deletions.
52 changes: 44 additions & 8 deletions src/org/ironchefpython/modapi/AbstractDynamicProperty.java
@@ -1,19 +1,55 @@
package org.ironchefpython.modapi;

import java.lang.reflect.InvocationTargetException;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtField;
import javassist.Modifier;
import javassist.NotFoundException;

import org.ironchefpython.modapi.error.PropertyError;
import org.ironchefpython.modapi.primitives.NumberProperty;

public abstract class AbstractDynamicProperty implements DynamicProperty {
public DynamicProperty cloneWith(Object object) throws PropertyError {
if (object == null) {
return this;
}
// FIXME (return a component or child component based on the
// string passed)
throw new NoSuchMethodError();
// public DynamicProperty cloneWith(Object object) throws PropertyError {
// if (object == null) {
// return this;
// }
// // FIXME (return a component or child component based on the
// // string passed)
// throw new NoSuchMethodError();
// }
//
public boolean isStatic() {
return false;
}

public Object getValue() {
// FIXME produce a better result for a null value
throw new NoSuchMethodError();
}

protected static DynamicProperty makeType(final Class<?> type, final Class<? extends DynamicProperty> propertyClass) {
return new AbstractDynamicProperty() {
public DynamicProperty cloneWith(Object object) {
try {
return (object == null ? this : propertyClass.getConstructor(type).newInstance(object));
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public Class<?> getJavaType() {
return type;
}

public Class<?> getFieldType() {
return type;
}


};
}
}
10 changes: 3 additions & 7 deletions src/org/ironchefpython/modapi/DynamicProperty.java
@@ -1,17 +1,13 @@
package org.ironchefpython.modapi;

import javassist.CannotCompileException;
import javassist.CtClass;
import javassist.NotFoundException;

import org.ironchefpython.modapi.error.PropertyError;
import org.mozilla.javascript.Callable;

public interface DynamicProperty {

public DynamicProperty cloneWith(Object object) throws PropertyError;
public Object getValue();
public Object addToClass(String key, CtClass comp) throws CannotCompileException, NotFoundException;
public Class<?> getType();
public Class<?> getJavaType();
public Class<?> getFieldType();
public boolean isStatic();

}
35 changes: 3 additions & 32 deletions src/org/ironchefpython/modapi/JsModManager.java
Expand Up @@ -15,11 +15,8 @@
import org.mockengine.Handler;

public class JsModManager extends ModManager {
private static final String INIT_SUFFIX = "!!INIT";
private Context cx;
private Scriptable scope;
private Map<String, Callable> methods;


public JsModManager(Engine game, String modName) {
super(game, modName);
Expand All @@ -28,8 +25,6 @@ public JsModManager(Engine game, String modName) {

ScriptableObject.putProperty(scope, "console", Context.javaToJS(new Console(), scope));
ScriptableObject.putProperty(scope, "manager", Context.javaToJS(new Facade(), scope));

methods = new HashMap<String, Callable>();
}

public void runScript(InputStream is, String name) throws IOException {
Expand All @@ -49,16 +44,6 @@ public void log(String s) {
System.err.println("js> " + s);
}
}

public Object callMethod(Object obj, String name) {
return methods.get(obj.getClass().getName()+'!'+name).call(cx, scope, (Scriptable) Context.javaToJS(obj, scope), null);
}

public void callInitializer(Object obj) {
System.out.println(obj.getClass().getName()+INIT_SUFFIX);
System.out.println(methods.keySet());
methods.get(obj.getClass().getName()+INIT_SUFFIX).call(cx, scope, (Scriptable) Context.javaToJS(obj, scope), null);
}

public class Facade {
@SuppressWarnings("unchecked")
Expand All @@ -74,8 +59,7 @@ public EventFactory registerEvent(ScriptableObject jsObject) throws InvalidEvent
public Prototype registerPrototype(ScriptableObject jsObject) throws GeneralModdingException {
return registerPrototype(null, jsObject);
}



@SuppressWarnings("unchecked")
public Prototype registerPrototype(Prototype prototype, ScriptableObject jsObject) throws GeneralModdingException {
String id = (String) jsObject.get("id");
Expand All @@ -98,26 +82,14 @@ public Prototype registerPrototype(Prototype prototype, ScriptableObject jsObjec

// At this point properties will have cloned version of the included
// properties, now we layer the defined properties on top of it.


properties.putAll(parseProperties((Map<String, Object>) jsObject.get("properties")));

Prototype result = new Prototype(id);
for (Map.Entry<String, DynamicProperty> e : properties.entrySet()) {

DynamicProperty p = e.getValue();
String name = e.getKey();
result.addProperty(name, p);
if (p instanceof CalculatedProperty) {
methods.put(id+"Component!"+name, (Callable) p.getValue());
}
result.addProperty(e.getKey(), e.getValue());
}


Map<String, Callable> listeners = (Map<String, Callable>) jsObject.get("listeners");



if (listeners != null) {
for (Map.Entry<String, Callable> e : listeners.entrySet()) {
Handler handler = new JsHandler(e.getValue());
Expand All @@ -126,9 +98,8 @@ public Prototype registerPrototype(Prototype prototype, ScriptableObject jsObjec
}

Prototype.ConstructorParams constructor = (Prototype.ConstructorParams) jsObject.get("constructor");
// getConstructorSig(constructor, properties);
result.addConstructor(constructor);
methods.put(id+"Component"+INIT_SUFFIX, constructor.getInitializer());

return JsModManager.this.registerPrototype(result);
}

Expand Down
2 changes: 1 addition & 1 deletion src/org/ironchefpython/modapi/Prototype.java
Expand Up @@ -106,7 +106,7 @@ public Constructor(ConstructorParams params) {
public CtClass[] getParamClasses() throws NotFoundException {
CtClass[] result = new CtClass[provided.length];
for (int i = 0; i < provided.length; i++) {
result[i] = ClassPool.getDefault().get(properties.get(provided[i]).getType().getCanonicalName());
result[i] = ClassPool.getDefault().get(properties.get(provided[i]).getJavaType().getCanonicalName());
};
return result;
}
Expand Down
4 changes: 2 additions & 2 deletions src/org/ironchefpython/modapi/primitives/BooleanProperty.java
Expand Up @@ -22,7 +22,7 @@ public Object addToClass(String key, CtClass comp) throws CannotCompileException
return null;
}

public Class<?> getType() {
public Class<?> getJavaType() {
return boolean.class;
}

Expand Down Expand Up @@ -55,7 +55,7 @@ public Object addToClass(String key, CtClass comp)
return null;
}

public Class<?> getType() {
public Class<?> getJavaType() {
return boolean.class;
}

Expand Down
35 changes: 15 additions & 20 deletions src/org/ironchefpython/modapi/primitives/CalculatedProperty.java
@@ -1,15 +1,7 @@
package org.ironchefpython.modapi.primitives;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtField;
import javassist.Modifier;
import javassist.NotFoundException;

import org.ironchefpython.modapi.AbstractDynamicProperty;
import org.ironchefpython.modapi.DynamicProperty;
import org.ironchefpython.modapi.Prototype;
import org.ironchefpython.modapi.error.PropertyError;
import org.mozilla.javascript.Callable;

Expand All @@ -25,11 +17,12 @@ public DynamicProperty cloneWith(Object object) {
//
return object == null ? this : new CalculatedProperty(type, (Callable)object);
}
public Object addToClass(String key, CtClass comp) throws CannotCompileException, NotFoundException {
throw new NoSuchMethodError();

public Class<?> getJavaType() {
return type.getJavaType();
}
public Class<?> getType() {
return type.getType();
public Class<?> getFieldType() {
return null;
}

}
Expand All @@ -52,16 +45,18 @@ public Object getValue() {
return callable;
}

public Object addToClass(String key, CtClass comp)
throws CannotCompileException, NotFoundException {
CtField f = new CtField(ClassPool.getDefault().get(Callable.class.getName()), key, comp);
f.setModifiers(Modifier.STATIC + Modifier.PUBLIC);
comp.addField(f);
return callable;


public Class<?> getJavaType() {
return type.getJavaType();
}

public boolean isStatic() {
return true;
}

public Class<?> getType() {
return type.getType();
public Class<?> getFieldType() {
return Callable.class;
}

}
Expand Down
45 changes: 8 additions & 37 deletions src/org/ironchefpython/modapi/primitives/NumberProperty.java
@@ -1,43 +1,18 @@
package org.ironchefpython.modapi.primitives;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtField;
import javassist.Modifier;
import javassist.NotFoundException;


import org.ironchefpython.modapi.AbstractDynamicProperty;
import org.ironchefpython.modapi.DynamicProperty;

import org.ironchefpython.modapi.error.PropertyError;

public class NumberProperty extends AbstractDynamicProperty {
public static final Class JAVA_CLASS = Number.class;
public static DynamicProperty NUMBER_TYPE = new AbstractDynamicProperty() {
public DynamicProperty cloneWith(Object object) {
return object == null ? this : new NumberProperty(Float.valueOf(object.toString()));
}

public Object addToClass(String key, CtClass comp) throws CannotCompileException, NotFoundException {
CtField f = new CtField(ClassPool.getDefault().get(JAVA_CLASS.getCanonicalName()), key, comp);
f.setModifiers(Modifier.PUBLIC);
comp.addField(f);
return null;
}

public Class<?> getType() {
return JAVA_CLASS;
}


};
public static final Class<?> JAVA_CLASS = Number.class;
public static DynamicProperty NUMBER_TYPE = AbstractDynamicProperty.makeType(JAVA_CLASS, NumberProperty.class);

private float value;
private Number value;

public NumberProperty(float f) {
this.value = f;
public NumberProperty(Number value) {
this.value = value;
}

public DynamicProperty cloneWith(Object object) throws PropertyError {
Expand All @@ -51,15 +26,11 @@ public Object getValue() {
return value;
}

public Object addToClass(String key, CtClass comp)
throws CannotCompileException, NotFoundException {
CtField f = new CtField(ClassPool.getDefault().get(JAVA_CLASS.getCanonicalName()), key, comp);
f.setModifiers(Modifier.STATIC + Modifier.PUBLIC);
comp.addField(f, CtField.Initializer.constant(value));
return null;
public Class<?> getJavaType() {
return JAVA_CLASS;
}

public Class<?> getType() {
public Class<?> getFieldType() {
return JAVA_CLASS;
}

Expand Down

0 comments on commit 362a665

Please sign in to comment.