Example usage for org.apache.commons.lang3 ClassUtils isAssignable

List of usage examples for org.apache.commons.lang3 ClassUtils isAssignable

Introduction

In this page you can find the example usage for org.apache.commons.lang3 ClassUtils isAssignable.

Prototype

public static boolean isAssignable(final Class<?> cls, final Class<?> toClass) 

Source Link

Document

Checks if one Class can be assigned to a variable of another Class .

Unlike the Class#isAssignableFrom(java.lang.Class) method, this method takes into account widenings of primitive classes and null s.

Primitive widenings allow an int to be assigned to a long, float or double.

Usage

From source file:objenome.util.TypeUtil.java

/**
 * Returns <code>true</code> if the given collection contains an element
 * which represents a class which is the same as or is a sub-type of the
 * given class/*w  ww. j a  v  a 2  s . com*/
 *
 * @param collection the collection to search for a class that is assignable
 * to <code>cls</code>
 * @param cls the class that is being searched against the given collection
 * @return <code>true</code> if the given collection contains a class which
 * is the same as or a sub-type of the given class parameter. It returns
 * <code>false</code> otherwise.
 */
public static boolean containsSub(Class<?>[] collection, Class<?> cls) {
    for (Class<?> c : collection) {
        if (ClassUtils.isAssignable(c, cls)) {
            return true;
        }
    }

    return false;
}

From source file:objenome.util.TypeUtil.java

/**
 * Returns <code>true</code> if the given collection contains an element
 * which represents a class which is the same as or is a super-type of the
 * given class//from   w  ww.  j  a va 2s .  c  om
 *
 * @param collection the classes to search for one that <code>cls</code> is
 * assignable to
 * @param cls the class that is being searched against
 * <code>collection</code>
 * @return <code>true</code> if the given collection contains a class which
 * is the same as or a super-type of the given class parameter. It returns
 * <code>false</code> otherwise.
 */
public static boolean containsSuper(Class<?>[] collection, Class<?> cls) {
    for (Class<?> c : collection) {
        if (ClassUtils.isAssignable(cls, c)) {
            return true;
        }
    }

    return false;
}

From source file:objenome.util.TypeUtil.java

/**
 * Returns <code>true</code> if all elements of <code>collection</code> are
 * sub-types of <code>cls</code>, or the same type
 *
 * @param collection the classes that must all be sub-types of
 * <code>cls</code>/*w  w  w.  java 2  s.  c om*/
 * @param cls the type to compare against <code>collection</code>
 * @return <code>true</code> if all classes in <code>collection</code> are
 * sub-types of, or the same type as, <code>cls</code> and returns
 * <code>false</code> otherwise
 */
public static boolean allSub(Class<?>[] collection, Class<?> cls) {
    for (Class<?> c : collection) {
        if (!ClassUtils.isAssignable(c, cls)) {
            return false;
        }
    }

    return true;
}

From source file:objenome.util.TypeUtil.java

/**
 * Returns <code>true</code> if all elements of <code>collection</code> are
 * super-types of <code>cls</code>, or the same type
 *
 * @param collection the classes that must all be super-types of
 * <code>cls</code>/*from w w w  .j a  va2 s . co m*/
 * @param cls the type to compare against <code>collection</code>
 * @return <code>true</code> if all classes in <code>collection</code> are
 * super-types of, or the same type as, <code>cls</code> and returns
 * <code>false</code> otherwise
 */
public static boolean allSuper(Class<?>[] collection, Class<?> cls) {
    for (Class<?> c : collection) {
        if (!ClassUtils.isAssignable(cls, c)) {
            return false;
        }
    }

    return true;
}

From source file:org.apache.karaf.cellar.dosgi.RemoteServiceCallHandler.java

/**
 * <p>Gets a matching method in the <code>Object targetService<code/>.<br/>
 * Inheritance is supported.</p>//from  w  w  w.  j a  v  a2s.co m
 *
 * @param eventParamTypes
 * @param targetService
 * @param event
 * @return a method instance from the <code>Object targetService<code/>
 * @throws NoSuchMethodException
 */
