This is an automated email from the ASF dual-hosted git repository. dfoulks pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/activemq-nms-openwire-generator.git
commit c31b80e30f6ef1ed68d9e38be7f4e38e714ce763 Author: Timothy A. Bish <[email protected]> AuthorDate: Tue Jul 28 20:16:49 2009 +0000 First workable version of Command Class generation. Needs more work now to implement specific Commands that require special methods and extensions. --- pom.xml | 12 + .../nms/openwire/tool/AmqCSharpGeneratorTask.java | 2 +- .../tool/commands/CommandClassGenerator.java | 304 ++++++++------------- .../tool/commands/CommandCodeGenerator.java | 105 ++----- .../commands/CommandCodeGeneratorsFactory.java | 5 - .../openwire/tool/commands/CommandGenerator.java | 8 +- 6 files changed, 160 insertions(+), 276 deletions(-) diff --git a/pom.xml b/pom.xml index 6978061..c3ad024 100644 --- a/pom.xml +++ b/pom.xml @@ -67,6 +67,7 @@ <ant-version>1.6.2</ant-version> <xstream-version>1.3.1</xstream-version> <activemq-core-dir>${ACTIVEMQ_SRC_HOME}/activemq-core/</activemq-core-dir> + <nms-activemq-dir>${NMS_ACTIVEMQ_HOME}</nms-activemq-dir> </properties> <dependencies> @@ -131,6 +132,17 @@ <target>1.5</target> </configuration> </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-antrun-plugin</artifactId> + <configuration> + <tasks> + <echo>Running OpenWire Generator</echo> + <taskdef name="generate" classname="org.apache.activemq.nms.openwire.tool.AmqCSharpGeneratorTask" classpathref="maven.compile.classpath" /> + <generate maxVersion="5" source="${activemq-core-dir}" target="${nms-activemq-dir}" /> + </tasks> + </configuration> + </plugin> </plugins> </build> diff --git a/src/main/java/org/apache/activemq/nms/openwire/tool/AmqCSharpGeneratorTask.java b/src/main/java/org/apache/activemq/nms/openwire/tool/AmqCSharpGeneratorTask.java index 66c8c93..cf04a10 100644 --- a/src/main/java/org/apache/activemq/nms/openwire/tool/AmqCSharpGeneratorTask.java +++ b/src/main/java/org/apache/activemq/nms/openwire/tool/AmqCSharpGeneratorTask.java @@ -76,7 +76,7 @@ public class AmqCSharpGeneratorTask extends Task { { CommandGenerator script = new CommandGenerator(); script.setJam(jam); - script.setTargetDir(target + "/src/main"); + script.setTargetDir(target + "/src/main/csharp"); script.setOpenwireVersion(maxVersion); script.run(); } diff --git a/src/main/java/org/apache/activemq/nms/openwire/tool/commands/CommandClassGenerator.java b/src/main/java/org/apache/activemq/nms/openwire/tool/commands/CommandClassGenerator.java index 9098802..f86b677 100644 --- a/src/main/java/org/apache/activemq/nms/openwire/tool/commands/CommandClassGenerator.java +++ b/src/main/java/org/apache/activemq/nms/openwire/tool/commands/CommandClassGenerator.java @@ -17,7 +17,6 @@ package org.apache.activemq.nms.openwire.tool.commands; import java.io.PrintWriter; -import java.util.Set; import org.codehaus.jam.JProperty; @@ -28,27 +27,17 @@ public class CommandClassGenerator extends CommandCodeGenerator { // Start with the license. generateLicence(out); - out.println("#ifndef _ACTIVEMQ_COMMANDS_"+getClassName().toUpperCase()+"_H_"); - out.println("#define _ACTIVEMQ_COMMANDS_"+getClassName().toUpperCase()+"_H_"); - out.println(""); - out.println("// Turn off warning message for ignored exception specification"); - out.println("#ifdef _MSC_VER"); - out.println("#pragma warning( disable : 4290 )"); - out.println("#endif"); - out.println(""); + out.println("using System;"); + out.println("using System.Collections;"); - populateIncludeFilesSet(); - for( String include : getIncludeFiles() ) { - if( include != null ) { - out.println("#include "+include); - } + if( getBaseClassName().equals( "BaseCommand" ) ) { + out.println(""); + out.println("using Apache.NMS.ActiveMQ.State;"); } out.println(""); - generateNamespaceWrapper( out ); - generateForwardDeclarations( out ); - - out.println(" using decaf::lang::Pointer;"); + out.println("namespace Apache.NMS.ActiveMQ.Commands"); + out.println("{"); out.println(""); out.println(" /*"); out.println(" *"); @@ -56,237 +45,176 @@ public class CommandClassGenerator extends CommandCodeGenerator { out.println(" *"); out.println(" * NOTE!: This file is auto generated - do not modify!"); out.println(" * if you need to make a change, please see the Java Classes"); - out.println(" * in the activemq-cpp-openwire-generator module"); + out.println(" * in the nms-activemq-openwire-generator module"); out.println(" *"); out.println(" */"); generateClassDefinition( out ); - generateProperties( out ); - if( !isAssignable() ) { - out.println(" protected:"); - out.println(""); - out.println(" "+getClassName()+"( const "+getClassName()+"& ) : " + getBaseClassName() + "() {};"); - out.println(" "+getClassName()+"& operator= ( const "+getClassName()+"& ) { return *this; };"); - out.println(""); - } - - out.println(" public:"); + out.println(" public const byte ID_"+ getClassName().toUpperCase() + " = " + getOpenWireOpCode() + ";"); out.println(""); - out.println(" "+getClassName()+"();"); - out.println(""); - - if( isAssignable() ) { - out.println(" "+getClassName()+"( const "+getClassName()+"& other );"); - out.println(""); - } - generateAdditionalConstructors( out ); + generateProperties( out ); - out.println(" virtual ~"+getClassName()+"();"); - out.println(""); - out.println(" /**"); - out.println(" * Get the unique identifier that this object and its own"); - out.println(" * Marshaler share."); - out.println(" * @returns new DataStructure type copy."); - out.println(" */"); - out.println(" virtual unsigned char getDataStructureType() const;"); out.println(""); - out.println(" /**"); - out.println(" * Clone this object and return a new instance that the"); - out.println(" * caller now owns, this will be an exact copy of this one"); - out.println(" * @returns new copy of this object."); - out.println(" */"); - out.println(" virtual "+getClassName()+"* cloneDataStructure() const;"); - out.println(""); - out.println(" /**"); - out.println(" * Copy the contents of the passed object into this object's"); - out.println(" * members, overwriting any existing data."); - out.println(" * @param src - Source Object"); - out.println(" */"); - out.println(" virtual void copyDataStructure( const DataStructure* src );"); - out.println(""); - out.println(" /**"); - out.println(" * Returns a string containing the information for this DataStructure"); - out.println(" * such as its type and value of its elements."); - out.println(" * @return formatted string useful for debugging."); - out.println(" */"); - out.println(" virtual std::string toString() const;"); - out.println(""); - out.println(" /**" ); - out.println(" * Compares the DataStructure passed in to this one, and returns if" ); - out.println(" * they are equivalent. Equivalent here means that they are of the" ); - out.println(" * same type, and that each element of the objects are the same." ); - out.println(" * @returns true if DataStructure's are Equal." ); - out.println(" */" ); - out.println(" virtual bool equals( const DataStructure* value ) const;" ); + out.println(" ///"); + out.println(" /// <summery>"); + out.println(" /// Get the unique identifier that this object and its own"); + out.println(" /// Marshaler share."); + out.println(" /// </summery>"); + out.println(" ///"); + out.println(" public override byte GetDataStructureType()"); + out.println(" {"); + out.println(" return ID_" + getClassName().toUpperCase() + ";"); + out.println(" }"); + out.println(""); +// out.println(" ///"); +// out.println(" /// <summery>"); +// out.println(" /// Clone this object and return a new instance that the caller now owns."); +// out.println(" /// </summery>"); +// out.println(" ///"); +// out.println(" public override Object Clone()"); +// out.println(" {"); +// generateCloneBody( out ); +// out.println(" }"); +// out.println(""); + out.println(" ///"); + out.println(" /// <summery>"); + out.println(" /// Returns a string containing the information for this DataStructure"); + out.println(" /// such as its type and value of its elements."); + out.println(" /// </summery>"); + out.println(" ///"); + out.println(" public override string ToString()"); + out.println(" {"); + generateToStringBody( out ); + out.println(" }"); out.println(""); generateAdditonalMembers( out ); generatePropertyAccessors( out ); - if( isComparable() ) { - out.println(" virtual int compareTo( const "+getClassName()+"& value ) const;"); - out.println(""); - out.println(" virtual bool equals( const "+getClassName()+"& value ) const;"); - out.println(""); - out.println(" virtual bool operator==( const "+getClassName()+"& value ) const;"); - out.println(""); - out.println(" virtual bool operator<( const "+getClassName()+"& value ) const;"); - out.println(""); - } - - if( isAssignable() ) { - out.println(" "+getClassName()+"& operator= ( const "+getClassName()+"& other );"); - out.println(""); - } - if( isGenIsClass() ) { - out.println(" /**"); - out.println(" * @return an answer of true to the is"+getClassName()+"() query."); - out.println(" */"); - out.println(" virtual bool is"+getClassName()+"() const {"); - out.println(" return true;"); + out.println(" ///"); + out.println(" /// <summery>"); + out.println(" /// Return an answer of true to the is"+getClassName()+"() query."); + out.println(" /// </summery>"); + out.println(" ///"); + out.println(" public override bool Is"+getClassName()); + out.println(" {"); + out.println(" get"); + out.println(" {"); + out.println(" return true;"); + out.println(" }"); out.println(" }"); out.println(""); } if( getBaseClassName().equals( "BaseCommand" ) ) { - out.println(" /**" ); - out.println(" * Allows a Visitor to visit this command and return a response to the" ); - out.println(" * command based on the command type being visited. The command will call" ); - out.println(" * the proper processXXX method in the visitor." ); - out.println(" * " ); - out.println(" * @return a Response to the visitor being called or NULL if no response." ); - out.println(" */" ); - out.println(" virtual Pointer<Command> visit( activemq::state::CommandVisitor* visitor )" ); - out.println(" throw( exceptions::ActiveMQException );" ); + out.println(" ///"); + out.println(" /// <summery>"); + out.println(" /// Allows a Visitor to visit this command and return a response to the" ); + out.println(" /// command based on the command type being visited. The command will call" ); + out.println(" /// the proper processXXX method in the visitor." ); + out.println(" /// </summery>"); + out.println(" ///"); + out.println(" public override Response visit( ICommandVisitor visitor )" ); + out.println(" {"); + generateVisitBody(out); + out.println(" }"); out.println(""); } out.println(" };"); out.println(""); - out.println("}}"); + out.println("}"); out.println(""); - out.println("#endif /*_ACTIVEMQ_COMMANDS_"+getClassName().toUpperCase()+"_H_*/"); } - protected void populateIncludeFilesSet() { - - Set<String> includes = getIncludeFiles(); + protected void generateClassDefinition( PrintWriter out ) { + out.print(" public class "+getClassName()+" : " ); - includes.add("<vector>"); - includes.add("<string>"); - includes.add("<activemq/util/Config.h>"); - includes.add("<activemq/commands/"+getBaseClassName()+".h>"); - includes.add("<decaf/lang/Pointer.h>"); + out.print( getBaseClassName() ); - if( isComparable() ) { - includes.add("<decaf/lang/Comparable.h>"); + if( isMarshalAware() ) { + out.print( ", MarshallAware" ); } + out.println(); + out.println(" {"); + } + + protected void generateProperties( PrintWriter out ) { + for( JProperty property : getProperties() ) { - if( !property.getType().isPrimitiveType() && - !property.getType().getSimpleName().equals("String") && - !property.getType().getSimpleName().equals("ByteSequence") ) - { - - if( toHeaderFileName( property.getType() ) != null ) { - includes.add( - "<activemq/commands/"+toHeaderFileName( property.getType() )+".h>"); - } - } + String type = toCSharpType(property.getType()); + String name = decapitalize(property.getSimpleName()); + + out.println(" "+type+" "+name+";"); } } - protected void generateNamespaceWrapper( PrintWriter out ) { - out.println("namespace activemq{"); - out.println("namespace commands{"); - out.println(""); + protected void generateVisitBody( PrintWriter out ) { + out.println(" return visitor.process"+getClassName()+"( this );"); } - protected void generateForwardDeclarations( PrintWriter out ) { - } + protected void generateToStringBody( PrintWriter out ) { - protected void generateClassDefinition( PrintWriter out ) { - out.print(" class AMQCPP_API "+getClassName()+" : " ); + out.println(" return GetType().Name + \"[\" + "); - if( isComparable() ) { - out.println("public "+ getBaseClassName() +", public decaf::lang::Comparable<"+getClassName()+"> {" ); - } else { - out.print("public "+ getBaseClassName() +" {" ); - out.println(""); + for( JProperty property : getProperties() ) { + String name = property.getSimpleName(); + + out.println(" \"" + name + "=\" + " + name + " + " ); } + + out.println(" \"]\";"); } - protected void generateProperties( PrintWriter out ) { - out.println(" protected:"); + protected void generateCloneBody( PrintWriter out ) { + + out.println(" // Since we are a derived class use the base's Clone()"); + out.println(" // to perform the shallow copy. Since it is shallow it"); + out.println(" // will include our derived class. Since we are derived,"); + out.println(" // this method is an override."); + out.println(" " + getClassName() + " o = (" + getClassName() + ") base.Clone();"); out.println(""); for( JProperty property : getProperties() ) { - String type = toCppType(property.getType()); + + String type = toCSharpType(property.getType()); String name = decapitalize(property.getSimpleName()); - if( !property.getType().isPrimitiveType() && - !property.getType().getSimpleName().equals("ByteSequence") && - !property.getType().getSimpleName().equals("String") && - !type.startsWith("std::vector") ) { + if( property.getType().isArrayType() ) { - type = "Pointer<" + type + ">"; - } + } else if( !property.getType().isPrimitiveType() ) { - out.println(" "+type+" "+name+";"); - } + out.println(" if(o." + name + " != null)"); + out.println(" {"); + out.println(" o." + name + " = (" + type + ") o." + name + ".Clone();"); + out.println(" }"); + out.println(""); + } - out.println(""); - out.println(" public:"); - out.println(""); - out.println(" const static unsigned char ID_"+ - getClassName().toUpperCase()+" = "+getOpenWireOpCode()+";"); - out.println(""); - if( isComparable() ) { - out.println(" typedef decaf::lang::PointerComparator<"+getClassName()+"> COMPARATOR;"); - out.println(""); } } - protected void generateAdditionalConstructors( PrintWriter out ) { - } - protected void generateAdditonalMembers( PrintWriter out ) { } protected void generatePropertyAccessors( PrintWriter out ) { for( JProperty property : getProperties() ) { - String type = toCppType(property.getType()); - String propertyName = property.getSimpleName(); - String parameterName = decapitalize(propertyName); - String constness = ""; - - if( !property.getType().isPrimitiveType() && - !property.getType().getSimpleName().equals("ByteSequence") && - !property.getType().getSimpleName().equals("String") && - !type.startsWith("std::vector") ) { - - type = "Pointer<" + type + ">&"; - constness = "const "; - } else if( property.getType().getSimpleName().equals("String") || - type.startsWith("std::vector") ) { - - type = type + "&"; - constness = "const "; - } - - if( property.getType().isPrimitiveType() ) { - out.println(" virtual "+type+" "+property.getGetter().getSimpleName()+"() const;"); - } else { - out.println(" virtual const "+type+" "+property.getGetter().getSimpleName()+"() const;"); - out.println(" virtual "+type+" "+property.getGetter().getSimpleName()+"();"); - } - - out.println(" virtual void "+property.getSetter().getSimpleName()+"( "+constness+type+" "+parameterName+" );"); + String type = toCSharpType(property.getType()); + String accessorName = property.getSimpleName(); + String propertyName = decapitalize(accessorName); + + out.println(" public " + type + " " + accessorName ); + out.println(" {"); + out.println(" get { return " + propertyName + "; }" ); + out.println(" set { this." + propertyName + " = value; }"); + out.println(" }"); out.println(""); + } } diff --git a/src/main/java/org/apache/activemq/nms/openwire/tool/commands/CommandCodeGenerator.java b/src/main/java/org/apache/activemq/nms/openwire/tool/commands/CommandCodeGenerator.java index 4f01948..cbacec2 100644 --- a/src/main/java/org/apache/activemq/nms/openwire/tool/commands/CommandCodeGenerator.java +++ b/src/main/java/org/apache/activemq/nms/openwire/tool/commands/CommandCodeGenerator.java @@ -18,8 +18,6 @@ package org.apache.activemq.nms.openwire.tool.commands; import java.io.PrintWriter; import java.util.List; -import java.util.Set; -import java.util.TreeSet; import org.codehaus.jam.JClass; import org.codehaus.jam.JProperty; @@ -31,10 +29,7 @@ public abstract class CommandCodeGenerator { private boolean marshalAware = false; private String className; private String baseClassName; - private Set<String> includeFiles = new TreeSet<String>(); private String openWireOpCode; - private boolean comparable = false; - private boolean assignable = false; private boolean genIsClass = false; public abstract void generate( PrintWriter out ); @@ -67,22 +62,6 @@ public abstract class CommandCodeGenerator { this.genIsClass = genIsClass; } - public boolean isComparable() { - return comparable; - } - - public void setComparable(boolean comparable) { - this.comparable = comparable; - } - - public boolean isAssignable() { - return assignable; - } - - public void setAssignable(boolean assignable) { - this.assignable = assignable; - } - public JClass getJClass() { return jClass; } @@ -123,14 +102,6 @@ public abstract class CommandCodeGenerator { this.baseClassName = baseClassName; } - public Set<String> getIncludeFiles() { - return includeFiles; - } - - public void setIncludeFiles(Set<String> includeFiles) { - this.includeFiles = includeFiles; - } - public String getOpenWireOpCode() { return openWireOpCode; } @@ -139,63 +110,41 @@ public abstract class CommandCodeGenerator { this.openWireOpCode = openWireOpCode; } - protected String toHeaderFileName( JClass type ) { - String name = type.getSimpleName(); - - if( name.equals( "String" ) ) { - return null; - } else if( type.isArrayType() ) { - JClass arrayClass = type.getArrayComponentType(); - return toHeaderFileName( arrayClass ); - } else if( name.equals( "Throwable" ) || name.equals( "Exception" ) ) { - return "BrokerError"; - } else if( name.equals("BaseDataStructure" ) ){ - return "DataStructure"; - } else if( name.equals("ByteSequence") ) { - return "std::vector<unsigned char>"; - } else if( !type.isPrimitiveType() ) { - return name; - } else { - return null; - } - } - - protected String toCppType(JClass type) { +// protected String toFileName( JClass type ) { +// String name = type.getSimpleName(); +// +// if( name.equals( "String" ) ) { +// return null; +// } else if( type.isArrayType() ) { +// JClass arrayClass = type.getArrayComponentType(); +// return toHeaderFileName( arrayClass ); +// } else if( name.equals( "Throwable" ) || name.equals( "Exception" ) ) { +// return "BrokerError"; +// } else if( name.equals("BaseDataStructure" ) ){ +// return "DataStructure"; +// } else if( name.equals("ByteSequence") ) { +// return "std::vector<unsigned char>"; +// } else if( !type.isPrimitiveType() ) { +// return name; +// } else { +// return null; +// } +// } + + protected String toCSharpType(JClass type) { String name = type.getSimpleName(); if (name.equals("String")) { - return "std::string"; - } - else if( type.isArrayType() ) { - if( name.equals( "byte[]" ) ) - name = "unsigned char[]"; - - JClass arrayClass = type.getArrayComponentType(); - - if( arrayClass.isPrimitiveType() ) { - return "std::vector<" + name.substring( 0, name.length()-2 ) + ">"; - } else { - return "std::vector< decaf::lang::Pointer<" + - name.substring( 0, name.length()-2 ) + "> >"; - } + return "string"; } else if( name.equals( "Throwable" ) || name.equals( "Exception" ) ) { return "BrokerError"; } - else if( name.equals("BaseDataStructure" ) ){ - return "DataStructure"; - } else if( name.equals("ByteSequence") ) { - return "std::vector<unsigned char>"; + return "byte[]"; } else if( name.equals("boolean") ) { return "bool"; } - else if( name.equals("long") ) { - return "long long"; - } - else if( name.equals("byte") ) { - return "unsigned char"; - } else if( !type.isPrimitiveType() ) { return name; } @@ -205,9 +154,9 @@ public abstract class CommandCodeGenerator { } /** - * Converts the Java type to a C++ default value + * Converts the Java type to a CSharp default value */ - protected String toCppDefaultValue(JClass type) { + protected String toCSharpDefaultValue(JClass type) { String name = type.getSimpleName(); if (name.equals("boolean")) { @@ -215,7 +164,7 @@ public abstract class CommandCodeGenerator { } else if( name.equals("String") ) { return "\"\""; } else if( !type.isPrimitiveType() ) { - return "NULL"; + return "null"; } else { return "0"; } diff --git a/src/main/java/org/apache/activemq/nms/openwire/tool/commands/CommandCodeGeneratorsFactory.java b/src/main/java/org/apache/activemq/nms/openwire/tool/commands/CommandCodeGeneratorsFactory.java index 64e4402..95ae4ce 100644 --- a/src/main/java/org/apache/activemq/nms/openwire/tool/commands/CommandCodeGeneratorsFactory.java +++ b/src/main/java/org/apache/activemq/nms/openwire/tool/commands/CommandCodeGeneratorsFactory.java @@ -83,11 +83,6 @@ public class CommandCodeGeneratorsFactory { generator = new CommandClassGenerator(); // } - if( className.endsWith("Id") ) { - generator.setComparable( true ); - generator.setAssignable( true ); - } - if( this.commandsWithShortcuts.contains( className ) ) { generator.setGenIsClass( true ); } diff --git a/src/main/java/org/apache/activemq/nms/openwire/tool/commands/CommandGenerator.java b/src/main/java/org/apache/activemq/nms/openwire/tool/commands/CommandGenerator.java index fec0bfe..efe5a67 100644 --- a/src/main/java/org/apache/activemq/nms/openwire/tool/commands/CommandGenerator.java +++ b/src/main/java/org/apache/activemq/nms/openwire/tool/commands/CommandGenerator.java @@ -34,7 +34,7 @@ public class CommandGenerator extends MultiSourceGenerator { private CommandCodeGeneratorsFactory generatorsFactory = new CommandCodeGeneratorsFactory(); - protected String targetDir="./src/main"; + protected String targetDir="./src/main/csharp"; public String getTargetDir() { return targetDir; @@ -64,10 +64,10 @@ public class CommandGenerator extends MultiSourceGenerator { } public Object run() { - filePostFix = ".cpp"; + filePostFix = ".cs"; if (destDir == null) { destDir = new File( - targetDir+"/activemq/commands"); + targetDir+"/Commands"); } return super.run(); @@ -75,7 +75,7 @@ public class CommandGenerator extends MultiSourceGenerator { protected void generateClassFile() throws Exception { - File headerFile = new File(destDir, className + ".cs"); + File headerFile = new File(destDir, className + filePostFix); CommandCodeGenerator generator = generatorsFactory.getCodeGenerator( className );
