diff options
author | wjaniszewski <wjaniszewski@13f79535-47bb-0310-9956-ffa450edef68> | 2009-04-16 18:01:41 +0000 |
---|---|---|
committer | wjaniszewski <wjaniszewski@13f79535-47bb-0310-9956-ffa450edef68> | 2009-04-16 18:01:41 +0000 |
commit | ca64a758cac9b5bd066eda16d569f1a0e3952c9e (patch) | |
tree | 75ab46a42e06450187bc6757474a4c0539ef974a /branches/sca-java-1.x/modules/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TypeHelpersProxy.java | |
parent | c86f42066a6a005d52f534694aecd6fdc1692217 (diff) |
Added support for Erlang Atoms
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@765708 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
-rw-r--r-- | branches/sca-java-1.x/modules/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TypeHelpersProxy.java | 73 |
1 files changed, 59 insertions, 14 deletions
diff --git a/branches/sca-java-1.x/modules/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TypeHelpersProxy.java b/branches/sca-java-1.x/modules/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TypeHelpersProxy.java index cbfd93796f..380abb852b 100644 --- a/branches/sca-java-1.x/modules/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TypeHelpersProxy.java +++ b/branches/sca-java-1.x/modules/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TypeHelpersProxy.java @@ -19,6 +19,7 @@ package org.apache.tuscany.sca.binding.erlang.impl.types;
+import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
@@ -26,6 +27,7 @@ import java.util.List; import java.util.Map;
import org.apache.tuscany.sca.binding.erlang.impl.TypeMismatchException;
+import org.apache.tuscany.sca.binding.erlang.meta.ErlangAtom;
import com.ericsson.otp.erlang.OtpErlangList;
import com.ericsson.otp.erlang.OtpErlangObject;
@@ -60,13 +62,34 @@ public class TypeHelpersProxy { primitiveTypes.put(Float.class, primitiveTypes.get(float.class));
primitiveTypes.put(Double.class, primitiveTypes.get(double.class));
primitiveTypes.put(byte[].class, new BinaryTypeHelper());
+ primitiveTypes.put(ErlangAtom.class, new AtomTypeHelper());
}
- private static TypeHelper getTypeHelper(Class<?> forClass) {
- TypeHelper typeHelper = primitiveTypes.get(forClass);
+ private static TypeHelper getTypeHelper(Class<?> forClass,
+ Annotation[] notes) {
+ TypeHelper typeHelper = null;
+ // check for special types marked by annotations
+ for (int i = 0; i < notes.length; i++) {
+ typeHelper = primitiveTypes.get(notes[i].annotationType());
+ if (typeHelper != null) {
+ // annotation found, check if it points to array
+ // FIXME: check if annotation points to proper type
+ // ie. ErlangAtom -> String. If not, then log? exception?
+ if (forClass.isArray()) {
+ typeHelper = new AnnotatedListTypeHelper(notes);
+ }
+ break;
+ }
+ }
+ // check for standard types
+ if (typeHelper == null) {
+ typeHelper = primitiveTypes.get(forClass);
+ }
+ // check for arrays
if (typeHelper == null && forClass.isArray()) {
typeHelper = new ListTypeHelper();
}
+ // others would be tuples
if (typeHelper == null) {
typeHelper = new TupleTypeHelper();
}
@@ -74,13 +97,26 @@ public class TypeHelpersProxy { }
/**
+ * Converts single Java object into Erlang
+ *
+ * @param object
+ * @param notes
+ * @return
+ */
+ public static OtpErlangObject toErlang(Object object, Annotation[] notes) {
+ TypeHelper helper = getTypeHelper(object.getClass(), notes);
+ return helper.toErlang(object);
+ }
+
+ /**
* Converts Java objects arrays to Erlang: 1. single object (if array arity
* == 1) or 2. tuple (if array arity > 1)
*
* @param objects
* @return
*/
- public static OtpErlangObject toErlang(Object[] objects) {
+ public static OtpErlangObject toErlang(Object[] objects,
+ Annotation[][] notes) {
OtpErlangObject result = null;
if (objects != null) {
TypeHelper helper = null;
@@ -89,13 +125,13 @@ public class TypeHelpersProxy { result = new OtpErlangList();
break;
case 1:
- helper = getTypeHelper(objects[0].getClass());
+ helper = getTypeHelper(objects[0].getClass(), notes[0]);
result = helper.toErlang(objects[0]);
break;
default:
OtpErlangObject[] erlObjects = new OtpErlangObject[objects.length];
for (int i = 0; i < objects.length; i++) {
- helper = getTypeHelper(objects[i].getClass());
+ helper = getTypeHelper(objects[i].getClass(), notes[i]);
erlObjects[i] = helper.toErlang(objects[i]);
}
result = new OtpErlangTuple(erlObjects);
@@ -111,15 +147,24 @@ public class TypeHelpersProxy { * @param array
* @return
*/
- public static OtpErlangList toErlangAsList(Object array) {
+ public static OtpErlangList toErlangAsList(Object array,
+ Annotation[][] notes) {
OtpErlangList result = null;
if (array != null) {
List<OtpErlangObject> attrsList = new ArrayList<OtpErlangObject>();
int i = 0;
while (true) {
try {
+ // FIXME: if notes.length == 1 then its used to annotate
+ // array. Clean up.
+ Annotation[] currNotes = null;
+ if (notes.length == 1) {
+ currNotes = notes[0];
+ } else {
+ currNotes = notes[i];
+ }
TypeHelper helper = getTypeHelper(Array.get(array, i)
- .getClass());
+ .getClass(), currNotes);
attrsList.add(helper.toErlang(Array.get(array, i)));
i++;
} catch (ArrayIndexOutOfBoundsException e) {
@@ -142,10 +187,10 @@ public class TypeHelpersProxy { * @return
* @throws Exception
*/
- public static Object toJava(OtpErlangObject object, Class<?> forClass)
- throws Exception {
+ public static Object toJava(OtpErlangObject object, Class<?> forClass,
+ Annotation[] notes) throws Exception {
try {
- TypeHelper helper = getTypeHelper(forClass);
+ TypeHelper helper = getTypeHelper(forClass, notes);
return helper.toJava(object, forClass);
} catch (ClassCastException e) {
throw new TypeMismatchException(forClass, object.getClass());
@@ -161,11 +206,11 @@ public class TypeHelpersProxy { * @throws Exception
*/
public static Object[] toJavaFromList(OtpErlangList objects,
- Class<?>[] forClass) throws Exception {
+ Class<?>[] forClass, Annotation[][] notes) throws Exception {
Object[] result = new Object[objects.arity()];
try {
for (int i = 0; i < objects.arity(); i++) {
- TypeHelper helper = getTypeHelper(forClass[i]);
+ TypeHelper helper = getTypeHelper(forClass[i], notes[i]);
result[i] = helper.toJava(objects.elementAt(i), forClass[i]);
}
} catch (Exception e) {
@@ -187,7 +232,7 @@ public class TypeHelpersProxy { * @throws Exception
*/
public static Object[] toJavaAsArgs(OtpErlangObject objects,
- Class<?>[] forClass) throws Exception {
+ Class<?>[] forClass, Annotation[][] notes) throws Exception {
OtpErlangObject[] args = null;
// normalize input
if (objects.getClass().equals(OtpErlangTuple.class)) {
@@ -202,7 +247,7 @@ public class TypeHelpersProxy { Object[] result = new Object[args.length];
try {
for (int i = 0; i < args.length; i++) {
- TypeHelper helper = getTypeHelper(forClass[i]);
+ TypeHelper helper = getTypeHelper(forClass[i], notes[i]);
result[i] = helper.toJava(args[i], forClass[i]);
}
} catch (Exception e) {
|