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

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

Introduction

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

Prototype

public static Class<?> getClass(final String className, final boolean initialize)
        throws ClassNotFoundException 

Source Link

Document

Returns the class represented by className using the current thread's context class loader.

Usage

From source file:org.lenskit.eval.traintest.metrics.MetricLoaderHelper.java

/**
 * Look up the class implementing a metric by name.
 * @param name The metric name./*from  w  ww.  j a va2 s .co m*/
 * @return The metric class.
 */
@Nullable
public Class<?> findClass(String name) {
    if (propFiles.containsKey(name.toLowerCase())) {
        String className = (String) propFiles.get(name.toLowerCase());
        logger.debug("resolving metric {} to class {}", name, className);
        try {
            return ClassUtils.getClass(loader, className);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("class " + className + " not found", e);
        }
    } else {
        try {
            logger.debug("trying to look up metric {} as class", name);
            return ClassUtils.getClass(loader, name);
        } catch (ClassNotFoundException e) {
            logger.debug("no metric {} found");
            return null;
        }
    }
}

From source file:org.springframework.jdbc.core.JdbcOperationsUtils.java

public static final RowMapper<Pair<String, Serializable>> getMappedPropertyValueMapper(final String propNameCol,
        final String propTypeCol, final String propValCol, final ClassLoader loader,
        final ConversionService converter) {
    return new RowMapper<Pair<String, Serializable>>() {
        @Override//ww w  .j a v a 2  s .  co m
        public Pair<String, Serializable> mapRow(ResultSet rs, int rowNum) throws SQLException {
            String propName = rs.getString(propNameCol);
            String propType = rs.getString(propTypeCol);
            final Class<?> targetType;
            try {
                targetType = ClassUtils.getClass(loader, propType);
            } catch (ClassNotFoundException e) {
                throw new SQLException(
                        "mapRow(" + propName + ")@" + rowNum + ": unknown property type: " + propType);
            }

            if (!Serializable.class.isAssignableFrom(targetType)) {
                throw new SQLException(
                        "mapRow(" + propName + ")@" + rowNum + ": not serializable: " + propType);
            }

            if (!converter.canConvert(String.class, targetType)) {
                throw new SQLException(
                        "mapRow(" + propName + ")@" + rowNum + ": cannot convert from " + propType);
            }

            String propValue = rs.getString(propValCol);
            Serializable convertedValue = (Serializable) converter.convert(propValue, targetType);
            return Pair.of(propName, convertedValue);
        }
    };
}