001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.jaxws.utility;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023:
024: import javax.jws.WebService;
025: import javax.xml.ws.Holder;
026: import javax.xml.ws.Service;
027: import javax.xml.ws.WebFault;
028: import javax.xml.ws.WebServiceClient;
029: import javax.xml.ws.WebServiceProvider;
030: import java.lang.reflect.Constructor;
031: import java.lang.reflect.InvocationTargetException;
032: import java.util.HashMap;
033:
034: /** Contains static Class utility methods related to method parameter/argument marshalling. */
035: public class ClassUtils {
036:
037: private static Log log = LogFactory.getLog(ClassUtils.class);
038:
039: /**
040: * Gets the RootCause for an throwable. The root cause is defined as the first
041: * non-InvocationTargetException.
042: *
043: * @param e Throwable
044: * @return Throwable root cause
045: */
046: public static Throwable getRootCause(Throwable e) {
047: Throwable t = null;
048:
049: if (e != null) {
050: if (e instanceof InvocationTargetException) {
051: t = ((InvocationTargetException) e)
052: .getTargetException();
053: } else {
054: t = null;
055: }
056:
057: if (t != null) {
058: e = getRootCause(t);
059: }
060: }
061: return e;
062: }
063:
064: private static HashMap loadClassMap = new HashMap();
065:
066: static {
067: loadClassMap.put("byte", byte.class);
068: loadClassMap.put("int", int.class);
069: loadClassMap.put("short", short.class);
070: loadClassMap.put("long", long.class);
071: loadClassMap.put("float", float.class);
072: loadClassMap.put("double", double.class);
073: loadClassMap.put("boolean", boolean.class);
074: loadClassMap.put("char", char.class);
075: loadClassMap.put("void", void.class);
076: }
077:
078: /** Converts text of the form Foo[] to the proper class name for loading [LFoo */
079: private static HashMap loadableMap = new HashMap();
080:
081: static {
082: loadableMap.put("byte", "B");
083: loadableMap.put("char", "C");
084: loadableMap.put("double", "D");
085: loadableMap.put("float", "F");
086: loadableMap.put("int", "I");
087: loadableMap.put("long", "J");
088: loadableMap.put("short", "S");
089: loadableMap.put("boolean", "Z");
090: }
091:
092: /**
093: * @param text String
094: * @return String that can be used for Class.forName
095: */
096: public static String getLoadableClassName(String text) {
097: int bracket = text.indexOf("[");
098: String className = text;
099:
100: // Get the className without any array brackets
101: if (bracket > 0) {
102: className = className.substring(0, bracket);
103: }
104:
105: // Now get the loadable name from the map or
106: // its L<className>;
107: String loadClass = (String) loadableMap.get(className);
108: if (loadClass == null) {
109: loadClass = "L" + className + ";";
110: }
111:
112: // Now prepend [ for each array dimension
113: if (bracket > 0) {
114: int i = text.indexOf("]");
115: while (i > 0) {
116: loadClass = "[" + loadClass;
117: i = text.indexOf("]", i + 1);
118: }
119: }
120: return loadClass;
121: }
122:
123: /** Converts text of the form [LFoo to the Foo[] */
124: public static String getTextClassName(String text) {
125: if (text == null || text.indexOf("[") != 0)
126: return text;
127: String className = "";
128: int index = 0;
129: while (index < text.length() && text.charAt(index) == '[') {
130: index++;
131: className += "[]";
132: }
133: if (index < text.length()) {
134: if (text.charAt(index) == 'B')
135: className = "byte" + className;
136: else if (text.charAt(index) == 'C')
137: className = "char" + className;
138: else if (text.charAt(index) == 'D')
139: className = "double" + className;
140: else if (text.charAt(index) == 'F')
141: className = "float" + className;
142: else if (text.charAt(index) == 'I')
143: className = "int" + className;
144: else if (text.charAt(index) == 'J')
145: className = "long" + className;
146: else if (text.charAt(index) == 'S')
147: className = "short" + className;
148: else if (text.charAt(index) == 'Z')
149: className = "boolean" + className;
150: else if (text.equals("void"))
151: className = "void";
152: else {
153: className = text
154: .substring(index + 1, text.indexOf(";"))
155: + className;
156: }
157: }
158: return className;
159: }
160:
161: /**
162: * @param primitive
163: * @return java wrapper class or null
164: */
165: public static Class getWrapperClass(Class primitive) {
166: if (primitive == int.class)
167: return java.lang.Integer.class;
168: else if (primitive == short.class)
169: return java.lang.Short.class;
170: else if (primitive == boolean.class)
171: return java.lang.Boolean.class;
172: else if (primitive == byte.class)
173: return java.lang.Byte.class;
174: else if (primitive == long.class)
175: return java.lang.Long.class;
176: else if (primitive == double.class)
177: return java.lang.Double.class;
178: else if (primitive == float.class)
179: return java.lang.Float.class;
180: else if (primitive == char.class)
181: return java.lang.Character.class;
182:
183: return null;
184: }
185:
186: /**
187: * @param wrapper
188: * @return primitive clas or null
189: */
190: public static Class getPrimitiveClass(Class wrapper) {
191: if (wrapper == java.lang.Integer.class)
192: return int.class;
193: else if (wrapper == java.lang.Short.class)
194: return short.class;
195: else if (wrapper == java.lang.Boolean.class)
196: return boolean.class;
197: else if (wrapper == java.lang.Byte.class)
198: return byte.class;
199: else if (wrapper == java.lang.Long.class)
200: return long.class;
201: else if (wrapper == java.lang.Double.class)
202: return double.class;
203: else if (wrapper == java.lang.Float.class)
204: return float.class;
205: else if (wrapper == java.lang.Character.class)
206: return char.class;
207:
208: return null;
209: }
210:
211: private static final Class[] noClass = new Class[] {};
212:
213: /**
214: * Get the default public constructor
215: *
216: * @param clazz
217: * @return Constructor or null
218: */
219: public static Constructor getDefaultPublicConstructor(Class clazz) {
220: try {
221: return clazz.getConstructor(noClass);
222: } catch (Exception e) {
223: return null;
224: }
225: }
226:
227: /**
228: * @param name of primitive type
229: * @return primitive Class or null
230: */
231: public static Class getPrimitiveClass(String text) {
232: return (Class) loadClassMap.get(text);
233: }
234:
235: /**
236: * @param cls
237: * @return true if this is a JAX-WS or JAX-WS generated class
238: */
239: public static final boolean isJAXWSClass(Class cls) {
240: // TODO Processing all of these annotations is very expensive. We need to cache the
241: // result in a static WeakHashMap<Class, Boolean>
242:
243: // Kinds of generated classes: Service, Provider, Impl, Exception, Holder
244: // Or the class is in the jaxws.xml.ws package
245:
246: // Check for Impl
247: WebService wsAnn = (WebService) cls
248: .getAnnotation(WebService.class);
249: if (wsAnn != null) {
250: return true;
251: }
252:
253: // Check for service
254: WebServiceClient wscAnn = (WebServiceClient) cls
255: .getAnnotation(WebServiceClient.class);
256: if (wscAnn != null) {
257: return true;
258: }
259:
260: // Check for provider
261: WebServiceProvider wspAnn = (WebServiceProvider) cls
262: .getAnnotation(WebServiceProvider.class);
263: if (wspAnn != null) {
264: return true;
265: }
266:
267: // Check for Exception
268: WebFault wfAnn = (WebFault) cls.getAnnotation(WebFault.class);
269: if (wfAnn != null) {
270: return true;
271: }
272:
273: // Check for Holder
274: if (Holder.class.isAssignableFrom(cls)) {
275: return true;
276: }
277:
278: // Check for a javax.xml.ws.Service class instance
279: if (Service.class.isAssignableFrom(cls)) {
280: return true;
281: }
282:
283: if (cls.getPackage() != null
284: && cls.getPackage().getName()
285: .startsWith("javax.xml.ws")) {
286: return true;
287: }
288: return false;
289: }
290:
291: }
|