| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ClassConverter |
|
| 1.8;1.8 |
| 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 | * @author Tomas Viberg | |
| 31 | * @version $Revision: 690380 $ $Date: 2008-08-29 16:04:38 -0400 (Fri, 29 Aug 2008) $ | |
| 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 | 716 | super(); |
| 42 | 716 | } |
| 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 | 2 | super(defaultValue); |
| 54 | 2 | } |
| 55 | ||
| 56 | /** | |
| 57 | * Return the default type this <code>Converter</code> handles. | |
| 58 | * | |
| 59 | * @return The default type this <code>Converter</code> handles. | |
| 60 | * @since 1.8.0 | |
| 61 | */ | |
| 62 | protected Class getDefaultType() { | |
| 63 | 1 | 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 | protected String convertToString(Object value) { | |
| 74 | 5 | return (value instanceof Class) ? ((Class)value).getName() : value.toString(); |
| 75 | } | |
| 76 | ||
| 77 | /** | |
| 78 | * <p>Convert the input object into a java.lang.Class.</p> | |
| 79 | * | |
| 80 | * @param type Data type to which this value should be converted. | |
| 81 | * @param value The input value to be converted. | |
| 82 | * @return The converted value. | |
| 83 | * @throws Throwable if an error occurs converting to the specified type | |
| 84 | * @since 1.8.0 | |
| 85 | */ | |
| 86 | protected Object convertToType(Class type, Object value) throws Throwable { | |
| 87 | 8 | ClassLoader classLoader = |
| 88 | Thread.currentThread().getContextClassLoader(); | |
| 89 | 8 | if (classLoader != null) { |
| 90 | try { | |
| 91 | 8 | return (classLoader.loadClass(value.toString())); |
| 92 | 5 | } catch (ClassNotFoundException ex) { |
| 93 | // Don't fail, carry on and try this class's class loader | |
| 94 | // (see issue# BEANUTILS-263) | |
| 95 | } | |
| 96 | } | |
| 97 | ||
| 98 | // Try this class's class loader | |
| 99 | 5 | classLoader = ClassConverter.class.getClassLoader(); |
| 100 | 5 | return (classLoader.loadClass(value.toString())); |
| 101 | } | |
| 102 | ||
| 103 | } |