Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » openwire » tool » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.activemq.openwire.tool;
   18   
   19   import org.codehaus.jam.JAnnotation;
   20   import org.codehaus.jam.JAnnotationValue;
   21   import org.codehaus.jam.JClass;
   22   import org.codehaus.jam.JField;
   23   import org.codehaus.jam.JMethod;
   24   import org.codehaus.jam.JProperty;
   25   import org.codehaus.jam.JamClassIterator;
   26   import org.codehaus.jam.JamService;
   27   
   28   /**
   29    * @version $Revision$
   30    */
   31   public abstract class OpenWireGenerator {
   32   
   33       protected int openwireVersion;
   34       protected String filePostFix = ".java";
   35       protected JamService jam;
   36   
   37       public boolean isValidProperty(JProperty it) {
   38           JMethod getter = it.getGetter();
   39           return getter != null && it.getSetter() != null && !getter.isStatic() && getter.getAnnotation("openwire:property") != null;
   40       }
   41   
   42       public boolean isCachedProperty(JProperty it) {
   43           JMethod getter = it.getGetter();
   44           if (!isValidProperty(it)) {
   45               return false;
   46           }
   47           JAnnotationValue value = getter.getAnnotation("openwire:property").getValue("cache");
   48           return value != null && value.asBoolean();
   49       }
   50   
   51       public boolean isAbstract(JClass j) {
   52           JField[] fields = j.getFields();
   53           for (int i = 0; i < fields.length; i++) {
   54               JField field = fields[i];
   55               if (field.isStatic() && field.isPublic() && field.isFinal() && field.getSimpleName().equals("DATA_STRUCTURE_TYPE")) {
   56                   return false;
   57               }
   58           }
   59           return true;
   60       }
   61   
   62       public boolean isThrowable(JClass j) {
   63           if (j.getQualifiedName().equals(Throwable.class.getName())) {
   64               return true;
   65           }
   66           return j.getSuperclass() != null && isThrowable(j.getSuperclass());
   67       }
   68   
   69       public boolean isMarshallAware(JClass j) {
   70           if (filePostFix.endsWith("java")) {
   71               JClass[] interfaces = j.getInterfaces();
   72               for (int i = 0; i < interfaces.length; i++) {
   73                   if (interfaces[i].getQualifiedName().equals("org.apache.activemq.command.MarshallAware")) {
   74                       return true;
   75                   }
   76               }
   77               return false;
   78           } else {
   79               String simpleName = j.getSimpleName();
   80               return simpleName.equals("ActiveMQMessage") || simpleName.equals("WireFormatInfo");
   81           }
   82           /*
   83            * else { // is it a message type String simpleName = j.getSimpleName();
   84            * JClass superclass = j.getSuperclass(); return
   85            * simpleName.equals("ActiveMQMessage") || (superclass != null &&
   86            * superclass.getSimpleName().equals("ActiveMQMessage")); }
   87            */
   88       }
   89   
   90       public JamService getJam() {
   91           return jam;
   92       }
   93   
   94       public JamClassIterator getClasses() {
   95           return getJam().getClasses();
   96       }
   97   
   98       public int getOpenwireVersion() {
   99           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   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » openwire » tool » [javadoc | source]