View Javadoc
1 /* 2 * Copyright (c) 2003, Henri Yandell 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or 6 * without modification, are permitted provided that the 7 * following conditions are met: 8 * 9 * + Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * + Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * + Neither the name of Genjava-Core nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package com.generationjava.beans; 33 34 import java.lang.reflect.InvocationTargetException; 35 import java.lang.reflect.Method; 36 37 import org.apache.commons.collections.CollectionUtils; 38 import org.apache.commons.lang.StringUtils; 39 40 /*** 41 * Uses Reflection to get a named property from an object. 42 */ 43 public class ReflectionBeanViewer extends AbstractBeanViewer { 44 45 public ReflectionBeanViewer() { 46 } 47 48 /*** 49 * Get an indexed property from a java bean. If the property is 50 * not indexed then a -1 is passed in. 51 * 52 * If it's not possible to get the object, then null is returned. 53 */ 54 public Object invokeGet(BeanViewRuntime runtime, Object bean, String methodName, Object index) { 55 int idx = -1; 56 boolean arrayLike = (index instanceof Integer); 57 if(arrayLike) { 58 idx = ((Integer)index).intValue(); 59 } 60 methodName = "get"+StringUtils.capitalise(methodName); 61 62 Method getMethod = null; 63 Class[] clss0 = new Class[0]; 64 Class[] clss = null; 65 try { 66 if(arrayLike) { 67 clss = new Class[1]; 68 clss[0] = Integer.TYPE; 69 } else { 70 clss = clss0; 71 } 72 getMethod = bean.getClass().getMethod(methodName, clss); 73 } catch(SecurityException se) { 74 } catch(NoSuchMethodException nsme) { 75 try { 76 if(clss != clss0) { 77 getMethod = bean.getClass().getMethod(methodName, clss0); 78 } 79 arrayLike = false; 80 } catch(SecurityException se2) { 81 } catch(NoSuchMethodException nsme2) { 82 } 83 } 84 85 if(getMethod == null) { 86 return null; 87 } 88 try { 89 Object[] param = null; 90 if(arrayLike) { 91 param = new Object[1]; 92 param[0] = (Integer)index; 93 } else { 94 param = new Object[0]; 95 } 96 Object obj = getMethod.invoke( bean, param ); 97 obj = CollectionUtils.index(obj, index); 98 99 return obj; 100 } catch(IllegalAccessException iae) { 101 iae.printStackTrace(); 102 return null; 103 } catch(IllegalArgumentException iae) { 104 iae.printStackTrace(); 105 return null; 106 } catch(InvocationTargetException ite) { 107 ite.printStackTrace(); 108 return null; 109 } 110 111 } 112 113 public void invokeSet(BeanViewRuntime runtime, Object bean, String methodName, Object idx, Object value) { 114 // int idx = -1; 115 // boolean arrayLike = (index instanceof Integer); 116 // if(arrayLike) { 117 // idx = ((Integer)index).intValue(); 118 // } 119 methodName = "set"+StringUtils.capitalise(methodName); 120 121 Method setMethod = null; 122 Method[] methods = bean.getClass().getMethods(); 123 int size = methods.length; 124 for(int i=0; i<size; i++) { 125 if(methods[i].getName().equals(methodName)) { 126 setMethod = methods[i]; 127 break; 128 } 129 } 130 131 /* OLD GET 132 Class[] clss0 = new Class[0]; 133 Class[] clss = null; 134 try { 135 if(arrayLike) { 136 clss = new Class[1]; 137 clss[0] = Integer.TYPE; 138 } else { 139 clss = clss0; 140 } 141 getMethod = bean.getClass().getMethod(methodName, clss); 142 } catch(SecurityException se) { 143 } catch(NoSuchMethodException nsme) { 144 try { 145 if(clss != clss0) { 146 getMethod = bean.getClass().getMethod(methodName, clss0); 147 } 148 arrayLike = false; 149 } catch(SecurityException se2) { 150 } catch(NoSuchMethodException nsme2) { 151 } 152 } 153 */ 154 155 if(setMethod == null) { 156 return; 157 } 158 159 System.err.println("CONVERT-TO"+setMethod.getParameterTypes()[0]); 160 System.err.println("CONVERT:"+value); 161 value = BeansW.convert(value, setMethod.getParameterTypes()[0]); 162 163 try { 164 Object[] param = null; 165 // if(arrayLike) { 166 // param = new Object[1]; 167 // param[0] = (Integer)index; 168 // } else { 169 param = new Object[1]; 170 param[0] = value; 171 // } 172 setMethod.invoke( bean, param ); 173 // obj = CollectionUtils.index(obj, index); 174 175 return; 176 } catch(IllegalAccessException iae) { 177 iae.printStackTrace(); 178 return; 179 } catch(IllegalArgumentException iae) { 180 iae.printStackTrace(); 181 return; 182 } catch(InvocationTargetException ite) { 183 ite.printStackTrace(); 184 return; 185 } 186 187 } 188 189 }

This page was automatically generated by Maven