KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > rebind > rpc > CustomFieldSerializerValidator


1 /*
2  * Copyright 2007 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.user.rebind.rpc;
17
18 import com.google.gwt.core.ext.typeinfo.JClassType;
19 import com.google.gwt.core.ext.typeinfo.JMethod;
20 import com.google.gwt.core.ext.typeinfo.JPrimitiveType;
21 import com.google.gwt.core.ext.typeinfo.JType;
22
23 import java.text.MessageFormat JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 /**
28  * Checks that a custom serializer is valid.
29  */

30 class CustomFieldSerializerValidator {
31   private static final String JavaDoc NO_DESERIALIZE_METHOD = "Custom Field Serializer ''{0}'' does not define a deserialize method: ''public static void deserialize({1} reader,{2} instance)''";
32   private static final String JavaDoc NO_INSTANTIATE_METHOD = "Custom Field Serializer ''{0}'' does not define an instantiate method: ''public static {1} instantiate({2} reader)''; but ''{1}'' is not default instantiable";
33   private static final String JavaDoc NO_SERIALIZE_METHOD = "Custom Field Serializer ''{0}'' does not define a serialize method: ''public static void serialize({1} writer,{2} instance)''";
34
35   /**
36    * Returns a list of error messages associated with the custom field
37    * serializer.
38    *
39    * @param streamReaderClass
40    * {@link com.google.gwt.user.client.rpc.SerializationStreamReader SerializationStreamReader}
41    * @param streamWriterClass
42    * {@link com.google.gwt.user.client.rpc.SerializationStreamWriter SerializationStreamWriter}
43    * @param serializer the class which performs the serialization
44    * @param serializee the class being serialized
45    * @return list of error messages, if any, associated with the custom field
46    * serializer
47    */

48   public static List JavaDoc /* <String> */validate(JClassType streamReaderClass,
49       JClassType streamWriterClass, JClassType serializer, JClassType serializee) {
50     List JavaDoc /* <String> */reasons = new ArrayList JavaDoc/* <String> */();
51
52     JMethod deserialize = serializer.findMethod("deserialize", new JType[] {
53         streamReaderClass, serializee});
54     if (!isValidCustomFieldSerializerMethod(deserialize, JPrimitiveType.VOID)) {
55       reasons.add(MessageFormat.format(NO_DESERIALIZE_METHOD, new String JavaDoc[] {
56           serializer.getQualifiedSourceName(),
57           streamReaderClass.getQualifiedSourceName(),
58           serializee.getQualifiedSourceName()}));
59     }
60
61     JMethod serialize = serializer.findMethod("serialize", new JType[] {
62         streamWriterClass, serializee});
63     if (!isValidCustomFieldSerializerMethod(serialize, JPrimitiveType.VOID)) {
64       reasons.add(MessageFormat.format(NO_SERIALIZE_METHOD, new String JavaDoc[] {
65           serializer.getQualifiedSourceName(),
66           streamWriterClass.getQualifiedSourceName(),
67           serializee.getQualifiedSourceName()}));
68     }
69
70     if (!serializee.isDefaultInstantiable()) {
71       JMethod instantiate = serializer.findMethod("instantiate",
72           new JType[] {streamReaderClass});
73       if (!isValidCustomFieldSerializerMethod(instantiate, serializee)) {
74         reasons.add(MessageFormat.format(NO_INSTANTIATE_METHOD, new String JavaDoc[] {
75             serializer.getQualifiedSourceName(),
76             serializee.getQualifiedSourceName(),
77             streamReaderClass.getQualifiedSourceName()}));
78       }
79     }
80
81     return reasons;
82   }
83
84   private static boolean isValidCustomFieldSerializerMethod(JMethod method,
85       JType returnType) {
86     if (method == null || method.getReturnType() != returnType
87         || !method.isPublic() || !method.isStatic()) {
88       return false;
89     }
90
91     return true;
92   }
93
94   private CustomFieldSerializerValidator() {
95   }
96 }
97
Popular Tags