01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: * @author Tomas Viberg
31: * @version $Revision: 555845 $ $Date: 2007-07-13 03:52:05 +0100 (Fri, 13 Jul 2007) $
32: * @since 1.4
33: */
34: public final class ClassConverter extends AbstractConverter {
35:
36: /**
37: * Construct a <b>java.lang.Class</b> <i>Converter</i> that throws
38: * a <code>ConversionException</code> if an error occurs.
39: */
40: public ClassConverter() {
41: super (Class.class);
42: }
43:
44: /**
45: * Construct a <b>java.lang.Class</b> <i>Converter</i> that returns
46: * a default value if an error occurs.
47: *
48: * @param defaultValue The default value to be returned
49: * if the value to be converted is missing or an error
50: * occurs converting the value.
51: */
52: public ClassConverter(Object defaultValue) {
53: super (Class.class, defaultValue);
54: }
55:
56: /**
57: * <p>Convert a java.lang.Class or object into a String.</p>
58: *
59: * @param value The input value to be converted
60: * @return the converted String value.
61: */
62: protected String convertToString(Object value) {
63: return (value instanceof Class) ? ((Class) value).getName()
64: : value.toString();
65: }
66:
67: /**
68: * <p>Convert the input object into a java.lang.Class.</p>
69: *
70: * @param type Data type to which this value should be converted.
71: * @param value The input value to be converted.
72: * @return The converted value.
73: * @throws Throwable if an error occurs converting to the specified type
74: */
75: protected Object convertToType(Class type, Object value)
76: throws Throwable {
77: ClassLoader classLoader = Thread.currentThread()
78: .getContextClassLoader();
79: if (classLoader != null) {
80: try {
81: return (classLoader.loadClass(value.toString()));
82: } catch (ClassNotFoundException ex) {
83: // Don't fail, carry on and try this class's class loader
84: // (see issue# BEANUTILS-263)
85: }
86: }
87:
88: // Try this class's class loader
89: classLoader = ClassConverter.class.getClassLoader();
90: return (classLoader.loadClass(value.toString()));
91: }
92:
93: }
|