Ejemplo n.º 1
0
  private MethodWrapper[] findMethods(TreeLogger logger, List introspectables) {
    HashSet methods = new HashSet();

    for (Iterator it = introspectables.iterator(); it.hasNext(); ) {
      BeanResolver info = (BeanResolver) it.next();
      logger.branch(
          TreeLogger.DEBUG, "Method Scanning: " + info.getType().getQualifiedSourceName(), null);

      try {
        if (info.getProperties().size() == 0) {
          continue;
        }

        Collection<RProperty> pds = info.getProperties().values();

        for (RProperty p : pds) {
          if (p.getReadMethod() != null) {
            p.getReadMethod().hashWithType = true;
            methods.add(p.getReadMethod());
          }

          if (p.getWriteMethod() != null) {
            p.getWriteMethod().hashWithType = true;
            methods.add(p.getWriteMethod());
          }
        }
      } catch (Exception e) {
        logger.log(TreeLogger.ERROR, "Unable to introspect class. Is class a bean?", e);
      }
    }

    MethodWrapper[] results = new MethodWrapper[methods.size()];
    Iterator it = methods.iterator();

    for (int i = 0; it.hasNext(); i++) {
      results[i] = (MethodWrapper) it.next();
    }

    return results;
  }
Ejemplo n.º 2
0
  private void writeBeanDescriptor(
      TreeLogger logger, BeanResolver info, MethodWrapper[] methods, SourceWriter writer) {
    writer.println("new BeanDescriptor() { ");
    writer.indent();
    writer.println("private HashMap lookup;");
    writer.println("private Property[] properties;");
    writer.println("public Property[] getProperties(){");
    writer.indent();

    {
      writer.println("if( this.properties != null ) ");
      writer.indentln("return this.properties;");
      writer.println("this.properties = new Property[" + (info.getProperties().size()) + "];");

      Collection pds = info.getProperties().values();
      String[] propertyNames = new String[pds.size()];
      logger.log(TreeLogger.SPAM, "" + (pds == null), null);

      if (pds != null) {
        int i = 0;

        for (Iterator it = pds.iterator(); it.hasNext(); i++) {
          RProperty p = (RProperty) it.next();
          propertyNames[i] = p.getName();
          writer.println("{");
          writer.indent();

          writer.print("Method readMethod = ");

          if (p.getReadMethod() == null) {
            writer.println("null;");
          } else {
            writer.println(
                this.packageName
                    + "."
                    + this.methodsImplementationName
                    + ".METHOD_"
                    + +this.find(methods, p.getReadMethod())
                    + ";");
          }

          writer.print("Method writeMethod = ");

          if (p.getWriteMethod() == null) {
            writer.println("null;");
          } else {
            writer.println(
                this.packageName
                    + "."
                    + this.methodsImplementationName
                    + ".METHOD_"
                    + +this.find(methods, p.getWriteMethod())
                    + ";");
          }

          logger.log(
              TreeLogger.DEBUG, p.getName() + " " + p.getType().getQualifiedSourceName(), null);

          JType ptype = this.resolveType(p.getType());

          logger.log(
              TreeLogger.DEBUG, p.getName() + " (Erased) " + ptype.getQualifiedSourceName(), null);
          writer.println(
              "this.properties["
                  + (i)
                  + "] = new Property( \""
                  + p.getName()
                  + "\", "
                  + ((p.getType() != null) ? ptype.getQualifiedSourceName() : "Object")
                  + ".class,  readMethod, writeMethod );");
          writer.outdent();
          writer.println("}");
        }
      }

      writer.println("return this.properties;");
    }

    writer.outdent();
    writer.println("} //end getProperties()");
    writer.println("public Property getProperty( String name ) {");
    writer.indent();
    // TODO Rewrite this to a nested if loop using the propertyNames parameter.
    writer.println("Property p = null;");
    writer.println("if( this.lookup != null ) {");
    writer.indentln("p = (Property) lookup.get(name); ");
    writer.println("} else {");
    writer.indent();
    writer.println("this.lookup = new HashMap();");
    writer.println("Property[] props = this.getProperties(); ");
    writer.println("for( int i=0; i < props.length; i++ ) {");
    writer.indent();
    writer.println("this.lookup.put( props[i].getName(), props[i] );");
    writer.outdent();
    writer.println("}");
    writer.println("p = (Property) this.lookup.get(name);");
    writer.outdent();
    writer.println("}");
    writer.println(
        "if( p == null ) throw new RuntimeException(\"Couldn't find property \"+name+\" for "
            + info.getType().getQualifiedSourceName()
            + "\");");
    writer.println("else return p;");
    writer.outdent();
    writer.println("}");

    writer.outdent();
    writer.print("}");
  }