예제 #1
0
파일: GWTClass.java 프로젝트: wclaeys/errai
  @Override
  public MetaMethod[] getMethods() {
    final Set<MetaMethod> meths = new LinkedHashSet<MetaMethod>();
    meths.addAll(getSpecialTypeMethods());

    JClassType type = getEnclosedMetaObject().isClassOrInterface();
    if (type == null) {
      return null;
    }

    do {
      for (final JMethod jMethod : type.getMethods()) {
        if (!jMethod.isPrivate()) {
          meths.add(new GWTMethod(oracle, jMethod));
        }
      }

      for (final JClassType interfaceType : type.getImplementedInterfaces()) {
        meths.addAll(Arrays.asList(GWTClass.newInstance(oracle, interfaceType).getMethods()));
      }
    } while ((type = type.getSuperclass()) != null
        && !type.getQualifiedSourceName().equals("java.lang.Object"));
    meths.addAll(overrideMethods);

    return meths.toArray(new MetaMethod[meths.size()]);
  }
예제 #2
0
 Collection<JMethod> extractMethods(
     TreeLogger logger, SourceBuilder<ModelMagic> sb, GeneratorContext ctx, JClassType type)
     throws UnableToCompleteException {
   assert type.isInterface() != null;
   ModelMagic models = sb.getPayload();
   Map<String, JMethod> uniqueMethods = new LinkedHashMap<String, JMethod>();
   JMethod[] existing = type.getInheritableMethods();
   Set<? extends JClassType> hierarchy = type.getFlattenedSupertypeHierarchy();
   if (ModelGeneratorGwt.allAbstract(existing)) {
     // still an interface; grab our root type.
     existing = models.getRootType(logger, ctx).getInheritableMethods();
   }
   for (JMethod method : existing) {
     if (!method.isAbstract())
       uniqueMethods.put(xapi.dev.model.ModelGeneratorGwt.toSignature(method), method);
   }
   boolean debug = logger.isLoggable(Type.DEBUG);
   for (JClassType next : hierarchy) {
     if (next.isInterface() != null) {
       sb.getClassBuffer().addInterfaces(next.getQualifiedSourceName());
       for (JMethod method : next.getMethods()) {
         String sig = ModelGeneratorGwt.toSignature(method);
         if (!uniqueMethods.containsKey(sig)) uniqueMethods.put(sig, method);
       }
     } else {
       for (JMethod method : next.getMethods()) {
         String sig = ModelGeneratorGwt.toSignature(method);
         if (uniqueMethods.containsKey(sig)) {
           if (debug)
             logger.log(
                 Type.WARN,
                 "Found multiple model methods for "
                     + type.getName()
                     + "::"
                     + sig
                     + "; preferring method from "
                     + uniqueMethods.get(sig).getEnclosingType().getName());
         } else {
           uniqueMethods.put(sig, method);
         }
       }
     }
   }
   return uniqueMethods.values();
 }
예제 #3
0
파일: GWTClass.java 프로젝트: wclaeys/errai
  @Override
  public MetaMethod[] getDeclaredMethods() {
    final JClassType type = getEnclosedMetaObject().isClassOrInterface();
    if (type == null) {
      return null;
    }

    return fromMethodArray(oracle, type.getMethods());
  }
예제 #4
0
 /**
  * @param srcWriter
  * @param type
  * @param parentVariable
  * @param added
  * @param iocContainerVariable
  * @param configurations
  */
 private static void injectMethods(
     SourcePrinter srcWriter,
     JClassType type,
     String parentVariable,
     Set<String> added,
     String iocContainerVariable,
     Map<String, IocConfig<?>> configurations) {
   for (JMethod method : type.getMethods()) {
     Inject inject = method.getAnnotation(Inject.class);
     if (inject != null && !method.isStatic()) {
       String methodName = method.getName();
       if (!added.contains(methodName + "()")) {
         added.add(methodName + "()");
         JParameter[] parameters = method.getParameters();
         List<String> params = new ArrayList<String>();
         for (JParameter parameter : parameters) {
           JType parameterType = parameter.getType();
           if ((parameterType.isPrimitive() != null)) {
             throw new IoCException(
                 "IoC Error Method ["
                     + methodName
                     + "] from class ["
                     + type.getQualifiedSourceName()
                     + "] declares an invalid parameter. Primitive types are not allowed here");
           }
           String variableName = "parameter_" + methodName + "_" + parameter.getName();
           params.add(variableName);
           String injectionExpression =
               getParameterInjectionExpression(parameter, iocContainerVariable, configurations);
           srcWriter.println(
               parameterType.getQualifiedSourceName()
                   + " "
                   + variableName
                   + " = "
                   + injectionExpression
                   + ";");
         }
         srcWriter.print(parentVariable + "." + methodName + "(");
         boolean first = true;
         for (String param : params) {
           if (!first) {
             srcWriter.print(", ");
           }
           first = false;
           srcWriter.print(param);
         }
         srcWriter.println(");");
       }
     }
   }
 }
  /** Find the to-be-exported method within a class. */
  private JMethod findExportedMethod(TreeLogger logger, JClassType clazz)
      throws UnableToCompleteException {

    JMethod[] methods = clazz.getMethods();
    if (methods.length == 0) {
      logger.log(
          TreeLogger.ERROR, "The JSFunction interface did not " + "declare any functions.", null);
      throw new UnableToCompleteException();
    } else if (methods.length == 1) {
      return methods[0];
    }

    try {
      JMethod toReturn = null;
      for (JMethod method : methods) {
        if (JSWrapperGenerator.hasTag(logger, method, Exported.class) != null) {
          if (toReturn == null) {
            toReturn = method;
          } else {
            // Can't declare two methods with export annotations
            throw new UnableToCompleteException();
          }
        }
      }
      if (toReturn != null) {
        return toReturn;
      } else {
        // Didn't find any methods with an export annotation
        throw new UnableToCompleteException();
      }
    } catch (UnableToCompleteException e) {
      logger.log(
          TreeLogger.ERROR,
          "JSFunctions with multiple methods must "
              + " specify exactly one method with an "
              + Exported.class.getName()
              + " annotation.",
          null);
      throw e;
    }
  }
