View Javadoc

1   /*
2      Copyright 2002-2006 Martin van den Bemt
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  package org.xulux.dataprovider.bean;
17  
18  import java.lang.reflect.Field;
19  import java.lang.reflect.Modifier;
20  
21  import org.xulux.utils.ClassLoaderUtils;
22  
23  /**
24   * Place holder and utility class for the parameter list
25   * that a method may need to get or set appropiate data.
26   *
27   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
28   * @version $Id: BeanParameter.java,v 1.1 2005/12/18 12:58:23 mvdb Exp $
29   */
30  public class BeanParameter {
31  
32      /**
33       * the type of parameter
34       */
35      private String type;
36      /**
37       * the value
38       */
39      private String value;
40      /**
41       * the object
42       */
43      private Object object;
44  
45      /**
46       *
47       */
48      public BeanParameter() {
49      }
50  
51      /**
52       * Convenient constructor
53       *
54       * @param type the type of parameter
55       * @param value the value of the type
56       */
57      public BeanParameter(String type, String value) {
58          setType(type);
59          setValue(value);
60      }
61  
62      /**
63       * @return the type of parameter
64       */
65      public String getType() {
66          return this.type;
67      }
68  
69      /**
70       * @return the value of the type
71       *          (eg string, className or className+field)
72       */
73      public String getValue() {
74          return this.value;
75      }
76  
77      /**
78       * Set the type.
79       * String or static
80       * @param type the type of parameter
81       */
82      public void setType(String type) {
83          this.type = type;
84      }
85  
86      /**
87       * Set the value of the type
88       * eg "M" or eg StaticClass.M
89       * @param value the value of the type
90       */
91      public void setValue(String value) {
92          this.value = value;
93      }
94  
95      /**
96       *
97       * @return the object that contains the value
98       *          we need to pass as the parameter
99       */
100     public Object getObject() {
101         if (this.object == null) {
102             if (getType().equalsIgnoreCase("string")) {
103                 this.object = value;
104             } else if (getType().equalsIgnoreCase("static")) {
105                 // static format is like package.Class.Field, so first
106                 // the class needs processing.
107                 int lastIndex = getValue().lastIndexOf(".");
108                 String field = getValue().substring(getValue().lastIndexOf('.') + 1);
109                 String classString = null;
110                 if (lastIndex != -1) {
111                     classString = getValue().substring(0, getValue().lastIndexOf("."));
112                 } else {
113                     // fall back to the value..
114                     classString = getValue();
115                 }
116                 Class clazz = ClassLoaderUtils.getClass(classString);
117                 if (clazz != null) {
118                     try {
119                         Field f = clazz.getDeclaredField(field);
120                         // we can pass in null, since it (should) be static
121                         if (Modifier.isStatic(f.getModifiers())) {
122                             this.object = f.get(null);
123                         }
124                     }
125                     catch (Exception e) {
126                         e.printStackTrace(System.out);
127                     }
128                 }
129             }
130         }
131         return this.object;
132     }
133 
134     /**
135      * @see java.lang.Object#toString()
136      */
137     public String toString() {
138         return getType() + ":" + getValue();
139     }
140 
141 }