summaryrefslogtreecommitdiffstats
path: root/sdo-java/branches/sdo-1.1-incubating/impl/src/main/java/org/apache/tuscany/sdo/codegen
diff options
context:
space:
mode:
Diffstat (limited to 'sdo-java/branches/sdo-1.1-incubating/impl/src/main/java/org/apache/tuscany/sdo/codegen')
-rw-r--r--sdo-java/branches/sdo-1.1-incubating/impl/src/main/java/org/apache/tuscany/sdo/codegen/BytecodeInterfaceGenerator.java98
-rw-r--r--sdo-java/branches/sdo-1.1-incubating/impl/src/main/java/org/apache/tuscany/sdo/codegen/GenerationException.java42
-rw-r--r--sdo-java/branches/sdo-1.1-incubating/impl/src/main/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGenerator.java179
-rw-r--r--sdo-java/branches/sdo-1.1-incubating/impl/src/main/java/org/apache/tuscany/sdo/codegen/NoJavaImplementationException.java61
4 files changed, 380 insertions, 0 deletions
diff --git a/sdo-java/branches/sdo-1.1-incubating/impl/src/main/java/org/apache/tuscany/sdo/codegen/BytecodeInterfaceGenerator.java b/sdo-java/branches/sdo-1.1-incubating/impl/src/main/java/org/apache/tuscany/sdo/codegen/BytecodeInterfaceGenerator.java
new file mode 100644
index 0000000000..3fe2cd952e
--- /dev/null
+++ b/sdo-java/branches/sdo-1.1-incubating/impl/src/main/java/org/apache/tuscany/sdo/codegen/BytecodeInterfaceGenerator.java
@@ -0,0 +1,98 @@
+/**
+ *
+ * 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.sdo.codegen;
+
+import java.util.List;
+
+import commonj.sdo.Property;
+import commonj.sdo.Type;
+import org.objectweb.asm.ClassWriter;
+import org.objectweb.asm.Opcodes;
+
+import org.apache.tuscany.sdo.SDOTypeVisitor;
+
+/**
+ * Implementation of a generator that will directly emit bytecode for an interface that
+ * corresponds to the static properties of a SDO type.
+ *
+ * @version $Rev$ $Date$
+ */
+public class BytecodeInterfaceGenerator implements SDOTypeVisitor {
+ private final ClassWriter cw;
+
+ public 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(Opcodes.V1_4,
+ Opcodes.ACC_PUBLIC + Opcodes.ACC_ABSTRACT + Opcodes.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(Opcodes.ACC_PUBLIC + Opcodes.ACC_ABSTRACT, "get" + propertyName, "()Ljava/util/List;", null, null).visitEnd();
+ } else {
+ if (boolean.class.equals(javaType)) {
+ cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_ABSTRACT, "is" + propertyName, "()Z", null, null).visitEnd();
+ } else {
+ cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_ABSTRACT, "get" + propertyName, "()" + desc, null, null).visitEnd();
+ }
+ if (!property.isReadOnly()) {
+ cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_ABSTRACT, "set" + propertyName, '(' + desc + ")V", null, null).visitEnd();
+ }
+ }
+ }
+
+ public void visitEnd() {
+ cw.visitEnd();
+ }
+
+ /**
+ * Return the bytecode for the interface class in a form that can be written
+ * to disk, added to a JAR file, or passed to a ClassLoader.
+ *
+ * @return the bytecode for the SDO Type's interface class
+ */
+ public byte[] getClassData() {
+ return cw.toByteArray();
+ }
+}
diff --git a/sdo-java/branches/sdo-1.1-incubating/impl/src/main/java/org/apache/tuscany/sdo/codegen/GenerationException.java b/sdo-java/branches/sdo-1.1-incubating/impl/src/main/java/org/apache/tuscany/sdo/codegen/GenerationException.java
new file mode 100644
index 0000000000..3d645be1da
--- /dev/null
+++ b/sdo-java/branches/sdo-1.1-incubating/impl/src/main/java/org/apache/tuscany/sdo/codegen/GenerationException.java
@@ -0,0 +1,42 @@
+/**
+ *
+ * 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.sdo.codegen;
+
+/**
+ * Exception indicating there was a problem with code generation.
+ *
+ * @version $Rev$ $Date$
+ */
+public abstract class GenerationException extends RuntimeException {
+ public GenerationException() {
+ }
+
+ public GenerationException(String message) {
+ super(message);
+ }
+
+ public GenerationException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public GenerationException(Throwable cause) {
+ super(cause);
+ }
+}
diff --git a/sdo-java/branches/sdo-1.1-incubating/impl/src/main/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGenerator.java b/sdo-java/branches/sdo-1.1-incubating/impl/src/main/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGenerator.java
new file mode 100644
index 0000000000..dc88a0d60d
--- /dev/null
+++ b/sdo-java/branches/sdo-1.1-incubating/impl/src/main/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGenerator.java
@@ -0,0 +1,179 @@
+/**
+ *
+ * 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.sdo.codegen;
+
+import java.io.PrintWriter;
+import java.util.List;
+
+import commonj.sdo.Property;
+import commonj.sdo.Type;
+
+import org.apache.tuscany.sdo.SDOTypeVisitor;
+
+/**
+ * Implementation of a generator that will output the source code for a Java interface
+ * that corresponds to the SDO Type's static properties.
+ *
+ * @version $Rev$ $Date$
+ */
+public class JavaInterfaceGenerator implements SDOTypeVisitor {
+
+ private static String canonicalize(String className) {
+ if (className == null) {
+ return "";
+ }
+ if (className.charAt(0) != '[') { // if not array
+ return className;
+ }
+ // process array
+ boolean invalidClassName = false;
+ int nestLevel = 1;
+ StringBuffer sb = new StringBuffer();
+ try {
+ while (className.charAt(nestLevel) == '[') {
+ nestLevel++;
+ }
+ char typeChar = className.charAt(nestLevel);
+ int end = nestLevel;
+ switch (typeChar) {
+ case 'L':
+ end = className.length() - 1;
+ if (className.charAt(end) != ';') {
+ invalidClassName = true;
+ } else {
+ sb.append(className.substring(nestLevel+1, end));
+ }
+ break;
+ case 'Z':
+ sb.append("boolean");
+ break;
+ case 'B':
+ sb.append("byte");
+ break;
+ case 'C':
+ sb.append("char");
+ break;
+ case 'D':
+ sb.append("double");
+ break;
+ case 'F':
+ sb.append("float");
+ break;
+ case 'I':
+ sb.append("int");
+ break;
+ case 'J':
+ sb.append("long");
+ break;
+ case 'S':
+ sb.append("short");
+ break;
+ default:
+ invalidClassName = true;
+ break;
+ }
+ if (end != (className.length() - 1)) {
+ invalidClassName = true; // we have not used all the characters
+ } else {
+ for (int i=0; i<nestLevel; i++) {
+ sb.append("[]");
+ }
+ }
+ } catch(Exception e) {
+ invalidClassName = true;
+ }
+ if (invalidClassName) {
+ System.err.println("unable to canonicalize class name: "+className);
+ return className;
+ }
+ return sb.toString();
+ }
+
+ private final PrintWriter writer;
+
+ /**
+ * Constructor providing the Writer to output the source to.
+ *
+ * @param writer where the generated source code will be written to
+ */
+ public JavaInterfaceGenerator(PrintWriter writer) {
+ this.writer = writer;
+ }
+
+ public void visitType(Type type) {
+ String name = type.getName();
+ int lastDot = name.lastIndexOf('.');
+ if (lastDot != -1) {
+ writer.print("package ");
+ 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(name);
+ List baseTypes = type.getBaseTypes();
+ for (int i = 0; i < baseTypes.size(); i++) {
+ Type baseType = (Type) baseTypes.get(i);
+ if (i == 0) {
+ writer.print(" extends ");
+ } else {
+ writer.print(", ");
+ }
+ writer.print(baseType.getInstanceClass().getName());
+ }
+
+ writer.println(" {");
+ }
+
+ public void visitProperty(Property property) {
+ String name = property.getName();
+ String propertyName = Character.toUpperCase(name.charAt(0)) + name.substring(1);
+ String javaType = canonicalize(property.getType().getInstanceClass().getName());
+
+ 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.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();
+ }
+}
diff --git a/sdo-java/branches/sdo-1.1-incubating/impl/src/main/java/org/apache/tuscany/sdo/codegen/NoJavaImplementationException.java b/sdo-java/branches/sdo-1.1-incubating/impl/src/main/java/org/apache/tuscany/sdo/codegen/NoJavaImplementationException.java
new file mode 100644
index 0000000000..a6253e8131
--- /dev/null
+++ b/sdo-java/branches/sdo-1.1-incubating/impl/src/main/java/org/apache/tuscany/sdo/codegen/NoJavaImplementationException.java
@@ -0,0 +1,61 @@
+/**
+ *
+ * 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.sdo.codegen;
+
+import commonj.sdo.Type;
+
+/**
+ * Exception that indicates there is no Java mapping for an SDO type.
+ *
+ * @version $Rev$ $Date$
+ */
+public class NoJavaImplementationException extends GenerationException {
+ private final Type type;
+
+ /**
+ * Constructor supplying the Type that did not have a Java implementation.
+ * A default message is provided of the form "${URI}#${Name}"
+ *
+ * @param type the type that did not have a Java implementation.
+ */
+ public NoJavaImplementationException(Type type) {
+ super(type == null ? null : type.getURI() + "#" + type.getName());
+ this.type = type;
+ }
+
+ /**
+ * Constructor supplying a message and the Type that did not have a Java implementation.
+ *
+ * @param message the message
+ * @param type the type that did not have a Java implementation.
+ */
+ public NoJavaImplementationException(String message, Type type) {
+ super(message);
+ this.type = type;
+ }
+
+ /**
+ * Return the type that did not have a Java implementation.
+ * @return the type that did not have a Java implementation
+ */
+ public Type getType() {
+ return type;
+ }
+}