1 15 package org.apache.hivemind.schema.rules; 16 17 import java.lang.reflect.InvocationTargetException ; 18 import java.lang.reflect.Method ; 19 import java.util.List ; 20 21 import org.apache.hivemind.ApplicationRuntimeException; 22 import org.apache.hivemind.Element; 23 import org.apache.hivemind.schema.SchemaProcessor; 24 25 33 public class InvokeParentRule extends BaseRule 34 { 35 private String _methodName; 36 37 private int _depth = 1; 38 private int _parameterCount = 1; 39 40 public InvokeParentRule() 41 { 42 43 } 44 45 public InvokeParentRule(String methodName) 46 { 47 _methodName = methodName; 48 } 49 50 53 public void begin(SchemaProcessor processor, Element element) 54 { 55 Object [] parameters = new Object [_parameterCount]; 56 Class [] parameterTypes = new Class [_parameterCount]; 57 for (int i = 0; i < parameters.length; i++) 59 { 60 parameters[i] = processor.peek(_parameterCount - i - 1); 61 if (parameterTypes[i] != null) 62 parameterTypes[i] = parameters[i].getClass(); 63 } 64 65 String methodName = _methodName; 66 Object parent = processor.peek(_parameterCount + _depth - 1); 67 if (processor.isInBackwardCompatibilityModeForMaps() && _parameterCount == 1 68 && ("addElement".equals(_methodName) || "add".equals(_methodName))) { 69 methodName = "addKeyedElement"; 70 parent = processor; 74 } else if (parent instanceof List && "addElement".equals(_methodName) ) { 75 methodName = "add"; 78 } 79 80 try 81 { 82 Method m = findMethod(parent, methodName, parameterTypes); 83 84 m.invoke(parent, parameters); 85 } 86 catch (InvocationTargetException ex) { 87 throw new ApplicationRuntimeException(RulesMessages.errorInvokingMethod( 88 methodName, 89 parent, 90 getLocation(), 91 ex.getTargetException()), getLocation(), ex.getTargetException()); 92 } catch (Exception ex) 93 { 94 throw new ApplicationRuntimeException(RulesMessages.errorInvokingMethod( 95 methodName, 96 parent, 97 getLocation(), 98 ex), getLocation(), ex); 99 } 100 } 101 102 public String getMethodName() 103 { 104 return _methodName; 105 } 106 107 public void setMethodName(String string) 108 { 109 _methodName = string; 110 } 111 112 115 public int getDepth() 116 { 117 return _depth; 118 } 119 120 123 public void setDepth(int i) 124 { 125 _depth = i; 126 } 127 128 135 private Method findMethod(Object target, String name, Class [] parameterTypes) 136 throws NoSuchMethodException 137 { 138 Method [] methods = target.getClass().getMethods(); 139 140 for (int i = 0; i < methods.length; i++) 141 { 142 Method m = methods[i]; 143 Class [] actualParameterTypes = m.getParameterTypes(); 144 145 if (actualParameterTypes.length != parameterTypes.length) 146 continue; 147 148 if (!m.getName().equals(name)) 149 continue; 150 151 for (int j = 0; j < actualParameterTypes.length; j++) 152 { 153 Class actualParameterType = actualParameterTypes[j]; 154 Class expectedParameterType = parameterTypes[j]; 155 if ((expectedParameterType != null && actualParameterType.isAssignableFrom(expectedParameterType)) 156 || (expectedParameterType == null && !actualParameterType.isPrimitive())) 157 return m; 158 159 } 160 } 161 162 throw new NoSuchMethodException (name); 163 } 164 165 public int getParameterCount() 166 { 167 return _parameterCount; 168 } 169 170 175 public void setParameterCount(int parameterCount) 176 { 177 _parameterCount = parameterCount; 178 } 179 180 } 181 | Popular Tags |