1 4 package com.tc.config.schema.dynamic; 5 6 import org.apache.xmlbeans.XmlObject; 7 import org.apache.xmlbeans.impl.common.XPath; 8 9 import com.tc.config.schema.context.ConfigContext; 10 import com.tc.util.Assert; 11 12 import java.lang.reflect.InvocationTargetException ; 13 import java.lang.reflect.Method ; 14 import java.lang.reflect.Modifier ; 15 16 20 public class StringArrayXPathBasedConfigItem extends XPathBasedConfigItem implements StringArrayConfigItem { 21 22 public StringArrayXPathBasedConfigItem(ConfigContext context, String xpath) { 23 super(context, xpath); 24 } 25 26 protected Object fetchDataFromXmlObject(XmlObject xmlObject) { 27 if (xmlObject == null) return null; 28 29 Method targetMethod = null; 33 Method [] allMethods = xmlObject.getClass().getMethods(); 34 35 for (int i = 0; i < allMethods.length; ++i) { 36 Method candidate = allMethods[i]; 37 if (candidate.getReturnType().equals(String [].class) && candidate.getParameterTypes().length == 0 38 && Modifier.isPublic(candidate.getModifiers()) && candidate.getName().startsWith("get") 39 && candidate.getName().endsWith("Array")) { 40 if (targetMethod != null) { 41 throw Assert 43 .failure("Whoa! There are multiple public methods that start with 'get', end with 'Array', take no parameters, " 44 + "and return String[] on class " 45 + xmlObject.getClass().getName() 46 + ". One is " 47 + targetMethod 48 + ", and another is " + candidate + ". We should fix " + "the program to account for this."); 49 } 50 51 targetMethod = candidate; 52 } 53 } 54 55 if (targetMethod == null) { 56 throw Assert.failure("Class " + xmlObject.getClass().getName() + " has no public methods that start with 'get', " 58 + "end with 'Array', take no parameters, and return String[]."); 59 } 60 61 try { 62 return targetMethod.invoke(xmlObject, new Object [0]); 63 } catch (IllegalArgumentException iae) { 64 throw Assert.failure("Couldn't invoke method " + targetMethod + " on object " + xmlObject + ": ", iae); 65 } catch (IllegalAccessException iae) { 66 throw Assert.failure("Couldn't invoke method " + targetMethod + " on object " + xmlObject + ": ", iae); 67 } catch (InvocationTargetException ite) { 68 throw Assert.failure("Couldn't invoke method " + targetMethod + " on object " + xmlObject + ": ", ite); 69 } 70 } 71 72 public String [] getStringArray() { 73 return (String []) getObject(); 74 } 75 76 } 77 | Popular Tags |