Skip to content
Permalink
Browse files
Commands Generator, produces CSharp Openwire Command classes with mos…
…tly all the same functionality as the older hand hacked ones, plus all the correct Openwire Protocol values for v1-v5
  • Loading branch information
Timothy A. Bish committed Jul 29, 2009
1 parent c31b80e commit 3b257ca54bba89e1d4bdda8bbb66ca80e185b09c
Showing 17 changed files with 725 additions and 116 deletions.
@@ -29,7 +29,7 @@

public class AmqCSharpGeneratorTask extends Task {

int maxVersion = 3;
int maxVersion = 5;
File source = new File(".");
File target = new File(".");

@@ -38,7 +38,6 @@ public void generate( PrintWriter out ) {
out.println("");
out.println("namespace Apache.NMS.ActiveMQ.Commands");
out.println("{");
out.println("");
out.println(" /*");
out.println(" *");
out.println(" * Command code for OpenWire format for "+getClassName() );
@@ -56,6 +55,10 @@ public void generate( PrintWriter out ) {

generateProperties( out );

out.println("");

generateConstructors(out);

out.println("");
out.println(" ///");
out.println(" /// <summery>");
@@ -68,16 +71,20 @@ public void generate( PrintWriter out ) {
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("");

if( isCloneable() ) {
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");
@@ -93,7 +100,30 @@ public void generate( PrintWriter out ) {
generateAdditonalMembers( out );
generatePropertyAccessors( out );

if( isGenIsClass() ) {
if( isComparable() ) {

out.println(" public override int GetHashCode()");
out.println(" {");
generateHashCodeBody(out);
out.println(" }");
out.println("");
out.println(" public override bool Equals(object that)");
out.println(" {");
out.println(" if(that is "+getClassName()+")");
out.println(" {");
out.println(" return Equals(("+getClassName()+") that);");
out.println(" }");
out.println(" return false;");
out.println(" }");
out.println("");
out.println(" public virtual bool Equals("+getClassName()+" that)");
out.println(" {");
generateEqualsBody(out);
out.println(" }");

}

if( getBaseClassName().equals( "BaseCommand" ) ) {
out.println(" ///");
out.println(" /// <summery>");
out.println(" /// Return an answer of true to the is"+getClassName()+"() query.");
@@ -117,7 +147,7 @@ public void generate( PrintWriter out ) {
out.println(" /// the proper processXXX method in the visitor." );
out.println(" /// </summery>");
out.println(" ///");
out.println(" public override Response visit( ICommandVisitor visitor )" );
out.println(" public override Response visit(ICommandVisitor visitor)" );
out.println(" {");
generateVisitBody(out);
out.println(" }");
@@ -135,14 +165,26 @@ protected void generateClassDefinition( PrintWriter out ) {

out.print( getBaseClassName() );

for( String name : getAdditionalBases() ) {
out.print( ", "+name );
}

if( isMarshalAware() ) {
out.print( ", MarshallAware" );
}

if( isCloneable() ) {
out.print( ", ICloneable" );
}

out.println();
out.println(" {");
}

protected void generateConstructors( PrintWriter out ) {
// The default constructor is fine for basic Commands.
}

protected void generateProperties( PrintWriter out ) {

for( JProperty property : getProperties() ) {
@@ -178,24 +220,7 @@ protected void generateCloneBody( PrintWriter out ) {
out.println(" // this method is an override.");
out.println(" " + getClassName() + " o = (" + getClassName() + ") base.Clone();");
out.println("");

for( JProperty property : getProperties() ) {

String type = toCSharpType(property.getType());
String name = decapitalize(property.getSimpleName());

if( property.getType().isArrayType() ) {

} else if( !property.getType().isPrimitiveType() ) {

out.println(" if(o." + name + " != null)");
out.println(" {");
out.println(" o." + name + " = (" + type + ") o." + name + ".Clone();");
out.println(" }");
out.println("");
}

}
out.println(" return o;");
}

protected void generateAdditonalMembers( PrintWriter out ) {
@@ -218,4 +243,33 @@ protected void generatePropertyAccessors( PrintWriter out ) {
}
}

protected void generateHashCodeBody( PrintWriter out ) {
out.println(" int answer = 0;");
out.println("");

for( JProperty property : getProperties() ) {
String accessorName = property.getSimpleName();

out.println(" answer = (answer * 37) + HashCode("+accessorName+");");
}

out.println("");
out.println(" return answer;");
}

protected void generateEqualsBody( PrintWriter out ) {

for( JProperty property : getProperties() ) {
String accessorName = property.getSimpleName();

out.println(" if(!Equals(this."+accessorName+", that."+accessorName+"))");
out.println(" {");
out.println(" return false;");
out.println(" }");
}

out.println("");
out.println(" return true;");
}

}
@@ -18,6 +18,8 @@

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;
@@ -26,11 +28,13 @@ public abstract class CommandCodeGenerator {

private JClass jClass;
private List<JProperty> properties;
private Set<String> additionalBases = new TreeSet<String>();
private boolean marshalAware = false;
private String className;
private String baseClassName;
private String openWireOpCode;
private boolean genIsClass = false;
private boolean cloneable;
private boolean comparable;

public abstract void generate( PrintWriter out );

@@ -54,14 +58,6 @@ protected void generateLicence(PrintWriter out) {
out.println("");
}

public boolean isGenIsClass() {
return genIsClass;
}

public void setGenIsClass(boolean genIsClass) {
this.genIsClass = genIsClass;
}

public JClass getJClass() {
return jClass;
}
@@ -78,6 +74,14 @@ public void setProperties(List<JProperty> properties) {
this.properties = properties;
}

public boolean isComparable() {
return comparable;
}

public void setComparable(boolean comparable) {
this.comparable = comparable;
}

public boolean isMarshalAware() {
return marshalAware;
}
@@ -86,6 +90,14 @@ public void setMarshalAware(boolean marshalAware) {
this.marshalAware = marshalAware;
}

public boolean isCloneable() {
return cloneable;
}

public void setCloneable(boolean cloneable) {
this.cloneable = cloneable;
}

public String getClassName() {
return className;
}
@@ -110,26 +122,17 @@ public void setOpenWireOpCode(String openWireOpCode) {
this.openWireOpCode = openWireOpCode;
}

// 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;
// }
// }
public void addAdditionalBase( String name ) {
this.additionalBases.add(name);
}

public Set<String> getAdditionalBases() {
return additionalBases;
}

public void setAdditionalBases(Set<String> additionalBases) {
this.additionalBases = additionalBases;
}

protected String toCSharpType(JClass type) {
String name = type.getSimpleName();
@@ -16,9 +16,6 @@
*/
package org.apache.activemq.nms.openwire.tool.commands;

import java.util.HashSet;
import java.util.Set;

/**
* Provides methods to get a Source file and Header file Code generator given a Class
* name.
@@ -27,35 +24,6 @@
*/
public class CommandCodeGeneratorsFactory {

private Set<String> commandsWithShortcuts;

/*
* Here we store all Commands that need to have a isXXX method generated
* such as isMessage. We then check in the <code>checkNeedsShortcut</code>
* method and if the Command being generated is in this list we create a
* method call to override the virtual method in the base Command interface.
*/
{
commandsWithShortcuts = new HashSet<String>();
commandsWithShortcuts.add( "Response" );
commandsWithShortcuts.add( "RemoveInfo" );
commandsWithShortcuts.add( "MessageDispatch" );
commandsWithShortcuts.add( "BrokerInfo" );
commandsWithShortcuts.add( "KeepAliveInfo" );
commandsWithShortcuts.add( "WireFormatInfo" );
commandsWithShortcuts.add( "Message" );
commandsWithShortcuts.add( "MessageAck" );
commandsWithShortcuts.add( "ProducerAck" );
commandsWithShortcuts.add( "ProducerInfo" );
commandsWithShortcuts.add( "MessageDispatchNotification" );
commandsWithShortcuts.add( "ShutdownInfo" );
commandsWithShortcuts.add( "TransactionInfo" );
commandsWithShortcuts.add( "ConnectionInfo" );
commandsWithShortcuts.add( "ConsumerInfo" );
commandsWithShortcuts.add( "RemoveSubscriptionInfo" );

}

/**
* Given a class name return an instance of a CSharp Class File Generator
* that can generate the file for the Class.
@@ -66,25 +34,23 @@ public class CommandCodeGeneratorsFactory {
*/
public CommandCodeGenerator getCodeGenerator( String className ) {

CommandCodeGenerator generator = null;
// if( className.equals("Message") ) {
// generator = new MessageHeaderGenerator();
// } else if( className.equals("ConnectionId") ) {
// generator = new ConnectionIdHeaderGenerator();
// } else if( className.equals("ConsumerId") ) {
// generator = new ConsumerIdHeaderGenerator();
// } else if( className.equals("ProducerId") ) {
// generator = new ProducerIdHeaderGenerator();
// } else if( className.equals("SessionId") ) {
// generator = new SessionIdHeaderGenerator();
// } else if( className.equals("SessionInfo") ) {
// generator = new SessionInfoHeaderGenerator();
// } else {
generator = new CommandClassGenerator();
// }
Class<?> generatorClass = null;

try {
generatorClass = Class.forName( "org.apache.activemq.nms.openwire.tool.commands." + className + "Generator" );
} catch (ClassNotFoundException e) {
return new CommandClassGenerator();
}

if( this.commandsWithShortcuts.contains( className ) ) {
generator.setGenIsClass( true );
CommandCodeGenerator generator;
try {
generator = (CommandCodeGenerator) generatorClass.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
return null;
} catch (IllegalAccessException e) {
e.printStackTrace();
return null;
}

return generator;

0 comments on commit 3b257ca

Please sign in to comment.