View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.beanutils.converters;
18  
19  /**
20   * {@link org.apache.commons.beanutils.Converter} implementaion that handles conversion
21   * to and from <b>java.lang.Class</b> objects.
22   * <p>
23   * The class will be loaded from the thread context class
24   * loader (if it exists); otherwise the class loader that loaded this class
25   * will be used.
26   * <p>
27   * Can be configured to either return a <i>default value</i> or throw a
28   * <code>ConversionException</code> if a conversion error occurs.
29   *
30   * @version $Id$
31   * @since 1.4
32   */
33  public final class ClassConverter extends AbstractConverter {
34  
35      /**
36       * Construct a <b>java.lang.Class</b> <i>Converter</i> that throws
37       * a <code>ConversionException</code> if an error occurs.
38       */
39      public ClassConverter() {
40          super();
41      }
42  
43      /**
44       * Construct a <b>java.lang.Class</b> <i>Converter</i> that returns
45       * a default value if an error occurs.
46       *
47       * @param defaultValue The default value to be returned
48       * if the value to be converted is missing or an error
49       * occurs converting the value.
50       */
51      public ClassConverter(final Object defaultValue) {
52          super(defaultValue);
53      }
54  
55      /**
56       * Return the default type this <code>Converter</code> handles.
57       *
58       * @return The default type this <code>Converter</code> handles.
59       * @since 1.8.0
60       */
61      @Override
62      protected Class<?> getDefaultType() {
63          return Class.class;
64      }
65  
66      /**
67       * <p>Convert a java.lang.Class or object into a String.</p>
68       *
69       * @param value The input value to be converted
70       * @return the converted String value.
71       * @since 1.8.0
72       */
73      @Override
74      protected String convertToString(final Object value) {
75          return (value instanceof Class) ? ((Class<?>)value).getName() : value.toString();
76      }
77  
78      /**
79       * <p>Convert the input object into a java.lang.Class.</p>
80       *
81       * @param <T> Target type of the conversion.
82       * @param type Data type to which this value should be converted.
83       * @param value The input value to be converted.
84       * @return The converted value.
85       * @throws Throwable if an error occurs converting to the specified type
86       * @since 1.8.0
87       */
88      @Override
89      protected <T> T convertToType(final Class<T> type, final Object value) throws Throwable {
90          if (Class.class.equals(type)) {
91              ClassLoader classLoader = Thread.currentThread()
92                      .getContextClassLoader();
93              if (classLoader != null) {
94                  try {
95                      return type.cast(classLoader.loadClass(value.toString()));
96                  } catch (final ClassNotFoundException ex) {
97                      // Don't fail, carry on and try this class's class loader
98                      // (see issue# BEANUTILS-263)
99                  }
100             }
101 
102             // Try this class's class loader
103             classLoader = ClassConverter.class.getClassLoader();
104             return type.cast(classLoader.loadClass(value.toString()));
105         }
106 
107         throw conversionException(type, value);
108     }
109 
110 }