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.DescriptionBuilderComposite;
022: import org.apache.axis2.jaxws.description.builder.FieldDescriptionComposite;
023: import org.apache.axis2.jaxws.description.builder.HandlerChainAnnot;
024: import org.apache.axis2.jaxws.description.builder.MethodDescriptionComposite;
025: import org.apache.axis2.jaxws.description.builder.ParameterDescriptionComposite;
026: import org.apache.axis2.jaxws.description.builder.SoapBindingAnnot;
027: import org.apache.axis2.jaxws.description.builder.TMAnnotationComposite;
028: import org.apache.axis2.jaxws.description.builder.TMFAnnotationComposite;
029: import org.apache.axis2.jaxws.description.builder.WebServiceRefAnnot;
030:
031: import javax.jws.HandlerChain;
032: import javax.jws.soap.SOAPBinding;
033: import javax.xml.bind.annotation.XmlList;
034: import javax.xml.ws.WebServiceRef;
035: import java.lang.annotation.Annotation;
036: import java.lang.reflect.AnnotatedElement;
037: import java.lang.reflect.GenericArrayType;
038: import java.lang.reflect.Method;
039: import java.lang.reflect.ParameterizedType;
040: import java.lang.reflect.Type;
041: import java.lang.reflect.WildcardType;
042: import java.util.List;
043:
044: public class ConverterUtils {
045:
046: /**
047: * Helper method to retrieve the annotation specified by a certain <code>Class</code>
048: *
049: * @param annotationClass - <code>Class</code> the annotation <code>Class</code>
050: * @param element - <code>AnnotatedElement</code> - the element on which we are looking for
051: * the annotation (i.e. Class, Method, Field)
052: * @return - <code>Annotation</code> annotation represented by the given <code>Class</code>
053: */
054: public static Annotation getAnnotation(Class annotationClass,
055: AnnotatedElement element) {
056: return element.getAnnotation(annotationClass);
057: }
058:
059: /**
060: * This is a helper method to create a <code>HandlerChainAnnot</code> since the
061: *
062: * @param handlerChain - <code>HandlerChain</code>
063: * @return - <code>HandlerChainAnnot</code>
064: * @HandlerChain annotation may be present on a Type, Method, or Field.
065: */
066: public static HandlerChainAnnot createHandlerChainAnnot(
067: HandlerChain handlerChain) {
068: HandlerChainAnnot hcAnnot = HandlerChainAnnot
069: .createHandlerChainAnnotImpl();
070: hcAnnot.setFile(handlerChain.file());
071: hcAnnot.setName(handlerChain.name());
072: return hcAnnot;
073: }
074:
075: /**
076: * This is a helper method to create a <code>SoapBindingAnnot</code> since the
077: *
078: * @param soapBinding - <code>SOAPBinding</code>
079: * @return - <code>SoapBindingAnnot</code>
080: * @SOAPBinding annotation may be present on a Type or Method.
081: */
082: public static SoapBindingAnnot createSoapBindingAnnot(
083: SOAPBinding soapBinding) {
084: SoapBindingAnnot sbAnnot = SoapBindingAnnot
085: .createSoapBindingAnnotImpl();
086: sbAnnot.setParameterStyle(soapBinding.parameterStyle());
087: sbAnnot.setStyle(soapBinding.style());
088: sbAnnot.setUse(soapBinding.use());
089: return sbAnnot;
090: }
091:
092: /**
093: * This is a helper method to create a <code>WebServiceRefAnnot</code> since the
094: *
095: * @param webServiceRef - <code>WebServiceRef</code>
096: * @return - <code>WebServiceRefAnnot</code>
097: * @WebServiceRef annotation may be present on a Type, Method, or Field.
098: */
099: public static WebServiceRefAnnot createWebServiceRefAnnot(
100: WebServiceRef webServiceRef) {
101: WebServiceRefAnnot wsrAnnot = WebServiceRefAnnot
102: .createWebServiceRefAnnotImpl();
103: wsrAnnot.setMappedName(webServiceRef.mappedName());
104: wsrAnnot.setName(webServiceRef.name());
105: wsrAnnot.setType(webServiceRef.type());
106: wsrAnnot.setValue(webServiceRef.value());
107: wsrAnnot.setWsdlLocation(webServiceRef.wsdlLocation());
108: return wsrAnnot;
109: }
110:
111: /**
112: * This method is use to attach @HandlerChain annotation data to a composite object.
113: *
114: * @param composite - <code>TMFAnnotationComposite</code>
115: * @param annotatedElement - <code>AnnotatedElement</code>
116: */
117: public static void attachHandlerChainAnnotation(
118: TMFAnnotationComposite composite,
119: AnnotatedElement annotatedElement) {
120: HandlerChain handlerChain = (HandlerChain) ConverterUtils
121: .getAnnotation(HandlerChain.class, annotatedElement);
122: if (handlerChain != null) {
123: HandlerChainAnnot hcAnnot = ConverterUtils
124: .createHandlerChainAnnot(handlerChain);
125: composite.setHandlerChainAnnot(hcAnnot);
126: }
127: }
128:
129: /**
130: * This method is use to attach @SOAPBinding annotation data to a composite object.
131: *
132: * @param composite - <code>TMAnnotationComposite</code>
133: * @param annotatedElement - <code>AnnotatedElement</code>
134: */
135: public static void attachSoapBindingAnnotation(
136: TMAnnotationComposite composite,
137: AnnotatedElement annotatedElement) {
138: SOAPBinding soapBinding = (SOAPBinding) ConverterUtils
139: .getAnnotation(SOAPBinding.class, annotatedElement);
140: if (soapBinding != null) {
141: SoapBindingAnnot sbAnnot = ConverterUtils
142: .createSoapBindingAnnot(soapBinding);
143: composite.setSoapBindingAnnot(sbAnnot);
144: }
145: }
146:
147: /**
148: * This method is use to attach @WebServiceRef annotation data to a composite object.
149: *
150: * @param composite - <code>TMFAnnotationComposite</code>
151: * @param annotatedElement - <code>AnnotatedElement</code>
152: */
153: public static void attachWebServiceRefAnnotation(
154: TMFAnnotationComposite composite,
155: AnnotatedElement annotatedElement) {
156: WebServiceRef webServiceRef = (WebServiceRef) ConverterUtils
157: .getAnnotation(WebServiceRef.class, annotatedElement);
158: if (webServiceRef != null) {
159: WebServiceRefAnnot wsrAnnot = ConverterUtils
160: .createWebServiceRefAnnot(webServiceRef);
161: composite.setWebServiceRefAnnot(wsrAnnot);
162: }
163: }
164:
165: /** This method will add FieldDescriptionComposite objects to a DescriptionBuilderComposite */
166: public static void attachFieldDescriptionComposites(
167: DescriptionBuilderComposite composite,
168: List<FieldDescriptionComposite> fdcList) {
169: for (FieldDescriptionComposite fdc : fdcList) {
170: composite.addFieldDescriptionComposite(fdc);
171: }
172: }
173:
174: /** This method will add MethodDescriptionComposite objects to a DescriptionBuilderComposite */
175: public static void attachMethodDescriptionComposites(
176: DescriptionBuilderComposite composite,
177: List<MethodDescriptionComposite> mdcList) {
178: for (MethodDescriptionComposite mdc : mdcList) {
179: composite.addMethodDescriptionComposite(mdc);
180: mdc.setDescriptionBuilderCompositeRef(composite);
181: }
182: }
183:
184: /** This method will add ParameterDescriptionComposite objects to a MethodDescriptionComposite */
185: public static void attachParameterDescriptionComposites(
186: List<ParameterDescriptionComposite> pdcList,
187: MethodDescriptionComposite mdc) {
188: for (ParameterDescriptionComposite pdc : pdcList) {
189: mdc.addParameterDescriptionComposite(pdc);
190: pdc.setMethodDescriptionCompositeRef(mdc);
191: }
192: }
193:
194: /**
195: * This method will check to see if a method's declaring class is the Object class.
196: *
197: * @param method - <code>Method</code>
198: * @return - <code>boolean</code>
199: */
200: public static boolean isInherited(Method method,
201: String declaringClass) {
202: if (method.getDeclaringClass().getName().equals(declaringClass)) {
203: return false;
204: }
205: return true;
206: }
207:
208: /**
209: * This method will construct a <code>String</code> that represents the
210: * full type of a parameterized variable.
211: * @param pt - <code>ParameterizedType</code>
212: * @param paramType - <code>String</code>
213: * @return - <code>String</code>
214: */
215: public static String getFullType(ParameterizedType pt,
216: String paramType) {
217: if (pt.getRawType() instanceof Class) {
218: Class rawClass = (Class) pt.getRawType();
219: paramType = paramType + rawClass.getName();
220: }
221: Type[] genericTypes = pt.getActualTypeArguments();
222: if (genericTypes.length > 0) {
223: paramType = paramType + "<";
224: for (int i = 0; i < genericTypes.length; i++) {
225: Type type = genericTypes[i];
226: paramType = getType(type, paramType);
227:
228: // Set string for more parameters OR close the generic if this is the last one.
229: if (i != genericTypes.length - 1) {
230: paramType = paramType + ", ";
231: } else {
232: paramType = paramType + ">";
233: }
234:
235: }
236: }
237: return paramType;
238: }
239:
240: public static String getType(Type type, String paramType) {
241: if (type instanceof Class) {
242: paramType = paramType + ((Class) type).getName();
243: } else if (type instanceof ParameterizedType) {
244: paramType = getFullType((ParameterizedType) type, paramType);
245: } else if (type instanceof WildcardType) {
246: paramType = paramType + "?";
247: } else if (type instanceof GenericArrayType) {
248: paramType = getType(((GenericArrayType) type)
249: .getGenericComponentType(), paramType)
250: + "[]";
251: }
252: return paramType;
253: }
254:
255: /**
256: * This method will search array of parameter annotations for the presence of the @XmlList
257: * annotation.
258: */
259: public static boolean hasXmlListAnnotation(Annotation[] annotations) {
260: for (Annotation annotation : annotations) {
261: if (annotation.annotationType() == XmlList.class) {
262: return true;
263: }
264: }
265: return false;
266: }
267: }
|