Example usage for java.lang Class isInstance

List of usage examples for java.lang Class isInstance

Introduction

In this page you can find the example usage for java.lang Class isInstance.

Prototype

@HotSpotIntrinsicCandidate
public native boolean isInstance(Object obj);

Source Link

Document

Determines if the specified Object is assignment-compatible with the object represented by this Class .

Usage

From source file:org.jruby.rack.mock.WebUtils.java

/**
 * Return an appropriate request object of the specified type, if available,
 * unwrapping the given request as far as necessary.
 * @param request the servlet request to introspect
 * @param requiredType the desired type of request object
 * @return the matching request object, or {@code null} if none
 * of that type is available/* w w w  . ja v  a  2s.c  o  m*/
 */
@SuppressWarnings("unchecked")
public static <T> T getNativeRequest(ServletRequest request, Class<T> requiredType) {
    if (requiredType != null) {
        if (requiredType.isInstance(request)) {
            return (T) request;
        } else if (request instanceof ServletRequestWrapper) {
            return getNativeRequest(((ServletRequestWrapper) request).getRequest(), requiredType);
        }
    }
    return null;
}

From source file:org.jruby.rack.mock.WebUtils.java

/**
 * Return an appropriate response object of the specified type, if available,
 * unwrapping the given response as far as necessary.
 * @param response the servlet response to introspect
 * @param requiredType the desired type of response object
 * @return the matching response object, or {@code null} if none
 * of that type is available/*w  w  w. j ava  2 s . c om*/
 */
@SuppressWarnings("unchecked")
public static <T> T getNativeResponse(ServletResponse response, Class<T> requiredType) {
    if (requiredType != null) {
        if (requiredType.isInstance(response)) {
            return (T) response;
        } else if (response instanceof ServletResponseWrapper) {
            return getNativeResponse(((ServletResponseWrapper) response).getResponse(), requiredType);
        }
    }
    return null;
}

From source file:net.pms.newgui.LooksFrame.java

/**
 * Safely checks whether a particular look and feel class is set.
 *
 * @param lnf//from  w  w  w.  java  2 s .co  m
 * @param lookAndFeelClassPath
 * @return whether the incoming look and feel class is set
 */
private static boolean isParticularLaFSet(LookAndFeel lnf, String lookAndFeelClassPath) {
    // as of Java 10, com.sun.java.swing.plaf.windows.WindowsLookAndFeel
    // is no longer available on macOS
    // thus "instanceof WindowsLookAndFeel" directives will result
    // in a NoClassDefFoundError during runtime
    if (lnf == null) {
        return false;
    } else {
        try {
            Class c = Class.forName(lookAndFeelClassPath);
            return c.isInstance(lnf);
        } catch (ClassNotFoundException cnfe) {
            // if it is not possible to load the Windows LnF class, the
            // given lnf instance cannot be an instance of the Windows
            // LnF class
            return false;
        }
    }
}

From source file:org.paxml.util.ReflectUtils.java

/**
 * Coerce object type./*  w w  w .j a v  a  2  s .c om*/
 * 
 * @param <T>
 *            the expected type
 * @param from
 *            from object
 * @param expectedType
 *            to object type
 * @return the to object
 */
public static <T> T coerceType(Object from, Class<? extends T> expectedType) {
    if (from == null) {
        return null;
    }
    Object targetValue = null;
    if (expectedType.isInstance(from)) {
        targetValue = from;
    } else if (expectedType.isEnum()) {
        for (Object e : expectedType.getEnumConstants()) {
            if (from.toString().equalsIgnoreCase(e.toString())) {
                targetValue = e;
                break;
            }
        }
        if (targetValue == null) {
            throw new PaxmlRuntimeException(
                    "No enum named '" + from + "' is defined in class: " + expectedType);
        }
    } else if (List.class.equals(expectedType)) {
        targetValue = new ArrayList();
        collect(from, (Collection) targetValue, true);
    } else if (Set.class.equals(expectedType)) {
        targetValue = new LinkedHashSet();
        collect(from, (Collection) targetValue, true);
    } else if (isImplementingClass(expectedType, Collection.class, false)) {
        try {
            targetValue = expectedType.newInstance();
        } catch (Exception e) {
            throw new PaxmlRuntimeException(e);
        }
        collect(from, (Collection) targetValue, true);
    } else if (Iterator.class.equals(expectedType)) {
        List list = new ArrayList();
        collect(from, list, true);
        targetValue = list.iterator();
    } else if (isImplementingClass(expectedType, Coerceable.class, false)) {

        try {
            Constructor c = expectedType.getConstructor(Object.class);
            targetValue = c.newInstance(from);
        } catch (Exception e) {
            throw new PaxmlRuntimeException(e);
        }
    } else if (from instanceof Map) {
        return mapToBean((Map) from, expectedType);
    } else {
        targetValue = ConvertUtils.convert(from.toString(), expectedType);
    }

    return (T) targetValue;
}

