KickJava   Java API By Example, From Geeks To Geeks.

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


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.user.rebind.rpc;
17
18 import com.google.gwt.core.ext.BadPropertyValueException;
19 import com.google.gwt.core.ext.PropertyOracle;
20 import com.google.gwt.core.ext.TreeLogger;
21 import com.google.gwt.core.ext.typeinfo.JClassType;
22 import com.google.gwt.core.ext.typeinfo.JParameterizedType;
23 import com.google.gwt.core.ext.typeinfo.JPrimitiveType;
24 import com.google.gwt.core.ext.typeinfo.JType;
25
26 import java.util.Locale JavaDoc;
27
28 class Shared {
29
30   /**
31    * Property used to control whether or not the RPC system will enforce the
32    * versioning scheme or not.
33    */

34   static final String JavaDoc RPC_PROP_ENFORCE_TYPE_VERSIONING = "gwt.enforceRPCTypeVersioning";
35
36   /**
37    * Capitalizes a name.
38    *
39    * @param name the string to be capitalized
40    * @return the capitalized string
41    */

42   static String JavaDoc capitalize(String JavaDoc name) {
43     return name.substring(0, 1).toUpperCase(Locale.US) + name.substring(1);
44   }
45
46   /**
47    * Gets the suffix needed to make a call for a particular type. For example,
48    * the <code>int</code> class needs methods named "readInt" and "writeInt".
49    *
50    * @param type the type in question
51    * @return the suffix of the method to call
52    */

53   static String JavaDoc getCallSuffix(JType type) {
54     JParameterizedType isParameterized = type.isParameterized();
55     if (isParameterized != null) {
56       return getCallSuffix(isParameterized.getRawType());
57     } else if (type.isPrimitive() != null) {
58       if (type == JPrimitiveType.BOOLEAN) {
59         return "Boolean";
60       } else if (type == JPrimitiveType.BYTE) {
61         return "Byte";
62       } else if (type == JPrimitiveType.CHAR) {
63         return "Char";
64       } else if (type == JPrimitiveType.DOUBLE) {
65         return "Double";
66       } else if (type == JPrimitiveType.FLOAT) {
67         return "Float";
68       } else if (type == JPrimitiveType.INT) {
69         return "Int";
70       } else if (type == JPrimitiveType.LONG) {
71         return "Long";
72       } else if (type == JPrimitiveType.SHORT) {
73         return "Short";
74       } else {
75         return null;
76       }
77     } else if (type.getQualifiedSourceName().equals("java.lang.String")) {
78       return "String";
79     } else {
80       return "Object";
81     }
82   }
83
84   /**
85    * Returns <code>true</code> if the generated code should enforce type
86    * versioning.
87    */

88   static boolean shouldEnforceTypeVersioning(TreeLogger logger, PropertyOracle propertyOracle) {
89     try {
90       String JavaDoc propVal = propertyOracle.getPropertyValue(logger,
91           RPC_PROP_ENFORCE_TYPE_VERSIONING);
92       if (propVal.equals("false")) {
93         return false;
94       }
95     } catch (BadPropertyValueException e) {
96       // Purposely ignored, because we want to enforce RPC versioning if
97
// the property is not defined.
98
}
99     // Assume type versioning unless the user explicitly turns it off.
100
return true;
101   }
102
103   /**
104    * Computes a good name for a class related to the specified type, such that
105    * the computed name can be a top-level class in the same package as the
106    * specified type.
107    *
108    * <p>
109    * This method does not currently check for collisions between the synthesized
110    * name and an existing top-level type in the same package. It is actually
111    * tricky to do so, because on subsequent runs, we'll view our own generated
112    * classes as collisions. There's probably some trick we can use in the future
113    * to make it totally bulletproof.
114    * </p>
115    *
116    * @param type the name of the base type, whose name will be built upon to
117    * synthesize a new type name
118    * @param suffix a suffix to be used to make the new synthesized type name
119    * @return an array of length 2 such that the first element is the package
120    * name and the second element is the synthesized class name
121    */

122   static String JavaDoc[] synthesizeTopLevelClassName(JClassType type, String JavaDoc suffix) {
123     // Gets the basic name of the type. If it's a nested type, the type name
124
// will contains dots.
125
//
126
String JavaDoc className = type.getName();
127
128     // Add the meaningful suffix.
129
//
130
className += suffix;
131
132     // Make it a top-level name.
133
//
134
className = className.replace('.', '_');
135
136     String JavaDoc packageName = type.getPackage().getName();
137     return new String JavaDoc[]{packageName, className};
138   }
139
140   /**
141    * Determines whether a particular type needs to be cast to become its final
142    * type. Primitives and Strings do not, as they are read directly as the
143    * correct type. All other Objects need a cast, except for Object itself.
144    *
145    * @param type the type in question
146    * @return <code>true</code> if the results of a read method must be cast,
147    * otherwise <code>false</code>.
148    */

149   static boolean typeNeedsCast(JType type) {
150     return type.isPrimitive() == null
151       && !type.getQualifiedSourceName().equals("java.lang.String")
152       && !type.getQualifiedSourceName().equals("java.lang.Object");
153   }
154 }
155
Popular Tags