Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/activemq/util/ReflectionSupport.java


1   /** 
2    * <a href="http://activemq.org">ActiveMQ: The Open Source Message Fabric</a>
3    * 
4    * Copyright 2004 Protique Ltd
5    * 
6    * Licensed under the Apache License, Version 2.0 (the "License"); 
7    * you may not use this file except in compliance with the License. 
8    * You may obtain a copy of the License at 
9    * 
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS, 
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
15   * See the License for the specific language governing permissions and 
16   * limitations under the License. 
17   * 
18   **/
19  package org.activemq.util;
20  
21  import java.beans.PropertyEditor;
22  import java.beans.PropertyEditorManager;
23  import java.lang.reflect.Method;
24  import java.net.URI;
25  import java.net.URISyntaxException;
26  import java.util.Iterator;
27  import java.util.Map;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  public class ReflectionSupport {
33      
34      private static final Log log = LogFactory.getLog(ReflectionSupport.class);
35      
36      public static void setProperties(Object target, Map props) {
37          if( target == null )
38              throw new IllegalArgumentException("target was null.");
39          if( props == null )
40              throw new IllegalArgumentException("props was null.");
41          
42          for (Iterator iter = props.keySet().iterator(); iter.hasNext();) {
43              String name = (String) iter.next();
44              Object value = props.get(name);
45              setProperty(target, name, value);
46          }
47      }
48  
49      private static void setProperty(Object target, String name, Object value) {
50          Class clazz = target.getClass();
51          Method setter = findSetterMethod(clazz, name);        
52          try {
53              // If the type is null or it matches the needed type, just use the value directly
54              if( value == null || value.getClass()==setter.getParameterTypes()[0] ) {
55                  setter.invoke(target, new Object[]{value});
56              } else {
57                  // We need to convert it
58                  setter.invoke(target, new Object[]{ convert(value, setter.getParameterTypes()[0]) });
59              }
60          } catch (Throwable e) {
61              log.debug("Could not set property: "+name+", value: "+value);
62          }
63      }
64  
65      private static Object convert(Object value, Class type) throws URISyntaxException {
66          PropertyEditor editor = PropertyEditorManager.findEditor(type);
67          if( editor != null ) { 
68              editor.setAsText(value.toString());
69              return editor.getValue();
70          }
71          if( type == URI.class ) {
72              return new URI(value.toString());
73          }
74          return null;
75      }
76  
77      private static Method findSetterMethod(Class clazz, String name) {
78          // Build the method name.
79          name = "set"+name.substring(0,1).toUpperCase()+name.substring(1);
80          Method[] methods = clazz.getMethods();
81          for (int i = 0; i < methods.length; i++) {
82              Method method = methods[i];
83              Class params[] = method.getParameterTypes();
84              if( method.getName().equals(name) 
85                      && params.length==1
86                      && isSettableType(params[0])) {
87                  return method;
88              }
89          }
90          return null;
91      }
92  
93      private static boolean isSettableType(Class clazz) {
94          if( PropertyEditorManager.findEditor(clazz)!=null )
95              return true;
96          if( clazz == URI.class )
97              return true;
98          return false;
99      }
100     
101 }