Exemplo n.º 1
0
  /**
   * Uses 'loader' to load class 'clzz', and calls the static method 'method'. If the return value
   * does not equal 'value' (or if an exception is thrown), then a test failure is indicated.
   *
   * <p>If 'value' is null, then no equality check is performed -- the assertion fails only if an
   * exception is thrown.
   */
  protected void assertStaticCallEquals(
      ClassLoader loader, Class clzz, String method, Object value) {
    java.lang.Class<?> cls = null;
    try {
      cls = java.lang.Class.forName(clzz.getName(), true, loader);
    } catch (ClassNotFoundException e) {
    }
    assertNotNull(cls);

    java.lang.reflect.Method m = null;
    try {
      m = cls.getMethod(method);
    } catch (NoSuchMethodException e) {
    }
    assertNotNull(m);

    try {
      Object res = m.invoke(null);
      assertNotNull(res);
      if (value != null) {
        assertEquals(res, value);
      }
    } catch (InvocationTargetException | IllegalAccessException e) {
      fail("Unexpected exception thrown: " + e.getCause(), e.getCause());
    }
  }
Exemplo n.º 2
0
  /**
   * Returns a class which has a static method with the same name as 'method', whose body creates an
   * new instance of 'specimen', casts it to 'iface' (including the type parameters) and invokes
   * 'method' upon it via an invokeinterface instruction with 'args' as function call parameters.
   */
  private Class invokeInterfaceHarness(
      Class specimen, Extends iface, AbstractMethod method, String... args) {
    Interface istub =
        new Interface(
            iface.getType().getName(),
            iface.getType().getAccessFlags(),
            iface.getType().getParameters(),
            null,
            Arrays.asList((Method) method));
    Class cstub = new Class(specimen.getName());

    String params = toJoinedString(args, ", ");

    ConcreteMethod sm =
        new ConcreteMethod(
            "int",
            SourceModel.stdMethodName,
            String.format(
                "return ((%s)(new %s())).%s(%s);",
                iface.toString(), specimen.getName(), method.getName(), params),
            new AccessFlag("public"),
            new AccessFlag("static"));
    sm.suppressWarnings();

    Class ii = new Class("II_" + specimen.getName() + "_" + iface.getType().getName(), sm);
    ii.addCompilationDependency(istub);
    ii.addCompilationDependency(cstub);
    ii.addCompilationDependency(method);
    return ii;
  }
Exemplo n.º 3
0
  /**
   * Returns a class which has a static method with the same name as 'method', whose body creates an
   * new instance of 'specimen' and invokes 'method' upon it via an invokevirtual instruction with
   * 'args' as function call parameters.
   *
   * <p>'returns' is a dummy return value that need only match 'methods' return type (it is only
   * used in the dummy class when compiling IV).
   */
  private Class invokeVirtualHarness(
      Class specimen, ConcreteMethod method, String returns, String... args) {
    Method cm =
        new ConcreteMethod(
            method.getReturnType(),
            method.getName(),
            "return " + returns + ";",
            method.getElements());
    Class stub = new Class(specimen.getName(), cm);

    String params = toJoinedString(args, ", ");

    ConcreteMethod sm =
        new ConcreteMethod(
            method.getReturnType(),
            method.getName(),
            String.format(
                "return (new %s()).%s(%s);", specimen.getName(), method.getName(), params),
            new AccessFlag("public"),
            new AccessFlag("static"));

    Class iv = new Class("IV_" + specimen.getName(), sm);

    iv.addCompilationDependency(stub);
    iv.addCompilationDependency(cm);

    return iv;
  }
Exemplo n.º 4
0
  /**
   * Creates a class which calls target::method(args) via invokevirtual, compiles and loads both the
   * new class and 'target', and then invokes the method. If an exception of type 'exceptionType' is
   * not thrown, then a test failure is indicated.
   */
  public void assertThrows(
      java.lang.Class<?> exceptionType,
      Class target,
      ConcreteMethod method,
      String returns,
      String... args) {

    Compiler compiler = compilerLocal.get();
    compiler.setFlags(compilerFlags());

    Class iv = invokeVirtualHarness(target, method, returns, args);
    ClassLoader loader = compiler.compile(iv, target);

    java.lang.Class<?> cls = null;
    try {
      cls = java.lang.Class.forName(iv.getName(), true, loader);
    } catch (ClassNotFoundException e) {
    }
    assertNotNull(cls);

    java.lang.reflect.Method m = null;
    try {
      m = cls.getMethod(method.getName());
    } catch (NoSuchMethodException e) {
    }
    assertNotNull(m);

    try {
      m.invoke(null);
      fail("Exception should have been thrown");
    } catch (InvocationTargetException | IllegalAccessException e) {
      if (verboseLocal.get() == Boolean.TRUE) {
        System.out.println(e.getCause());
      }
      assertTrue(exceptionType.isAssignableFrom(e.getCause().getClass()));
    }
    compiler.cleanup();
  }