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 */ 017 package org.apache.commons.beanutils.converters; 018 019 /** 020 * {@link org.apache.commons.beanutils.Converter} implementaion that handles conversion 021 * to and from <b>java.lang.Class</b> 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 <i>default value</i> or throw a 028 * <code>ConversionException</code> if a conversion error occurs. 029 * 030 * @author Tomas Viberg 031 * @version $Revision: 690380 $ $Date: 2008-08-29 21:04:38 +0100 (Fri, 29 Aug 2008) $ 032 * @since 1.4 033 */ 034 public final class ClassConverter extends AbstractConverter { 035 036 /** 037 * Construct a <b>java.lang.Class</b> <i>Converter</i> that throws 038 * a <code>ConversionException</code> if an error occurs. 039 */ 040 public ClassConverter() { 041 super(); 042 } 043 044 /** 045 * Construct a <b>java.lang.Class</b> <i>Converter</i> that returns 046 * a default value if an error occurs. 047 * 048 * @param defaultValue The default value to be returned 049 * if the value to be converted is missing or an error 050 * occurs converting the value. 051 */ 052 public ClassConverter(Object defaultValue) { 053 super(defaultValue); 054 } 055 056 /** 057 * Return the default type this <code>Converter</code> handles. 058 * 059 * @return The default type this <code>Converter</code> handles. 060 * @since 1.8.0 061 */ 062 protected Class getDefaultType() { 063 return Class.class; 064 } 065 066 /** 067 * <p>Convert a java.lang.Class or object into a String.</p> 068 * 069 * @param value The input value to be converted 070 * @return the converted String value. 071 * @since 1.8.0 072 */ 073 protected String convertToString(Object value) { 074 return (value instanceof Class) ? ((Class)value).getName() : value.toString(); 075 } 076 077 /** 078 * <p>Convert the input object into a java.lang.Class.</p> 079 * 080 * @param type Data type to which this value should be converted. 081 * @param value The input value to be converted. 082 * @return The converted value. 083 * @throws Throwable if an error occurs converting to the specified type 084 * @since 1.8.0 085 */ 086 protected Object convertToType(Class type, Object value) throws Throwable { 087 ClassLoader classLoader = 088 Thread.currentThread().getContextClassLoader(); 089 if (classLoader != null) { 090 try { 091 return (classLoader.loadClass(value.toString())); 092 } catch (ClassNotFoundException ex) { 093 // Don't fail, carry on and try this class's class loader 094 // (see issue# BEANUTILS-263) 095 } 096 } 097 098 // Try this class's class loader 099 classLoader = ClassConverter.class.getClassLoader(); 100 return (classLoader.loadClass(value.toString())); 101 } 102 103 }