KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > propertyeditors > ClassEditor


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.beans.propertyeditors;
18
19 import java.beans.PropertyEditorSupport JavaDoc;
20
21 import org.springframework.util.ClassUtils;
22 import org.springframework.util.StringUtils;
23
24 /**
25  * Property editor for {@link java.lang.Class java.lang.Class}, to enable the direct
26  * population of a <code>Class</code> property without recourse to having to use a
27  * String class name property as bridge.
28  *
29  * <p>Also supports "java.lang.String[]"-style array class names, in contrast to the
30  * standard {@link Class#forName(String)} method.
31  *
32  * @author Juergen Hoeller
33  * @author Rick Evans
34  * @since 13.05.2003
35  * @see java.lang.Class#forName
36  * @see org.springframework.util.ClassUtils#forName(String, ClassLoader)
37  */

38 public class ClassEditor extends PropertyEditorSupport JavaDoc {
39
40     private final ClassLoader JavaDoc classLoader;
41
42
43     /**
44      * Create a default ClassEditor, using the thread context ClassLoader.
45      */

46     public ClassEditor() {
47         this(null);
48     }
49
50     /**
51      * Create a default ClassEditor, using the given ClassLoader.
52      * @param classLoader the ClassLoader to use
53      * (or <code>null</code> for the thread context ClassLoader)
54      */

55     public ClassEditor(ClassLoader JavaDoc classLoader) {
56         this.classLoader =
57                 (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
58     }
59
60
61     public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
62         if (StringUtils.hasText(text)) {
63             setValue(ClassUtils.resolveClassName(text.trim(), this.classLoader));
64         }
65         else {
66             setValue(null);
67         }
68     }
69
70     public String JavaDoc getAsText() {
71         Class JavaDoc clazz = (Class JavaDoc) getValue();
72         if (clazz != null) {
73             return ClassUtils.getQualifiedName(clazz);
74         }
75         else {
76             return "";
77         }
78     }
79
80 }
81
Popular Tags