added the autogeneration of the C++ headers and cpp files and refactored the class generation scripts so that they are easier to work with

git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@379734 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
James Strachan 2006-02-22 09:35:03 +00:00
parent 277f3b3d5a
commit 6f82a857c7
7 changed files with 369 additions and 25 deletions

View File

@ -16,13 +16,13 @@
*/
package org.apache.activemq.openwire.tool;
import mx4j.tools.adaptor.http.GetAttributeCommandProcessor;
import org.codehaus.jam.JClass;
import org.codehaus.jam.JProperty;
import org.codehaus.jam.JamClassIterator;
import java.io.File;
import java.io.*;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.*;
/**
@ -66,18 +66,33 @@ public abstract class OpenWireClassesScript extends OpenWireScript {
return null;
}
/**
* Returns all the valid properties available on the current class
*/
public List getProperties() {
List answer = new ArrayList();
JProperty[] properties = jclass.getDeclaredProperties();
for (int i = 0; i < properties.length; i++) {
JProperty property = properties[i];
if (isValidProperty(property)) {
answer.add(property);
}
}
return answer;
}
protected boolean isValidClass(JClass jclass) {
if (jclass.getAnnotation("openwire:marshaller") == null) {
return false;
}
return manuallyMaintainedClasses.contains(jclass.getSimpleName());
return !manuallyMaintainedClasses.contains(jclass.getSimpleName());
}
protected void processClass(JClass jclass) {
simpleName = jclass.getSimpleName();
superclass = jclass.getSuperclass();
System.out.println("Processing class: " + simpleName);
System.out.println(getClass().getName() + " processing class: " + simpleName);
className = getClassName(jclass);
@ -88,6 +103,7 @@ public abstract class OpenWireClassesScript extends OpenWireScript {
PrintWriter out = null;
try {
out = new PrintWriter(new FileWriter(destFile));
generateFile(out);
}
catch (Exception e) {
throw new RuntimeException(e);
@ -113,7 +129,7 @@ public abstract class OpenWireClassesScript extends OpenWireScript {
}
protected String getClassName(JClass jclass) {
return "AbstractCommand";
return jclass.getSimpleName();
}
}

View File

@ -0,0 +1,70 @@
/**
*
* Copyright 2005-2006 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.activemq.openwire.tool;
import org.codehaus.jam.JClass;
import java.io.File;
/**
*
* @version $Revision$
*/
public abstract class OpenWireCppClassesScript extends OpenWireClassesScript {
public Object run() {
filePostFix = getFilePostFix();
destDir = new File("../openwire-cpp/src/command");
return super.run();
}
protected String getFilePostFix() {
return ".cpp";
}
/**
* Converts the Java type to a C++ type name
*/
public String toCppType(JClass type) {
String name = type.getSimpleName();
if (name.equals("String")) {
return "p<string>";
}
else if (name.equals("Throwable") || name.equals("Exception")) {
return "BrokerError";
}
else if (name.equals("ByteSequence")) {
return "void*";
}
else if (name.equals("boolean")) {
return "bool";
}
else if (name.endsWith("Id")) {
return "p<" + name + ">";
}
else {
return name;
}
}
/**
* Converts the Java type to a C++ default value
*/
public String toCppDefaultValue(JClass type) {
return "NULL";
}
}

View File

@ -0,0 +1,30 @@
/**
*
* Copyright 2005-2006 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.activemq.openwire.tool;
/**
*
* @version $Revision$
*/
public abstract class OpenWireCppHeadersScript extends OpenWireCppClassesScript {
protected String getFilePostFix() {
return ".hpp";
}
}

View File

@ -17,7 +17,6 @@
package org.apache.activemq.openwire.tool;
import org.codehaus.gram.GramSupport;
import org.codehaus.jam.JAnnotation;
import org.codehaus.jam.JAnnotationValue;
import org.codehaus.jam.JClass;
import org.codehaus.jam.JField;
@ -103,6 +102,8 @@ public abstract class OpenWireScript extends GramSupport {
return name;
}
}
public String getOpenWireOpCode(JClass aClass) {
return annotationValue(aClass, "openwire:marshaller", "code", "0");
}

View File

@ -17,21 +17,29 @@
import org.apache.activemq.openwire.tool.OpenWireCSharpClassesScript
/**
* Generates the C# marshalling code for the Open Wire Format
* Generates the C# commands for the Open Wire Format
*
* @version $Revision$
*/
class GenerateCSharpClasses extends OpenWireCSharpClassesScript {
void generateFile(PrintWriter out) {
out << """//
// Marshalling code for Open Wire Format for ${jclass.simpleName}
//
//
// NOTE!: This file is autogenerated - do not modify!
// if you need to make a change, please see the Groovy scripts in the
// activemq-openwire module
//
out << """/*
* Copyright 2006 The Apache Software Foundation or its licensors, as
* applicable.
*
* 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.
*/
using System;
using System.Collections;
@ -41,13 +49,20 @@ using OpenWire.Client.Core;
namespace OpenWire.Client.Commands
{
//
// Marshalling code for Open Wire Format for ${jclass.simpleName}
//
//
// NOTE!: This file is autogenerated - do not modify!
// if you need to make a change, please see the Groovy scripts in the
// activemq-core module
//
public class ${jclass.simpleName} : $baseClass
{
public const byte ID_${jclass.simpleName} = ${getOpenWireOpCode(jclass)};
"""
for (property in properties) {
def type = toCSharpType(property.type)
def name = decapitalize(property.simpleName)
out << """ $type $name;
@ -56,7 +71,6 @@ namespace OpenWire.Client.Commands
out << """
// TODO generate Equals method
// TODO generate GetHashCode method
// TODO generate ToString method

View File

@ -0,0 +1,92 @@
/**
*
* Copyright 2005-2006 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.
*/
import org.apache.activemq.openwire.tool.OpenWireCppClassesScript
/**
* Generates the C++ commands for the Open Wire Format
*
* @version $Revision$
*/
class GenerateCppClasses extends OpenWireCppClassesScript {
void generateFile(PrintWriter out) {
out << """/*
* Copyright 2006 The Apache Software Foundation or its licensors, as
* applicable.
*
* 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.
*/
#include "command/${className}.hpp"
using namespace apache::activemq::client::command;
/*
*
* Marshalling code for Open Wire Format for ${className}
*
*
* NOTE!: This file is autogenerated - do not modify!
* if you need to make a change, please see the Groovy scripts in the
* activemq-core module
*
*/
${className}::${className}()
{"""
for (property in properties) {
def value = toCppDefaultValue(property.type)
def propertyName = property.simpleName
def parameterName = decapitalize(propertyName)
out << """
this->${parameterName} = ${value} ;"""
}
out << """
}
${className}::~${className}()
{
}
"""
for (property in properties) {
def type = toCppType(property.type)
def propertyName = property.simpleName
def parameterName = decapitalize(propertyName)
out << """
${type} ${className}::get${propertyName}()
{
return ${parameterName} ;
}
void ${className}::set${propertyName}(${type} ${parameterName})
{
this->${parameterName} = ${parameterName} ;
}
"""
}
}
}

View File

@ -0,0 +1,121 @@
/**
*
* Copyright 2005-2006 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.
*/
import org.apache.activemq.openwire.tool.OpenWireCppHeadersScript
/**
* Generates the C++ commands for the Open Wire Format
*
* @version $Revision$
*/
class GenerateCppHeaders extends OpenWireCppHeadersScript {
void generateFile(PrintWriter out) {
out << """/*
* Copyright 2006 The Apache Software Foundation or its licensors, as
* applicable.
*
* 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.
*/
#ifndef ${className}_hpp_
#define ${className}_hpp_
#include <string>
/* we could cut this down - for now include all possible headers */
#include "command/BaseCommand.hpp"
#include "command/BrokerId.hpp"
#include "command/ConnectionId.hpp"
#include "command/ConsumerId.hpp"
#include "command/ProducerId.hpp"
#include "command/SessionId.hpp"
#include "command/${baseClass}.hpp"
#include "util/ifr/p"
namespace apache
{
namespace activemq
{
namespace client
{
namespace command
{
using namespace ifr;
using namespace apache::activemq::client;
/*
*
* Marshalling code for Open Wire Format for ${className}
*
*
* NOTE!: This file is autogenerated - do not modify!
* if you need to make a change, please see the Groovy scripts in the
* activemq-core module
*
*/
class ${className} : public ${baseClass}
{
private:
"""
for (property in properties) {
def type = toCppType(property.type)
def name = decapitalize(property.simpleName)
out << """ $type $name ;
"""
}
out << """
public:
const static int TYPE = ${getOpenWireOpCode(jclass)};
public:
${className}() ;
virtual ~${className}() ;
"""
for (property in properties) {
def type = toCppType(property.type)
def propertyName = property.simpleName
def parameterName = decapitalize(propertyName)
out << """
virtual ${type} get${propertyName}() ;
virtual void set${propertyName}(${type} ${parameterName}) ;
"""
}
out << """
} ;
/* namespace */
}
}
}
}
#endif /*${className}_hpp_*/
"""
}
}