예제 #6
0
  @Override
  public void visitClientRpc(TreeLogger logger, JClassType type, ConnectorBundle bundle)
      throws UnableToCompleteException {
    checkGenericType(logger, type);
    Set<? extends JClassType> hierarchy = type.getFlattenedSupertypeHierarchy();
    for (JClassType subType : hierarchy) {
      JMethod[] methods = subType.getMethods();
      for (JMethod method : methods) {
        checkReturnType(logger, method);

        bundle.setNeedsInvoker(type, method);
        bundle.setNeedsParamTypes(type, method);
        if (method.getAnnotation(NoLayout.class) != null) {
          bundle.setMethodAttribute(type, method, MethodAttribute.NO_LAYOUT);
        }

        JType[] parameterTypes = method.getParameterTypes();
        for (JType paramType : parameterTypes) {
          bundle.setNeedsSerialize(paramType);
        }
      }
    }
  }
  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("}");
  }
예제 #8
0
  private RequestQueueModel collectModel(
      TreeLogger logger, GeneratorContext context, JClassType toGenerate)
      throws UnableToCompleteException {
    RequestQueueModel.Builder rqBuilder = new RequestQueueModel.Builder();

    TypeOracle typeOracle = context.getTypeOracle();
    JClassType requestQueue = typeOracle.findType(RequestQueue.class.getName());
    JClassType asyncCallback = typeOracle.findType(AsyncCallback.class.getName());
    JClassType voidType = typeOracle.findType(Void.class.getName());
    rqBuilder.setRequestQueueInterfaceName(toGenerate.getParameterizedQualifiedSourceName());

    AsyncServiceModel.Builder serviceBuilder = new AsyncServiceModel.Builder();
    for (JMethod m : toGenerate.getMethods()) {
      TreeLogger serviceLogger =
          logger.branch(Type.DEBUG, "Reading async service " + m.getReadableDeclaration());

      // Skip those defined at RequestQueue
      if (m.getEnclosingType().equals(requestQueue)) {
        continue;
      }
      JClassType returnType = m.getReturnType().isClassOrInterface();
      if (returnType == null) {
        serviceLogger.log(Type.ERROR, "Unexpected method return type " + returnType);
        throw new UnableToCompleteException();
      }

      serviceBuilder.setAsyncServiceInterfaceName(returnType.getParameterizedQualifiedSourceName());
      serviceBuilder.setDeclaredMethodName(m.getName());

      Service serviceAnnotation = m.getAnnotation(Service.class);
      if (serviceAnnotation == null) {
        serviceLogger.log(Type.ERROR, "Missing @Service annotation");
        throw new UnableToCompleteException();
      }
      serviceBuilder.setService(serviceAnnotation.value().getName());

      AsyncServiceMethodModel.Builder methodBuilder = new AsyncServiceMethodModel.Builder();
      for (JMethod asyncMethod : m.getReturnType().isClassOrInterface().getMethods()) {
        TreeLogger methodLogger =
            serviceLogger.branch(
                Type.DEBUG, "Reading service method " + asyncMethod.getReadableDeclaration());

        List<JType> types = new ArrayList<JType>();
        methodBuilder.setReturnTypeName(voidType.getParameterizedQualifiedSourceName());
        boolean asyncFound = false;
        for (JType param : asyncMethod.getParameterTypes()) {
          if (asyncFound) {
            methodLogger.log(
                Type.WARN, "Already passed an AsyncCallback param - is that what you meant?");
          }
          if (param.isClassOrInterface() != null
              && param.isClassOrInterface().isAssignableTo(asyncCallback)) {
            JClassType boxedReturnType =
                ModelUtils.findParameterizationOf(asyncCallback, param.isClassOrInterface())[0];
            methodBuilder
                .setHasCallback(true)
                .setReturnTypeName(boxedReturnType.getParameterizedQualifiedSourceName());
            asyncFound = true;
            continue; // should be last, check for this...
          }
          types.add(param);
        }
        Set<JClassType> throwables = new HashSet<JClassType>();
        Throws t = asyncMethod.getAnnotation(Throws.class);
        if (t != null) {
          for (Class<? extends Throwable> throwable : t.value()) {
            throwables.add(typeOracle.findType(throwable.getName()));
          }
        }

        methodBuilder
            .setMethodName(asyncMethod.getName())
            .setArgTypes(types)
            .setThrowables(throwables);

        serviceBuilder.addMethod(methodBuilder.build());
      }
      rqBuilder.addService(serviceBuilder.build());
    }

    return rqBuilder.build();
  }