You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by rf...@apache.org on 2007/04/08 08:32:51 UTC

svn commit: r526533 - in /incubator/tuscany/java/sca/modules/interface-java/src: main/java/org/apache/tuscany/interfacedef/java/impl/ test/java/org/ test/java/org/apache/ test/java/org/apache/tuscany/ test/java/org/apache/tuscany/interfacedef/ test/jav...

Author: rfeng
Date: Sat Apr  7 23:32:50 2007
New Revision: 526533

URL: http://svn.apache.org/viewvc?view=rev&rev=526533
Log:
Add the JavaInterfaceUtil

Added:
    incubator/tuscany/java/sca/modules/interface-java/src/main/java/org/apache/tuscany/interfacedef/java/impl/JavaInterfaceUtil.java   (with props)
    incubator/tuscany/java/sca/modules/interface-java/src/test/java/org/
    incubator/tuscany/java/sca/modules/interface-java/src/test/java/org/apache/
    incubator/tuscany/java/sca/modules/interface-java/src/test/java/org/apache/tuscany/
    incubator/tuscany/java/sca/modules/interface-java/src/test/java/org/apache/tuscany/interfacedef/
    incubator/tuscany/java/sca/modules/interface-java/src/test/java/org/apache/tuscany/interfacedef/java/
    incubator/tuscany/java/sca/modules/interface-java/src/test/java/org/apache/tuscany/interfacedef/java/impl/
    incubator/tuscany/java/sca/modules/interface-java/src/test/java/org/apache/tuscany/interfacedef/java/impl/JavaInterfaceUtilTestCase.java   (with props)

