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:de.vandermeer.skb.interfaces.application.App_CliParser.java

/**
 * Prints usage information for the CLI parser including all CLI options.
 * @param width the console columns or width of each output line
 * @return list of lines with usage information
 */// w  w w.j  a v  a 2s .com
default ArrayList<StrBuilder> usage(int width) {
    TreeMap<String, ApoBaseC> map = CliOptionList.sortedMap(this.getAllOptions(), this.numberShort(),
            this.numberLong());
    Map<String, String> helpMap = new LinkedHashMap<>();
    int length = 0;
    STGroupFile stg = new STGroupFile("de/vandermeer/skb/interfaces/application/help.stg");
    for (Object option : map.values()) {
        ST sto = stg.getInstanceOf("option");
        String description = null;
        if (ClassUtils.isAssignable(option.getClass(), Apo_SimpleC.class)) {
            sto.add("cliShort", ((Apo_SimpleC) option).getCliShort());
            sto.add("cliLong", ((Apo_SimpleC) option).getCliLong());
            description = ((Apo_SimpleC) option).getDescription();
        }
        if (ClassUtils.isAssignable(option.getClass(), Apo_TypedC.class)) {
            sto.add("cliShort", ((Apo_TypedC<?>) option).getCliShort());
            sto.add("cliLong", ((Apo_TypedC<?>) option).getCliLong());
            sto.add("argName", ((Apo_TypedC<?>) option).getCliArgumentName());
            sto.add("argOptional", ((Apo_TypedC<?>) option).cliArgIsOptional());
            description = ((Apo_TypedC<?>) option).getDescription();
        }
        String line = sto.render();
        if (line.length() > length) {
            length = line.length();
        }
        helpMap.put(line, description);
    }
    length += 4;

    ArrayList<StrBuilder> ret = new ArrayList<>();
    for (Entry<String, String> entry : helpMap.entrySet()) {
        StrBuilder argLine = new StrBuilder();
        argLine.append(entry.getKey()).appendPadding(length - argLine.length(), ' ');
        StrBuilder padLine = new StrBuilder();
        padLine.appendPadding(length, ' ');

        Collection<StrBuilder> text = Text_To_FormattedText.left(entry.getValue(), width - length);
        int i = 0;
        for (StrBuilder b : text) {
            if (i == 0) {
                b.insert(0, argLine);
            } else {
                b.insert(0, padLine);
            }
            ret.add(b);
            i++;
        }
    }
    return ret;
}

From source file:de.unentscheidbar.validation.swing.trigger.DocumentChangeTrigger.java

private static boolean supportsClass(Class<?> clazz) {

    return ClassUtils.isAssignable(clazz, JTextComponent.class)
            || Beans.hasReadableProperty(clazz, DOCUMENT_PROPERTY, Document.class);
}

From source file:io.dropwizard.sharding.dao.LookupDao.java

/**
 * Creates a new sharded DAO. The number of managed shards and bucketing is controlled by the {@link ShardManager}.
 *
 * @param sessionFactories a session provider for each shard
 *//*ww  w . j  a v a2s.  com*/
public LookupDao(List<SessionFactory> sessionFactories, Class<T> entityClass, ShardManager shardManager,
        BucketIdExtractor<String> bucketIdExtractor) {
    this.shardManager = shardManager;
    this.bucketIdExtractor = bucketIdExtractor;
    this.daos = sessionFactories.stream().map(LookupDaoPriv::new).collect(Collectors.toList());
    this.entityClass = entityClass;

    Field fields[] = FieldUtils.getFieldsWithAnnotation(entityClass, LookupKey.class);
    Preconditions.checkArgument(fields.length != 0, "At least one field needs to be sharding key");
    Preconditions.checkArgument(fields.length == 1, "Only one field can be sharding key");
    keyField = fields[0];
    if (!keyField.isAccessible()) {
        try {
            keyField.setAccessible(true);
        } catch (SecurityException e) {
            log.error("Error making key field accessible please use a public method and mark that as LookupKey",
                    e);
            throw new IllegalArgumentException("Invalid class, DAO cannot be created.", e);
        }
    }
    Preconditions.checkArgument(ClassUtils.isAssignable(keyField.getType(), String.class),
            "Key field must be a string");
}