From source file:com.gamesalutes.utils.ByteUtils.java

/**
 * Deserializes a string serialized using {@link #stringSerialize}.
 * // w w  w  .j ava2 s. co  m
 * @param data the serialized string data
 * @param type the type of the deserialized object
 * @throws Exception if error occurs during deserialization
 */
public static <T> T stringDeserialize(String data, Class<T> type) throws Exception {
    if (MiscUtils.isEmpty(data))
        return null;
    // decode the Base64 String into the raw object bytes
    byte[] bytes = Base64.decodeBase64(data.getBytes("UTF-8"));
    Object object = ByteUtils.readObject(bytes);
    if (!type.isInstance(object)) {
        throw new IllegalArgumentException(
                MiscUtils.getClassName(object) + " : " + object + " is not: " + type.getName());
    }
    return (T) object;
}

From source file:org.apache.taverna.databundle.DataBundles.java

private static <T> Stream<T> resolveItemAsStream(Path path, Class<T> type, ResolveOptions options)
        throws UncheckedIOException {
    try {//  www .jav  a 2s  . c om
        Object value = resolve(path, options);
        if (type.isInstance(value)) {
            return Stream.of(type.cast(value));
        }
        return Stream.empty();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:info.magnolia.jcr.util.NodeUtil.java

public static boolean isWrappedWith(Node node, Class<? extends DelegateNodeWrapper> wrapper) {
    if (wrapper.isInstance(node)) {
        return true;
    }/*from   ww  w. j  a  v  a  2s. c  om*/

    if (node instanceof DelegateNodeWrapper) {
        return isWrappedWith(((DelegateNodeWrapper) node).getWrappedNode(), wrapper);
    }
    return false;
}

From source file:org.web4thejob.web.util.ZkUtil.java

@SuppressWarnings("unchecked")
public static <T extends org.web4thejob.web.panel.Panel> void appendPanelsOfType(Component parent,
        Class<T> panelType, List<T> list) {
    for (Component child : parent.getChildren()) {
        if (panelType.isInstance(child.getAttribute(OWNING_PANEL))) {
            list.add((T) child.getAttribute(OWNING_PANEL));
        }/*w w w.ja va2 s.c  o m*/
        appendPanelsOfType(child, panelType, list);
    }
}

From source file:com.amalto.workbench.compare.Utilities.java

private static ArrayList internalGetResources(ISelection selection, Class type) {
    ArrayList tmp = new ArrayList();
    if (selection instanceof IStructuredSelection) {
        Object[] s = ((IStructuredSelection) selection).toArray();

        for (int i = 0; i < s.length; i++) {
            IResource resource = null;/*from   w  ww  .  j  a  va  2  s .c  o  m*/
            Object o = s[i];
            if (type.isInstance(o)) {
                resource = (IResource) o;

            } else if (o instanceof ResourceMapping) {
                try {
                    ResourceTraversal[] travs = ((ResourceMapping) o)
                            .getTraversals(ResourceMappingContext.LOCAL_CONTEXT, null);
                    if (travs != null) {
                        for (int k = 0; k < travs.length; k++) {
                            IResource[] resources = travs[k].getResources();
                            for (int j = 0; j < resources.length; j++) {
                                if (type.isInstance(resources[j]) && resources[j].isAccessible())
                                    tmp.add(resources[j]);
                            }
                        }
                    }
                } catch (CoreException ex) {
                    log.error(ex.getMessage(), ex);
                }
            } else if (o instanceof IAdaptable) {
                IAdaptable a = (IAdaptable) o;
                Object adapter = a.getAdapter(IResource.class);
                if (type.isInstance(adapter))
                    resource = (IResource) adapter;
            }

            if (resource != null && resource.isAccessible())
                tmp.add(resource);
        }
    }
    return tmp;
}

From source file:com.helpinput.core.Utils.java

@SuppressWarnings("unchecked")
public static <T> T getInstance(Object o, Class<T> clz) {
    if (o == null)
        return null;
    if (clz.isInstance(o))
        return (T) o;
    return null;/*w  ww .ja  v  a2 s.  c o m*/
}