Пример #1
0
 /**
  * Casts this {@code Class} to represent a subclass of the specified class. If successful, this
  * {@code Class} is returned; otherwise a {@code ClassCastException} is thrown.
  *
  * @param clazz the required type.
  * @return this {@code Class} cast as a subclass of the given type.
  * @throws ClassCastException if this {@code Class} cannot be cast to the specified type.
  */
 @SuppressWarnings("unchecked")
 public <U> Class<? extends U> asSubclass(Class<U> clazz) {
   if (clazz.isAssignableFrom(this)) {
     return (Class<? extends U>) this;
   }
   String actualClassName = this.getName();
   String desiredClassName = clazz.getName();
   throw new ClassCastException(actualClassName + " cannot be cast to " + desiredClassName);
 }
Пример #2
0
  /**
   * The abstract environment factory implementation is adaptable. The default implementation adapts
   * to and interface actually implemented by the receiver.
   *
   * <p>Subclasses may override or extend this implementation.
   *
   * @since 1.2
   */
  @SuppressWarnings("unchecked")
  public <T> T getAdapter(java.lang.Class<T> adapterType) {
    T result;

    if (adapterType.isAssignableFrom(getClass())) {
      result = (T) this;
    } else {
      result = null;
    }

    return result;
  }
Пример #3
0
 protected void assertInvokeInterfaceThrows(
     java.lang.Class<? extends Throwable> errorClass,
     Class target,
     Extends iface,
     AbstractMethod method,
     String... args) {
   try {
     assertInvokeInterfaceEquals(0, target, iface, method, args);
     fail("Expected exception: " + errorClass);
   } catch (AssertionError e) {
     Throwable cause = e.getCause();
     if (cause == null) throw e;
     else if ((errorClass.isAssignableFrom(cause.getClass()))) {
       // this is success
       return;
     } else throw e;
   }
 }
Пример #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();
  }
 // #ifdef JAVA6
 public boolean isWrapperFor(java.lang.Class<?> iface) throws java.sql.SQLException {
   return (iface != null && iface.isAssignableFrom(this.getClass()));
 }
Пример #6
0
 @SuppressWarnings("unchecked")
 public <U> Class<? extends U> asSubclass(Class<U> klass) {
   if (!klass.isAssignableFrom(this)) throw new ClassCastException();
   return (Class<? extends U>) this;
 }