tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jboy...@apache.org
Subject svn commit: r374561 - in /incubator/tuscany/java/sdo/impl: ./ src/main/java/org/apache/tuscany/sdo/ src/main/java/org/apache/tuscany/sdo/codegen/ src/test/java/org/apache/tuscany/sdo/codegen/
Date Fri, 03 Feb 2006 01:57:52 GMT
Author: jboynes
Date: Thu Feb  2 17:57:42 2006
New Revision: 374561

URL: http://svn.apache.org/viewcvs?rev=374561&view=rev
Log:
refactor the visitor interface and support bytecode generation of interface classes

Added:
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/BytecodeInterfaceGenerator.java
  (with props)
    incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/BytecodeInterfaceGeneratorTestCase.java
  (with props)
    incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Foo.java
  (with props)
    incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/MockProperty.java
  (with props)
    incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/MockType.java
  (with props)
Removed:
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/SDOPropertyVisitor.java
Modified:
    incubator/tuscany/java/sdo/impl/pom.xml
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/SDOTypeVisitor.java
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/GenerationException.java
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGenerator.java
    incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGeneratorTestCase.java

Modified: incubator/tuscany/java/sdo/impl/pom.xml
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sdo/impl/pom.xml?rev=374561&r1=374560&r2=374561&view=diff
==============================================================================
--- incubator/tuscany/java/sdo/impl/pom.xml (original)
+++ incubator/tuscany/java/sdo/impl/pom.xml Thu Feb  2 17:57:42 2006
@@ -45,6 +45,13 @@
         </dependency>
 
         <dependency>
+            <groupId>asm</groupId>
+            <artifactId>asm</artifactId>
+            <version>2.2</version>
+            <scope>compile</scope>
+        </dependency>
+
+        <dependency>
             <groupId>org.eclipse.emf</groupId>
             <artifactId>common</artifactId>
             <version>2.2.0-M4</version>

Modified: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/SDOTypeVisitor.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/SDOTypeVisitor.java?rev=374561&r1=374560&r2=374561&view=diff
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/SDOTypeVisitor.java
(original)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/SDOTypeVisitor.java
Thu Feb  2 17:57:42 2006
@@ -16,10 +16,9 @@
  */
 package org.apache.tuscany.sdo;
 
+import commonj.sdo.Property;
 import commonj.sdo.Type;
 
-import org.apache.tuscany.sdo.codegen.GenerationException;
-
 /**
  * Visitor interface that allows applications to process SDO type metadata.
  *
@@ -27,56 +26,21 @@
  */
 public interface SDOTypeVisitor {
     /**
-     * Visit the type definition.
-     *
-     * @param namespace  the namespace that the type is declared in
-     * @param name       the name of the type
-     * @param open       true if the type is open
-     * @param sequenced  true if the type is sequenced
-     * @param isAbstract true if the type is abstract
-     * @return this visitor
-     */
-    SDOTypeVisitor visitType(String namespace, String name, boolean open, boolean sequenced,
boolean isAbstract);
-
-    /**
-     * Visit the declaration of a base type this type extends.
-     *
-     * @param baseType a base type this type extends
-     * @return this visitor
-     */
-    SDOTypeVisitor visitBaseType(Type baseType) throws GenerationException;
-
-    /**
-     * Visit the declaration of an alias for this type.
-     *
-     * @param aliasName an alias for this type
-     * @return this visitor
-     */
-    SDOTypeVisitor visitAlias(String aliasName);
-
-    /**
-     * Visit a property declared by this type. Property indices are determined by the order
in which
-     * properties are visited.
+     * Visit a type definition.
      *
-     * @param name     the name of the property
-     * @param type     the SDO type of the property
-     * @param many     true if the property is multi-valued
-     * @param contains true if the property is a containing property
-     * @param readOnly true if the property only supports read access through the SDO API
-     * @return a property visitor that can be used to visit the property
+     * @param type the type to visit
      */
-    SDOPropertyVisitor visitProperty(String name, Type type, boolean many, boolean contains,
boolean readOnly) throws GenerationException;
+    void visitType(Type type);
 
     /**
-     * Visit the Java type associated with this SDO type.
+     * Visit a property definition.
      *
-     * @param javaType the Java type for an interface associated with this SDO type
-     * @return this visitor
+     * @param property the property to visit
      */
-    SDOTypeVisitor visitJavaType(java.lang.reflect.Type javaType);
+    void visitProperty(Property property);
 
     /**
-     * Indicate that this visitor will not be called again.
+     * Visit after all properties.
      */
     void visitEnd();
 }