private Method getMethod(Class[] eventParamTypes, Object targetService, RemoteServiceCall event)
        throws NoSuchMethodException {

    Method result = null;
    if (eventParamTypes.length > 0) {
        for (Method remoteMethod : targetService.getClass().getMethods()) {
            //need to find a method with a matching name and with the same number of parameters
            if (remoteMethod.getName().equals(event.getMethod())
                    && remoteMethod.getParameterTypes().length == eventParamTypes.length) {
                boolean allParamsFound = true;
                for (int i = 0; i < remoteMethod.getParameterTypes().length; i++) {
                    allParamsFound = allParamsFound
                            && ClassUtils.isAssignable(eventParamTypes[i], remoteMethod.getParameterTypes()[i]);
                }

                // if already found a matching method, no need to continue looking for one
                if (allParamsFound) {
                    result = remoteMethod;
                    break;
                }
            }
        }
    } else {
        result = targetService.getClass().getMethod(event.getMethod());
    }

    //if method was not found go out with a bang
    if (result == null) {
        throw new NoSuchMethodException(String.format("No match for method [%s] %s", event.getMethod(),
                Arrays.toString(eventParamTypes)));
    }

    return result;
}

From source file:org.apache.syncope.client.console.wicket.markup.html.list.ConnConfPropertyListView.java

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
protected void populateItem(final ListItem<ConnConfProperty> item) {
    final ConnConfProperty property = item.getModelObject();

    final String label = StringUtils.isBlank(property.getSchema().getDisplayName())
            ? property.getSchema().getName()
            : property.getSchema().getDisplayName();

    final FieldPanel<? extends Serializable> field;
    boolean required = false;
    boolean isArray = false;

    if (property.getSchema().isConfidential()
            || Constants.GUARDED_STRING.equalsIgnoreCase(property.getSchema().getType())
            || Constants.GUARDED_BYTE_ARRAY.equalsIgnoreCase(property.getSchema().getType())) {

        field = new AjaxPasswordFieldPanel("panel", label, new Model<>(), false);
        ((PasswordTextField) field.getField()).setResetPassword(false);

        required = property.getSchema().isRequired();
    } else {/*w  w  w .j  a v  a2  s. c  o  m*/
        Class<?> propertySchemaClass;
        try {
            propertySchemaClass = ClassUtils.getClass(property.getSchema().getType());
            if (ClassUtils.isPrimitiveOrWrapper(propertySchemaClass)) {
                propertySchemaClass = org.apache.commons.lang3.ClassUtils
                        .primitiveToWrapper(propertySchemaClass);
            }
        } catch (ClassNotFoundException e) {
            LOG.error("Error parsing attribute type", e);
            propertySchemaClass = String.class;
        }

        if (ClassUtils.isAssignable(Number.class, propertySchemaClass)) {
            @SuppressWarnings("unchecked")
            Class<Number> numberClass = (Class<Number>) propertySchemaClass;
            field = new AjaxSpinnerFieldPanel.Builder<>().build("panel", label, numberClass, new Model<>());
            required = property.getSchema().isRequired();
        } else if (ClassUtils.isAssignable(Boolean.class, propertySchemaClass)) {
            field = new AjaxCheckBoxPanel("panel", label, new Model<>());
        } else {
            field = new AjaxTextFieldPanel("panel", label, new Model<>());
            required = property.getSchema().isRequired();
        }

        if (propertySchemaClass.isArray()) {
            isArray = true;
        }
    }

    field.setIndex(item.getIndex());
    field.setTitle(property.getSchema().getHelpMessage(), true);

    final AbstractFieldPanel<? extends Serializable> fieldPanel;
    if (isArray) {
        final MultiFieldPanel multiFieldPanel = new MultiFieldPanel.Builder(
                new PropertyModel<>(property, "values")).setEventTemplate(true).build("panel", label, field);
        item.add(multiFieldPanel);
        fieldPanel = multiFieldPanel;
    } else {
        setNewFieldModel(field, property.getValues());
        item.add(field);
        fieldPanel = field;
    }

    if (required) {
        fieldPanel.addRequiredLabel();
    }

    if (withOverridable) {
        fieldPanel.showExternAction(addCheckboxToggle(property));
    }
}

From source file:org.fit.cssbox.scriptbox.script.javascript.java.HostedJavaMethod.java

