Esempio n. 1
0
 /**
  * Creates 'readResolve(Object)' method for this enumeration class.
  *
  * @param jClass The enumeration class to create this method for.
  */
 private void createReadResolveMethod(final JClass jClass) {
   JDocComment jdc;
   JSourceCode jsc;
   JMethod mReadResolve = new JMethod("readResolve", SGTypes.OBJECT, "this deserialized object");
   mReadResolve.getModifiers().makePrivate();
   jClass.addMethod(mReadResolve);
   jdc = mReadResolve.getJDocComment();
   jdc.appendComment(" will be called during deserialization to replace ");
   jdc.appendComment("the deserialized object with the correct constant ");
   jdc.appendComment("instance.");
   jsc = mReadResolve.getSourceCode();
   jsc.add("return valueOf(this.stringValue);");
 }
Esempio n. 2
0
 /**
  * Creates 'enumerate()' method for this enumeration class.
  *
  * @param jClass The enumeration class to create this method for.
  * @param className The name of the class.
  */
 private void createEnumerateMethod(final JClass jClass, final String className) {
   // TODO for the time being return Enumeration<Object> for Java 5.0; change
   JMethod mEnumerate =
       new JMethod(
           "enumerate",
           SGTypes.createEnumeration(SGTypes.OBJECT, getConfig().useJava50(), true),
           "an Enumeration over all possible instances of " + className);
   mEnumerate.getModifiers().setStatic(true);
   jClass.addMethod(mEnumerate);
   JDocComment jdc = mEnumerate.getJDocComment();
   jdc.appendComment("Returns an enumeration of all possible instances of ");
   jdc.appendComment(className);
   mEnumerate.getSourceCode().add("return _memberTable.elements();");
 }
Esempio n. 3
0
 /**
  * Creates 'init()' method for this enumeration class.
  *
  * @param jClass The enumeration class to create this method for.
  * @return an 'init()' method for this enumeration class.
  */
 private JMethod createInitMethod(final JClass jClass) {
   final String initMethodName = getInitMethodName(_maxSuffix);
   JMethod mInit =
       new JMethod(
           initMethodName,
           SGTypes.createHashtable(getConfig().useJava50()),
           "the initialized Hashtable for the member table");
   jClass.addMethod(mInit);
   mInit.getModifiers().makePrivate();
   mInit.getModifiers().setStatic(true);
   if (getConfig().useJava50()) {
     mInit
         .getSourceCode()
         .add(
             "java.util.Hashtable<Object, Object> members"
                 + " = new java.util.Hashtable<Object, Object>();");
   } else {
     mInit.getSourceCode().add("java.util.Hashtable members = new java.util.Hashtable();");
   }
   return mInit;
 }
Esempio n. 4
0
  /**
   * Creates 'valueOf(String)' method for this enumeration class.
   *
   * @param jClass The enumeration class to create this method for.
   * @param className The name of the class.
   */
  private void createValueOfMethod(final JClass jClass, final String className) {
    JMethod mValueOf =
        new JMethod("valueOf", jClass, "the " + className + " value of parameter 'string'");
    mValueOf.addParameter(new JParameter(SGTypes.STRING, "string"));
    mValueOf.getModifiers().setStatic(true);
    jClass.addMethod(mValueOf);

    JDocComment jdc = mValueOf.getJDocComment();
    jdc.appendComment("Returns a new " + className);
    jdc.appendComment(" based on the given String value.");

    JSourceCode jsc = mValueOf.getSourceCode();
    jsc.add(
        "java.lang.Object obj = null;\n"
            + "if (string != null) {\n"
            + " obj = _memberTable.get(string{1});\n"
            + "}\n"
            + "if (obj == null) {\n"
            + " String err = \"'\" + string + \"' is not a valid {0}\";\n"
            + " throw new IllegalArgumentException(err);\n"
            + "}\n"
            + "return ({0}) obj;",
        className, (_caseInsensitive ? ".toLowerCase()" : ""));
  }
Esempio n. 5
0
  /**
   * Creates all the necessary enumeration code from the given SimpleType. Enumerations are handled
   * by creating an Object like the following:
   *
   * <pre>
   *     public class {name} {
   *         // list of values
   *         {type}[] values = {
   *             ...
   *         };
   *
   *         // Returns true if the given value is part
   *         // of this enumeration
   *         public boolean contains({type} value);
   *
   *         // Returns the {type} value whose String value
   *         // is equal to the given String
   *         public {type} valueOf(String strValue);
   *     }
   * </pre>
   *
   * @param binding Extended binding instance
   * @param simpleType the SimpleType we are processing an enumeration for
   * @param state our current state
   */
  void processEnumerationAsBaseType(
      final ExtendedBinding binding, final SimpleType simpleType, final FactoryState state) {
    SimpleType base = (SimpleType) simpleType.getBaseType();
    XSType baseType = null;

    if (base == null) {
      baseType = new XSString();
    } else {
      baseType = _typeConversion.convertType(base, getConfig().useJava50());
    }

    Enumeration enumeration = simpleType.getFacets(Facet.ENUMERATION);

    JClass jClass = state.getJClass();
    String className = jClass.getLocalName();

    JField fValues = null;
    JDocComment jdc = null;
    JSourceCode jsc = null;

    // -- modify constructor
    JConstructor constructor = jClass.getConstructor(0);
    constructor.getModifiers().makePrivate();

    fValues = new JField(new JArrayType(baseType.getJType(), getConfig().useJava50()), "values");

    // -- Loop through "enumeration" facets
    // -- and create the default values for the type.
    int count = 0;

    StringBuilder values = new StringBuilder("{\n");

    while (enumeration.hasMoreElements()) {
      Facet facet = (Facet) enumeration.nextElement();
      String value = facet.getValue();

      // -- Should we make sure the value is valid before proceeding??

      // -- we need to move this code to XSType so that we don't have to do
      // -- special code here for each type

      if (count > 0) {
        values.append(",\n");
      }

      // -- indent for fun
      values.append("    ");

      if (baseType.getType() == XSType.STRING_TYPE) {
        values.append('\"');
        // -- escape value
        values.append(escapeValue(value));
        values.append('\"');
      } else {
        values.append(value);
      }

      ++count;
    }

    values.append("\n}");

    fValues.setInitString(values.toString());
    jClass.addField(fValues);

    // -- #valueOf method
    JMethod method =
        new JMethod("valueOf", jClass, "the String value of the provided " + baseType.getJType());
    method.addParameter(new JParameter(SGTypes.STRING, "string"));
    method.getModifiers().setStatic(true);
    jClass.addMethod(method);
    jdc = method.getJDocComment();
    jdc.appendComment("Returns the " + baseType.getJType());
    jdc.appendComment(" based on the given String value.");
    jsc = method.getSourceCode();

    jsc.add("for (int i = 0; i < values.length; i++) {");
    jsc.add("}");
    jsc.add("throw new IllegalArgumentException(\"");
    jsc.append("Invalid value for ");
    jsc.append(className);
    jsc.append(": \" + string + \".\");");
  } // -- processEnumerationAsBaseType