ScriptingObjectMagicTest.java
/***************************************************************************
Copyright 2014 Emily Estes
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***************************************************************************/
package net.metanotion.scripting;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.metanotion.util.Dictionary;
import net.metanotion.util.Dispatcher;
import net.metanotion.util.Extends;
import net.metanotion.util.Message;
import net.metanotion.util.MessageLiteral;
public final class ScriptingObjectMagicTest {
private static final Logger logger = LoggerFactory.getLogger(ScriptingObjectMagicTest.class);
@Scriptable public static final class Test {
@Scriptable public final String aField = "a test string";
@Scriptable("test2") public final Integer foo = 25;
public String testFunction(int a, String b) {
return "The number was: " + Integer.toString(a) + " and the string is:" + b;
}
@Scriptable public final HashMap<String,String> aMap = new HashMap<String,String>() {{
put("key", "test string");
}};
@Scriptable public final String generateMe() {
return "String";
}
}
public static void main(final String[] args) throws Exception {
logger.debug("Create ScriptingObjectMagic instance");
final ScriptingObjectMagic som = new ScriptingObjectMagic(Test.class);
final Test test = new Test();
logger.debug("Get Object Server");
final ObjectServer os = som.instance(test);
logger.debug("Look up an object");
Dispatcher d = os.dispatcher("Test");
logger.debug("Interpret a message");
Message m = d.dispatch(MessageLiteral.get("aField"));
logger.debug("Retrieve the receiver");
Object o = os.get("Test");
logger.debug("Evaluate the message");
logger.debug("'{}'", m.call(o));
logger.debug("Interpret a message");
m = d.dispatch(MessageLiteral.get("foo"));
logger.debug("Evaluate the message");
logger.debug("'{}'", m.call(o));
m = d.dispatch(MessageLiteral.get("testFunction", Integer.valueOf(14), "A String"));
logger.debug("{}", m.call(o));
logger.debug("Look up an object");
d = os.dispatcher("test2");
logger.debug("Interpret a message");
m = d.dispatch(MessageLiteral.get("intValue"));
logger.debug("Retrieve the receiver");
o = os.get("test2");
logger.debug("Evaluate the message");
logger.debug("'{}'", m.call(o));
logger.debug("Look up an object");
d = os.dispatcher("aMap");
logger.debug("Interpret a message");
m = d.dispatch(MessageLiteral.get("get", "key"));
logger.debug("Retrieve the receiver");
o = os.get("aMap");
logger.debug("Evaluate the message");
logger.debug("'{}'", m.call(o));
logger.debug("Look up an object");
d = os.dispatcher("generateMe");
logger.debug("Interpret a message");
m = d.dispatch(MessageLiteral.get("toString"));
logger.debug("Retrieve the receiver");
o = os.get("generateMe");
logger.debug("Evaluate the message");
logger.debug("'{}'", m.call(o));
}
}