From source file:com.nova.geracao.portfolio.BlogServlet.java

private Object getInstanceFromPropertiesMap(EmbeddedEntity entity, Class<?> klass) {
    Object result = null;// w  ww .j a v  a 2s.  co m
    try {
        Field[] fields = klass.getDeclaredFields();
        result = klass.newInstance();
        for (int i = 0; i < fields.length; i++) {
            if (entity.hasProperty(fields[i].getName())) {
                fields[i].setAccessible(true);
                Object value = entity.getProperty(fields[i].getName());
                if (!ClassUtils.isAssignable(fields[i].getType(), BaseDataClass.class)) {
                    fields[i].set(result, value);
                } else {
                    Object embedValue = getInstanceFromPropertiesMap((EmbeddedEntity) value,
                            fields[i].getType());
                    fields[i].set(result, embedValue);
                }
            }
        }

        Field fieldId = klass.getDeclaredField("id");
        fieldId.setAccessible(true);
        fieldId.set(result, entity.getKey().getId());
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:io.github.moosbusch.lumpi.util.LumpiUtil.java

public static boolean isPrimitiveTypeOrString(Class<?> type) {
    return (ClassUtils.isPrimitiveOrWrapper(type) || (ClassUtils.isAssignable(type, String.class)));
}

From source file:lineage2.gameserver.scripts.Scripts.java

/**
 * Method reload.//w  w  w.  ja  v  a  2  s.c  o m
 * @param target String
 * @return boolean
 */
public boolean reload(String target) {
    List<Class<?>> classes = new ArrayList<>();

    if (load(classes, target)) {
        _log.info("Scripts: Reloaded " + classes.size() + " classes.");
    } else {
        _log.error("Scripts: Failed reloading script(s): " + target + "!");
        return false;
    }

    Class<?> clazz, prevClazz;
    for (int i = 0; i < classes.size(); i++) {
        clazz = classes.get(i);

        prevClazz = _classes.put(clazz.getName(), clazz);
        if (prevClazz != null) {
            if (ClassUtils.isAssignable(prevClazz, ScriptFile.class)) {
                try {
                    ((ScriptFile) prevClazz.newInstance()).onReload();
                } catch (Exception e) {
                    _log.error("Scripts: Failed running " + prevClazz.getName() + ".onReload()", e);
                }
            }

            removeHandlers(prevClazz);
        }

        if (Config.DONTLOADQUEST) {
            if (ClassUtils.isAssignable(clazz, Quest.class)) {
                continue;
            }
        }

        if (ClassUtils.isAssignable(clazz, ScriptFile.class)) {
            try {
                ((ScriptFile) clazz.newInstance()).onLoad();
            } catch (Exception e) {
                _log.error("Scripts: Failed running " + clazz.getName() + ".onLoad()", e);
            }
        }

        addHandlers(clazz);
    }

    return true;
}

From source file:io.github.moosbusch.lumpi.util.LumpiUtil.java

public static boolean isPivotCollection(Class<?> type) {
    return ClassUtils.isAssignable(type, org.apache.pivot.collections.Collection.class);
}

From source file:io.github.moosbusch.lumpi.util.LumpiUtil.java

public static boolean isCollection(Class<?> type) {
    return ((ClassUtils.isAssignable(type, java.util.Collection.class)))
            || (ClassUtils.isAssignable(type, org.apache.pivot.collections.Collection.class));
}

From source file:io.github.moosbusch.lumpi.util.LumpiUtil.java

public static boolean isSequence(Class<?> type) {
    return ClassUtils.isAssignable(type, org.apache.pivot.collections.Sequence.class);
}

From source file:io.github.moosbusch.lumpi.util.LumpiUtil.java

public static boolean isCollectionOrSequence(Class<?> type) {
    return (ClassUtils.isAssignable(type, java.util.Collection.class))
            || (ClassUtils.isAssignable(type, org.apache.pivot.collections.Collection.class))
            || (ClassUtils.isAssignable(type, org.apache.pivot.collections.Sequence.class));
}