diff options
Diffstat (limited to '')
13 files changed, 625 insertions, 0 deletions
diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/BooleanTypeHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/BooleanTypeHelper.java new file mode 100644 index 0000000000..dd9f65b4e9 --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/BooleanTypeHelper.java @@ -0,0 +1,40 @@ +/* + * 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.OtpErlangBoolean; +import com.ericsson.otp.erlang.OtpErlangObject; + +public class BooleanTypeHelper implements TypeHelper { + + public OtpErlangObject toErlang(Object object) { + return new OtpErlangBoolean((Boolean)object); + } + + public Object toJava(OtpErlangObject object, Class<?> forClass) throws Exception { + if (object.getClass().equals(OtpErlangAtom.class)) { + return ((OtpErlangAtom)object).booleanValue(); + } else { + return ((OtpErlangBoolean)object).booleanValue(); + } + } + +} diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ByteTypeHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ByteTypeHelper.java new file mode 100644 index 0000000000..d4a92e768a --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ByteTypeHelper.java @@ -0,0 +1,35 @@ +/* + * 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.OtpErlangByte; +import com.ericsson.otp.erlang.OtpErlangLong; +import com.ericsson.otp.erlang.OtpErlangObject; + +public class ByteTypeHelper implements TypeHelper { + + public OtpErlangObject toErlang(Object object) { + return new OtpErlangByte((Byte)object); + } + + public Object toJava(OtpErlangObject object, Class<?> forClass) throws Exception { + return (byte)((OtpErlangLong)object).longValue(); + } +} diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/CharTypeHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/CharTypeHelper.java new file mode 100644 index 0000000000..17100c65b8 --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/CharTypeHelper.java @@ -0,0 +1,36 @@ +/* + * 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.OtpErlangChar; +import com.ericsson.otp.erlang.OtpErlangLong; +import com.ericsson.otp.erlang.OtpErlangObject; + +public class CharTypeHelper implements TypeHelper { + + public OtpErlangObject toErlang(Object object) { + return new OtpErlangChar((Character)object); + } + + public Object toJava(OtpErlangObject object, Class<?> forClass) throws Exception { + return (char)((OtpErlangLong)object).longValue(); + } + +} diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/DoubleTypeHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/DoubleTypeHelper.java new file mode 100644 index 0000000000..760fb0723b --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/DoubleTypeHelper.java @@ -0,0 +1,35 @@ +/* + * 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.OtpErlangDouble; +import com.ericsson.otp.erlang.OtpErlangObject; + +public class DoubleTypeHelper implements TypeHelper { + + public OtpErlangObject toErlang(Object object) { + return new OtpErlangDouble((Double)object); + } + + public Object toJava(OtpErlangObject object, Class<?> forClass) throws Exception { + return ((OtpErlangDouble)object).doubleValue(); + } + +} diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/FloatTypeHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/FloatTypeHelper.java new file mode 100644 index 0000000000..851d0982f3 --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/FloatTypeHelper.java @@ -0,0 +1,35 @@ +/* + * 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.OtpErlangDouble; +import com.ericsson.otp.erlang.OtpErlangObject; + +public class FloatTypeHelper implements TypeHelper { + + public OtpErlangObject toErlang(Object object) { + return new OtpErlangDouble((Float)object); + } + + public Object toJava(OtpErlangObject object, Class<?> forClass) throws Exception { + return (float)((OtpErlangDouble)object).doubleValue(); + } + +} diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/IntTypeHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/IntTypeHelper.java new file mode 100644 index 0000000000..3e79577b1e --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/IntTypeHelper.java @@ -0,0 +1,36 @@ +/* + * 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.OtpErlangInt; +import com.ericsson.otp.erlang.OtpErlangLong; +import com.ericsson.otp.erlang.OtpErlangObject; + +public class IntTypeHelper implements TypeHelper { + + public OtpErlangObject toErlang(Object object) { + return new OtpErlangInt((Integer)object); + } + + public Object toJava(OtpErlangObject object, Class<?> forClass) throws Exception { + return (int)((OtpErlangLong)object).longValue(); + } + +} diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ListTypeHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ListTypeHelper.java new file mode 100644 index 0000000000..86dcd4fc0f --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ListTypeHelper.java @@ -0,0 +1,57 @@ +/* + * 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.reflect.Array; +import java.util.ArrayList; +import java.util.List; + +import com.ericsson.otp.erlang.OtpErlangList; +import com.ericsson.otp.erlang.OtpErlangObject; + +public class ListTypeHelper implements TypeHelper { + + public OtpErlangObject toErlang(Object object) { + int i = 0; + 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)); + 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())); + } + return result; + } + +} diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/LongTypeHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/LongTypeHelper.java new file mode 100644 index 0000000000..1437c8f8da --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/LongTypeHelper.java @@ -0,0 +1,35 @@ +/* + * 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.OtpErlangLong; +import com.ericsson.otp.erlang.OtpErlangObject; + +public class LongTypeHelper implements TypeHelper { + + public OtpErlangObject toErlang(Object object) { + return new OtpErlangLong((Long)object); + } + + public Object toJava(OtpErlangObject object, Class<?> forClass) throws Exception { + return ((OtpErlangLong)object).longValue(); + } + +} diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ShortTypeHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ShortTypeHelper.java new file mode 100644 index 0000000000..594875f348 --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ShortTypeHelper.java @@ -0,0 +1,36 @@ +/* + * 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.OtpErlangLong; +import com.ericsson.otp.erlang.OtpErlangObject; +import com.ericsson.otp.erlang.OtpErlangShort; + +public class ShortTypeHelper implements TypeHelper { + + public OtpErlangObject toErlang(Object object) { + return new OtpErlangShort((Short)object); + } + + public Object toJava(OtpErlangObject object, Class<?> forClass) throws Exception { + return (short)((OtpErlangLong)object).longValue(); + } + +} diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/StringTypeHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/StringTypeHelper.java new file mode 100644 index 0000000000..8a10e1818a --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/StringTypeHelper.java @@ -0,0 +1,35 @@ +/* + * 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.OtpErlangObject; +import com.ericsson.otp.erlang.OtpErlangString; + +public class StringTypeHelper implements TypeHelper { + + public OtpErlangObject toErlang(Object object) { + return new OtpErlangString((String)object); + } + + public Object toJava(OtpErlangObject object, Class<?> forClass) throws Exception { + return ((OtpErlangString)object).stringValue(); + } + +} diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TupleTypeHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TupleTypeHelper.java new file mode 100644 index 0000000000..86a78ccf87 --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TupleTypeHelper.java @@ -0,0 +1,68 @@ +/* + * 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.reflect.Field; +import java.util.ArrayList; +import java.util.List; + +import com.ericsson.otp.erlang.OtpErlangObject; +import com.ericsson.otp.erlang.OtpErlangTuple; + +public class TupleTypeHelper implements TypeHelper { + + public OtpErlangObject toErlang(Object object) { + Class<?> forClass = object.getClass(); + List<OtpErlangObject> tupleMembers = new ArrayList<OtpErlangObject>(); + Field[] fields = forClass.getFields(); + for (int i = 0; i < fields.length; i++) { + Object[] args; + try { + args = new Object[] {fields[i].get(object)}; + OtpErlangObject member = TypeHelpersProxy.toErlang(args); + tupleMembers.add(member); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + OtpErlangObject result = new OtpErlangTuple(tupleMembers.toArray(new OtpErlangObject[tupleMembers.size()])); + return result; + } + + public Object toJava(OtpErlangObject object, Class<?> forClass) throws Exception { + Object result = null; + OtpErlangTuple tuple = (OtpErlangTuple)object; + Field[] fields = forClass.getFields(); + if (fields.length != tuple.arity()) { + // TODO: received tuple with different element count - wrong + // message, exception! + } + result = forClass.newInstance(); + for (int i = 0; i < tuple.arity(); i++) { + OtpErlangObject tupleMember = tuple.elementAt(i); + Object javaMember = TypeHelpersProxy.toJava(tupleMember, fields[i].getType()); + fields[i].setAccessible(true); + fields[i].set(result, javaMember); + } + return result; + } + +} diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TypeHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TypeHelper.java new file mode 100644 index 0000000000..2d32ed586b --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TypeHelper.java @@ -0,0 +1,30 @@ +/*
+ * 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.OtpErlangObject;
+
+public interface TypeHelper {
+
+ Object toJava(OtpErlangObject object, Class<?> forClass) throws Exception;
+
+ OtpErlangObject toErlang(Object object);
+
+}
diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TypeHelpersProxy.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TypeHelpersProxy.java new file mode 100644 index 0000000000..e695f87dce --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TypeHelpersProxy.java @@ -0,0 +1,147 @@ +/*
+ * 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.reflect.Array;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.tuscany.sca.binding.erlang.impl.TypeMismatchException;
+
+import com.ericsson.otp.erlang.OtpErlangList;
+import com.ericsson.otp.erlang.OtpErlangObject;
+import com.ericsson.otp.erlang.OtpErlangTuple;
+
+public class TypeHelpersProxy {
+
+ private static Map<Class<?>, TypeHelper> primitiveTypes = null;
+
+ static {
+ // initiate type helpers
+ primitiveTypes = new HashMap<Class<?>, TypeHelper>();
+ primitiveTypes.put(boolean.class, new BooleanTypeHelper());
+ primitiveTypes.put(short.class, new ShortTypeHelper());
+ primitiveTypes.put(byte.class, new ByteTypeHelper());
+ primitiveTypes.put(char.class, new CharTypeHelper());
+ primitiveTypes.put(int.class, new IntTypeHelper());
+ primitiveTypes.put(long.class, new LongTypeHelper());
+ primitiveTypes.put(float.class, new FloatTypeHelper());
+ primitiveTypes.put(double.class, new DoubleTypeHelper());
+ primitiveTypes.put(String.class, new StringTypeHelper());
+ primitiveTypes.put(Boolean.class, primitiveTypes.get(boolean.class));
+ primitiveTypes.put(Character.class, primitiveTypes.get(char.class));
+ primitiveTypes.put(Short.class, primitiveTypes.get(char.class));
+ primitiveTypes.put(Byte.class, primitiveTypes.get(byte.class));
+ primitiveTypes.put(Short.class, primitiveTypes.get(short.class));
+ primitiveTypes.put(Integer.class, primitiveTypes.get(int.class));
+ primitiveTypes.put(Long.class, primitiveTypes.get(long.class));
+ primitiveTypes.put(Float.class, primitiveTypes.get(float.class));
+ primitiveTypes.put(Double.class, primitiveTypes.get(double.class));
+ primitiveTypes.put(String.class, primitiveTypes.get(String.class));
+ }
+
+ private static TypeHelper getTypeHelper(Class<?> forClass) {
+ TypeHelper typeHelper = null;
+ if (forClass.isArray()) {
+ typeHelper = new ListTypeHelper();
+ } else {
+ typeHelper = primitiveTypes.get(forClass);
+ }
+ if (typeHelper == null) {
+ typeHelper = new TupleTypeHelper();
+ }
+ return typeHelper;
+ }
+
+ public static OtpErlangObject toErlang(Object[] objects) {
+ OtpErlangObject result = null;
+ if (objects != null) {
+ TypeHelper helper = null;
+ switch (objects.length) {
+ case 0:
+ result = new OtpErlangList();
+ break;
+ case 1:
+ helper = getTypeHelper(objects[0].getClass());
+ 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());
+ erlObjects[i] = helper.toErlang(objects[i]);
+ }
+ result = new OtpErlangTuple(erlObjects);
+ break;
+ }
+ }
+ return result;
+ }
+
+ public static OtpErlangList toErlangAsList(Object array) {
+ OtpErlangList result = null;
+ if (array != null) {
+ if (!array.getClass().isArray()) {
+ array = new Object[] {array};
+ }
+ List<OtpErlangObject> attrsList = new ArrayList<OtpErlangObject>();
+ int i = 0;
+ while (true) {
+ try {
+ TypeHelper helper = getTypeHelper(Array.get(array, i).getClass());
+ attrsList.add(helper.toErlang(Array.get(array, i)));
+ i++;
+ } catch (ArrayIndexOutOfBoundsException e) {
+ break;
+ }
+ }
+ result = new OtpErlangList(attrsList.toArray(new OtpErlangObject[attrsList.size()]));
+ } else {
+ result = new OtpErlangList();
+ }
+ return result;
+ }
+
+ public static Object toJava(OtpErlangObject object, Class<?> forClass) throws Exception {
+ try {
+ TypeHelper helper = getTypeHelper(forClass);
+ return helper.toJava(object, forClass);
+ } catch (ClassCastException e) {
+ throw new TypeMismatchException(forClass, object.getClass());
+ }
+ }
+
+ public static Object[] toJavaFromList(OtpErlangList objects, Class<?>[] forClass) throws Exception {
+ Object[] result = new Object[objects.arity()];
+ try {
+ for (int i = 0; i < objects.arity(); i++) {
+ TypeHelper helper = getTypeHelper(forClass[i]);
+ result[i] = helper.toJava(objects.elementAt(i), forClass[i]);
+ }
+ } catch (ClassCastException e) {
+ e.printStackTrace();
+ // throw new TypeMismatchException(forClass, objects[i].getClass());
+ }
+ return result;
+ }
+
+}
|