コード例 #1
0
ファイル: JavaModelOutput.java プロジェクト: ZFIN/intermine
 /** Perform the mapping. */
 public void process() {
   for (ClassDescriptor cld : model.getClassDescriptors()) {
     String cldName = cld.getName();
     if (!"org.intermine.model.InterMineObject".equals(cldName)) {
       String pkg = TypeUtil.packageName(cldName);
       String cls = TypeUtil.unqualifiedName(cld.getName());
       String separator = File.separator;
       // Escape windows path seperator
       if ("\\".equals(separator)) {
         separator = "\\\\";
       }
       File dir = new File(file, pkg.replaceAll("[.]", separator));
       dir.mkdirs();
       File path = new File(dir, cls + ".java");
       try {
         path.delete();
         BufferedWriter fos = new BufferedWriter(new FileWriter(path, true));
         fos.write(generate(cld, false));
         fos.close();
         if (cld.isInterface()) {
           path = new File(dir, cls + "Shadow.java");
           path.delete();
           fos = new BufferedWriter(new FileWriter(path, true));
           fos.write(generate(cld, true));
           fos.close();
         }
       } catch (IOException e) {
         throw new RuntimeException("Error creating java", e);
       }
     }
   }
 }
コード例 #2
0
ファイル: JavaModelOutput.java プロジェクト: ZFIN/intermine
 /**
  * Generates the setoBJECT method for deserialising objects.
  *
  * @param cld a ClassDescriptor
  * @return a String containing the method
  */
 public String generateSetObject(ClassDescriptor cld) {
   StringBuffer sb = new StringBuffer();
   sb.append(INDENT)
       .append("public void setoBJECT(String notXml, ObjectStore os) {\n")
       .append(INDENT + INDENT)
       .append("setoBJECT(NotXmlParser.SPLITTER.split(notXml), os);\n")
       .append(INDENT)
       .append("}\n")
       .append(INDENT)
       .append("public void setoBJECT(final String[] notXml, final ObjectStore os) {\n")
       .append(INDENT + INDENT)
       .append(
           "if (!"
               + cld.getName()
               + (cld.isInterface() ? "Shadow" : "")
               + ".class.equals(getClass())) {\n")
       .append(INDENT + INDENT + INDENT)
       .append(
           "throw new IllegalStateException(\"Class \" + getClass().getName() + \""
               + " does not match code ("
               + cld.getName()
               + ")\");\n")
       .append(INDENT + INDENT)
       .append("}\n")
       .append(INDENT + INDENT)
       .append("for (int i = 2; i < notXml.length;) {\n")
       .append(INDENT + INDENT + INDENT)
       .append("int startI = i;\n");
   for (FieldDescriptor field : cld.getAllFieldDescriptors()) {
     String fieldName = field.getName();
     if ("notXml".equals(fieldName)) {
       fieldName = "this.notXml";
     } else if ("os".equals(fieldName)) {
       fieldName = "this.os";
     }
     if (field instanceof AttributeDescriptor) {
       AttributeDescriptor attribute = (AttributeDescriptor) field;
       sb.append(INDENT + INDENT + INDENT)
           .append("if ((i < notXml.length) && \"a" + fieldName + "\".equals(notXml[i])) {\n")
           .append(INDENT + INDENT + INDENT + INDENT)
           .append("i++;\n")
           .append(INDENT + INDENT + INDENT + INDENT);
       if ("boolean".equals(attribute.getType())) {
         sb.append(fieldName + " = Boolean.parseBoolean(notXml[i]);\n");
       } else if ("short".equals(attribute.getType())) {
         sb.append(fieldName + " = Short.parseShort(notXml[i]);\n");
       } else if ("int".equals(attribute.getType())) {
         sb.append(fieldName + " = Integer.parseInt(notXml[i]);\n");
       } else if ("long".equals(attribute.getType())) {
         sb.append(fieldName + " = Long.parseLong(notXml[i]);\n");
       } else if ("float".equals(attribute.getType())) {
         sb.append(fieldName + " = Float.parseFloat(notXml[i]);\n");
       } else if ("double".equals(attribute.getType())) {
         sb.append(fieldName + " = Double.parseDouble(notXml[i]);\n");
       } else if ("java.lang.Boolean".equals(attribute.getType())) {
         sb.append(fieldName + " = Boolean.valueOf(notXml[i]);\n");
       } else if ("java.lang.Short".equals(attribute.getType())) {
         sb.append(fieldName + " = Short.valueOf(notXml[i]);\n");
       } else if ("java.lang.Integer".equals(attribute.getType())) {
         sb.append(fieldName + " = Integer.valueOf(notXml[i]);\n");
       } else if ("java.lang.Long".equals(attribute.getType())) {
         sb.append(fieldName + " = Long.valueOf(notXml[i]);\n");
       } else if ("java.lang.Float".equals(attribute.getType())) {
         sb.append(fieldName + " = Float.valueOf(notXml[i]);\n");
       } else if ("java.lang.Double".equals(attribute.getType())) {
         sb.append(fieldName + " = Double.valueOf(notXml[i]);\n");
       } else if ("java.util.Date".equals(attribute.getType())) {
         sb.append(fieldName + " = new java.util.Date(Long.parseLong(notXml[i]));\n");
       } else if ("java.math.BigDecimal".equals(attribute.getType())) {
         sb.append(fieldName + " = new java.math.BigDecimal(notXml[i]);\n");
       } else if ("org.intermine.objectstore.query.ClobAccess".equals(attribute.getType())) {
         sb.append(
             fieldName
                 + " = org.intermine.objectstore.query.ClobAccess"
                 + ".decodeDbDescription(os, notXml[i]);\n");
       } else if ("java.lang.String".equals(attribute.getType())) {
         sb.append("StringBuilder string = null;\n")
             .append(INDENT + INDENT + INDENT + INDENT)
             .append(
                 "while ((i + 1 < notXml.length) && (notXml[i + 1].charAt(0) == '"
                     + ENCODED_DELIM
                     + "')) {\n")
             .append(INDENT + INDENT + INDENT + INDENT + INDENT)
             .append("if (string == null) string = new StringBuilder(notXml[i]);\n")
             .append(INDENT + INDENT + INDENT + INDENT + INDENT)
             .append("i++;\n")
             .append(INDENT + INDENT + INDENT + INDENT + INDENT)
             .append("string.append(\"" + DELIM + "\").append(notXml[i].substring(1));\n")
             .append(INDENT + INDENT + INDENT + INDENT)
             .append("}\n")
             .append(INDENT + INDENT + INDENT + INDENT)
             .append(fieldName + " = string == null ? notXml[i] : string.toString();\n");
       } else {
         throw new IllegalArgumentException("Unknown type " + attribute.getType());
       }
       sb.append(INDENT + INDENT + INDENT + INDENT)
           .append("i++;\n")
           .append(INDENT + INDENT + INDENT)
           .append("}\n");
     } else if (field.isReference()) {
       ReferenceDescriptor reference = (ReferenceDescriptor) field;
       sb.append(INDENT + INDENT + INDENT)
           .append("if ((i < notXml.length) &&\"r" + fieldName + "\".equals(notXml[i])) {\n")
           .append(INDENT + INDENT + INDENT + INDENT)
           .append("i++;\n")
           .append(INDENT + INDENT + INDENT + INDENT)
           .append(
               fieldName
                   + " = new ProxyReference(os, Integer.valueOf(notXml[i])"
                   + ", "
                   + reference.getReferencedClassName()
                   + ".class);\n")
           .append(INDENT + INDENT + INDENT + INDENT)
           .append("i++;\n")
           .append(INDENT + INDENT + INDENT)
           .append("};\n");
     }
   }
   sb.append(INDENT + INDENT + INDENT)
       .append("if (startI == i) {\n")
       .append(INDENT + INDENT + INDENT + INDENT)
       .append("throw new IllegalArgumentException(\"Unknown field \" + notXml[i]);\n")
       .append(INDENT + INDENT + INDENT)
       .append("}\n")
       .append(INDENT + INDENT)
       .append("}\n");
   for (FieldDescriptor field : cld.getAllFieldDescriptors()) {
     String fieldName = field.getName();
     if ("notXml".equals(fieldName)) {
       fieldName = "this.notXml";
     } else if ("os".equals(fieldName)) {
       fieldName = "this.os";
     }
     if (field instanceof CollectionDescriptor) {
       CollectionDescriptor coll = (CollectionDescriptor) field;
       sb.append(INDENT + INDENT)
           .append(
               fieldName
                   + " = new ProxyCollection<"
                   + coll.getReferencedClassName()
                   + ">(os, this, \""
                   + fieldName
                   + "\", "
                   + coll.getReferencedClassName()
                   + ".class);\n");
     }
   }
   sb.append(INDENT).append("}\n");
   return sb.toString();
 }
