Exemplo n.º 1
0
  @Override
  public Type type(Context ctx) {
    List<Class<?>> argumentClasses = new ArrayList<>();
    List<Type> argumentTypes = new ArrayList<>();
    for (Expression argument : arguments) {
      argumentTypes.add(argument.type(ctx));
      if (argument.type(ctx).equals(getType(Object[].class))) {
        argumentClasses.add(Object[].class);
      } else {
        argumentClasses.add(getJavaType(ctx.getClassLoader(), argument.type(ctx)));
      }
    }
    Type returnType;
    try {
      if (ctx.getThisType().equals(owner.type(ctx))) {
        for (org.objectweb.asm.commons.Method method : ctx.getMethods().keySet()) {
          if (method.getName().equals(methodName)) {
            if (method.getArgumentTypes().length == arguments.size()) {
              Type[] methodTypes = method.getArgumentTypes();
              boolean isSame = true;
              for (int i = 0; i < arguments.size(); i++) {
                if (!methodTypes[i].equals(argumentTypes.get(i))) {
                  isSame = false;
                  break;
                }
              }
              if (isSame) return method.getReturnType();
            }
          }
        }
        throw new NoSuchMethodException();
      } else {
        Class<?> ownerJavaType = getJavaType(ctx.getClassLoader(), owner.type(ctx));
        Method method =
            ownerJavaType.getMethod(methodName, argumentClasses.toArray(new Class<?>[] {}));
        Class<?> returnClass = method.getReturnType();
        returnType = getType(returnClass);
      }

    } catch (NoSuchMethodException e) {
      throw new RuntimeException(
          format(
              "No method %s.%s(%s). %s",
              owner.type(ctx).getClassName(),
              methodName,
              (!argumentClasses.isEmpty() ? argsToString(argumentClasses) : ""),
              exceptionInGeneratedClass(ctx)));
    }

    return returnType;
  }
Exemplo n.º 2
0
  @Override
  public Type load(Context ctx) {
    GeneratorAdapter g = ctx.getGeneratorAdapter();

    owner.load(ctx);

    List<Class<?>> argumentClasses = new ArrayList<>();
    List<Type> argumentTypes = new ArrayList<>();
    for (Expression argument : arguments) {
      argument.load(ctx);
      argumentTypes.add(argument.type(ctx));
      argumentClasses.add(getJavaType(ctx.getClassLoader(), argument.type(ctx)));
    }

    Type returnType;
    try {
      if (ctx.getThisType().equals(owner.type(ctx))) {
        org.objectweb.asm.commons.Method method = null;
        for (org.objectweb.asm.commons.Method m : ctx.getMethods().keySet()) {
          if (m.getName().equals(methodName)) {
            if (m.getArgumentTypes().length == arguments.size()) {
              Type[] methodTypes = m.getArgumentTypes();
              boolean isSame = true;
              for (int i = 0; i < arguments.size(); i++) {
                if (!methodTypes[i].equals(argumentTypes.get(i))) {
                  isSame = false;
                  break;
                }
              }
              if (isSame) {
                method = m;
                break;
              }
            }
          }
        }
        if (method == null) throw new NoSuchMethodException();
        g.invokeVirtual(owner.type(ctx), method);
        return method.getReturnType();
      }
      Class<?> ownerJavaType = getJavaType(ctx.getClassLoader(), owner.type(ctx));
      Method method =
          ownerJavaType.getMethod(methodName, argumentClasses.toArray(new Class<?>[] {}));
      Class<?> returnClass = method.getReturnType();
      returnType = getType(returnClass);

      invokeVirtualOrInterface(
          g,
          ownerJavaType,
          new org.objectweb.asm.commons.Method(
              methodName, returnType, argumentTypes.toArray(new Type[] {})));

    } catch (NoSuchMethodException e) {
      throw new RuntimeException(
          format(
              "No method %s.%s(%s). %s",
              owner.type(ctx).getClassName(),
              methodName,
              (!argumentClasses.isEmpty() ? argsToString(argumentClasses) : ""),
              exceptionInGeneratedClass(ctx)));
    }
    return returnType;
  }