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:com.glaf.core.util.ReflectUtils.java

/**
 * is compatible.//from   w w w .ja  v a  2 s  .c  o  m
 * 
 * @param c
 *            class.
 * @param o
 *            instance.
 * @return compatible or not.
 */
public static boolean isCompatible(Class<?> c, Object o) {
    boolean pt = c.isPrimitive();
    if (o == null)
        return !pt;

    if (pt) {
        if (c == int.class)
            c = Integer.class;
        else if (c == boolean.class)
            c = Boolean.class;
        else if (c == long.class)
            c = Long.class;
        else if (c == float.class)
            c = Float.class;
        else if (c == double.class)
            c = Double.class;
        else if (c == char.class)
            c = Character.class;
        else if (c == byte.class)
            c = Byte.class;
        else if (c == short.class)
            c = Short.class;
    }
    if (c == o.getClass())
        return true;
    return c.isInstance(o);
}

From source file:com.smartbear.postman.PostmanImporter.java

private <T> T getTestRequestStep(WsdlProject project, Class<T> stepClass) {
    if (project.getTestSuiteCount() > 0) {
        WsdlTestSuite testSuite = project.getTestSuiteAt(project.getTestSuiteCount() - 1);
        if (testSuite != null && testSuite.getTestCaseCount() > 0) {
            WsdlTestCase testCase = testSuite.getTestCaseAt(testSuite.getTestCaseCount() - 1);
            if (testCase != null && testCase.getTestStepCount() > 0) {
                WsdlTestStep testStep = testCase.getTestStepAt(testCase.getTestStepCount() - 1);
                if (stepClass.isInstance(testStep)) {
                    return (T) testStep;
                }//from   w w  w . j av a 2  s  .  c om
            }
        }
    }
    return null;
}

From source file:com.jaspersoft.studio.utils.ModelUtils.java

/**
 * Verifies that there is only one EditPart selected referring to a model object of the allowed class types.
 * //from ww w . j  a v  a  2s. c  om
 * @param editPart
 *          the object supposed to be {@link EditPart}
 * @param allowed
 *          determines if the list is of allowed (true) or excluded (false) types
 * @param classes
 *          the list of type(s)
 * @return <code>true</code> if the single model object respects the condition,<code>false</code> otherwise
 */
public static boolean checkTypesForSingleEditPartModel(Object editPart, boolean allowed, Class<?>... classes) {
    if (editPart instanceof EditPart) {
        Object node = ((EditPart) editPart).getModel();
        for (Class<?> clazz : classes) {
            if (clazz.isInstance(node))
                return allowed;
        }
    }
    return !allowed;
}

From source file:ResultSetIterator.java

/**
 * ResultSet.getObject() returns an Integer object for an INT column.  The
 * setter method for the property might take an Integer or a primitive int.
 * This method returns true if the value can be successfully passed into
 * the setter method.  Remember, Method.invoke() handles the unwrapping
 * of Integer into an int.//  w ww  .java 2 s  .  c o  m
 * 
 * @param value The value to be passed into the setter method.
 * @param type The setter's parameter type.
 * @return boolean True if the value is compatible.
 */
private boolean isCompatibleType(Object value, Class type) {
    // Do object check first, then primitives
    if (value == null || type.isInstance(value)) {
        return true;

    } else if (type.equals(Integer.TYPE) && Integer.class.isInstance(value)) {
        return true;

    } else if (type.equals(Long.TYPE) && Long.class.isInstance(value)) {
        return true;

    } else if (type.equals(Double.TYPE) && Double.class.isInstance(value)) {
        return true;

    } else if (type.equals(Float.TYPE) && Float.class.isInstance(value)) {
        return true;

    } else if (type.equals(Short.TYPE) && Short.class.isInstance(value)) {
        return true;

    } else if (type.equals(Byte.TYPE) && Byte.class.isInstance(value)) {
        return true;

    } else if (type.equals(Character.TYPE) && Character.class.isInstance(value)) {
        return true;

    } else if (type.equals(Boolean.TYPE) && Boolean.class.isInstance(value)) {
        return true;

    } else {
        return false;
    }

}

From source file:com.codelanx.codelanxlib.config.Config.java

/**
 * Attempts to return the {@link Config} value as a casted type. If the
 * value cannot be casted it will attempt to return the default value. If
 * the default value is inappropriate for the class, the method will
 * throw a {@link ClassCastException}.// w  ww .  j av  a2s. c  o m
 * 
 * @since 0.1.0
 * @version 0.1.0
 * 
 * @param <T> The type of the casting class
 * @param c The class type to cast to
 * @return A casted value, or {@code null} if unable to cast. If the passed
 *         class parameter is of a primitive type or autoboxed primitive,
 *         then a casted value of -1 is returned, or {@code false} for
 *         booleans. If the passed class parameter is for {@link String},
 *         then {@link Object#toString()} is called on the value instead
 */
