xmlbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From z...@apache.org
Subject cvs commit: xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/bts JavaTypeName.java
Date Tue, 04 May 2004 01:34:58 GMT
zieg        2004/05/03 18:34:58

  Modified:    v2/src/binding/org/apache/xmlbeans/impl/binding/bts
                        JavaTypeName.java
  Log:
  just parse the string in JavaTypeName.forClassName instead of using jam
  
  Revision  Changes    Path
  1.13      +350 -272  xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/bts/JavaTypeName.java
  
  Index: JavaTypeName.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/bts/JavaTypeName.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- JavaTypeName.java	17 Apr 2004 23:47:23 -0000	1.12
  +++ JavaTypeName.java	4 May 2004 01:34:58 -0000	1.13
  @@ -14,11 +14,9 @@
    */
   package org.apache.xmlbeans.impl.binding.bts;
   
  -import org.apache.xmlbeans.impl.jam.JClass;
   import org.apache.xmlbeans.XmlObject;
  -import org.apache.xmlbeans.XmlRuntimeException;
  +import org.apache.xmlbeans.impl.jam.JClass;
   
  -import java.io.IOException;
   import java.lang.reflect.Array;
   
   /**
  @@ -36,280 +34,360 @@
    *
    * "x=org.apache.xmlbeans.XmlInt"
    */
  -public final class JavaTypeName {
  +public final class JavaTypeName
  +{
  +
  +    // ========================================================================
  +    // Constants
  +
  +    private static String XMLOBJECT_CLASSNAME = XmlObject.class.getName();
  +
  +    // ========================================================================
  +    // Variables
  +
  +    private final String className;
  +    private final String arrayString;
  +    private final boolean isXmlObject;
  +
  +    // ========================================================================
  +    // Factories
  +
  +    /**
  +     * Returns a JavaTypeName object for a fully-qualified class name.
  +     * The class-name should be dot-separated for packages and
  +     * dollar-separated for inner classes.
  +     *
  +     * The names "int", "byte" etc are considered special.
  +     * Arrays are dealt with as [][][] at the end.
  +     *
  +     * This is a static function to permit pooling in the future.
  +     */
  +    public static JavaTypeName forString(String className)
  +    {
  +        return new JavaTypeName(className);
  +    }
  +
  +
  +    /**
  +     * Builds a JavaTypeName for the array containing items with
  +     * the given JavaTypeName.
  +     */
  +    public static JavaTypeName forArray(JavaTypeName itemType, int depth)
  +    {
  +        // efficiency later
  +        String arrayBrackets = "";
  +        while (depth-- > 0)
  +            arrayBrackets += "[]";
  +        return forString(itemType.toString() + arrayBrackets);
  +    }
  +
  +    /**
  +     * Builds a JavaTypeName for the given JClass.
  +     */
  +    public static JavaTypeName forJClass(JClass jClass)
  +    {
  +        if (jClass.isArrayType()) {
  +            return forArray(forJClass(jClass.getArrayComponentType()),
  +                            jClass.getArrayDimensions());
  +        }
  +        JClass[] interfaces = jClass.getInterfaces();
  +        for (int i = 0; i < interfaces.length; i++) {
  +            if (interfaces[i].getQualifiedName().equals(XMLOBJECT_CLASSNAME))
  +                return forString("x=" + jClass.getQualifiedName());
  +        }
  +
  +        return forString(jClass.getQualifiedName());
  +    }
  +
  +
   
  -  // ========================================================================
  -  // Constants
   
  -  private static String XMLOBJECT_CLASSNAME = XmlObject.class.getName();
  +    // ========================================================================
  +    // Constructors
   
  -  // ========================================================================
  -  // Variables
  -
  -  private final String className;
  -  private final String arrayString;
  -  private final boolean isXmlObject;
  -
  -  // ========================================================================
  -  // Factories
  -
  -  /**
  -   * Returns a JavaTypeName object for a fully-qualified class name.
  -   * The class-name should be dot-separated for packages and
  -   * dollar-separated for inner classes.
  -   *
  -   * The names "int", "byte" etc are considered special.
  -   * Arrays are dealt with as [][][] at the end.
  -   *
  -   * This is a static function to permit pooling in the future.
  -   */
  -  public static JavaTypeName forString(String className) {
  -    return new JavaTypeName(className);
  -  }
  -
  -
  -  /**
  -   * Builds a JavaTypeName for the array containing items with
  -   * the given JavaTypeName.
  -   */
  -  public static JavaTypeName forArray(JavaTypeName itemType, int depth) {
  -    // efficiency later
  -    String arrayBrackets = "";
  -    while (depth-- > 0)
  -      arrayBrackets += "[]";
  -    return forString(itemType.toString() + arrayBrackets);
  -  }
  -
  -  /**
  -   * Builds a JavaTypeName for the given JClass.
  -   */
  -  public static JavaTypeName forJClass(JClass jClass) {
  -    if (jClass.isArrayType()) {
  -      return forArray(forJClass(jClass.getArrayComponentType()),
  -                      jClass.getArrayDimensions());
  -    }
  -    JClass[] interfaces = jClass.getInterfaces();
  -    for (int i = 0; i < interfaces.length; i++) {
  -      if (interfaces[i].getQualifiedName().equals(XMLOBJECT_CLASSNAME))
  -        return forString("x=" + jClass.getQualifiedName());
  -    }
  -
  -    return forString(jClass.getQualifiedName());
  -  }
  -
  -
  -
  -
  -  // ========================================================================
  -  // Constructors
  -
  -  /**
  -   * Do not use this constructor; use forClassName instead.
  -   */
  -  private JavaTypeName(String className) {
  -    if (className == null)
  -      throw new IllegalArgumentException();
  -
  -    if (className.startsWith("x=")) {
  -      this.isXmlObject = true;
  -      className = className.substring(2);
  -    } else {
  -      this.isXmlObject = false;
  -    }
  -
  -    int arrayDepth = 0;
  -    for (int i = className.length() - 2; i >= 0; i -= 2) {
  -      if (className.charAt(i) != '[' || className.charAt(i + 1) != ']')
  -        break;
  -      arrayDepth += 1;
  -    }
  -    this.className = className.substring(0, className.length() - 2 * arrayDepth);
  -    this.arrayString = className.substring(className.length() - 2 * arrayDepth);
  -  }
  -
  -  // ========================================================================
  -  // Public methods
  -
  -  /**
  -   * True for classnames that are XmlObjects.
  -   */
  -  public boolean isXmlObject() { return isXmlObject; }
  -
  -  /**
  -   * Returns the array depth, 0 for non-arrays, 1 for [], 2 for [][], etc.
  -   */
  -  public int getArrayDepth() { return arrayString.length() / 2; }
  -
  -  /**
  -   * Returns the array item type (peeling off "n" array indexes)
  -   */
  -  public JavaTypeName getArrayItemType(int depth) {
  -    if (arrayString.length() < depth * 2)
  -      return null;
  -    return forString(className + arrayString.substring(0, arrayString.length() - 2 * depth));
  -  }
  -
  -  /**
  -   * Returns the dot-separated package name.
  -   */
  -  public String getPackage() {
  -    int index = className.lastIndexOf('.');
  -    if (index <= 0)
  -      return "";
  -    return className.substring(0, index);
  -  }
  -
  -  /**
  -   * Returns the array string, peeling off the first depth levels
  -   */
  -  public String getArrayString(int depth) {
  -    if (arrayString.length() < depth * 2)
  -      return null;
  -    return arrayString.substring(2 * depth, arrayString.length());
  -  }
  -
  -  /**
  -   * True if this is an inner class name.
  -   */
  -  public boolean isInnerClass() {
  -    return (className.lastIndexOf('$') >= 0);
  -  }
  -
  -  /**
  -   * Returns the JavaTypeName of the containing class, or null if this
  -   * is not an inner class name.
  -   */
  -  public JavaTypeName getContainingClass() {
  -    int index = className.lastIndexOf('$');
  -    if (index < 0)
  -      return null;
  -    return JavaTypeName.forString(className.substring(0, index));
  -  }
  -
  -  /**
  -   * Returns the short class name (i.e., no dots or dollars).
  -   */
  -  public String getShortClassName() {
  -    int index = className.lastIndexOf('$');
  -    int index2 = className.lastIndexOf('.');
  -    if (index2 > index)
  -      index = index2;
  -    if (index < 0)
  -      return className;
  -    return className.substring(index + 1);
  -  }
  -
  -
  -  // ========================================================================
  -  // Object implementation
  -
  -  /**
  -   * Returns the fully-qualified class name.
  -   */
  -  public String toString() {
  -    if (isXmlObject)
  -      return "x=" + className + arrayString;
  -    return className + arrayString;
  -  }
  -
  -  public boolean equals(Object o) {
  -    if (this == o) return true;
  -    if (!(o instanceof JavaTypeName)) return false;
  -
  -    final JavaTypeName javaName = (JavaTypeName) o;
  -
  -    if (isXmlObject != javaName.isXmlObject) return false;
  -    if (!className.equals(javaName.className)) return false;
  -    if (!arrayString.equals(javaName.arrayString)) return false;
  -
  -    return true;
  -  }
  -
  -  public int hashCode() {
  -    return className.hashCode() + arrayString.length() + (isXmlObject ? 1 : 0);
  -  }
  -
  -
  -  // ========================================================================
  -  // Deprecated methods
  -
  -  /**
  -   * Loads the class represented by this JavaTypeName in the given
  -   * ClassLoader.  This is really horrible - the impedance mismatch
  -   * in the naming here is very really painful.  Need to do something better.
  -   */
  -  /**
  -   * @deprecated
  -   */
  -  public Class loadClassIn(ClassLoader loader) throws ClassNotFoundException {
  -    int d = getArrayDepth();
  -    if (d == 0) {
  -      String s = toString();
  -      Class out = org.apache.xmlbeans.impl.jam_old.internal.PrimitiveJClass.getPrimitiveClass(s);
  -      if (out != null) return out;
  -      return loader.loadClass(s);
  -    } else {
  -      Class clazz = org.apache.xmlbeans.impl.jam_old.internal.PrimitiveJClass.getPrimitiveClass(className);
  -      if (clazz == null) clazz = loader.loadClass(className);
  -      int[] dimensions = new int[d];
  -      return Array.newInstance(clazz, dimensions).getClass();
  -
  -      /* THIS IS COMMENTED OUT BECAUSE IT IS BROKEN ON THE CURRENT 1.5 BETA
  -         SEE SUN BUG 4983838.  IT'S NOT CLEAR WHETHER OR NOT THEY'RE GOING
  -         TO FIX IT.  MAYBE THE WORKAROUND IS BETTER ANYWAY, THOUGH :(
  -      StringWriter buff = new StringWriter();
  -      for(int i=0; i<d; i++) buff.write("[");
  -      String s = toString();
  -      s = s.substring(0,s.indexOf("["));
  -      String fd = PrimitiveJClass.getFieldDescriptor(s);
  -      if (fd != null) {
  -      buff.write(fd);
  -      } else {
  -      buff.write("L");
  -      buff.write(s);
  -      buff.write(";");
  -      }
  -      return loader.loadClass(buff.toString());
  -      */
  -    }
  -  }
  -
  -  /**
  -   * @deprecated
  -   */
  -  public static JavaTypeName forClassName(String type)
  -  {
  -    final org.apache.xmlbeans.impl.jam_old.JServiceFactory jserv_factory =
  -      org.apache.xmlbeans.impl.jam_old.JServiceFactory.getInstance();
  -    final org.apache.xmlbeans.impl.jam_old.JServiceParams params = jserv_factory.createServiceParams();
  -    final org.apache.xmlbeans.impl.jam_old.JService service;
  -    try {
  -      service = jserv_factory.createService(params);
  -    }
  -    catch (IOException e) {
  -      throw new XmlRuntimeException(e);
  -    }
  -    final  org.apache.xmlbeans.impl.jam_old.JClassLoader jcl = service.getClassLoader();
  -    final  org.apache.xmlbeans.impl.jam_old.JClass jc = jcl.loadClass(type);
  -    return forJClass(jc);
  -  }
  -
  -  /**
  -   * @deprecated
  -   */
  -  public static JavaTypeName forJClass(org.apache.xmlbeans.impl.jam_old.JClass jClass)
{
  -    if (jClass.isArray()) {
  -      return forArray(forJClass(jClass.getArrayComponentType()), jClass.getArrayDimensions());
  -    }
  -
  -    org.apache.xmlbeans.impl.jam_old.JClass[] interfaces = jClass.getInterfaces();
  -    for (int i = 0; i < interfaces.length; i++) {
  -      if (interfaces[i].getQualifiedName().equals(XMLOBJECT_CLASSNAME))
  -        return forString("x=" + jClass.getQualifiedName());
  +    /**
  +     * Do not use this constructor; use forClassName instead.
  +     */
  +    private JavaTypeName(String className)
  +    {
  +        if (className == null)
  +            throw new IllegalArgumentException();
  +
  +        if (className.startsWith("x=")) {
  +            this.isXmlObject = true;
  +            className = className.substring(2);
  +        } else {
  +            this.isXmlObject = false;
  +        }
  +
  +        int arrayDepth = 0;
  +        for (int i = className.length() - 2; i >= 0; i -= 2) {
  +            if (className.charAt(i) != '[' || className.charAt(i + 1) != ']')
  +                break;
  +            arrayDepth += 1;
  +        }
  +        this.className = className.substring(0, className.length() - 2 * arrayDepth);
  +        this.arrayString = className.substring(className.length() - 2 * arrayDepth);
       }
   
  -    return forString(jClass.getQualifiedName());
  -  }
  +    private JavaTypeName(String nonArrayClassName,
  +                         String arrayString,
  +                         boolean isXmlObject)
  +    {
  +        assert !nonArrayClassName.startsWith("[");
  +
  +        this.className = nonArrayClassName;
  +        this.arrayString = arrayString;
  +        this.isXmlObject = isXmlObject;
  +    }
  +
  +
  +    // ========================================================================
  +    // Public methods
  +
  +    /**
  +     * True for classnames that are XmlObjects.
  +     */
  +    public boolean isXmlObject()
  +    {
  +        return isXmlObject;
  +    }
  +
  +    /**
  +     * Returns the array depth, 0 for non-arrays, 1 for [], 2 for [][], etc.
  +     */
  +    public int getArrayDepth()
  +    {
  +        return arrayString.length() / 2;
  +    }
  +
  +    /**
  +     * Returns the array item type (peeling off "n" array indexes)
  +     */
  +    public JavaTypeName getArrayItemType(int depth)
  +    {
  +        if (arrayString.length() < depth * 2)
  +            return null;
  +        return forString(className + arrayString.substring(0, arrayString.length() - 2
* depth));
  +    }
  +
  +    /**
  +     * Returns the dot-separated package name.
  +     */
  +    public String getPackage()
  +    {
  +        int index = className.lastIndexOf('.');
  +        if (index <= 0)
  +            return "";
  +        return className.substring(0, index);
  +    }
  +
  +    /**
  +     * Returns the array string, peeling off the first depth levels
  +     */
  +    public String getArrayString(int depth)
  +    {
  +        if (arrayString.length() < depth * 2)
  +            return null;
  +        return arrayString.substring(2 * depth, arrayString.length());
  +    }
   
  +    /**
  +     * True if this is an inner class name.
  +     */
  +    public boolean isInnerClass()
  +    {
  +        return (className.lastIndexOf('$') >= 0);
  +    }
  +
  +    /**
  +     * Returns the JavaTypeName of the containing class, or null if this
  +     * is not an inner class name.
  +     */
  +    public JavaTypeName getContainingClass()
  +    {
  +        int index = className.lastIndexOf('$');
  +        if (index < 0)
  +            return null;
  +        return JavaTypeName.forString(className.substring(0, index));
  +    }
  +
  +    /**
  +     * Returns the short class name (i.e., no dots or dollars).
  +     */
  +    public String getShortClassName()
  +    {
  +        int index = className.lastIndexOf('$');
  +        int index2 = className.lastIndexOf('.');
  +        if (index2 > index)
  +            index = index2;
  +        if (index < 0)
  +            return className;
  +        return className.substring(index + 1);
  +    }
  +
  +
  +    // ========================================================================
  +    // Object implementation
  +
  +    /**
  +     * Returns the fully-qualified class name.
  +     */
  +    public String toString()
  +    {
  +        if (isXmlObject)
  +            return "x=" + className + arrayString;
  +        return className + arrayString;
  +    }
  +
  +    public boolean equals(Object o)
  +    {
  +        if (this == o) return true;
  +        if (!(o instanceof JavaTypeName)) return false;
  +
  +        final JavaTypeName javaName = (JavaTypeName)o;
  +
  +        if (isXmlObject != javaName.isXmlObject) return false;
  +        if (!className.equals(javaName.className)) return false;
  +        if (!arrayString.equals(javaName.arrayString)) return false;
  +
  +        return true;
  +    }
  +
  +    public int hashCode()
  +    {
  +        return className.hashCode() + arrayString.length() + (isXmlObject ? 1 : 0);
  +    }
  +
  +
  +    // ========================================================================
  +    // Deprecated methods
  +
  +    /**
  +     * Loads the class represented by this JavaTypeName in the given
  +     * ClassLoader.  This is really horrible - the impedance mismatch
  +     * in the naming here is very really painful.  Need to do something better.
  +     */
  +    /**
  +     * @deprecated
  +     */
  +    public Class loadClassIn(ClassLoader loader) throws ClassNotFoundException
  +    {
  +        int d = getArrayDepth();
  +        if (d == 0) {
  +            String s = toString();
  +            Class out = org.apache.xmlbeans.impl.jam_old.internal.PrimitiveJClass.getPrimitiveClass(s);
  +            if (out != null) return out;
  +            return loader.loadClass(s);
  +        } else {
  +            Class clazz = org.apache.xmlbeans.impl.jam_old.internal.PrimitiveJClass.getPrimitiveClass(className);
  +            if (clazz == null) clazz = loader.loadClass(className);
  +            int[] dimensions = new int[d];
  +            return Array.newInstance(clazz, dimensions).getClass();
  +
  +            /* THIS IS COMMENTED OUT BECAUSE IT IS BROKEN ON THE CURRENT 1.5 BETA
  +               SEE SUN BUG 4983838.  IT'S NOT CLEAR WHETHER OR NOT THEY'RE GOING
  +               TO FIX IT.  MAYBE THE WORKAROUND IS BETTER ANYWAY, THOUGH :(
  +            StringWriter buff = new StringWriter();
  +            for(int i=0; i<d; i++) buff.write("[");
  +            String s = toString();
  +            s = s.substring(0,s.indexOf("["));
  +            String fd = PrimitiveJClass.getFieldDescriptor(s);
  +            if (fd != null) {
  +            buff.write(fd);
  +            } else {
  +            buff.write("L");
  +            buff.write(s);
  +            buff.write(";");
  +            }
  +            return loader.loadClass(buff.toString());
  +            */
  +        }
  +    }
  +
  +
  +    /**
  +     * @deprecated
  +     */
  +    public static JavaTypeName forJClass(org.apache.xmlbeans.impl.jam_old.JClass jClass)
  +    {
  +        if (jClass.isArray()) {
  +            return forArray(forJClass(jClass.getArrayComponentType()), jClass.getArrayDimensions());
  +        }
  +
  +        org.apache.xmlbeans.impl.jam_old.JClass[] interfaces = jClass.getInterfaces();
  +        for (int i = 0; i < interfaces.length; i++) {
  +            if (interfaces[i].getQualifiedName().equals(XMLOBJECT_CLASSNAME))
  +                return forString("x=" + jClass.getQualifiedName());
  +        }
  +
  +        return forString(jClass.getQualifiedName());
  +    }
  +
  +    /**
  +     * create a JavaTypeName for a String in the form return by
  +     * java.lang.Class.getName()
  +     *
  +     * @param class_name
  +     * @return
  +     */
  +    public static JavaTypeName forClassName(final String class_name)
  +    {
  +        if ('[' != class_name.charAt(0)) {
  +            return new JavaTypeName(class_name, "", false);
  +        }
  +
  +        final String compname;
  +        int dims = 0;
  +
  +        int first_bracket = class_name.indexOf('[');
  +        int last_bracket = class_name.lastIndexOf('[');
  +        int semi = class_name.indexOf(';', last_bracket);
  +
  +        if (semi == -1) {
  +            char array_type = class_name.charAt(1 + last_bracket);
  +            compname = getPrimitiveArrayBase(array_type);
  +        } else {
  +            compname = class_name.substring(last_bracket + 2, semi);
  +        }
  +        dims = (1 + last_bracket - first_bracket);
  +
  +        assert compname.length() > 0 ;
  +
  +
  +        StringBuffer array_str = new StringBuffer(2 * dims);
  +        for (int i = 0; i < dims; i++) {
  +            array_str.append("[]");
  +        }
  +
  +        final JavaTypeName jtn = new JavaTypeName(compname,
  +                                                  array_str.toString(),
  +                                                  false);
  +        return jtn;
  +    }
  +
  +    private static String getPrimitiveArrayBase(char array_type)
  +    {
  +        switch (array_type) {
  +            case 'B':
  +                return "byte";
  +            case 'C':
  +                return "char";
  +            case 'D':
  +                return "double";
  +            case 'F':
  +                return "float";
  +            case 'I':
  +                return "int";
  +            case 'J':
  +                return "long";
  +            case 'S':
  +                return "short";
  +            case 'Z':
  +                return "boolean";
  +            default:
  +                throw new AssertionError("unknown array type: " + array_type);
  +        }
  +    }
   
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xmlbeans-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-cvs-help@xml.apache.org


Mime
View raw message