1 16 package com.google.gwt.dev.util.xml; 17 18 import java.lang.reflect.Field ; 19 import java.lang.reflect.Method ; 20 21 25 public final class HandlerParam { 26 27 30 public static HandlerParam create(Method method, String normalizedTagName, 31 int paramIndex) { 32 Class paramType = method.getParameterTypes()[paramIndex]; 33 Field [] fields = method.getDeclaringClass().getDeclaredFields(); 34 String fieldNamePrefix = normalizedTagName + "_" + (paramIndex + 1); 35 Field matchingField = null; 36 String fieldName = null; 37 for (int i = 0, n = fields.length; i < n; ++i) { 38 Field testField = fields[i]; 39 fieldName = testField.getName(); 40 if (fieldName.startsWith(fieldNamePrefix)) { 41 matchingField = testField; 42 break; 43 } 44 } 45 46 if (matchingField == null) { 47 throw new IllegalArgumentException ("Expecting a meta field with prefix '" 48 + fieldNamePrefix + "'"); 49 } 50 51 int under = fieldName.indexOf("_", fieldNamePrefix.length()); 52 if (under == -1) { 53 throw new IllegalArgumentException ( 56 "Expecting a normalized attribute name suffix (e.g. \"_attr_name\") on field '" 57 + fieldName + "'"); 58 } 59 60 String normalizedAttrName = fieldName.substring(under + 1); 63 64 if (!String .class.equals(matchingField.getType())) { 67 throw new IllegalArgumentException ("GWT field '" + fieldName 70 + "' must be of type String"); 71 } 72 73 matchingField.setAccessible(true); 76 HandlerParam handlerParam = new HandlerParam(paramType, matchingField, 77 normalizedAttrName); 78 return handlerParam; 79 } 80 81 private final Class paramType; 82 83 private final Field metaField; 84 85 private final String normalizedAttrName; 86 87 private HandlerParam(Class paramType, Field metaField, 88 String normalizedAttrName) { 89 this.paramType = paramType; 90 this.metaField = metaField; 91 this.normalizedAttrName = normalizedAttrName; 92 } 93 94 97 public String getDefaultValue(Schema schema) { 98 Throwable caught = null; 99 try { 100 return (String ) metaField.get(schema); 101 } catch (IllegalArgumentException e) { 102 caught = e; 103 } catch (IllegalAccessException e) { 104 caught = e; 105 } 106 107 throw (IllegalStateException ) new IllegalStateException ( 112 "Unable to get attribute default value from meta field '" 113 + metaField.getName() + "'").initCause(caught); 114 } 115 116 public String getNormalizedName() { 117 return normalizedAttrName; 118 } 119 120 public Class getParamType() { 121 return paramType; 122 } 123 } 124 | Popular Tags |