KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > openwire > tool > OpenWireGenerator


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.openwire.tool;
19
20 import org.codehaus.jam.JAnnotation;
21 import org.codehaus.jam.JAnnotationValue;
22 import org.codehaus.jam.JClass;
23 import org.codehaus.jam.JField;
24 import org.codehaus.jam.JMethod;
25 import org.codehaus.jam.JProperty;
26 import org.codehaus.jam.JamClassIterator;
27 import org.codehaus.jam.JamService;
28
29 /**
30  * @version $Revision$
31  */

32 public abstract class OpenWireGenerator {
33
34     protected int openwireVersion;
35     protected String JavaDoc filePostFix = ".java";
36     protected JamService jam;
37     
38
39     public boolean isValidProperty(JProperty it) {
40         JMethod getter = it.getGetter();
41         return getter != null && it.getSetter() != null && getter.isStatic() == false && getter.getAnnotation("openwire:property") != null;
42     }
43
44     public boolean isCachedProperty(JProperty it) {
45         JMethod getter = it.getGetter();
46         if (!isValidProperty(it))
47             return false;
48         JAnnotationValue value = getter.getAnnotation("openwire:property").getValue("cache");
49         return value != null && value.asBoolean();
50     }
51
52     public boolean isAbstract(JClass j) {
53         JField[] fields = j.getFields();
54         for (int i = 0; i < fields.length; i++) {
55             JField field = fields[i];
56             if (field.isStatic() && field.isPublic() && field.isFinal() && field.getSimpleName().equals("DATA_STRUCTURE_TYPE")) {
57                 return false;
58             }
59         }
60         return true;
61     }
62
63     public boolean isThrowable(JClass j) {
64         if (j.getQualifiedName().equals(Throwable JavaDoc.class.getName())) {
65             return true;
66         }
67         return j.getSuperclass() != null && isThrowable(j.getSuperclass());
68     }
69
70     public boolean isMarshallAware(JClass j) {
71         if (filePostFix.endsWith("java")) {
72             JClass[] interfaces = j.getInterfaces();
73             for (int i = 0; i < interfaces.length; i++) {
74                 if (interfaces[i].getQualifiedName().equals("org.apache.activemq.command.MarshallAware")) {
75                     return true;
76                 }
77             }
78             return false;
79         } else {
80             String JavaDoc simpleName = j.getSimpleName();
81             return simpleName.equals("ActiveMQMessage") || simpleName.equals("WireFormatInfo");
82         }
83         /*
84          * else { // is it a message type String simpleName = j.getSimpleName();
85          * JClass superclass = j.getSuperclass(); return
86          * simpleName.equals("ActiveMQMessage") || (superclass != null &&
87          * superclass.getSimpleName().equals("ActiveMQMessage")); }
88          */

89     }
90
91     public JamService getJam() {
92         return jam;
93     }
94
95     public JamClassIterator getClasses() {
96         return getJam().getClasses();
97     }
98
99     public int getOpenwireVersion() {
100         return openwireVersion;
101     }
102
103     public void setOpenwireVersion(int openwireVersion) {
104         this.openwireVersion = openwireVersion;
105     }
106
107     /**
108      * Converts the Java type to a C# type name
109      */

110     public String JavaDoc toCSharpType(JClass type) {
111         String JavaDoc name = type.getSimpleName();
112         if (name.equals("String")) {
113             return "string";
114         }
115         else if (name.equals("Throwable") || name.equals("Exception")) {
116             return "BrokerError";
117         }
118         else if (name.equals("ByteSequence")) {
119             return "byte[]";
120         }
121         else if (name.equals("boolean")) {
122             return "bool";
123         }
124         else {
125             return name;
126         }
127     }
128
129     public String JavaDoc getOpenWireOpCode(JClass element) {
130         if (element != null) {
131             JAnnotation annotation = element.getAnnotation("openwire:marshaller");
132             return stringValue(annotation, "code", "0");
133         }
134         return "0";
135     }
136     
137     protected String JavaDoc stringValue(JAnnotation annotation, String JavaDoc name) {
138         return stringValue(annotation, name, null);
139     }
140     
141     protected String JavaDoc stringValue(JAnnotation annotation, String JavaDoc name, String JavaDoc defaultValue) {
142         if (annotation != null) {
143             JAnnotationValue value = annotation.getValue(name);
144             if (value != null) {
145                 return value.asString();
146             }
147         }
148         return defaultValue;
149     }
150
151     public void setJam(JamService jam) {
152         this.jam = jam;
153     }
154     
155     public String JavaDoc decapitalize(String JavaDoc text) {
156         if (text == null) {
157             return null;
158         }
159         return text.substring(0, 1).toLowerCase() + text.substring(1);
160     }
161
162     public String JavaDoc capitalize(String JavaDoc text) {
163         if (text == null) {
164             return null;
165         }
166         return text.substring(0, 1).toUpperCase() + text.substring(1);
167     }
168 }
169
Popular Tags