default public <T> T as(Class<T> c) {
    Validate.notNull(c, "Cannot cast to null");
    Validate.isTrue(Primitives.unwrap(c) != void.class, "Cannot cast to a void type");
    boolean primitive = Primitives.isWrapperType(c) || Primitives.isWrapperType(Primitives.wrap(c));
    Object o = this.get();
    if (primitive) {
        T back;
        if (o == null) {
            return Reflections.defaultPrimitiveValue(c);
        } else {
            back = Primitives.wrap(c).cast(o);
        }
        return back;
    }
    if (o == null) {
        return null;
    }
    if (c == String.class) {
        return (T) String.valueOf(o);
    }
    if (c.isInstance(o)) {
        return c.cast(o);
    }
    if (c.isInstance(this.getDefault())) {
        return c.cast(this.getDefault());
    }
    throw new ClassCastException("Unable to cast config value");
}

From source file:com.jaspersoft.jasperserver.ws.axis2.scheduling.ReportJobBeanTraslator.java

protected Object toParameterValue(JRParameter parameter, Object value) {
    Class paramType = parameter.getValueClass();
    Object reportValue = value;//w  w  w.  j  av  a 2  s . c  o  m
    if (value != null) {
        if (value.getClass().isArray()) {
            reportValue = toCollectionValue(paramType, value);
        } else if (!paramType.isInstance(value)) {
            if (value instanceof String && ((String) value).length() == 0) {
                reportValue = null;
            } else {
                reportValue = convertToParameter(paramType, value);
            }
        }
    }
    return reportValue;
}

From source file:org.malaguna.cmdit.bbeans.AbstractBean.java

/**
 * Method that obtains the selected row from a UIComponent. It can check super class
 * /* ww  w  . j  ava 2s .  c  o m*/
 * @param anEvent
 */
protected Object selectRowFromEvent(UIComponent tmpComponent, Class<?> clazz, boolean checkSuper) {
    Object tmpRowData = null;

    if (tmpComponent != null) {
        while (null != tmpComponent && !(tmpComponent instanceof UIData))
            tmpComponent = tmpComponent.getParent();

        if (tmpComponent != null && (tmpComponent instanceof UIData)) {
            tmpRowData = ((UIData) tmpComponent).getRowData();

            boolean checked = clazz.isAssignableFrom(tmpRowData.getClass());

            if (!checked && checkSuper) {
                checked = clazz.isInstance(tmpRowData);
            }

            if (!checked) {
                setErrorMessage("Casting from event error:",
                        "Error de casting para el objeto fila: se esperaba [" + clazz.toString() + "] y es ["
                                + tmpRowData.getClass().toString() + "]");
                tmpRowData = null;
            }
        }
    }

    return tmpRowData;
}

From source file:com.francetelecom.clara.cloud.logicalmodel.LogicalDeployment.java

/**
 * List all execution nodes . (Read-only)
 * @param filteredType the class to filter execNode or null to return all nodes
 * @return an unmodifiable list of execution nodes sorted by type and label
 *//*from www  .  j  a va2s. c o  m*/
//TODO: return a Set instead of a List: no duplicates allowed in ExecNode.
public <E extends ProcessingNode> List<E> listProcessingNodes(Class<E> filteredType) {

    if (filteredType == null) {
        List<ProcessingNode> nodes = new ArrayList<ProcessingNode>(this.processingNodes);
        Collections.sort(nodes);
        return (List<E>) Collections.unmodifiableList(nodes);
    } else {
        List<E> matchingNodes = new ArrayList<E>();
        for (ProcessingNode executionNode : processingNodes) {
            if (filteredType.isInstance(executionNode)) {
                matchingNodes.add((E) executionNode);
            }
        }
        Collections.sort(matchingNodes);
        return Collections.unmodifiableList(matchingNodes); //overkill ?
    }

}

From source file:org.nabucco.alfresco.enhScriptEnv.common.script.converter.rhino.WrapFactoryConverter.java

/**
 * {@inheritDoc}/* ww w  .j a  v  a 2 s.c o m*/
 */
@Override
public Object convertValueForScript(final Object value, final ValueConverter globalDelegate,
        final Class<?> expectedClass) {
    // no further distinction - always delegate
    final Context currentContext = Context.getCurrentContext();
    final WrapFactory factory = currentContext.getWrapFactory();

    final Object result;

    // mark for recursion prevention (DelegatingWrapFactory may otherwise indirectly delegate back)
    this.currentConversions.get().add(value);
    try {
        // if conversion call is made in a scope-ful context, the caller needs to take care of setting parentScope for Scriptable
        result = factory.wrap(currentContext, null, value, null);
    } finally {
        this.currentConversions.get().remove(value);
    }

    // we tried to check in advance as best as possible in #canConvertValueForScript
    return expectedClass.isInstance(result) ? result : null;
}

From source file:net.fenyo.gnetwatch.GUI.VisualElement.java

/**
 * Returns every ascendant of this element, restricted to a given type.
 * @param clazz type./* w  w w.  j a  v a  2  s  .c om*/
 * @param elts elts list that will be updated.
 */
private void getAllParents(final Class clazz, final java.util.List<VisualElement> elts) {
    for (final VisualElement elt : getParents())
        elt.getAllParents(clazz, elts);
    if (clazz.isInstance(this) && !elts.contains(this))
        elts.add(this);
}