1 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 32 public abstract class OpenWireGenerator { 33 34 protected int openwireVersion; 35 protected String 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 .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 simpleName = j.getSimpleName(); 81 return simpleName.equals("ActiveMQMessage") || simpleName.equals("WireFormatInfo"); 82 } 83 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 110 public String toCSharpType(JClass type) { 111 String 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 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 stringValue(JAnnotation annotation, String name) { 138 return stringValue(annotation, name, null); 139 } 140 141 protected String stringValue(JAnnotation annotation, String name, String 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 decapitalize(String text) { 156 if (text == null) { 157 return null; 158 } 159 return text.substring(0, 1).toLowerCase() + text.substring(1); 160 } 161 162 public String capitalize(String text) { 163 if (text == null) { 164 return null; 165 } 166 return text.substring(0, 1).toUpperCase() + text.substring(1); 167 } 168 } 169 | Popular Tags |