Added: incubator/tuscany/java/sca/modules/interface-java/src/main/java/org/apache/tuscany/interfacedef/java/impl/JavaInterfaceUtil.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/interface-java/src/main/java/org/apache/tuscany/interfacedef/java/impl/JavaInterfaceUtil.java?view=auto&rev=526533
==============================================================================
--- incubator/tuscany/java/sca/modules/interface-java/src/main/java/org/apache/tuscany/interfacedef/java/impl/JavaInterfaceUtil.java (added)
+++ incubator/tuscany/java/sca/modules/interface-java/src/main/java/org/apache/tuscany/interfacedef/java/impl/JavaInterfaceUtil.java Sat Apr  7 23:32:50 2007
@@ -0,0 +1,115 @@
+/*
+ * 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.interfacedef.java.impl;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Type;
+import java.util.Collection;
+import java.util.List;
+
+import org.apache.tuscany.interfacedef.DataType;
+import org.apache.tuscany.interfacedef.Operation;
+
+/**
+ * Contains methods for mapping between an operation in a {@link org.apache.tuscany.spi.model.ServiceContract} and a
+ * method defined by a Java interface
+ *
+ * @version $Rev$ $Date$
+ */
+public final class JavaInterfaceUtil {
+
+    private JavaInterfaceUtil() {
+    }
+
+    /**
+     * Return the method on the implementation class that matches the operation.
+     *
+     * @param implClass the implementation class or interface
+     * @param operation the operation to match
+     * @return the method described by the operation
+     * @throws NoSuchMethodException if no such method exists
+     * @Deprecated
+     */
+    public static  Method findMethod(Class<?> implClass, Operation operation) throws NoSuchMethodException {
+        String name = operation.getName();
+        Class<?>[] paramTypes = getPhysicalTypes(operation);
+        return implClass.getMethod(name, paramTypes);
+    }
+
+    /**
+     * @Deprecated
+     */
+    private static  Class<?>[] getPhysicalTypes(Operation operation) {
+        DataType<List<DataType>> inputType = operation.getInputType();
+        List<DataType> types = inputType.getLogical();
+        Class<?>[] javaTypes = new Class<?>[types.size()];
+        for (int i = 0; i < javaTypes.length; i++) {
+            Type physical = types.get(i).getPhysical();
+            if (physical instanceof Class<?>) {
+                javaTypes[i] = (Class<?>) physical;
+            } else {
+                throw new UnsupportedOperationException();
+            }
+        }
+        return javaTypes;
+    }
+
+    /**
+     * Searches a collection of operations for a match against the given method
+     *
+     * @param method     the method to match
+     * @param operations the operations to match against
+     * @return a matching operation or null
+     * @Deprecated
+     */
+    public static Operation findOperation(Method method, Collection<Operation> operations) {
+        for (Operation operation : operations) {
+            if (match(operation, method)) {
+                return operation;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Determines if the given operation matches the given method
+     *
+     * @return true if the operation matches, false if does not
+     */
+    private static  boolean match(Operation operation, Method method) {
+        Class<?>[] params = method.getParameterTypes();
+        DataType<List<DataType>> inputType = operation.getInputType();
+        List<DataType> types = inputType.getLogical();
+        boolean found = true;
+        if (types.size() == params.length && method.getName().equals(operation.getName())) {
+            for (int i = 0; i < params.length; i++) {
+                Class<?> clazz = params[i];
+                if (!clazz.equals(operation.getInputType().getLogical().get(i).getPhysical())) {
+                    found = false;
+                }
+            }
+        } else {
+            found = false;
+        }
+        return found;
+
+    }
+
+
+}

Propchange: incubator/tuscany/java/sca/modules/interface-java/src/main/java/org/apache/tuscany/interfacedef/java/impl/JavaInterfaceUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/modules/interface-java/src/main/java/org/apache/tuscany/interfacedef/java/impl/JavaInterfaceUtil.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/tuscany/java/sca/modules/interface-java/src/test/java/org/apache/tuscany/interfacedef/java/impl/JavaInterfaceUtilTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/interface-java/src/test/java/org/apache/tuscany/interfacedef/java/impl/JavaInterfaceUtilTestCase.java?view=auto&rev=526533
==============================================================================
--- incubator/tuscany/java/sca/modules/interface-java/src/test/java/org/apache/tuscany/interfacedef/java/impl/JavaInterfaceUtilTestCase.java (added)
+++ incubator/tuscany/java/sca/modules/interface-java/src/test/java/org/apache/tuscany/interfacedef/java/impl/JavaInterfaceUtilTestCase.java Sat Apr  7 23:32:50 2007
@@ -0,0 +1,114 @@
+/*
+ * 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.interfacedef.java.impl;
+
+import static org.apache.tuscany.interfacedef.java.impl.JavaInterfaceUtil.findOperation;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.apache.tuscany.interfacedef.DataType;
+import org.apache.tuscany.interfacedef.Operation;
+import org.apache.tuscany.interfacedef.impl.DataTypeImpl;
+import org.apache.tuscany.interfacedef.impl.OperationImpl;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class JavaInterfaceUtilTestCase extends TestCase {
+    private List<Operation> operations;
+
+    public void testNoParamsFindOperation() throws Exception {
+        Method method = Foo.class.getMethod("foo");
+        Operation ret = findOperation(method, operations);
+        assertEquals("foo", ret.getName());
+        assertEquals(0, method.getParameterTypes().length);
+    }
+
+    public void testParamsFindOperation() throws Exception {
+        Method method = Foo.class.getMethod("foo", String.class);
+        Operation ret = findOperation(method, operations);
+        assertEquals("foo", ret.getName());
+        assertEquals(String.class, method.getParameterTypes()[0]);
+    }
+
+    public void testPrimitiveParamFindOperation() throws NoSuchMethodException {
+        Method method = Foo.class.getMethod("foo", Integer.TYPE);
+        Operation operation = findOperation(method, operations);
+        assertEquals(Integer.TYPE, operation.getInputType().getLogical().get(0).getPhysical());
+    }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        Operation operation = new OperationImpl("foo");
+        List<DataType> types = new ArrayList<DataType>();
+        DataType<List<DataType>> inputType = new DataTypeImpl<List<DataType>>(Object[].class, types);
+        operation.setInputType(inputType);
+
+        operations = new ArrayList<Operation>();
+        operations.add(operation);
+
+        types = new ArrayList<DataType>();
+        inputType = new DataTypeImpl<List<DataType>>(Object[].class, types);
+        DataType type = new DataTypeImpl<Class>(String.class, Object.class);
+        types.add(type);
+        operation = new OperationImpl("foo");
+        operation.setInputType(inputType);
+        operations.add(operation);
+
+        types = new ArrayList<DataType>();
+        type = new DataTypeImpl<Class>(String.class, Object.class);
+        DataType type2 = new DataTypeImpl<Class>(String.class, Object.class);
+        types.add(type);
+        types.add(type2);
+        inputType = new DataTypeImpl<List<DataType>>(Object[].class, types);
+        operation = new OperationImpl("foo");
+        operation.setInputType(inputType);
+        operations.add(operation);
+
+        types = new ArrayList<DataType>();
+        type = new DataTypeImpl<Class>(Integer.class, Object.class);
+        types.add(type);
+        inputType = new DataTypeImpl<List<DataType>>(Object[].class, types);
+        operation = new OperationImpl("foo");
+        operation.setInputType(inputType);
+        operations.add(operation);
+
+        types = new ArrayList<DataType>();
+        type = new DataTypeImpl<Class>(Integer.TYPE, Object.class);
+        types.add(type);
+        inputType = new DataTypeImpl<List<DataType>>(Object[].class, types);
+        operation = new OperationImpl("foo");
+        operation.setInputType(inputType);
+        operations.add(operation);
+
+    }
+
+    private interface Foo {
+        void foo();
+
+        void foo(String foo);
+
+        void foo(int b);
+    }
+}

Propchange: incubator/tuscany/java/sca/modules/interface-java/src/test/java/org/apache/tuscany/interfacedef/java/impl/JavaInterfaceUtilTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/modules/interface-java/src/test/java/org/apache/tuscany/interfacedef/java/impl/JavaInterfaceUtilTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org