Пример #1
0
  /**
   * Adds the given JField to this JStructure.
   *
   * <p>This method is implemented by subclasses and should only accept the proper fields for the
   * subclass otherwise an IllegalArgumentException will be thrown. For example a JInterface will
   * only accept static fields.
   *
   * <p>
   *
   * @param jField, the JField to add
   * @exception java.lang.IllegalArgumentException when the given JField has a name of an existing
   *     JField
   */
  public void addField(JField jField) throws IllegalArgumentException {
    if (jField == null) {
      throw new IllegalArgumentException("argument 'jField' cannot be null");
    }

    String name = jField.getName();

    // -- check for duplicate field name
    if ((fields != null) && (fields.get(name) != null)) {
      String err = "duplicate name found: " + name;
      throw new IllegalArgumentException(err);
    }

    // -- check for proper modifiers
    JModifiers modifiers = jField.getModifiers();
    if (!modifiers.isStatic()) {
      throw new IllegalArgumentException("Fields added to a JInterface must be static.");
    }
    if (modifiers.isPrivate()) {
      throw new IllegalArgumentException("Fields added to a JInterface must not be private.");
    }

    // -- only initialize fields if we need it, many interfaces
    // -- don't contain any fields, no need to waste space
    if (fields == null) {
      fields = new JNamedMap(3);
    }

    fields.put(name, jField);
  }
Пример #2
0
 public JMember[] getMembers() {
   int size = members.size();
   JMember[] marray = new JMember[size];
   for (int i = 0; i < size; i++) {
     marray[i] = (JMember) members.get(i);
   }
   return marray;
 } // -- getMembers
Пример #3
0
 /**
  * Returns an array of all the JFields of this JStructure
  *
  * @return an array of all the JFields of this JStructure
  */
 public JField[] getFields() {
   if (fields == null) {
     return new JField[0];
   }
   int size = fields.size();
   JField[] farray = new JField[size];
   for (int i = 0; i < size; i++) {
     farray[i] = (JField) fields.get(i);
   }
   return farray;
 } // -- getFields
Пример #4
0
  public void addMember(JMember jMember) throws IllegalArgumentException {
    if (jMember == null) {
      throw new IllegalArgumentException("Class members cannot be null");
    }
    if (members.get(jMember.getName()) != null) {
      String err = "duplicate name found: " + jMember.getName();
      throw new IllegalArgumentException(err);
    }
    members.put(jMember.getName(), jMember);

    // if member is of a type not imported by this class
    // then add import
    JType type = jMember.getType();
    while (type.isArray()) type = type.getComponentType();
    if (!type.isPrimitive()) {
      addImport(((JClass) type).getName());
    }
  } // -- addMember
