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.beanutils2.converters;
018
019/**
020 * {@link org.apache.commons.beanutils2.Converter} implementation that handles conversion to and from <strong>java.lang.Class</strong> objects.
021 * <p>
022 * The class will be loaded from the thread context class loader (if it exists); otherwise the class loader that loaded this class will be used.
023 * <p>
024 * Can be configured to either return a <em>default value</em> or throw a {@code ConversionException} if a conversion error occurs.
025 *
026 * @param <T> the type of the class.
027 * @since 1.4
028 */
029public final class ClassConverter<T> extends AbstractConverter<Class<T>> {
030
031    /**
032     * Constructs a <strong>java.lang.Class</strong> <em>Converter</em> that throws a {@code ConversionException} if an error occurs.
033     */
034    public ClassConverter() {
035    }
036
037    /**
038     * Constructs a <strong>java.lang.Class</strong> <em>Converter</em> that returns a default value if an error occurs.
039     *
040     * @param defaultValue The default value to be returned if the value to be converted is missing or an error occurs converting the value.
041     */
042    public ClassConverter(final Class<T> defaultValue) {
043        super(defaultValue);
044    }
045
046    /**
047     * <p>
048     * Converts a java.lang.Class or object into a String.
049     * </p>
050     *
051     * @param value The input value to be converted
052     * @return the converted String value.
053     * @since 1.8.0
054     */
055    @Override
056    protected String convertToString(final Object value) {
057        return value instanceof Class ? ((Class<?>) value).getName() : value.toString();
058    }
059
060    /**
061     * <p>
062     * Converts the input object into a java.lang.Class.
063     * </p>
064     *
065     * @param <R>   Target type of the conversion.
066     * @param type  Data type to which this value should be converted.
067     * @param value The input value to be converted.
068     * @return The converted value.
069     * @throws Throwable if an error occurs converting to the specified type
070     * @since 1.8.0
071     */
072    @Override
073    protected <R> R convertToType(final Class<R> type, final Object value) throws Throwable {
074        if (Class.class.equals(type)) {
075            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
076            if (classLoader != null) {
077                try {
078                    return type.cast(classLoader.loadClass(value.toString()));
079                } catch (final ClassNotFoundException ex) {
080                    // Don't fail, carry on and try this class's class loader
081                    // (see issue# BEANUTILS-263)
082                }
083            }
084
085            // Try this class's class loader
086            classLoader = ClassConverter.class.getClassLoader();
087            return type.cast(classLoader.loadClass(value.toString()));
088        }
089
090        throw conversionException(type, value);
091    }
092
093    /**
094     * Gets the default type this {@code Converter} handles.
095     *
096     * @return The default type this {@code Converter} handles.
097     * @since 1.8.0
098     */
099    @SuppressWarnings({ "unchecked", "rawtypes" })
100    @Override
101    protected Class<Class<T>> getDefaultType() {
102        return (Class) Class.class;
103    }
104
105}