Added: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/BytecodeInterfaceGenerator.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/BytecodeInterfaceGenerator.java?rev=374561&view=auto
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/BytecodeInterfaceGenerator.java
(added)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/BytecodeInterfaceGenerator.java
Thu Feb  2 17:57:42 2006
@@ -0,0 +1,84 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  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 org.apache.tuscany.sdo.codegen;
+
+import java.util.List;
+
+import commonj.sdo.Property;
+import commonj.sdo.Type;
+import org.objectweb.asm.ClassWriter;
+import static org.objectweb.asm.Opcodes.*;
+
+import org.apache.tuscany.sdo.SDOTypeVisitor;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class BytecodeInterfaceGenerator implements SDOTypeVisitor {
+    private final ClassWriter cw;
+
+    protected BytecodeInterfaceGenerator() {
+        cw = new ClassWriter(false);
+    }
+
+    public void visitType(Type type) {
+        String name = type.getName();
+        int lastDot = name.lastIndexOf('.');
+        if (lastDot != -1) {
+            name = name.replace('.', '/');
+        } else {
+            name = Character.toUpperCase(name.charAt(0)) + name.substring(1);
+        }
+
+        List baseTypes = type.getBaseTypes();
+        String[] interfaces = new String[baseTypes.size()];
+        for (int i = 0; i < baseTypes.size(); i++) {
+            Type baseType = (Type) baseTypes.get(i);
+            interfaces[i] = baseType.getInstanceClass().getName().replace('.', '/');
+        }
+
+        cw.visit(V1_5, ACC_PUBLIC + ACC_ABSTRACT + ACC_INTERFACE, name, null, "java/lang/Object",
interfaces);
+    }
+
+    public void visitProperty(Property property) {
+        String name = property.getName();
+        String propertyName = Character.toUpperCase(name.charAt(0)) + name.substring(1);
+        Class<?> javaType = property.getType().getInstanceClass();
+        String desc = org.objectweb.asm.Type.getDescriptor(javaType);
+
+        if (property.isMany()) {
+            cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "get" + propertyName, "()Ljava/util/List;",
null, null).visitEnd();
+        } else {
+            if (boolean.class.equals(javaType)) {
+                cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "is" + propertyName, "()Z", null,
null).visitEnd();
+            } else {
+                cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "get" + propertyName, "()" + desc,
null, null).visitEnd();
+            }
+            if (!property.isReadOnly()) {
+                cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "set" + propertyName, '(' + desc
+ ")V", null, null).visitEnd();
+            }
+        }
+    }
+
+    public void visitEnd() {
+        cw.visitEnd();
+    }
+
+    public byte[] getClassData() {
+        return cw.toByteArray();
+    }
+}

Propchange: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/BytecodeInterfaceGenerator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/BytecodeInterfaceGenerator.java
------------------------------------------------------------------------------
    svn:keywords = Rev,Date

Modified: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/GenerationException.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/GenerationException.java?rev=374561&r1=374560&r2=374561&view=diff
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/GenerationException.java
(original)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/GenerationException.java
Thu Feb  2 17:57:42 2006
@@ -21,7 +21,7 @@
  *
  * @version $Rev$ $Date$
  */