Пример #5
0
  /**
   * Prints the source code for this JInterface to the given JSourceWriter
   *
   * @param jsw the JSourceWriter to print to. [May not be null]
   */
  public void print(JSourceWriter jsw, boolean classOnly) {

    if (jsw == null) {
      throw new IllegalArgumentException("argument 'jsw' should not be null.");
    }

    StringBuilder buffer = new StringBuilder();

    if (!classOnly) {
      printHeader(jsw);
      printPackageDeclaration(jsw);
      printImportDeclarations(jsw);
    }

    // ------------/
    // - Java Doc -/
    // ------------/

    getJDocComment().print(jsw);

    JAnnotations annotations = getAnnotations();
    if (annotations != null) annotations.print(jsw);

    // -- print class information
    // -- we need to add some JavaDoc API adding comments

    buffer.setLength(0);

    JModifiers modifiers = getModifiers();
    if (modifiers.isPrivate()) {
      buffer.append("private ");
    } else if (modifiers.isPublic()) {
      buffer.append("public ");
    }

    if (modifiers.isAbstract()) {
      buffer.append("abstract ");
    }

    buffer.append("interface ");
    buffer.append(getLocalName());
    jsw.writeln(buffer.toString());
    buffer.setLength(0);
    jsw.indent();

    if (getInterfaceCount() > 0) {
      Enumeration<String> e = getInterfaces();
      buffer.append("extends ");
      while (e.hasMoreElements()) {
        buffer.append(e.nextElement());
        if (e.hasMoreElements()) buffer.append(", ");
      }

      jsw.writeln(buffer.toString());
      buffer.setLength(0);
    }

    jsw.unindent();

    jsw.writeln('{');

    jsw.indent();

    // -- declare static members

    if (fields != null) {
      if (fields.size() > 0) {
        jsw.writeln();
        jsw.writeln("  //--------------------------/");
        jsw.writeln(" //- Class/Member Variables -/");
        jsw.writeln("//--------------------------/");
        jsw.writeln();
      }

      for (int i = 0; i < fields.size(); i++) {

        JField jField = (JField) fields.get(i);

        // -- print Java comment
        JDocComment comment = jField.getComment();
        if (comment != null) comment.print(jsw);

        // -- print member
        jsw.write(jField.getModifiers().toString());
        jsw.write(' ');

        JType type = jField.getType();
        String typeName = type.toString();
        // -- for esthetics use short name in some cases
        if (typeName.equals(toString())) {
          typeName = type.getLocalName();
        }
        jsw.write(typeName);
        jsw.write(' ');
        jsw.write(jField.getName());

        String init = jField.getInitString();
        if (init != null) {
          jsw.write(" = ");
          jsw.write(init);
        }

        jsw.writeln(';');
        jsw.writeln();
      }
    }

    // -- print method signatures

    if (methods.size() > 0) {
      jsw.writeln();
      jsw.writeln("  //-----------/");
      jsw.writeln(" //- Methods -/");
      jsw.writeln("//-----------/");
      jsw.writeln();
    }

    for (int i = 0; i < methods.size(); i++) {
      JMethodSignature signature = (JMethodSignature) methods.elementAt(i);
      signature.print(jsw);
      jsw.writeln(';');
    }

    for (String sourceCodeEntry : sourceCodeEntries) {
      jsw.writeln(sourceCodeEntry);
    }

    jsw.unindent();
    jsw.writeln('}');
    jsw.flush();
    jsw.close();
  } // -- printSource
Пример #6
0
 /**
  * Returns the field with the given name, or null if no field was found with the given name.
  *
  * @param name the name of the field to return.
  * @return the field with the given name, or null if no field was found with the given name.
  */
 public JField getField(String name) {
   if (fields == null) return null;
   return (JField) fields.get(name);
 } // -- getField