コード例 #3
0
ファイル: JavaModelOutput.java プロジェクト: ZFIN/intermine
 /**
  * Generates the getoBJECT method for producing NotXml.
  *
  * @param cld the ClassDescriptor
  * @return generated java code as a String
  */
 protected String generateGetObject(ClassDescriptor cld) {
   StringBuffer sb = new StringBuffer();
   sb.append(INDENT)
       .append("public StringConstructor getoBJECT() {\n")
       .append(INDENT + INDENT)
       .append(
           "if (!"
               + cld.getName()
               + (cld.isInterface() ? "Shadow" : "")
               + ".class.equals(getClass())) {\n")
       .append(INDENT + INDENT + INDENT)
       .append("return NotXmlRenderer.render(this);\n")
       .append(INDENT + INDENT)
       .append("}\n")
       .append(INDENT + INDENT)
       .append("StringConstructor sb = new StringConstructor();\n")
       .append(INDENT + INDENT)
       .append("sb.append(\"" + DELIM + cld.getName() + "\");\n");
   for (FieldDescriptor field : cld.getAllFieldDescriptors()) {
     if (field instanceof AttributeDescriptor) {
       AttributeDescriptor attribute = (AttributeDescriptor) field;
       if (attribute.getType().startsWith("java.")) {
         sb.append(INDENT + INDENT)
             .append("if (" + attribute.getName() + " != null) {\n")
             .append(INDENT + INDENT + INDENT)
             .append("sb.append(\"" + DELIM + "a" + field.getName() + DELIM + "\")");
         if ("java.util.Date".equals(attribute.getType())) {
           sb.append(".append(" + attribute.getName() + ".getTime());\n");
         } else if ("java.lang.String".equals(attribute.getType())) {
           sb.append(";\n")
               .append(INDENT + INDENT + INDENT)
               .append("String string = " + attribute.getName() + ";\n")
               .append(INDENT + INDENT + INDENT)
               .append("while (string != null) {\n")
               .append(INDENT + INDENT + INDENT + INDENT)
               .append("int delimPosition = string.indexOf(\"" + DELIM + "\");\n")
               .append(INDENT + INDENT + INDENT + INDENT)
               .append("if (delimPosition == -1) {\n")
               .append(INDENT + INDENT + INDENT + INDENT + INDENT)
               .append("sb.append(string);\n")
               .append(INDENT + INDENT + INDENT + INDENT + INDENT)
               .append("string = null;\n")
               .append(INDENT + INDENT + INDENT + INDENT)
               .append("} else {\n")
               .append(INDENT + INDENT + INDENT + INDENT + INDENT)
               .append("sb.append(string.substring(0, delimPosition + 3));\n")
               .append(INDENT + INDENT + INDENT + INDENT + INDENT)
               .append("sb.append(\"" + ENCODED_DELIM + "\");\n")
               .append(INDENT + INDENT + INDENT + INDENT + INDENT)
               .append("string = string.substring(delimPosition + 3);\n")
               .append(INDENT + INDENT + INDENT + INDENT)
               .append("}\n")
               .append(INDENT + INDENT + INDENT)
               .append("}\n");
         } else {
           sb.append(".append(" + attribute.getName() + ");\n");
         }
         sb.append(INDENT + INDENT).append("}\n");
       } else if ("org.intermine.objectstore.query.ClobAccess".equals(attribute.getType())) {
         sb.append(INDENT + INDENT)
             .append("if (" + attribute.getName() + " != null) {\n")
             .append(INDENT + INDENT + INDENT)
             .append("sb.append(\"" + DELIM + "a" + field.getName() + DELIM + "\" + ")
             .append(attribute.getName() + ".getDbDescription());\n")
             .append(INDENT + INDENT)
             .append("}\n");
       } else {
         sb.append(INDENT)
             .append(INDENT)
             .append("sb.append(\"" + DELIM + "a" + field.getName() + DELIM + "\")")
             .append(".append(" + field.getName() + ");\n");
       }
     } else if (field.isReference()) {
       sb.append(INDENT + INDENT)
           .append("if (" + field.getName() + " != null) {\n")
           .append(INDENT + INDENT + INDENT)
           .append("sb.append(\"" + DELIM + "r" + field.getName() + DELIM + "\")")
           .append(".append(" + field.getName() + ".getId());\n")
           .append(INDENT + INDENT)
           .append("}\n");
     }
   }
   sb.append(INDENT + INDENT).append("return sb;\n").append(INDENT).append("}\n");
   return sb.toString();
 }
コード例 #4
0
ファイル: JavaModelOutput.java プロジェクト: ZFIN/intermine
  /**
   * 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();
  }