diff options
Diffstat (limited to 'branches/sca-java-1.x/modules/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types')
5 files changed, 178 insertions, 23 deletions
diff --git a/branches/sca-java-1.x/modules/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/AnnotatedListTypeHelper.java b/branches/sca-java-1.x/modules/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/AnnotatedListTypeHelper.java new file mode 100644 index 0000000000..154f0c27fc --- /dev/null +++ b/branches/sca-java-1.x/modules/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/AnnotatedListTypeHelper.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 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.List; + +import com.ericsson.otp.erlang.OtpErlangList; +import com.ericsson.otp.erlang.OtpErlangObject; + +/** + * @version $Rev$ $Date$ + */ +public class AnnotatedListTypeHelper implements TypeHelper { + + private Annotation[] notes; + + public AnnotatedListTypeHelper(Annotation[] notes) { + this.notes = notes; + } + + public OtpErlangObject toErlang(Object object) { + int i = 0; + List<OtpErlangObject> elements = new ArrayList<OtpErlangObject>(); + while (true) { + try { + elements.add(TypeHelpersProxy.toErlang(Array.get(object, i), + notes)); + i++; + } catch (ArrayIndexOutOfBoundsException e) { + // expected + break; + } + } + return new OtpErlangList(elements.toArray(new OtpErlangObject[elements + .size()])); + } + + public Object toJava(OtpErlangObject object, Class<?> forClass) + throws Exception { + OtpErlangList erlangList = (OtpErlangList) object; + Object result = Array.newInstance(forClass.getComponentType(), + erlangList.arity()); + for (int i = 0; i < erlangList.arity(); i++) { + Array.set(result, i, TypeHelpersProxy.toJava(erlangList + .elementAt(i), forClass.getComponentType(), + new Annotation[0])); + } + return result; + } + +} diff --git a/branches/sca-java-1.x/modules/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/AtomTypeHelper.java b/branches/sca-java-1.x/modules/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/AtomTypeHelper.java new file mode 100644 index 0000000000..94285c66bf --- /dev/null +++ b/branches/sca-java-1.x/modules/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/AtomTypeHelper.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.tuscany.sca.binding.erlang.impl.types; + +import com.ericsson.otp.erlang.OtpErlangAtom; +import com.ericsson.otp.erlang.OtpErlangObject; + +/** + * @version $Rev$ $Date$ + */ +public class AtomTypeHelper implements TypeHelper { + + public OtpErlangObject toErlang(Object object) { + return new OtpErlangAtom((String) object); + } + + public Object toJava(OtpErlangObject object, Class<?> forClass) + throws Exception { + return ((OtpErlangAtom) object).atomValue(); + } + +} diff --git a/branches/sca-java-1.x/modules/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ListTypeHelper.java b/branches/sca-java-1.x/modules/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ListTypeHelper.java index adac10bcef..7bb62d4cf8 100644 --- a/branches/sca-java-1.x/modules/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ListTypeHelper.java +++ b/branches/sca-java-1.x/modules/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ListTypeHelper.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.List; @@ -36,9 +37,8 @@ public class ListTypeHelper implements TypeHelper { List<OtpErlangObject> elements = new ArrayList<OtpErlangObject>(); while (true) { try { - Object arrElement = Array.get(object, i); - Object[] args = new Object[] { arrElement }; - elements.add(TypeHelpersProxy.toErlang(args)); + elements.add(TypeHelpersProxy.toErlang(Array.get(object, i), + new Annotation[0])); i++; } catch (ArrayIndexOutOfBoundsException e) { // expected @@ -56,7 +56,8 @@ public class ListTypeHelper implements TypeHelper { erlangList.arity()); for (int i = 0; i < erlangList.arity(); i++) { Array.set(result, i, TypeHelpersProxy.toJava(erlangList - .elementAt(i), forClass.getComponentType())); + .elementAt(i), forClass.getComponentType(), + new Annotation[0])); } return result; } diff --git a/branches/sca-java-1.x/modules/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TupleTypeHelper.java b/branches/sca-java-1.x/modules/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TupleTypeHelper.java index 474459b9dc..64ea57f836 100644 --- a/branches/sca-java-1.x/modules/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TupleTypeHelper.java +++ b/branches/sca-java-1.x/modules/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TupleTypeHelper.java @@ -36,16 +36,15 @@ public class TupleTypeHelper implements TypeHelper { List<OtpErlangObject> tupleMembers = new ArrayList<OtpErlangObject>(); Field[] fields = forClass.getFields(); for (int i = 0; i < fields.length; i++) { - Object[] args = null; try { - args = new Object[] { fields[i].get(object) }; + OtpErlangObject member = TypeHelpersProxy.toErlang(fields[i] + .get(object), fields[i].getAnnotations()); + tupleMembers.add(member); } catch (IllegalArgumentException e) { // no problem should occur here } catch (IllegalAccessException e) { // and here } - OtpErlangObject member = TypeHelpersProxy.toErlang(args); - tupleMembers.add(member); } OtpErlangObject result = new OtpErlangTuple(tupleMembers .toArray(new OtpErlangObject[tupleMembers.size()])); @@ -61,7 +60,7 @@ public class TupleTypeHelper implements TypeHelper { for (int i = 0; i < tuple.arity(); i++) { OtpErlangObject tupleMember = tuple.elementAt(i); Object javaMember = TypeHelpersProxy.toJava(tupleMember, fields[i] - .getType()); + .getType(), fields[i].getAnnotations()); fields[i].setAccessible(true); fields[i].set(result, javaMember); } 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) {
|