Ejemplo n.º 1
0
 /**
  * Generate all FieldDescriptors for a class/interface
  *
  * @param cld the ClassDescriptor of the class
  * @param supers true if go up the inheritence tree and output fields
  * @return the generated String
  */
 protected String generateFieldDescriptors(ClassDescriptor cld, boolean supers) {
   Set<FieldDescriptor> superclassFields = Collections.emptySet();
   if (supers && (cld.getSuperclassDescriptor() != null)) {
     superclassFields = cld.getSuperclassDescriptor().getAllFieldDescriptors();
   }
   StringBuffer sb = new StringBuffer();
   Iterator<FieldDescriptor> iter;
   if (supers) {
     iter = cld.getAllFieldDescriptors().iterator();
   } else {
     iter = cld.getFieldDescriptors().iterator();
   }
   while (iter.hasNext()) {
     FieldDescriptor fd = iter.next();
     if (!superclassFields.contains(fd)) {
       if (fd instanceof AttributeDescriptor) {
         sb.append(generate((AttributeDescriptor) fd, supers));
       } else if (fd instanceof CollectionDescriptor) {
         sb.append(generate((CollectionDescriptor) fd, supers));
       } else if (fd instanceof ReferenceDescriptor) {
         sb.append(generate((ReferenceDescriptor) fd, supers));
       }
     }
   }
   return sb.toString();
 }
Ejemplo n.º 2
0
  /**
   * Generate the output for a ClassDescriptor.
   *
   * @param cld the ClassDescriptor
   * @param shadow whether to generate the shadow class of an interface
   * @return the relevant String representation
   */
  protected String generate(ClassDescriptor cld, boolean shadow) {
    StringBuffer sb = new StringBuffer();

    String packageName = TypeUtil.packageName(cld.getName());

    if (packageName.length() > 0) {
      sb.append("package ").append(packageName).append(";" + ENDL + ENDL);
    }
    if ((!cld.isInterface()) || shadow) {
      boolean hasCollections = false;
      boolean hasReferences = false;
      for (FieldDescriptor fd : cld.getAllFieldDescriptors()) {
        if (fd instanceof CollectionDescriptor) {
          hasCollections = true;
        } else if (fd instanceof ReferenceDescriptor) {
          hasReferences = true;
        }
      }
      sb.append("import org.intermine.objectstore.ObjectStore;" + ENDL);
      sb.append("import org.intermine.objectstore.intermine.NotXmlParser;" + ENDL);
      sb.append("import org.intermine.objectstore.intermine.NotXmlRenderer;" + ENDL);
      if (hasCollections) {
        sb.append("import org.intermine.objectstore.proxy.ProxyCollection;" + ENDL);
      }
      if (hasReferences) {
        sb.append("import org.intermine.objectstore.proxy.ProxyReference;" + ENDL);
      }
      sb.append("import org.intermine.util.StringConstructor;" + ENDL);
      sb.append("import org.intermine.util.TypeUtil;" + ENDL);
      if (shadow) {
        sb.append("import org.intermine.model.ShadowClass;" + ENDL);
      }
      sb.append(ENDL);
    }
    sb.append("public ")
        .append((cld.isInterface() && (!shadow)) ? "interface " : "class ")
        .append(TypeUtil.unqualifiedName(cld.getName()))
        .append(shadow ? "Shadow" : "");

    if (shadow) {
      sb.append(" implements ")
          .append(TypeUtil.unqualifiedName(cld.getName()))
          .append(", ShadowClass");
    } else {
      if (!cld.isInterface()) {
        if (cld.getSuperclassDescriptor() != null) {
          sb.append(" extends ").append(cld.getSuperclassDescriptor().getName());
        }
      }

      boolean firstTime = true;

      if (cld.getSuperDescriptors().size() > 0) {
        for (ClassDescriptor superCld : cld.getSuperDescriptors()) {
          if (superCld.isInterface()) {
            if (firstTime) {
              sb.append(cld.isInterface() ? " extends " : " implements ");
              firstTime = false;
            } else {
              sb.append(", ");
            }
            sb.append(superCld.getName());
          }
        }
      } else {
        sb.append(" implements org.intermine.model.FastPathObject");
      }
    }

    sb.append(ENDL).append("{" + ENDL);

    if (shadow) {
      sb.append(INDENT)
          .append("public static final Class<")
          .append(TypeUtil.unqualifiedName(cld.getName()))
          .append("> shadowOf = ")
          .append(TypeUtil.unqualifiedName(cld.getName()))
          .append(".class;" + ENDL);
    }

    // FieldDescriptors defined for this class/interface
    if (cld.isInterface() && (!shadow)) {
      sb.append(generateFieldDescriptors(cld, false));
    } else {
      sb.append(generateFieldDescriptors(cld, true))
          .append(generateEquals(cld))
          .append(generateHashCode(cld))
          .append(generateToString(cld))
          .append(generateGetFieldValue(cld, false))
          .append(generateGetFieldValue(cld, true))
          .append(generateSetFieldValue(cld))
          .append(generateGetFieldType(cld));
      if (cld.getSuperDescriptors().size() > 0) {
        sb.append(generateGetObject(cld))
            .append(generateSetObject(cld))
            .append(generateAddCollectionElement(cld))
            .append(generateGetElementType(cld));
      }
    }

    sb.append("}" + ENDL);
    return sb.toString();
  }