001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.beanutils.converters;
018
019/**
020 * {@link org.apache.commons.beanutils.Converter} implementation that handles conversion
021 * to and from <strong>java.lang.Class</strong> objects.
022 * <p>
023 * The class will be loaded from the thread context class
024 * loader (if it exists); otherwise the class loader that loaded this class
025 * will be used.
026 * <p>
027 * Can be configured to either return a <em>default value</em> or throw a
028 * <code>ConversionException</code> if a conversion error occurs.
029 *
030 * @since 1.4
031 */
032public final class ClassConverter extends AbstractConverter {
033
034    /**
035     * Construct a <strong>java.lang.Class</strong> <em>Converter</em> that throws
036     * a <code>ConversionException</code> if an error occurs.
037     */
038    public ClassConverter() {
039    }
040
041    /**
042     * Construct a <strong>java.lang.Class</strong> <em>Converter</em> that returns
043     * a default value if an error occurs.
044     *
045     * @param defaultValue The default value to be returned
046     * if the value to be converted is missing or an error
047     * occurs converting the value.
048     */
049    public ClassConverter(final Object defaultValue) {
050        super(defaultValue);
051    }
052
053    /**
054     * <p>Convert a java.lang.Class or object into a String.</p>
055     *
056     * @param value The input value to be converted
057     * @return the converted String value.
058     * @since 1.8.0
059     */
060    @Override
061    protected String convertToString(final Object value) {
062        return value instanceof Class ? ((Class<?>)value).getName() : value.toString();
063    }
064
065    /**
066     * <p>Convert the input object into a java.lang.Class.</p>
067     *
068     * @param <T> Target type of the conversion.
069     * @param type Data type to which this value should be converted.
070     * @param value The input value to be converted.
071     * @return The converted value.
072     * @throws Throwable if an error occurs converting to the specified type
073     * @since 1.8.0
074     */
075    @Override
076    protected <T> T convertToType(final Class<T> type, final Object value) throws Throwable {
077        if (Class.class.equals(type)) {
078            ClassLoader classLoader = Thread.currentThread()
079                    .getContextClassLoader();
080            if (classLoader != null) {
081                try {
082                    return type.cast(classLoader.loadClass(value.toString()));
083                } catch (final ClassNotFoundException ex) {
084                    // Don't fail, carry on and try this class's class loader
085                    // (see issue# BEANUTILS-263)
086                }
087            }
088
089            // Try this class's class loader
090            classLoader = ClassConverter.class.getClassLoader();
091            return type.cast(classLoader.loadClass(value.toString()));
092        }
093
094        throw conversionException(type, value);
095    }
096
097    /**
098     * Return the default type this <code>Converter</code> handles.
099     *
100     * @return The default type this <code>Converter</code> handles.
101     * @since 1.8.0
102     */
103    @Override
104    protected Class<?> getDefaultType() {
105        return Class.class;
106    }
107
108}