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.description.builder.converter;
020:
021: import org.apache.axis2.jaxws.description.builder.MethodDescriptionComposite;
022: import org.apache.axis2.jaxws.description.builder.ParameterDescriptionComposite;
023: import org.apache.axis2.jaxws.description.builder.RequestWrapperAnnot;
024: import org.apache.axis2.jaxws.description.builder.ResponseWrapperAnnot;
025: import org.apache.axis2.jaxws.description.builder.WebEndpointAnnot;
026: import org.apache.axis2.jaxws.description.builder.WebMethodAnnot;
027: import org.apache.axis2.jaxws.description.builder.WebResultAnnot;
028:
029: import javax.jws.Oneway;
030: import javax.jws.WebMethod;
031: import javax.jws.WebResult;
032: import javax.xml.ws.RequestWrapper;
033: import javax.xml.ws.ResponseWrapper;
034: import javax.xml.ws.WebEndpoint;
035:
036: import java.lang.reflect.Constructor;
037: import java.lang.reflect.Method;
038: import java.lang.reflect.ParameterizedType;
039: import java.lang.reflect.Type;
040: import java.util.ArrayList;
041: import java.util.List;
042:
043: public class JavaMethodsToMDCConverter {
044:
045: private Method[] methods;
046: private Constructor[] constructors;
047: private String declaringClass;
048:
049: public JavaMethodsToMDCConverter(Method[] methods,
050: Constructor[] constructors, String declaringClass) {
051: this .methods = methods;
052: this .constructors = constructors;
053: this .declaringClass = declaringClass;
054: }
055:
056: /**
057: * This will drive the creation of a <code>MethodDescriptionComposite</code> for every Java Method
058: * in the methods array and every Java Constructor in the constructors array.
059: *
060: * @return - <code>List</code>
061: */
062: public List<MethodDescriptionComposite> convertMethods() {
063: List<MethodDescriptionComposite> mdcList = new ArrayList<MethodDescriptionComposite>();
064: for (Method method : methods) {
065: if (!ConverterUtils.isInherited(method, declaringClass)) {
066: MethodDescriptionComposite mdc = new MethodDescriptionComposite();
067: setExceptionList(mdc, method);
068: mdc.setMethodName(method.getName());
069: setReturnType(mdc, method);
070: setIsListType(mdc, method);
071: mdc.setDeclaringClass(method.getDeclaringClass()
072: .getName());
073: attachHandlerChainAnnotation(mdc, method);
074: attachOnewayAnnotation(mdc, method);
075: attachSoapBindingAnnotation(mdc, method);
076: attachRequestWrapperAnnotation(mdc, method);
077: attachResponseWrapperAnnotation(mdc, method);
078: attachWebEndpointAnnotation(mdc, method);
079: attachWebMethodAnnotation(mdc, method);
080: attachWebResultAnnotation(mdc, method);
081: attachWebServiceRefAnnotation(mdc, method);
082: if (method.getGenericParameterTypes().length > 0) {
083: JavaParamToPDCConverter paramConverter = new JavaParamToPDCConverter(
084: method.getGenericParameterTypes(), method
085: .getParameterAnnotations());
086: List<ParameterDescriptionComposite> pdcList = paramConverter
087: .convertParams();
088: ConverterUtils
089: .attachParameterDescriptionComposites(
090: pdcList, mdc);
091: }
092: mdcList.add(mdc);
093: }
094: }
095:
096: for (Constructor constructor : constructors) {
097: MethodDescriptionComposite mdc = new MethodDescriptionComposite();
098: mdc.setMethodName("<init>");
099: mdc.setDeclaringClass(constructor.getDeclaringClass()
100: .getName());
101: mdcList.add(mdc);
102: if (constructor.getGenericParameterTypes().length > 0) {
103: JavaParamToPDCConverter paramConverter = new JavaParamToPDCConverter(
104: constructor.getGenericParameterTypes(),
105: constructor.getParameterAnnotations());
106: List<ParameterDescriptionComposite> pdcList = paramConverter
107: .convertParams();
108: ConverterUtils.attachParameterDescriptionComposites(
109: pdcList, mdc);
110: }
111: }
112:
113: return mdcList;
114: }
115:
116: /**
117: * This method attaches the list of exceptions for a Java Method to the
118: * <code>MethodDescriptionComposite</code>
119: *
120: * @param mdc - <code>MethodDescriptionComposite</code>
121: * @param method - <code>Method</code>
122: */
123: private void setExceptionList(MethodDescriptionComposite mdc,
124: Method method) {
125: if (method.getExceptionTypes().length > 0) {
126: Type[] exceptionTypes = method.getGenericExceptionTypes();
127: String[] exceptions = new String[exceptionTypes.length];
128: for (int i = 0; i < exceptionTypes.length; i++) {
129: Type type = exceptionTypes[i];
130: if (type instanceof ParameterizedType) {
131: ParameterizedType pt = (ParameterizedType) type;
132: String fullType = "";
133: fullType = ConverterUtils.getFullType(pt, fullType);
134: exceptions[i] = fullType;
135: } else if (type instanceof Class) {
136: exceptions[i] = ((Class) type).getName();
137: }
138: }
139: mdc.setExceptions(exceptions);
140: }
141: }
142:
143: /**
144: * This method will drive the attachment of @HandlerChain annotation data to the
145: * <code>MethodDescriptionComposite</code>
146: *
147: * @param mdc - <code>MethodDescriptionComposite</code>
148: * @param method - <code>Method</code>
149: */
150: private void attachHandlerChainAnnotation(
151: MethodDescriptionComposite mdc, Method method) {
152: ConverterUtils.attachHandlerChainAnnotation(mdc, method);
153: }
154:
155: /**
156: * This method will be used to drive the setting of @SOAPBinding annotation data to the
157: * <code>MethodDescriptionComposite</code>
158: *
159: * @param composite - <code>MethodDescriptionComposite</code>
160: */
161: private void attachSoapBindingAnnotation(
162: MethodDescriptionComposite mdc, Method method) {
163: ConverterUtils.attachSoapBindingAnnotation(mdc, method);
164: }
165:
166: /**
167: * This method will drive the attachment of @Oneway annotation data to the
168: * <code>MethodDescriptionComposite</code>
169: *
170: * @param mdc - <code>MethodDescriptionComposite</code>
171: * @param method - <code>Method</code>
172: */
173: private void attachOnewayAnnotation(MethodDescriptionComposite mdc,
174: Method method) {
175: Oneway oneway = (Oneway) ConverterUtils.getAnnotation(
176: Oneway.class, method);
177: if (oneway != null) {
178: mdc.setOneWayAnnot(true);
179: } else {
180: mdc.setOneWayAnnot(false);
181: }
182: }
183:
184: /**
185: * This method will drive the attachment of @RequestWrapper annotation data to the
186: * <code>MethodDescriptionComposite</code>
187: *
188: * @param mdc - <code>MethodDescriptionComposite</code>
189: * @param method - <code>Method</code>
190: */
191: private void attachRequestWrapperAnnotation(
192: MethodDescriptionComposite mdc, Method method) {
193: RequestWrapper requestWrapper = (RequestWrapper) ConverterUtils
194: .getAnnotation(RequestWrapper.class, method);
195: if (requestWrapper != null) {
196: RequestWrapperAnnot rwAnnot = RequestWrapperAnnot
197: .createRequestWrapperAnnotImpl();
198: rwAnnot.setClassName(requestWrapper.className());
199: rwAnnot.setLocalName(requestWrapper.localName());
200: rwAnnot
201: .setTargetNamespace(requestWrapper
202: .targetNamespace());
203: mdc.setRequestWrapperAnnot(rwAnnot);
204: }
205: }
206:
207: /**
208: * This method will drive the attachment of @ResponeWrapper annotation data to the
209: * <code>MethodDescriptionComposite</code>
210: *
211: * @param mdc - <code>MethodDescriptionComposite</code>
212: * @param method - <code>Method</code>
213: */
214: private void attachResponseWrapperAnnotation(
215: MethodDescriptionComposite mdc, Method method) {
216: ResponseWrapper responseWrapper = (ResponseWrapper) ConverterUtils
217: .getAnnotation(ResponseWrapper.class, method);
218: if (responseWrapper != null) {
219: ResponseWrapperAnnot rwAnnot = ResponseWrapperAnnot
220: .createResponseWrapperAnnotImpl();
221: rwAnnot.setClassName(responseWrapper.className());
222: rwAnnot.setLocalName(responseWrapper.localName());
223: rwAnnot.setTargetNamespace(responseWrapper
224: .targetNamespace());
225: mdc.setResponseWrapperAnnot(rwAnnot);
226: }
227: }
228:
229: /**
230: * This method will drive the attachment of @WebEndpoint annotation data to the
231: * <code>MethodDescriptionComposite</code>
232: *
233: * @param mdc - <code>MethodDescriptionComposite</code>
234: * @param method - <code>Method</code>
235: */
236: private void attachWebEndpointAnnotation(
237: MethodDescriptionComposite mdc, Method method) {
238: WebEndpoint webEndpoint = (WebEndpoint) ConverterUtils
239: .getAnnotation(WebEndpoint.class, method);
240: if (webEndpoint != null) {
241: WebEndpointAnnot weAnnot = WebEndpointAnnot
242: .createWebEndpointAnnotImpl();
243: weAnnot.setName(webEndpoint.name());
244: mdc.setWebEndpointAnnot(weAnnot);
245: }
246: }
247:
248: /**
249: * This method will drive the attachment of @WebMethod annotation data to the
250: * <code>MethodDescriptionComposite</code>
251: *
252: * @param mdc - <code>MethodDescriptionComposite</code>
253: * @param method - <code>Method</code>
254: */
255: private void attachWebMethodAnnotation(
256: MethodDescriptionComposite mdc, Method method) {
257: WebMethod webMethod = (WebMethod) ConverterUtils.getAnnotation(
258: WebMethod.class, method);
259: if (webMethod != null) {
260: WebMethodAnnot wmAnnot = WebMethodAnnot
261: .createWebMethodAnnotImpl();
262: wmAnnot.setAction(webMethod.action());
263: wmAnnot.setExclude(webMethod.exclude());
264: wmAnnot.setOperationName(webMethod.operationName());
265: mdc.setWebMethodAnnot(wmAnnot);
266: }
267: }
268:
269: /**
270: * This method will drive the attachment of @WebResult annotation data to the
271: * <code>MethodDescriptionComposite</code>
272: *
273: * @param mdc - <code>MethodDescriptionComposite</code>
274: * @param method - <code>Method</code>
275: */
276: private void attachWebResultAnnotation(
277: MethodDescriptionComposite mdc, Method method) {
278: WebResult webResult = (WebResult) ConverterUtils.getAnnotation(
279: WebResult.class, method);
280: if (webResult != null) {
281: WebResultAnnot wrAnnot = WebResultAnnot
282: .createWebResultAnnotImpl();
283: wrAnnot.setHeader(webResult.header());
284: wrAnnot.setName(webResult.name());
285: wrAnnot.setPartName(webResult.partName());
286: wrAnnot.setTargetNamespace(webResult.targetNamespace());
287: mdc.setWebResultAnnot(wrAnnot);
288: }
289: }
290:
291: /**
292: * This method will drive the attachment of @WebServiceRef annotation data to the
293: * <code>MethodDescriptionComposite</code>
294: *
295: * @param mdc - <code>MethodDescriptionComposite</code>
296: * @param method - <code>Method</code>
297: */
298: private void attachWebServiceRefAnnotation(
299: MethodDescriptionComposite mdc, Method method) {
300: ConverterUtils.attachWebServiceRefAnnotation(mdc, method);
301: }
302:
303: /**
304: * This method will determine the return type of a <code>Method</code> and
305: * attach it to a <code>MethodDescriptionComposite</code> object.
306: * @param mdc - <code>MethodDescriptionComposite</code>
307: * @param method - <code>Method</code>
308: */
309: private void setReturnType(MethodDescriptionComposite mdc,
310: Method method) {
311: Type type = method.getGenericReturnType();
312: if (type == null) {
313: mdc.setReturnType("void");
314: } else if (type instanceof ParameterizedType) {
315: ParameterizedType pt = (ParameterizedType) type;
316: String fullType = "";
317: fullType = ConverterUtils.getFullType(pt, fullType);
318: mdc.setReturnType(fullType);
319: } else if (type instanceof Class) {
320: mdc.setReturnType(((Class) type).getName());
321: }
322: }
323:
324: private void setIsListType(MethodDescriptionComposite mdc,
325: Method method) {
326: mdc.setIsListType(ConverterUtils.hasXmlListAnnotation(method
327: .getAnnotations()));
328: }
329:
330: }
|