-public abstract class GenerationException extends Exception {
+public abstract class GenerationException extends RuntimeException {
     public GenerationException() {
     }
 

Modified: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGenerator.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGenerator.java?rev=374561&r1=374560&r2=374561&view=diff
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGenerator.java
(original)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGenerator.java
Thu Feb  2 17:57:42 2006
@@ -17,164 +17,80 @@
 package org.apache.tuscany.sdo.codegen;
 
 import java.io.PrintWriter;
-import java.io.StringWriter;
-import java.util.ArrayList;
 import java.util.List;
-import java.lang.reflect.Method;
 
-import commonj.sdo.Type;
 import commonj.sdo.Property;
+import commonj.sdo.Type;
 
-import org.apache.tuscany.sdo.SDOPropertyVisitor;
 import org.apache.tuscany.sdo.SDOTypeVisitor;
 
 /**
  * @version $Rev$ $Date$
  */
 public class JavaInterfaceGenerator implements SDOTypeVisitor {
-    private String packageName;
-    private String interfaceName;
-    private List<Type> baseTypes = new ArrayList<Type>();
-    private List<PropVisitor> props = new ArrayList<PropVisitor>();
-
-    public SDOTypeVisitor visitType(String namespace, String name, boolean open, boolean
sequenced, boolean isAbstract) {
-        int lastDot = name.lastIndexOf('.');
-        if (lastDot == -1) {
-            packageName = null;
-            this.interfaceName = name;
-        } else {
-            packageName = name.substring(0, lastDot);
-            interfaceName = name.substring(lastDot + 1);
-        }
-        return this;
-    }
-
-    public SDOTypeVisitor visitBaseType(Type baseType) throws NoJavaImplementationException
{
-        if (baseType.getInstanceClass() == null) {
-            throw new NoJavaImplementationException(baseType);
-        }
-        baseTypes.add(baseType);
-        return this;
-    }
-
-    public SDOTypeVisitor visitAlias(String aliasName) {
-        return this;
-    }
-
-    public SDOPropertyVisitor visitProperty(String name, Type type, boolean many, boolean
contains, boolean readOnly) throws NoJavaImplementationException {
-        if (type.getInstanceClass() == null) {
-            throw new NoJavaImplementationException(type);
-        }
-        PropVisitor propVisitor = new PropVisitor(name, type, many, readOnly);
-        props.add(propVisitor);
-        return propVisitor;
-    }
+    private final PrintWriter writer;
 
-    public SDOTypeVisitor visitJavaType(java.lang.reflect.Type javaType) {
-        if (javaType instanceof Class<?>) {
-            Class<?> javaClass = (Class<?>) javaType;
-            packageName = javaClass.getPackage().getName();
-            interfaceName = javaClass.getSimpleName();
-        } else {
-            throw new UnsupportedOperationException();
-        }
-        return this;
+    public JavaInterfaceGenerator(PrintWriter writer) {
+        this.writer = writer;
     }
 
-    public void visitEnd() {
-    }
-
-    public void writeTo(PrintWriter writer) {
-        if (packageName != null) {
+    public void visitType(Type type) {
+        String name = type.getName();
+        int lastDot = name.lastIndexOf('.');
+        if (lastDot != -1) {
             writer.print("package ");
-            writer.print(packageName);
+            writer.print(name.substring(0, lastDot));
             writer.println(';');
             writer.println();
+
+            name = name.substring(lastDot + 1);
+        } else {
+            name = Character.toUpperCase(name.charAt(0)) + name.substring(1);
         }
+
         writer.print("public interface ");
-        writer.print(interfaceName);
+        writer.print(name);
+        List baseTypes = type.getBaseTypes();
         for (int i = 0; i < baseTypes.size(); i++) {
-            Type type = baseTypes.get(i);
+            Type baseType = (Type) baseTypes.get(i);
             if (i == 0) {
                 writer.print(" extends ");
             } else {
                 writer.print(", ");
             }
-            writer.print(type.getInstanceClass().getName());
+            writer.print(baseType.getInstanceClass().getName());
         }
-        writer.println(" {");
-        for (PropVisitor prop : props) {
-            prop.writeTo(writer);
-        }
-        writer.println('}');
-    }
 
-    public String toString() {
-        StringWriter out = new StringWriter();
-        PrintWriter writer = new PrintWriter(out);
-        writeTo(writer);
-        writer.flush();
-        return out.toString();
+        writer.println(" {");
     }
 
-    private static class PropVisitor implements SDOPropertyVisitor {
-        private final String name;
-        private final Type type;
-        private final boolean many;
-        private final boolean readOnly;
-
-        public PropVisitor(String name, Type type, boolean many, boolean readOnly) {
-            this.name = name;
-            this.type = type;
-            this.many = many;
-            this.readOnly = readOnly;
-        }
-
-        public SDOPropertyVisitor visitDefault(Object defaultValue) {
-            return this;
-        }
-
-        public SDOPropertyVisitor visitAlias(String aliasName) {
-            throw new UnsupportedOperationException();
-        }
-
-        public SDOPropertyVisitor visitOpposite(Property opposite) {
-            return this;
-        }
-
-        public SDOPropertyVisitor visitGetter(Method getter) {
-            throw new UnsupportedOperationException();
-        }
-
-        public SDOPropertyVisitor visitSetter(Method setter) {
-            throw new UnsupportedOperationException();
-        }
-
-        public void visitEnd() {
-        }
-
-        public void writeTo(PrintWriter writer) {
-            String propertyName = Character.toUpperCase(name.charAt(0)) + name.substring(1);
-            String javaType = type.getInstanceClass().getCanonicalName();
-
-            if (!many) {
-                writer.print("    ");
-                writer.print(javaType);
-                writer.print("boolean".equals(javaType) ? " is" : " get");
+    public void visitProperty(Property property) {
+        String name = property.getName();
+        String propertyName = Character.toUpperCase(name.charAt(0)) + name.substring(1);
+        String javaType = property.getType().getInstanceClass().getCanonicalName();
+
+        if (!property.isMany()) {
+            writer.print("    ");
+            writer.print(javaType);
+            writer.print("boolean".equals(javaType) ? " is" : " get");
+            writer.print(propertyName);
+            writer.println("();");
+            if (!property.isReadOnly()) {
+                writer.print("    void set");
                 writer.print(propertyName);
-                writer.println("();");
-                if (!readOnly) {
-                    writer.print("    void set");
-                    writer.print(propertyName);
-                    writer.print('(');
-                    writer.print(javaType);
-                    writer.println(" value);");
-                }
-            } else {
-                writer.print("    java.util.List get");
-                writer.print(propertyName);
-                writer.println("();");
+                writer.print('(');
+                writer.print(javaType);
+                writer.println(" value);");
             }
+        } else {
+            writer.print("    java.util.List get");
+            writer.print(propertyName);
+            writer.println("();");
         }
     }
-}
+
+    public void visitEnd() {
+        writer.println('}');
+        writer.flush();
+    }
+}
\ No newline at end of file

Added: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/BytecodeInterfaceGeneratorTestCase.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/BytecodeInterfaceGeneratorTestCase.java?rev=374561&view=auto
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/BytecodeInterfaceGeneratorTestCase.java
(added)
+++ incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/BytecodeInterfaceGeneratorTestCase.java
Thu Feb  2 17:57:42 2006
@@ -0,0 +1,164 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  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 org.apache.tuscany.sdo.codegen;
+
+import java.lang.reflect.Method;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class BytecodeInterfaceGeneratorTestCase extends TestCase {
+    private MockType foo;
+    private BytecodeInterfaceGenerator gen;
+    private TestClassLoader cl;
+
+    public void testHeaderNoBaseClass() {
+        gen.visitType(foo);
+        gen.visitEnd();
+        Class<?> c = cl.addClass(gen.getClassData());
+        assertEquals("Foo", c.getName());
+        assertTrue(c.isInterface());
+        assertEquals(0, c.getInterfaces().length);
+        assertEquals(0, c.getMethods().length);
+    }
+
+    public void testHeaderWithPackage() {
+        gen.visitType(new MockType("org.apache.Foo", null));
+        gen.visitEnd();
+        Class<?> c = cl.addClass(gen.getClassData());
+        assertEquals("org.apache.Foo", c.getName());
+        assertEquals(0, c.getInterfaces().length);
+        assertEquals(0, c.getMethods().length);
+    }
+
+    public void testHeaderOneBaseClass() throws NoJavaImplementationException {
+        foo.addBaseType(new MockType("bar1", Bar1.class));
+        gen.visitType(foo);
+        gen.visitEnd();
+        Class<?> c = cl.addClass(gen.getClassData());
+        assertEquals("Foo", c.getName());
+        assertEquals(1, c.getInterfaces().length);
+        assertEquals(Bar1.class, c.getInterfaces()[0]);
+        assertEquals(0, c.getMethods().length);
+    }
+
+    public void testHeaderMultipleBaseClass() throws NoJavaImplementationException {
+        foo.addBaseType(new MockType("bar1", Bar1.class));
+        foo.addBaseType(new MockType("bar2", Bar2.class));
+        gen.visitType(foo);
+        gen.visitEnd();
+        Class<?> c = cl.addClass(gen.getClassData());
+        assertEquals("Foo", c.getName());
+        assertEquals(2, c.getInterfaces().length);
+        assertEquals(Bar1.class, c.getInterfaces()[0]);
+        assertEquals(Bar2.class, c.getInterfaces()[1]);
+        assertEquals(0, c.getMethods().length);
+    }
+
+    public void testBooleanProperty() throws NoJavaImplementationException, NoSuchMethodException
{
+        gen.visitType(foo);
+        gen.visitProperty(new MockProperty("true", Boolean.TYPE, false, false));
+        gen.visitEnd();
+        Class<?> c = cl.addClass(gen.getClassData());
+        assertEquals(2, c.getMethods().length);
+        Method getter = c.getMethod("isTrue");
+        assertEquals(boolean.class, getter.getReturnType());
+        Method setter = c.getMethod("setTrue", boolean.class);
+        assertEquals(void.class, setter.getReturnType());
+    }
+
+    public void testByteArrayProperty() throws NoJavaImplementationException, NoSuchMethodException
{
+        gen.visitType(foo);
+        gen.visitProperty(new MockProperty("bytes", byte[].class, false, false));
+        gen.visitEnd();
+        Class<?> c = cl.addClass(gen.getClassData());
+        assertEquals(2, c.getMethods().length);
+        Method getter = c.getMethod("getBytes");
+        assertEquals(byte[].class, getter.getReturnType());
+        Method setter = c.getMethod("setBytes", byte[].class);
+        assertEquals(void.class, setter.getReturnType());
+    }
+
+    public void testObjectProperty() throws NoJavaImplementationException, NoSuchMethodException
{
+        gen.visitType(foo);
+        gen.visitProperty(new MockProperty("bar", Bar1.class, false, false));
+        gen.visitEnd();
+        Class<?> c = cl.addClass(gen.getClassData());
+        assertEquals(2, c.getMethods().length);
+        Method getter = c.getMethod("getBar");
+        assertEquals(Bar1.class, getter.getReturnType());
+        Method setter = c.getMethod("setBar", Bar1.class);
+        assertEquals(void.class, setter.getReturnType());
+    }
+
+    public void testReadOnlyProperty() throws NoJavaImplementationException, NoSuchMethodException
{
+        gen.visitType(foo);
+        gen.visitProperty(new MockProperty("int", Integer.TYPE, false, true));
+        gen.visitEnd();
+        Class<?> c = cl.addClass(gen.getClassData());
+        assertEquals(1, c.getMethods().length);
+        Method getter = c.getMethod("getInt");
+        assertEquals(int.class, getter.getReturnType());
+    }
+
+    public void testManyProperty() throws NoJavaImplementationException, NoSuchMethodException
{
+        gen.visitType(foo);
+        gen.visitProperty(new MockProperty("list", Integer.TYPE, true, false));
+        gen.visitEnd();
+        Class<?> c = cl.addClass(gen.getClassData());
+        assertEquals(1, c.getMethods().length);
+        Method getter = c.getMethod("getList");
+        assertEquals(List.class, getter.getReturnType());
+    }
+
+    public void testTwoProperties() throws NoJavaImplementationException, NoSuchMethodException
{
+        gen.visitType(foo);
+        gen.visitProperty(new MockProperty("true", Boolean.TYPE, false, false));
+        gen.visitProperty(new MockProperty("int", Integer.TYPE, false, false));
+        gen.visitEnd();
+        Class<?> c = cl.addClass(gen.getClassData());
+        assertEquals(4, c.getMethods().length);
+        Method getter = c.getMethod("isTrue");
+        assertEquals(boolean.class, getter.getReturnType());
+        Method setter = c.getMethod("setTrue", boolean.class);
+        assertEquals(void.class, setter.getReturnType());
+        getter = c.getMethod("getInt");
+        assertEquals(int.class, getter.getReturnType());
+        setter = c.getMethod("setInt", int.class);
+        assertEquals(void.class, setter.getReturnType());
+    }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        foo = new MockType("foo", null);
+        gen = new BytecodeInterfaceGenerator();
+        cl = new TestClassLoader();
+    }
+
+    private class TestClassLoader extends ClassLoader {
+        public TestClassLoader() {
+            super(TestClassLoader.class.getClassLoader());
+        }
+
+        Class<?> addClass(byte[] bytes) {
+            return defineClass(null, bytes, 0, bytes.length);
+        }
+    }
+}

Propchange: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/BytecodeInterfaceGeneratorTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/BytecodeInterfaceGeneratorTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Rev,Date

Added: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Foo.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Foo.java?rev=374561&view=auto
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Foo.java
(added)
+++ incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Foo.java
Thu Feb  2 17:57:42 2006
@@ -0,0 +1,23 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  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 org.apache.tuscany.sdo.codegen;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public interface Foo {
+}

Propchange: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Foo.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Foo.java
------------------------------------------------------------------------------
    svn:keywords = Rev,Date

Modified: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGeneratorTestCase.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGeneratorTestCase.java?rev=374561&r1=374560&r2=374561&view=diff
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGeneratorTestCase.java
(original)
+++ incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGeneratorTestCase.java
Thu Feb  2 17:57:42 2006
@@ -16,11 +16,10 @@
  */
 package org.apache.tuscany.sdo.codegen;
 
-import java.util.List;
+import java.io.PrintWriter;
+import java.io.StringWriter;
 
 import junit.framework.TestCase;
-import commonj.sdo.Type;
-import commonj.sdo.Property;
 
 /**
  * @version $Rev$ $Date$
@@ -29,87 +28,84 @@
     private static final String SEP = System.getProperty("line.separator");
 
     private JavaInterfaceGenerator gen;
+    private StringWriter writer;
+    private MockType foo;
 
     public void testHeaderNoBaseClass() {
-        gen.visitType(null, "Foo", false, false, false);
+        gen.visitType(foo);
         gen.visitEnd();
-        assertEquals(localize("public interface Foo {\n}\n"), gen.toString());
+        assertEquals(localize("public interface Foo {\n}\n"), writer.toString());
     }
 
     public void testHeaderWithPackage() {
-        gen.visitType(null, "org.apache.Foo", false, false, false);
+        gen.visitType(new MockType("org.apache.Foo", null));
         gen.visitEnd();
-        assertEquals(localize("package org.apache;\n\npublic interface Foo {\n}\n"), gen.toString());
-    }
-
-    public void testJavaTypeOverridesSDO() {
-        gen.visitType(null, "org.apache.Foo", false, false, false);
-        gen.visitJavaType(Bar1.class);
-        gen.visitEnd();
-        assertEquals(localize("package org.apache.tuscany.sdo.codegen;\n\npublic interface
Bar1 {\n}\n"), gen.toString());
+        assertEquals(localize("package org.apache;\n\npublic interface Foo {\n}\n"), writer.toString());
     }
 
     public void testHeaderOneBaseClass() throws NoJavaImplementationException {
-        gen.visitType(null, "Foo", false, false, false);
-        gen.visitBaseType(new MockType(Bar1.class));
+        foo.addBaseType(new MockType("bar1", Bar1.class));
+        gen.visitType(foo);
         gen.visitEnd();
-        assertEquals(localize("public interface Foo extends org.apache.tuscany.sdo.codegen.Bar1
{\n}\n"), gen.toString());
+        assertEquals(localize("public interface Foo extends org.apache.tuscany.sdo.codegen.Bar1
{\n}\n"), writer.toString());
     }
 
     public void testHeaderMultipleBaseClass() throws NoJavaImplementationException {
-        gen.visitType(null, "Foo", false, false, false);
-        gen.visitBaseType(new MockType(Bar1.class));
-        gen.visitBaseType(new MockType(Bar2.class));
+        foo.addBaseType(new MockType("bar1", Bar1.class));
+        foo.addBaseType(new MockType("bar2", Bar2.class));
+        gen.visitType(foo);
         gen.visitEnd();
-        assertEquals(localize("public interface Foo extends org.apache.tuscany.sdo.codegen.Bar1,
org.apache.tuscany.sdo.codegen.Bar2 {\n}\n"), gen.toString());
+        assertEquals(localize("public interface Foo extends org.apache.tuscany.sdo.codegen.Bar1,
org.apache.tuscany.sdo.codegen.Bar2 {\n}\n"), writer.toString());
     }
 
     public void testBooleanProperty() throws NoJavaImplementationException {
-        gen.visitType(null, "Foo", false, false, false);
-        gen.visitProperty("true", new MockType(Boolean.TYPE), false, false, false);
+        gen.visitType(foo);
+        gen.visitProperty(new MockProperty("true", Boolean.TYPE, false, false));
         gen.visitEnd();
-        assertEquals(localize("public interface Foo {\n\tboolean isTrue();\n\tvoid setTrue(boolean
value);\n}\n"), gen.toString());
+        assertEquals(localize("public interface Foo {\n\tboolean isTrue();\n\tvoid setTrue(boolean
value);\n}\n"), writer.toString());
     }
 
     public void testByteArrayProperty() throws NoJavaImplementationException {
-        gen.visitType(null, "Foo", false, false, false);
-        gen.visitProperty("bytes", new MockType(byte[].class), false, false, false);
+        gen.visitType(foo);
+        gen.visitProperty(new MockProperty("bytes", byte[].class, false, false));
         gen.visitEnd();
-        assertEquals(localize("public interface Foo {\n\tbyte[] getBytes();\n\tvoid setBytes(byte[]
value);\n}\n"), gen.toString());
+        assertEquals(localize("public interface Foo {\n\tbyte[] getBytes();\n\tvoid setBytes(byte[]
value);\n}\n"), writer.toString());
     }
 
     public void testObjectProperty() throws NoJavaImplementationException {
-        gen.visitType(null, "Foo", false, false, false);
-        gen.visitProperty("bar", new MockType(Bar1.class), false, false, false);
+        gen.visitType(foo);
+        gen.visitProperty(new MockProperty("bar", Bar1.class, false, false));
         gen.visitEnd();
-        assertEquals(localize("public interface Foo {\n\torg.apache.tuscany.sdo.codegen.Bar1
getBar();\n\tvoid setBar(org.apache.tuscany.sdo.codegen.Bar1 value);\n}\n"), gen.toString());
+        assertEquals(localize("public interface Foo {\n\torg.apache.tuscany.sdo.codegen.Bar1
getBar();\n\tvoid setBar(org.apache.tuscany.sdo.codegen.Bar1 value);\n}\n"), writer.toString());
     }
 
     public void testReadOnlyProperty() throws NoJavaImplementationException {
-        gen.visitType(null, "Foo", false, false, false);
-        gen.visitProperty("int", new MockType(Integer.TYPE), false, false, true);
+        gen.visitType(foo);
+        gen.visitProperty(new MockProperty("int", Integer.TYPE, false, true));
         gen.visitEnd();
-        assertEquals(localize("public interface Foo {\n\tint getInt();\n}\n"), gen.toString());
+        assertEquals(localize("public interface Foo {\n\tint getInt();\n}\n"), writer.toString());
     }
 
     public void testManyProperty() throws NoJavaImplementationException {
-        gen.visitType(null, "Foo", false, false, false);
-        gen.visitProperty("list", new MockType(Integer.TYPE), true, false, false);
+        gen.visitType(foo);
+        gen.visitProperty(new MockProperty("list", Integer.TYPE, true, false));
         gen.visitEnd();
-        assertEquals(localize("public interface Foo {\n\tjava.util.List getList();\n}\n"),
gen.toString());
+        assertEquals(localize("public interface Foo {\n\tjava.util.List getList();\n}\n"),
writer.toString());
     }
 
     public void testTwoProperties() throws NoJavaImplementationException {
-        gen.visitType(null, "Foo", false, false, false);
-        gen.visitProperty("true", new MockType(Boolean.TYPE), false, false, false);
-        gen.visitProperty("int", new MockType(Integer.TYPE), false, false, false);
+        gen.visitType(foo);
+        gen.visitProperty(new MockProperty("true", Boolean.TYPE, false, false));
+        gen.visitProperty(new MockProperty("int", Integer.TYPE, false, false));
         gen.visitEnd();
-        assertEquals(localize("public interface Foo {\n\tboolean isTrue();\n\tvoid setTrue(boolean
value);\n\tint getInt();\n\tvoid setInt(int value);\n}\n"), gen.toString());
+        assertEquals(localize("public interface Foo {\n\tboolean isTrue();\n\tvoid setTrue(boolean
value);\n\tint getInt();\n\tvoid setInt(int value);\n}\n"), writer.toString());
     }
 
     protected void setUp() throws Exception {
         super.setUp();
-        gen = new JavaInterfaceGenerator();
+        writer = new StringWriter();
+        gen = new JavaInterfaceGenerator(new PrintWriter(writer));
+        foo = new MockType("foo", null);
     }
 
     private String localize(String s) {
@@ -127,63 +123,4 @@
         return b.toString();
     }
 
-    private static class MockType implements Type {
-        private final Class javaClass;
-
-        public MockType(Class javaClass) {
-            this.javaClass = javaClass;
-        }
-
-        public String getName() {
-            return javaClass.toString();
-        }
-
-        public String getURI() {
-            throw new UnsupportedOperationException();
-        }
-
-        public Class getInstanceClass() {
-            return javaClass;
-        }
-
-        public boolean isInstance(Object object) {
-            throw new UnsupportedOperationException();
-        }
-
-        public List /*Property*/ getProperties() {
-            throw new UnsupportedOperationException();
-        }
-
-        public Property getProperty(String propertyName) {
-            throw new UnsupportedOperationException();
-        }
-
-        public boolean isDataType() {
-            throw new UnsupportedOperationException();
-        }
-
-        public boolean isOpen() {
-            throw new UnsupportedOperationException();
-        }
-
-        public boolean isSequenced() {
-            throw new UnsupportedOperationException();
-        }
-
-        public boolean isAbstract() {
-            throw new UnsupportedOperationException();
-        }
-
-        public List /*Type*/ getBaseTypes() {
-            throw new UnsupportedOperationException();
-        }
-
-        public List /*Property*/ getDeclaredProperties() {
-            throw new UnsupportedOperationException();
-        }
-
-        public List /*String*/ getAliasNames() {
-            throw new UnsupportedOperationException();
-        }
-    }
 }

Added: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/MockProperty.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/MockProperty.java?rev=374561&view=auto
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/MockProperty.java
(added)
+++ incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/MockProperty.java
Thu Feb  2 17:57:42 2006
@@ -0,0 +1,75 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  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 org.apache.tuscany.sdo.codegen;
+
+import java.util.List;
+
+import commonj.sdo.Property;
+import commonj.sdo.Type;
+
+/**
+ * @version $Rev$ $Date$
+ */
+class MockProperty implements Property {
+    private final String name;
+    private final Type type;
+    private final boolean many;
+    private final boolean readOnly;
+
+    public MockProperty(String name, Class type, boolean many, boolean readOnly) {
+        this.name = name;
+        this.type = new MockType(null, type);
+        this.many = many;
+        this.readOnly = readOnly;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public Type getType() {
+        return type;
+    }
+
+    public boolean isMany() {
+        return many;
+    }
+
+    public boolean isContainment() {
+        throw new UnsupportedOperationException();
+    }
+
+    public Type getContainingType() {
+        throw new UnsupportedOperationException();
+    }
+
+    public Object getDefault() {
+        throw new UnsupportedOperationException();
+    }
+
+    public boolean isReadOnly() {
+        return readOnly;
+    }
+
+    public Property getOpposite() {
+        throw new UnsupportedOperationException();
+    }
+
+    public List /*String*/ getAliasNames() {
+        throw new UnsupportedOperationException();
+    }
+}

Propchange: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/MockProperty.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/MockProperty.java
------------------------------------------------------------------------------
    svn:keywords = Rev,Date

Added: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/MockType.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/MockType.java?rev=374561&view=auto
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/MockType.java
(added)
+++ incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/MockType.java
Thu Feb  2 17:57:42 2006
@@ -0,0 +1,93 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  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 org.apache.tuscany.sdo.codegen;
+
+import java.util.List;
+import java.util.ArrayList;
+
+import commonj.sdo.Type;
+import commonj.sdo.Property;
+
+/**
+ * @version $Rev$ $Date$
+ */
+class MockType implements Type {
+    private final String name;
+    private final Class javaClass;
+    private final List<Type> baseTypes = new ArrayList<Type>();
+
+    public MockType(String name, Class javaClass) {
+        this.javaClass = javaClass;
+        this.name = name;
+    }
+
+    public void addBaseType(Type baseType) {
+        baseTypes.add(baseType);
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public String getURI() {
+        throw new UnsupportedOperationException();
+    }
+
+    public Class getInstanceClass() {
+        return javaClass;
+    }
+
+    public boolean isInstance(Object object) {
+        throw new UnsupportedOperationException();
+    }
+
+    public List /*Property*/ getProperties() {
+        throw new UnsupportedOperationException();
+    }
+
+    public Property getProperty(String propertyName) {
+        throw new UnsupportedOperationException();
+    }
+
+    public boolean isDataType() {
+        throw new UnsupportedOperationException();
+    }
+
+    public boolean isOpen() {
+        throw new UnsupportedOperationException();
+    }
+
+    public boolean isSequenced() {
+        throw new UnsupportedOperationException();
+    }
+
+    public boolean isAbstract() {
+        throw new UnsupportedOperationException();
+    }
+
+    public List /*Type*/ getBaseTypes() {
+        return baseTypes;
+    }
+
+    public List /*Property*/ getDeclaredProperties() {
+        throw new UnsupportedOperationException();
+    }
+
+    public List /*String*/ getAliasNames() {
+        throw new UnsupportedOperationException();
+    }
+}

Propchange: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/MockType.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/MockType.java
------------------------------------------------------------------------------
    svn:keywords = Rev,Date



Mime
View raw message