001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.openwire.tool;
018
019import org.codehaus.jam.JAnnotation;
020import org.codehaus.jam.JAnnotationValue;
021import org.codehaus.jam.JClass;
022import org.codehaus.jam.JField;
023import org.codehaus.jam.JMethod;
024import org.codehaus.jam.JProperty;
025import org.codehaus.jam.JamClassIterator;
026import org.codehaus.jam.JamService;
027
028/**
029 * 
030 */
031public abstract class OpenWireGenerator {
032
033    protected int openwireVersion;
034    protected String filePostFix = ".java";
035    protected JamService jam;
036
037    public boolean isValidProperty(JProperty it) {
038        JMethod getter = it.getGetter();
039        return getter != null && it.getSetter() != null && !getter.isStatic() && getter.getAnnotation("openwire:property") != null;
040    }
041
042    public boolean isCachedProperty(JProperty it) {
043        JMethod getter = it.getGetter();
044        if (!isValidProperty(it)) {
045            return false;
046        }
047        JAnnotationValue value = getter.getAnnotation("openwire:property").getValue("cache");
048        return value != null && value.asBoolean();
049    }
050
051    public boolean isAbstract(JClass j) {
052        JField[] fields = j.getFields();
053        for (int i = 0; i < fields.length; i++) {
054            JField field = fields[i];
055            if (field.isStatic() && field.isPublic() && field.isFinal() && field.getSimpleName().equals("DATA_STRUCTURE_TYPE")) {
056                return false;
057            }
058        }
059        return true;
060    }
061
062    public boolean isThrowable(JClass j) {
063        if (j.getQualifiedName().equals(Throwable.class.getName())) {
064            return true;
065        }
066        return j.getSuperclass() != null && isThrowable(j.getSuperclass());
067    }
068
069    public boolean isMarshallAware(JClass j) {
070        if (filePostFix.endsWith("java")) {
071            JClass[] interfaces = j.getInterfaces();
072            for (int i = 0; i < interfaces.length; i++) {
073                if (interfaces[i].getQualifiedName().equals("org.apache.activemq.command.MarshallAware")) {
074                    return true;
075                }
076            }
077            return false;
078        } else {
079            String simpleName = j.getSimpleName();
080            return simpleName.equals("ActiveMQMessage") || simpleName.equals("WireFormatInfo");
081        }
082        /*
083         * else { // is it a message type String simpleName = j.getSimpleName();
084         * JClass superclass = j.getSuperclass(); return
085         * simpleName.equals("ActiveMQMessage") || (superclass != null &&
086         * superclass.getSimpleName().equals("ActiveMQMessage")); }
087         */
088    }
089
090    public JamService getJam() {
091        return jam;
092    }
093
094    public JamClassIterator getClasses() {
095        return getJam().getClasses();
096    }
097
098    public int getOpenwireVersion() {
099        return openwireVersion;
100    }
101
102    public void setOpenwireVersion(int openwireVersion) {
103        this.openwireVersion = openwireVersion;
104    }
105
106    /**
107     * Converts the Java type to a C# type name
108     */
109    public String toCSharpType(JClass type) {
110        String name = type.getSimpleName();
111        if (name.equals("String")) {
112            return "string";
113        } else if (name.equals("Throwable") || name.equals("Exception")) {
114            return "BrokerError";
115        } else if (name.equals("ByteSequence")) {
116            return "byte[]";
117        } else if (name.equals("boolean")) {
118            return "bool";
119        } else {
120            return name;
121        }
122    }
123
124    public String getOpenWireOpCode(JClass element) {
125        if (element != null) {
126            JAnnotation annotation = element.getAnnotation("openwire:marshaller");
127            return stringValue(annotation, "code", "0");
128        }
129        return "0";
130    }
131
132    protected String stringValue(JAnnotation annotation, String name) {
133        return stringValue(annotation, name, null);
134    }
135
136    protected String stringValue(JAnnotation annotation, String name, String defaultValue) {
137        if (annotation != null) {
138            JAnnotationValue value = annotation.getValue(name);
139            if (value != null) {
140                return value.asString();
141            }
142        }
143        return defaultValue;
144    }
145
146    public void setJam(JamService jam) {
147        this.jam = jam;
148    }
149
150    public String decapitalize(String text) {
151        if (text == null) {
152            return null;
153        }
154        return text.substring(0, 1).toLowerCase() + text.substring(1);
155    }
156
157    public String capitalize(String text) {
158        if (text == null) {
159            return null;
160        }
161        return text.substring(0, 1).toUpperCase() + text.substring(1);
162    }
163}