Пример #7
0
  /**
   * Prints the source code for this JClass
   *
   * @param lineSeparator the line separator to use at the end of each line. If null, then the
   *     default line separator for the runtime platform will be used.
   */
  public void print(String lineSeparator) {

    // -- open output file
    String name = getLocalName();
    String filename = name + ".java";

    if ((packageName != null) && (packageName.length() > 0)) {
      String path = packageName.replace('.', File.separatorChar);
      File pathFile = new File(path);
      if (!pathFile.exists()) {
        pathFile.mkdirs();
      }
      filename = path + File.separator + filename;
    }

    File file = new File(filename);
    JSourceWriter jsw = null;
    try {
      jsw = new JSourceWriter(new FileWriter(file));
    } catch (java.io.IOException ioe) {
      System.out.println("unable to create class file: " + filename);
      return;
    }

    if (lineSeparator == null) {
      lineSeparator = System.getProperty("line.separator");
    }
    jsw.setLineSeparator(lineSeparator);

    StringBuffer buffer = new StringBuffer();

    // -- write class header
    if (header != null) header.print(jsw);
    else {
      jsw.writeln("/*");
      jsw.writeln(" * " + DEFAULT_HEADER);
      jsw.writeln("*/");
    }
    jsw.writeln();
    jsw.flush();

    // -- print package name
    if ((packageName != null) && (packageName.length() > 0)) {

      buffer.setLength(0);
      buffer.append("package ");
      buffer.append(packageName);
      buffer.append(';');
      jsw.writeln(buffer.toString());
      jsw.writeln();
    }

    // -- print imports
    jsw.writeln("  //---------------------------------/");
    jsw.writeln(" //- Imported classes and packages -/");
    jsw.writeln("//---------------------------------/");
    jsw.writeln();
    for (int i = 0; i < imports.size(); i++) {
      jsw.write("import ");
      jsw.write(imports.elementAt(i));
      jsw.writeln(';');
    }
    jsw.writeln();

    // ------------/
    // - Java Doc -/
    // ------------/

    jdc.print(jsw);

    // -- print class information
    // -- we need to add some JavaDoc API adding comments

    buffer.setLength(0);

    if (modifiers.isPrivate()) {
      buffer.append("private ");
    } else if (modifiers.isPublic()) {
      buffer.append("public ");
    }

    if (modifiers.isAbstract()) {
      buffer.append("abstract ");
    }

    buffer.append("class ");
    buffer.append(getLocalName());
    buffer.append(' ');
    if (superClass != null) {
      buffer.append("extends ");
      buffer.append(superClass);
      buffer.append(' ');
    }
    if (interfaces.size() > 0) {
      int iSize = interfaces.size();
      boolean endl = false;
      if ((iSize > 1) || (superClass != null)) {
        jsw.writeln(buffer.toString());
        buffer.setLength(0);
        endl = true;
      }
      buffer.append("implements ");
      for (int i = 0; i < iSize; i++) {
        if (i > 0) buffer.append(", ");
        buffer.append(interfaces.elementAt(i));
      }
      if (endl) {
        jsw.writeln(buffer.toString());
        buffer.setLength(0);
      } else buffer.append(' ');
    }

    buffer.append('{');
    jsw.writeln(buffer.toString());
    buffer.setLength(0);
    jsw.writeln();

    jsw.indent();

    // -- declare members

    if (members.size() > 0) {
      jsw.writeln();
      jsw.writeln("  //--------------------/");
      jsw.writeln(" //- Member Variables -/");
      jsw.writeln("//--------------------/");
      jsw.writeln();
    }

    for (int i = 0; i < members.size(); i++) {

      JMember jMember = (JMember) members.get(i);

      // -- print Java comment
      JDocComment comment = jMember.getComment();
      if (comment != null) comment.print(jsw);

      // -- print member
      jsw.write(jMember.getModifiers().toString());
      jsw.write(' ');

      JType type = jMember.getType();
      String typeName = type.toString();
      // -- for esthetics use short name in some cases
      if (typeName.equals(toString())) {
        typeName = type.getLocalName();
      }
      jsw.write(typeName);
      jsw.write(' ');
      jsw.write(jMember.getName());

      String init = jMember.getInitString();
      if (init != null) {
        jsw.write(" = ");
        jsw.write(init);
      }

      jsw.writeln(';');
      jsw.writeln();
    }

    // -- print constructors
    if (constructors.size() > 0) {
      jsw.writeln();
      jsw.writeln("  //----------------/");
      jsw.writeln(" //- Constructors -/");
      jsw.writeln("//----------------/");
      jsw.writeln();
    }
    for (int i = 0; i < constructors.size(); i++) {
      JConstructor jConstructor = (JConstructor) constructors.elementAt(i);
      jConstructor.print(jsw);
      jsw.writeln();
    }

    // -- print methods
    if (methods.size() > 0) {
      jsw.writeln();
      jsw.writeln("  //-----------/");
      jsw.writeln(" //- Methods -/");
      jsw.writeln("//-----------/");
      jsw.writeln();
    }

    for (int i = 0; i < methods.size(); i++) {
      JMethod jMethod = (JMethod) methods.elementAt(i);
      jMethod.print(jsw);
      jsw.writeln();
    }

    jsw.unindent();
    jsw.writeln('}');
    jsw.flush();
    jsw.close();
  } // -- printSource
Пример #8
0
 /**
  * Returns the member with the given name, or null if no member was found with the given name
  *
  * @param name the name of the member to return
  * @return the member with the given name, or null if no member was found with the given name
  */
 public JMember getMember(String name) {
   return (JMember) members.get(name);
 } // -- getMember