01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
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 {
39:
40: private final ClassLoader classLoader;
41:
42: /**
43: * Create a default ClassEditor, using the thread context ClassLoader.
44: */
45: public ClassEditor() {
46: this (null);
47: }
48:
49: /**
50: * Create a default ClassEditor, using the given ClassLoader.
51: * @param classLoader the ClassLoader to use
52: * (or <code>null</code> for the thread context ClassLoader)
53: */
54: public ClassEditor(ClassLoader classLoader) {
55: this .classLoader = (classLoader != null ? classLoader
56: : ClassUtils.getDefaultClassLoader());
57: }
58:
59: public void setAsText(String text) throws IllegalArgumentException {
60: if (StringUtils.hasText(text)) {
61: setValue(ClassUtils.resolveClassName(text.trim(),
62: this .classLoader));
63: } else {
64: setValue(null);
65: }
66: }
67:
68: public String getAsText() {
69: Class clazz = (Class) getValue();
70: if (clazz != null) {
71: return ClassUtils.getQualifiedName(clazz);
72: } else {
73: return "";
74: }
75: }
76:
77: }
|