Java Class.isInstance Examples

Java Class.isInstance - 30 examples found. These are the top rated real world Java examples of Class.isInstance extracted from open source projects. You can rate examples to help us improve the quality of examples.
  public static FileDescriptor getDescriptorFromChannel(Channel channel) {
    if (SEL_CH_IMPL_GET_FD != null && SEL_CH_IMPL.isInstance(channel)) {
      // Pipe Source and Sink, Sockets, and other several other selectable channels
      try {
        return (FileDescriptor) SEL_CH_IMPL_GET_FD.invoke(channel);
      } catch (Exception e) {
        // return bogus below
      }
    } else if (FILE_CHANNEL_IMPL_FD != null && FILE_CHANNEL_IMPL.isInstance(channel)) {
      // FileChannels
      try {
        return (FileDescriptor) FILE_CHANNEL_IMPL_FD.get(channel);
      } catch (Exception e) {
        // return bogus below
      }
    } else if (FILE_DESCRIPTOR_FD != null) {
      FileDescriptor unixFD = new FileDescriptor();

      // UNIX sockets, from jnr-unixsocket
      try {
        if (channel instanceof UnixSocketChannel) {
          FILE_DESCRIPTOR_FD.set(unixFD, ((UnixSocketChannel) channel).getFD());
          return unixFD;
        } else if (channel instanceof UnixServerSocketChannel) {
          FILE_DESCRIPTOR_FD.set(unixFD, ((UnixServerSocketChannel) channel).getFD());
          return unixFD;
        }
      } catch (Exception e) {
        // return bogus below
      }
    }
    return new FileDescriptor();
  }
Example #2
0
  @SuppressWarnings("unchecked")
  @Override
  public <T> T unwrap(Class<T> iface) throws SQLException {
    if (iface == null) {
      return null;
    }

    if (iface == Connection.class) {
      if (conn instanceof ConnectionProxy) {
        return conn.unwrap(iface);
      }

      return (T) conn;
    }

    if (iface.isInstance(conn)) {
      return (T) conn;
    }

    if (iface.isInstance(this)) {
      return (T) this;
    }

    return conn.unwrap(iface);
  }
  /**
   * Copied from Util.getAdapter (from eclipse 3.3: not available in eclipse 3.2)
   *
   * <p>If it is possible to adapt the given object to the given type, this returns the adapter.
   * Performs the following checks:
   *
   * <ol>
   *   <li>Returns <code>sourceObject</code> if it is an instance of the adapter type.
   *   <li>If sourceObject implements IAdaptable, it is queried for adapters.
   *   <li>If sourceObject is not an instance of PlatformObject (which would have already done so),
   *       the adapter manager is queried for adapters
   * </ol>
   *
   * Otherwise returns null.
   *
   * @param sourceObject object to adapt, or null
   * @param adapterType type to adapt to
   * @return a representation of sourceObject that is assignable to the adapter type, or null if no
   *     such representation exists
   */
  public static Object utilGetAdapter(Object sourceObject, Class adapterType) {
    Assert.isNotNull(adapterType);
    if (sourceObject == null) {
      return null;
    }
    if (adapterType.isInstance(sourceObject)) {
      return sourceObject;
    }

    if (sourceObject instanceof IAdaptable) {
      IAdaptable adaptable = (IAdaptable) sourceObject;

      Object result = adaptable.getAdapter(adapterType);
      if (result != null) {
        // Sanity-check
        Assert.isTrue(adapterType.isInstance(result));
        return result;
      }
    }

    if (!(sourceObject instanceof PlatformObject)) {
      Object result = Platform.getAdapterManager().getAdapter(sourceObject, adapterType);
      if (result != null) {
        return result;
      }
    }

    return null;
  }
 /**
  * Returns parent component with this type
  *
  * @param <T> searching type
  * @param component child component
  * @param type parent type
  * @return found parent
  */
 protected <T> T getParentComponent(final Component component, final Class<T> type) {
   Component comp = component;
   while (!(type.isInstance(comp)) && comp.getParent() != null) {
     comp = comp.getParent();
   }
   return (comp == null || !(type.isInstance(comp))) ? null : type.cast(comp);
 }
Example #5
0
  /**
   * Passe un peuple se trouvant sur le jeu dans le tas
   *
   * @param peuple Peuple à remettre
   */
  public void remettreBoite(Peuple peuple) {
    // Recherche de la classe du peuple
    Iterator<Class<? extends Peuple>> it = peuplesPris.iterator();
    Class<? extends Peuple> peupleClass = null;

    while (it.hasNext() && peupleClass == null) {
      Class<? extends Peuple> tmp = it.next();

      if (tmp.isInstance(peuple)) {
        peupleClass = tmp;
      }
    }

    peuplesPris.remove(peupleClass);
    peuplesDispo.add(peupleClass);

    // Recherche de la classe du pouvoir
    Iterator<Class<? extends Pouvoir>> ite = pouvoirsPris.iterator();
    Class<? extends Pouvoir> pouvoirClass = null;

    while (ite.hasNext() && pouvoirClass == null) {
      Class<? extends Pouvoir> tmp = ite.next();

      if (tmp.isInstance(peuple.getPouvoir())) {
        pouvoirClass = tmp;
      }
    }

    pouvoirsPris.remove(pouvoirClass);
    pouvoirsDispo.add(pouvoirClass);
  }
 public static <E1 extends Exception, E2 extends Exception, E3 extends Exception>
     ValueOrException3<E1, E2, E3> downconvert(
         ValueOrException4<E1, E2, E3, BottomException> voe,
         Class<E1> exceptionClass1,
         Class<E2> exceptionClass2,
         Class<E3> exceptionClass3) {
   Exception e = voe.getException();
   if (e == null) {
     return new ValueOrException3ValueImpl<>(voe.getValue());
   }
   if (exceptionClass1.isInstance(e)) {
     return new ValueOrException3Exn1Impl<>(exceptionClass1.cast(e));
   }
   if (exceptionClass2.isInstance(e)) {
     return new ValueOrException3Exn2Impl<>(exceptionClass2.cast(e));
   }
   if (exceptionClass3.isInstance(e)) {
     return new ValueOrException3Exn3Impl<>(exceptionClass3.cast(e));
   }
   throw new IllegalStateException(
       "shouldn't reach here "
           + e.getClass()
           + " "
           + exceptionClass1
           + " "
           + exceptionClass2
           + " "
           + exceptionClass3,
       e);
 }