private static boolean isValidCustomFieldSerializerMethod(JMethod method) {
    if (method == null) {
      return false;
    }

    if (!method.isStatic()) {
      return false;
    }

    if (!method.isPublic()) {
      return false;
    }

    return true;
  }
  private void createFindMethod(SourceWriter sourceWriter) {

    // We create more than one findMethod as the body of one method would be too large. This is the
    // int that we
    // increment to add to the name
    // i.e. findMethod0()
    int methodNum = 0;

    // This int keeps track of how many methods are generated. When it gets to 200 we create a new
    // findMethodX()
    // and chain it to the previous.
    int methodCount = 0;

    sourceWriter.println(
        "private GwtBindingMethod findOrCreateMethod(String obj, String methodName){ ");
    sourceWriter.indent();

    sourceWriter.println("GwtBindingMethod newMethod;");

    // dummy first condition, rest are "else if". Keeps us from having conditional logic.
    sourceWriter.println("if(false){ }");

    for (JClassType type : implementingTypes) {

      // close last method, chain it to a new one.
      if (methodCount > 200) {
        sourceWriter.println("return findOrCreateMethod" + (methodNum) + "(obj, methodName);");
        sourceWriter.println("}");

        sourceWriter.println(
            "private GwtBindingMethod findOrCreateMethod"
                + (methodNum++)
                + "(String obj, String methodName){ ");
        sourceWriter.println("GwtBindingMethod newMethod;");
        // dummy first condition, rest are "else if". Keeps us from having conditional logic.
        sourceWriter.println("if(false){ }");

        methodCount = 0;
      }

      String keyRoot = generateTypeKey(type);

      // if(type.isAbstract()){
      // System.out.println("abstract");
      // continue;
      // }

      // determine if there are any methods that are bindable before writing out conditional for
      // class

      JClassType loopType = type;
      boolean hasBindableMethods = false;
      JClassType eventSourceType = null;
      try {
        eventSourceType = typeOracle.getType("org.pentaho.ui.xul.XulEventSource");
      } catch (NotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
      // CHECKSTYLE IGNORE Indentation FOR NEXT 1 LINES
      outer:
      while (loopType.getSuperclass() != null
          && loopType.getSimpleSourceName().equals("Object") == false
          && loopType.isAssignableTo(eventSourceType)) {

        for (JMethod m : loopType.getMethods()) {
          if (m.isPublic() && m.getAnnotation(Bindable.class) != null) {
            hasBindableMethods = true;
            break outer;
          }
        }
        loopType = loopType.getSuperclass();
      }
      if (hasBindableMethods == false) {
        continue;
      }
      sourceWriter.println("else if(obj.equals(\"" + type.getQualifiedSourceName() + "\")){ ");
      try {

        loopType = type;
        sourceWriter.indent();

        // Loop over class heirarchy and generate methods for every object that is declared a
        // XulEventSource
        while (loopType.getSuperclass() != null
            && loopType.getSimpleSourceName().equals("Object") == false
            && loopType.isAssignableTo(eventSourceType)) {
          String superName = generateTypeKey(loopType);

          boolean first = true;
          for (JMethod m : loopType.getMethods()) {
            methodCount++;
            if (!m.isPublic() || m.getAnnotation(Bindable.class) == null) {
              continue;
            }

            sourceWriter.println(
                (first ? "" : "else ") + "if(methodName.equals(\"" + m.getName() + "\")){ ");
            if (first) {
              first = false;
            }
            sourceWriter.indent();

            String methodName = m.getName();

            // check to see if we've already processed this classes' method. Point to that class
            // instead.
            if (generatedMethods.contains((superName + "_" + methodName).toLowerCase())
                && type != loopType) {

              sourceWriter.println("return findOrCreateMethod(\"" + superName + "\", methodName);");

            } else {
              // See if it's already been created and cached. If so, return that.
              String keyName = (keyRoot + "_" + methodName).toLowerCase();

              sourceWriter.println(
                  "GwtBindingMethod found = wrappedTypes.get(\"" + keyName + "\");");
              sourceWriter.println("if(found != null){");
              sourceWriter.indent();
              sourceWriter.println("return found;");
              sourceWriter.outdent();
              sourceWriter.println("} else {");
              sourceWriter.indent();

              // Not cached, create a new instance and put it in the cache.
              sourceWriter.println("newMethod = new GwtBindingMethod(){");

              sourceWriter.println(
                  "public Object invoke(Object obj, Object[] args) throws XulException { ");
              sourceWriter.indent();
              sourceWriter.println("try{");
              sourceWriter.println(
                  loopType.getQualifiedSourceName()
                      + " target = ("
                      + loopType.getQualifiedSourceName()
                      + ") obj;");

              JParameter[] params = m.getParameters();
              String argList = "";
              int pos = 0;
              for (JParameter param : params) {
                if (pos > 0) {
                  argList += ", ";
                }
                argList += "(" + getTypeName(param.getType()) + ") args[" + pos + "]";
                pos++;
              }

              if (isVoidReturn(m.getReturnType())) {
                sourceWriter.println("target." + methodName + "(" + argList + ");");
                sourceWriter.println("return null;");
              } else {
                sourceWriter.println(
                    "return " + boxReturnType(m) + " target." + methodName + "(" + argList + ");");
              }

              sourceWriter.println(
                  "}catch(Exception e){ e.printStackTrace(); throw new XulException(\"error with "
                      + type.getQualifiedSourceName()
                      + "\"+e.getMessage());}");
              sourceWriter.println("}");

              sourceWriter.outdent();
              sourceWriter.println("};");

              // Add it to the HashMap cache as type and decendant type if available.
              sourceWriter.println("wrappedTypes.put((\"" + keyName + "\"), newMethod);");
              if (keyRoot.equals(superName) == false) {
                sourceWriter.println("wrappedTypes.put((\"" + keyName + "\"), newMethod);");
              }
              generatedMethods.add((keyRoot + "_" + methodName).toLowerCase());
              generatedMethods.add((superName + "_" + methodName).toLowerCase());

              sourceWriter.println("return newMethod;");

              sourceWriter.outdent();
              sourceWriter.println("}");
            }
            sourceWriter.outdent();

            sourceWriter.println("}");
          }

          // go up a level in the heirarchy and check again.
          loopType = loopType.getSuperclass();
        }
        sourceWriter.outdent();
        sourceWriter.println("}");

      } catch (Exception e) {

        // record to logger that Map generation threw an exception
        logger.log(TreeLogger.ERROR, "PropertyMap ERROR!!!", e);
      }
    }

    sourceWriter.outdent();

    // This is the end of the line, if not found return null.
    sourceWriter.println("return null;");
    sourceWriter.println("}");
  }
Example #3
0
 public boolean isPublic() {
   return method.isPublic();
 }