Skip to content

Commit

Permalink
Refactored most properties into DynamicValueProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
ironchefpython committed May 3, 2012
1 parent 362a665 commit ae0d966
Show file tree
Hide file tree
Showing 16 changed files with 167 additions and 445 deletions.
2 changes: 1 addition & 1 deletion .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="test"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.apache.ivyde.eclipse.cpcontainer.IVYDE_CONTAINER/?project=RhinoTechDemo&amp;ivyXmlPath=ivy.xml&amp;confs=*"/>
<classpathentry kind="con" path="org.apache.ivyde.eclipse.cpcontainer.IVYDE_CONTAINER/?project=modapi&amp;ivyXmlPath=ivy.xml&amp;confs=*"/>
<classpathentry kind="output" path="bin"/>
</classpath>
55 changes: 0 additions & 55 deletions src/org/ironchefpython/modapi/AbstractDynamicProperty.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,32 +1,9 @@
package org.ironchefpython.modapi.primitives;
package org.ironchefpython.modapi;

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

public class CalculatedProperty implements DynamicProperty {

public static class CalculatedType extends AbstractDynamicProperty {
private DynamicProperty type;
public CalculatedType(DynamicProperty type) {
this.type = type;
}
public DynamicProperty cloneWith(Object object) {
// FIXME validate that object is a callable
//
return object == null ? this : new CalculatedProperty(type, (Callable)object);
}

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

}

private DynamicProperty type;
private Callable callable;

Expand All @@ -45,20 +22,47 @@ public Object getValue() {
return callable;
}


public boolean isStatic() {
return true;
}

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

public boolean isStatic() {
return true;
}

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


// public static class CalculatedType implements DynamicProperty {
// private DynamicProperty type;
// public CalculatedType(DynamicProperty type) {
// this.type = type;
// }
// public DynamicProperty cloneWith(Object object) {
// // FIXME validate that object is a callable
// //
// return object == null ? this : new CalculatedProperty(type, (Callable)object);
// }
//
// public Class<?> getJavaType() {
// return type.getJavaType();
// }
// public Class<?> getFieldType() {
// throw new NoSuchMethodError();
// }
//
// public Object getValue() {
// throw new NoSuchMethodError();
// }
//
// public boolean isStatic() {
// return false;
// }
//
//}

}


71 changes: 71 additions & 0 deletions src/org/ironchefpython/modapi/DynamicValueProperty.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.ironchefpython.modapi;

import org.ironchefpython.modapi.error.PropertyError;

public class DynamicValueProperty implements DynamicProperty {
private Class<?> type;
private Object value;

public DynamicValueProperty(Class<?> type, Object value) {
this.type = type;
this.value = value;
}

public boolean isStatic() {
return false;
}

public Object getValue() {
return value;
}

public DynamicProperty cloneWith(Object object) throws PropertyError {
if (object == null) {
return this;
}
throw PropertyError.IllegalAccessException(this.getClass().getName());
}

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

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

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

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

abstract static class DynamicTypeProperty implements DynamicProperty {
public boolean isStatic() {
return false;
}

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

public abstract Class<?> getJavaType();

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

}

}
57 changes: 48 additions & 9 deletions src/org/ironchefpython/modapi/JsModManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import java.util.*;

import org.mozilla.javascript.*;
import org.mozilla.javascript.ast.Scope;
import org.ironchefpython.modapi.error.*;
import org.ironchefpython.modapi.primitives.*;

import org.mockengine.Engine;
import org.mockengine.Entity;
Expand All @@ -18,6 +16,47 @@ public class JsModManager extends ModManager {
private Context cx;
private Scriptable scope;

public enum Primitive implements DynamicProperty {
BOOLEAN(boolean.class),
STRING(String.class),
NUMBER(Number.class),
COLOR(String.class),
FUNCTION(Callable.class),
TEXTURE(String.class),
;

private final Class<?> type;
Primitive(Class<?> type) {
this.type = type;
}

public DynamicProperty cloneWith(Object object) {
try {
return (object == null ? this : new DynamicValueProperty(type, object));
} catch (Exception e) {
throw new RuntimeException(e);
}
}

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

public boolean isStatic() {
return false;
}

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

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

}

public JsModManager(Engine game, String modName) {
super(game, modName);
cx = Context.enter();
Expand Down Expand Up @@ -108,28 +147,28 @@ public Prototype.ConstructorParams makeConstructor(String[] provided, Callable i
}

public DynamicProperty getStringType() {
return StringProperty.STRING_TYPE;
return Primitive.STRING;
}

public DynamicProperty getNumberType() {
return NumberProperty.NUMBER_TYPE;
return Primitive.NUMBER;
}

public DynamicProperty getBooleanType() {
return BooleanProperty.BOOLEAN_TYPE;
return Primitive.BOOLEAN;
}

public DynamicProperty getColorType() {
return ColorProperty.COLOR_TYPE;
return Primitive.COLOR;
}

public DynamicProperty getFunctionType() {
return FunctionProperty.FUNCTION_TYPE;
return Primitive.FUNCTION;
}


public DynamicProperty getTextureType() {
return TextureProperty.TEXTURE_TYPE;
return Primitive.TEXTURE;
}

public DynamicProperty calculatedType(DynamicProperty type, Callable callable) {
Expand Down Expand Up @@ -201,7 +240,7 @@ private DynamicProperty makeProperty(Object def) {
if (def instanceof DynamicProperty) {
return (DynamicProperty) def;
} else if (def instanceof Callable) {
return new FunctionProperty((Callable) def);
return new DynamicValueProperty(Callable.class, def);
}
throw new NoSuchMethodError();
}
Expand Down
13 changes: 0 additions & 13 deletions src/org/ironchefpython/modapi/ModManager.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
package org.ironchefpython.modapi;

import java.io.*;
import java.util.HashMap;
import java.util.Map;


import org.ironchefpython.modapi.error.GeneralModdingException;
import org.ironchefpython.modapi.error.InvalidComponentRegistration;
import org.ironchefpython.modapi.error.InvalidEventRegistration;
import org.ironchefpython.modapi.error.PropertyError;
import org.ironchefpython.modapi.error.UnregisteredEventException;
import org.ironchefpython.modapi.primitives.BooleanProperty;
import org.ironchefpython.modapi.primitives.ColorProperty;
import org.ironchefpython.modapi.primitives.FunctionProperty;
import org.ironchefpython.modapi.primitives.NumberProperty;
import org.ironchefpython.modapi.primitives.StringProperty;
import org.ironchefpython.modapi.primitives.TextureProperty;
import org.mockengine.*;
import org.mozilla.javascript.*;



Expand All @@ -29,8 +18,6 @@ public class ModManager {
private String name;
private static Map<String, ModManager> mods = new HashMap<String, ModManager>();



public static final String[] STRING_ARRAY = new String[0];

public ModManager(Engine game, String modName) {
Expand Down
Loading

0 comments on commit ae0d966

Please sign in to comment.