/**
 * Casts the given arguments into given expected types.
 * /*from w  w  w.j av  a2 s .  c  o m*/
 * @param expectedTypes Types into which should be casted the given arguments.
 * @param args Arguments to be casted.
 * @return Array of the casted arguments if casting was successful, otherwise null.
 */
public static Object[] castArgs(Class<?>[] expectedTypes, Object... args) {
    if (expectedTypes != null && args != null) {

        if (expectedTypes.length <= args.length + 1 && expectedTypes.length > 0) {
            Class<?> lastType = expectedTypes[expectedTypes.length - 1];
            if (lastType.isArray()) {
                Class<?> arrayType = lastType.getComponentType();

                boolean maybeVarargs = true;
                if (expectedTypes.length == args.length) {
                    Object lastArg = args[args.length - 1];
                    Class<?> lastArgClass = (lastArg != null) ? lastArg.getClass() : null;
                    maybeVarargs = lastArgClass != null && !ClassUtils.isAssignable(lastArgClass, lastType);
                }

                if (maybeVarargs) {
                    for (int i = expectedTypes.length - 1; i < args.length; i++) {
                        if (args[i] == null) {
                            continue;
                        }
                        Class<?> argType = args[i].getClass();

                        if (!ClassUtils.isAssignable(argType, arrayType)) {
                            maybeVarargs = false;
                            break;
                        }
                    }

                    if (maybeVarargs) {
                        Object[] oldArgs = args;
                        args = new Object[expectedTypes.length];

                        for (int i = 0; i < expectedTypes.length - 1; i++) {
                            args[i] = oldArgs[i];
                        }

                        Object[] varargs = new Object[oldArgs.length - expectedTypes.length + 1];

                        for (int i = expectedTypes.length - 1; i < oldArgs.length; i++) {
                            varargs[i - expectedTypes.length + 1] = oldArgs[i];
                        }

                        args[expectedTypes.length - 1] = varargs;
                    }
                }
            }
        }

        if (expectedTypes.length == args.length) {
            Object[] castedArgs = new Object[args.length];
            for (int i = 0; i < args.length; i++) {
                Object arg = args[i];
                Class<?> expectedType = expectedTypes[i];
                if (arg == null) {
                    castedArgs[i] = null;
                } else if (arg == Undefined.instance) {
                    castedArgs[i] = null;
                } else if (arg instanceof ConsString) {
                    castedArgs[i] = ((ConsString) arg).toString();
                } else if (arg instanceof Double
                        && (expectedType.equals(Integer.class) || expectedType.equals(int.class)
                                || expectedType.equals(Long.class) || expectedType.equals(long.class))) {
                    castedArgs[i] = ((Double) arg).intValue();
                } else {
                    castedArgs[i] = WindowJavaScriptEngine.jsToJava(arg);
                    //castedArgs[i] = Context.jsToJava(castedArgs[i], expectedType);
                }

                castedArgs[i] = ClassField.wrap(expectedTypes[i], castedArgs[i]);
            }

            return castedArgs;
        }
    }

    return null;
}

From source file:org.force66.beantester.valuegens.GenericValueGenerator.java

@Override
public boolean canGenerate(Class<?> targetClass) {
    if (targetClass == null)
        return false;
    if (values.length > 0) {
        return ClassUtils.isAssignable(values[0].getClass(), targetClass);
    }//from  w  w  w  .java 2 s.  c o m
    return false;
}

From source file:org.force66.vobase.ValueObjectUtils.java

protected static void copyAllFields(Object source, Object target) {
    Validate.notNull(source, "Null source object fields can't be copied");
    Validate.notNull(target, "Null target object fields can't be copied");
    Validate.isTrue(ClassUtils.isAssignable(target.getClass(), source.getClass()),
            "Source and target classes must be assignable");

    Object value = null;/*from w w w  . ja  v  a 2s.  c o m*/
    for (Field field : FieldUtils.getAllFields(source.getClass())) {
        if (!Modifier.isFinal(field.getModifiers())) {
            value = copyField(source, target, value, field);
        }
    }
}

From source file:org.jmingo.document.id.IdFieldGenerator.java

private boolean instanceOf(Class<?> cls, Class<?> toClass) {
    cls = ClassUtils.primitiveToWrapper(cls);
    toClass = ClassUtils.primitiveToWrapper(toClass);
    return ClassUtils.isAssignable(cls, toClass);
}