KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > util > xml > HandlerParam


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.util.xml;
17
18 import java.lang.reflect.Field JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
20
21 /**
22  * Represents metadata about a parameter in a handler method in a class derived
23  * from {@link Schema}.
24  */

25 public final class HandlerParam {
26
27   /**
28    * Looks for a field of the form [methodName]_[paramIndex]_[attrName].
29    */

30   public static HandlerParam create(Method JavaDoc method, String JavaDoc normalizedTagName,
31       int paramIndex) {
32     Class JavaDoc paramType = method.getParameterTypes()[paramIndex];
33     Field JavaDoc[] fields = method.getDeclaringClass().getDeclaredFields();
34     String JavaDoc fieldNamePrefix = normalizedTagName + "_" + (paramIndex + 1);
35     Field JavaDoc matchingField = null;
36     String JavaDoc fieldName = null;
37     for (int i = 0, n = fields.length; i < n; ++i) {
38       Field JavaDoc 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 JavaDoc("Expecting a meta field with prefix '"
48           + fieldNamePrefix + "'");
49     }
50
51     int under = fieldName.indexOf("_", fieldNamePrefix.length());
52     if (under == -1) {
53       // Not a valid signature.
54
//
55
throw new IllegalArgumentException JavaDoc(
56           "Expecting a normalized attribute name suffix (e.g. \"_attr_name\") on field '"
57               + fieldName + "'");
58     }
59
60     // Infer the associated attribute name.
61
//
62
String JavaDoc normalizedAttrName = fieldName.substring(under + 1);
63
64     // GWT fields values must be of type String.
65
//
66
if (!String JavaDoc.class.equals(matchingField.getType())) {
67       // Type mismatch.
68
//
69
throw new IllegalArgumentException JavaDoc("GWT field '" + fieldName
70           + "' must be of type String");
71     }
72
73     // Instantiate one.
74
//
75
matchingField.setAccessible(true);
76     HandlerParam handlerParam = new HandlerParam(paramType, matchingField,
77         normalizedAttrName);
78     return handlerParam;
79   }
80
81   private final Class JavaDoc paramType;
82
83   private final Field JavaDoc metaField;
84
85   private final String JavaDoc normalizedAttrName;
86
87   private HandlerParam(Class JavaDoc paramType, Field JavaDoc metaField,
88       String JavaDoc normalizedAttrName) {
89     this.paramType = paramType;
90     this.metaField = metaField;
91     this.normalizedAttrName = normalizedAttrName;
92   }
93
94   /**
95    * Called while parsing to get the default value for an attribute.
96    */

97   public String JavaDoc getDefaultValue(Schema schema) {
98     Throwable JavaDoc caught = null;
99     try {
100       return (String JavaDoc) metaField.get(schema);
101     } catch (IllegalArgumentException JavaDoc e) {
102       caught = e;
103     } catch (IllegalAccessException JavaDoc e) {
104       caught = e;
105     }
106
107     // IllegalStateException has no (String, Throwable) constructor in 1.4,
108
// which forces us to use this incantation. See the top of
109
// {@link java.lang.Throwable} for details.
110
//
111
throw (IllegalStateException JavaDoc) new IllegalStateException JavaDoc(
112         "Unable to get attribute default value from meta field '"
113             + metaField.getName() + "'").initCause(caught);
114   }
115
116   public String JavaDoc getNormalizedName() {
117     return normalizedAttrName;
118   }
119
120   public Class JavaDoc getParamType() {
121     return paramType;
122   }
123 }
124
Popular Tags