KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > common > PropertyUtils


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * PropertyUtils.java
26  *
27  * Created on March 18, 2001, 12:30 PM
28  */

29
30 package com.sun.enterprise.tools.common;
31
32 import java.lang.reflect.Method JavaDoc;
33 import java.beans.PropertyDescriptor JavaDoc;
34
35 import com.sun.enterprise.tools.common.util.diagnostics.Reporter;
36 import com.sun.enterprise.tools.common.util.diagnostics.StackTrace;
37
38 /**
39  *
40  * @author vkraemer
41  * @version
42  */

43 public class PropertyUtils {
44
45     /** Creates new PropertyUtils */
46     private PropertyUtils() {
47     }
48
49     public static Method JavaDoc getWriter(Object JavaDoc target, String JavaDoc destFieldName) throws java.beans.IntrospectionException JavaDoc {
50         try {
51             PropertyDescriptor JavaDoc destPd = new PropertyDescriptor JavaDoc(destFieldName, target.getClass());
52             return destPd.getWriteMethod();
53         }
54         catch (java.beans.IntrospectionException JavaDoc t) {
55             Reporter.critical(new StackTrace(t)); //NOI18N
56
throw t;
57         }
58     }
59     
60         public static Method JavaDoc getReader(Object JavaDoc target, String JavaDoc destFieldName) throws java.beans.IntrospectionException JavaDoc {
61         try {
62             PropertyDescriptor JavaDoc destPd = new PropertyDescriptor JavaDoc(destFieldName, target.getClass());
63             return destPd.getReadMethod();
64         }
65         catch (java.beans.IntrospectionException JavaDoc t) {
66             //Reporter.critical(new StackTrace(t)); //NOI18N
67
//throw t;
68
return getReader2(target,destFieldName);
69         }
70     }
71
72         public static Method JavaDoc getReader2(Object JavaDoc target, String JavaDoc destFieldName) throws java.beans.IntrospectionException JavaDoc {
73             String JavaDoc getterName = createGetterName(destFieldName);
74             Class JavaDoc targetClass = null;
75         try {
76             targetClass = target.getClass();
77             Method JavaDoc reader = targetClass.getMethod(getterName,null);
78             return reader;
79             //PropertyDescriptor destPd = new PropertyDescriptor(destFieldName, target.getClass());
80
//return destPd.getReadMethod();
81
}
82         catch (Throwable JavaDoc t) {
83             Method JavaDoc[] allmethods = targetClass.getMethods();
84             for (int i = 0; null != allmethods && i < allmethods.length; i++)
85                 Reporter.info(allmethods[i].getReturnType() + " " +allmethods[i].getName());//NOI18N
86
Reporter.critical(new StackTrace(t)); //NOI18N
87
throw new java.beans.IntrospectionException JavaDoc(getterName);
88             //return getReader2(target,destFieldName);
89
}
90         }
91         
92         static private String JavaDoc createGetterName(String JavaDoc propName) {
93             String JavaDoc retVal = "get";//NOI18N
94
String JavaDoc capitalizedProp = propName.toUpperCase();
95             return retVal + capitalizedProp.substring(0,1) + propName.substring(1);
96         }
97     }
98
99
Popular Tags