KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > jmx > converter > DefaultValueConverter


1 package org.apache.tools.ant.taskdefs.optional.jmx.converter;
2
3 /*
4  * ============================================================================
5  * The Apache Software License, Version 1.1
6  * ============================================================================
7  *
8  * Copyright (C) 2000-2002 The Apache Software Foundation. All
9  * rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without modifica-
12  * tion, are permitted provided that the following conditions are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright notice,
15  * this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright notice,
18  * this list of conditions and the following disclaimer in the documentation
19  * and/or other materials provided with the distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any, must
22  * include the following acknowledgment: "This product includes software
23  * developed by the Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself, if
25  * and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Ant" and "Apache Software Foundation" must not be used to
28  * endorse or promote products derived from this software without prior
29  * written permission. For written permission, please contact
30  * apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache", nor may
33  * "Apache" appear in their name, without prior written permission of the
34  * Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
37  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
38  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
39  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
40  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
41  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
42  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
43  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
45  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46  *
47  * This software consists of voluntary contributions made by many individuals
48  * on behalf of the Apache Software Foundation. For more information on the
49  * Apache Software Foundation, please see <http://www.apache.org/>.
50  *
51  */

52
53
54 import java.util.Map JavaDoc;
55 import java.util.TreeMap JavaDoc;
56
57 /**
58  * Converts a String to its equivalent object type. This implementation
59  * supports most java.lang.* primitives and their equivalent object types
60  * (e.g. int -> Integer, boolean -> Boolean etc.) using the valueOf method
61  * through reflection and using a constructor with a single String
62  * argument.
63  *
64  * @author <a HREF="mailto:bdueck@yahoo.com">Brian Dueck</a>
65  * @version $Version:$
66  *
67  */

68 public class DefaultValueConverter implements ValueConverter {
69     
70     private static String JavaDoc[] supportedTypes = null;
71     
72     private static Map JavaDoc primitiveToClass = new TreeMap JavaDoc(); // primitive -> primitive class (e.g. "boolean" -> Boolean.class
73

74     static {
75         primitiveToClass.put("boolean", Boolean JavaDoc.class);
76         primitiveToClass.put("byte", Byte JavaDoc.class);
77         primitiveToClass.put("char", Character JavaDoc.class);
78         primitiveToClass.put("double", Double JavaDoc.class);
79         primitiveToClass.put("float", Float JavaDoc.class);
80         primitiveToClass.put("int", Integer JavaDoc.class);
81         primitiveToClass.put("long", Long JavaDoc.class);
82         primitiveToClass.put("short", Short JavaDoc.class);
83         
84         // populate the supportedTypes by converting the primitiveToClass
85
// to an array
86

87         supportedTypes = new String JavaDoc[primitiveToClass.size()+1]; // +1 to make room for String
88
supportedTypes[0] = String JavaDoc.class.getName();
89         
90         java.util.Iterator JavaDoc it = primitiveToClass.keySet().iterator();
91         
92         int counter = 1; // 1 because we've already added java.lang.String
93
while (it.hasNext()) {
94             supportedTypes[counter] = (String JavaDoc) it.next();
95             counter++;
96         }
97     }
98     
99     /**
100      * Creates a new instance of DefaultValueConverter
101      *
102      */

103     public DefaultValueConverter() {
104     }
105     
106     public Object JavaDoc valueOf(String JavaDoc value, String JavaDoc type) throws Exception JavaDoc {
107
108         // Create an mbean attribute value. Uses reflection to call the <code>valueOf</code>
109
// method of java.lang.* classes to convert from a String value to a specific data type.
110
//
111

112         Class JavaDoc targetClass = null;
113
114         // find the primitive types equivalent java.lang class
115
//
116
targetClass = (Class JavaDoc) primitiveToClass.get(type);
117         if (targetClass == null) {
118             // not a primitive, assume that type is a full class name
119
targetClass = java.lang.Class.forName(type);
120         }
121
122         try {
123             // find the valueOf method on the class
124
//
125
java.lang.reflect.Method JavaDoc valueOfMethod = null;
126             try {
127                 Class JavaDoc[] paramTypes = {java.lang.String JavaDoc.class};
128                 valueOfMethod = targetClass.getMethod("valueOf",paramTypes);
129             } catch (NoSuchMethodException JavaDoc x) {
130                 Class JavaDoc[] paramTypes = {java.lang.Object JavaDoc.class};
131                 valueOfMethod = targetClass.getMethod("valueOf",paramTypes);
132             }
133
134             // invoke the valueOf method on the class
135
//
136
Object JavaDoc[] argValues = {value};
137             return valueOfMethod.invoke(null,argValues);
138         } catch (NoSuchMethodException JavaDoc x2) {
139             // try a constructor with a single string
140
// argument
141
// i.e. public ClassName(String value)
142
//
143
Class JavaDoc[] paramTypes = {java.lang.String JavaDoc.class};
144             java.lang.reflect.Constructor JavaDoc stringConstructor = targetClass.getConstructor(paramTypes);
145             Object JavaDoc[] argValues = {value};
146             return stringConstructor.newInstance(argValues);
147         }
148
149     }
150     
151     public String JavaDoc[] getSupportedTypes() {
152         return supportedTypes;
153     }
154     
155     
156 }
157
158 /*
159  * $Log: DefaultValueConverter.java,v $
160  * Revision 1.3 2003/05/28 22:28:26 bdueck
161  * *** empty log message ***
162  *
163  * Revision 1.2 2003/04/21 15:29:42 bdueck
164  * Various changes in preparation for version 1.2.
165  *
166  *
167  */
Popular Tags