Wed, 27 Apr 2016 01:27:09 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.tools.internal.ws.processor.modeler.annotation; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.codemodel.internal.CodeWriter; |
aoqi@0 | 29 | import com.sun.codemodel.internal.JAnnotationArrayMember; |
aoqi@0 | 30 | import com.sun.codemodel.internal.JAnnotationUse; |
aoqi@0 | 31 | import com.sun.codemodel.internal.JBlock; |
aoqi@0 | 32 | import com.sun.codemodel.internal.JCodeModel; |
aoqi@0 | 33 | import com.sun.codemodel.internal.JCommentPart; |
aoqi@0 | 34 | import com.sun.codemodel.internal.JDefinedClass; |
aoqi@0 | 35 | import com.sun.codemodel.internal.JDocComment; |
aoqi@0 | 36 | import com.sun.codemodel.internal.JExpr; |
aoqi@0 | 37 | import com.sun.codemodel.internal.JFieldVar; |
aoqi@0 | 38 | import com.sun.codemodel.internal.JMethod; |
aoqi@0 | 39 | import com.sun.codemodel.internal.JMod; |
aoqi@0 | 40 | import com.sun.codemodel.internal.JType; |
aoqi@0 | 41 | import com.sun.codemodel.internal.JVar; |
aoqi@0 | 42 | import com.sun.codemodel.internal.writer.ProgressCodeWriter; |
aoqi@0 | 43 | import com.sun.tools.internal.jxc.ap.InlineAnnotationReaderImpl; |
aoqi@0 | 44 | import com.sun.tools.internal.jxc.model.nav.ApNavigator; |
aoqi@0 | 45 | import com.sun.tools.internal.ws.ToolVersion; |
aoqi@0 | 46 | import com.sun.tools.internal.ws.processor.generator.GeneratorBase; |
aoqi@0 | 47 | import com.sun.tools.internal.ws.processor.generator.GeneratorConstants; |
aoqi@0 | 48 | import com.sun.tools.internal.ws.processor.generator.Names; |
aoqi@0 | 49 | import com.sun.tools.internal.ws.processor.modeler.ModelerException; |
aoqi@0 | 50 | import com.sun.tools.internal.ws.processor.util.DirectoryUtil; |
aoqi@0 | 51 | import com.sun.tools.internal.ws.resources.WebserviceapMessages; |
aoqi@0 | 52 | import com.sun.tools.internal.ws.util.ClassNameInfo; |
aoqi@0 | 53 | import com.sun.tools.internal.ws.wscompile.FilerCodeWriter; |
aoqi@0 | 54 | import com.sun.tools.internal.ws.wscompile.WsgenOptions; |
aoqi@0 | 55 | import com.sun.tools.internal.ws.wsdl.document.soap.SOAPStyle; |
aoqi@0 | 56 | import com.sun.xml.internal.bind.v2.model.annotation.AnnotationReader; |
aoqi@0 | 57 | import com.sun.xml.internal.bind.v2.model.nav.Navigator; |
aoqi@0 | 58 | import com.sun.xml.internal.ws.model.AbstractWrapperBeanGenerator; |
aoqi@0 | 59 | import com.sun.xml.internal.ws.spi.db.BindingHelper; |
aoqi@0 | 60 | import com.sun.xml.internal.ws.util.StringUtils; |
aoqi@0 | 61 | |
aoqi@0 | 62 | import javax.jws.Oneway; |
aoqi@0 | 63 | import javax.jws.WebMethod; |
aoqi@0 | 64 | import javax.jws.WebService; |
aoqi@0 | 65 | import javax.lang.model.element.ExecutableElement; |
aoqi@0 | 66 | import javax.lang.model.element.Name; |
aoqi@0 | 67 | import javax.lang.model.element.TypeElement; |
aoqi@0 | 68 | import javax.lang.model.type.DeclaredType; |
aoqi@0 | 69 | import javax.lang.model.type.MirroredTypeException; |
aoqi@0 | 70 | import javax.lang.model.type.TypeKind; |
aoqi@0 | 71 | import javax.lang.model.type.TypeMirror; |
aoqi@0 | 72 | import javax.xml.bind.annotation.XmlAccessType; |
aoqi@0 | 73 | import javax.xml.bind.annotation.XmlAccessorType; |
aoqi@0 | 74 | import javax.xml.bind.annotation.XmlAttachmentRef; |
aoqi@0 | 75 | import javax.xml.bind.annotation.XmlElement; |
aoqi@0 | 76 | import javax.xml.bind.annotation.XmlList; |
aoqi@0 | 77 | import javax.xml.bind.annotation.XmlMimeType; |
aoqi@0 | 78 | import javax.xml.bind.annotation.XmlRootElement; |
aoqi@0 | 79 | import javax.xml.bind.annotation.XmlType; |
aoqi@0 | 80 | import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; |
aoqi@0 | 81 | import javax.xml.namespace.QName; |
aoqi@0 | 82 | import javax.xml.ws.RequestWrapper; |
aoqi@0 | 83 | import javax.xml.ws.ResponseWrapper; |
aoqi@0 | 84 | import javax.xml.ws.WebFault; |
aoqi@0 | 85 | import javax.xml.ws.WebServiceException; |
aoqi@0 | 86 | import java.io.File; |
aoqi@0 | 87 | import java.io.IOException; |
aoqi@0 | 88 | import java.lang.annotation.Annotation; |
aoqi@0 | 89 | import java.util.Collection; |
aoqi@0 | 90 | import java.util.HashSet; |
aoqi@0 | 91 | import java.util.List; |
aoqi@0 | 92 | import java.util.Set; |
aoqi@0 | 93 | |
aoqi@0 | 94 | import static com.sun.codemodel.internal.ClassType.CLASS; |
aoqi@0 | 95 | import static com.sun.tools.internal.ws.processor.modeler.annotation.WebServiceConstants.BEAN; |
aoqi@0 | 96 | import static com.sun.tools.internal.ws.processor.modeler.annotation.WebServiceConstants.FAULT_INFO; |
aoqi@0 | 97 | import static com.sun.tools.internal.ws.processor.modeler.annotation.WebServiceConstants.JAXWS_PACKAGE_PD; |
aoqi@0 | 98 | import static com.sun.tools.internal.ws.processor.modeler.annotation.WebServiceConstants.PD_JAXWS_PACKAGE_PD; |
aoqi@0 | 99 | import static com.sun.tools.internal.ws.processor.modeler.annotation.WebServiceConstants.RESPONSE; |
aoqi@0 | 100 | |
aoqi@0 | 101 | /** |
aoqi@0 | 102 | * This class generates the request/response and Exception Beans |
aoqi@0 | 103 | * used by the JAX-WS runtime. |
aoqi@0 | 104 | * |
aoqi@0 | 105 | * @author WS Development Team |
aoqi@0 | 106 | */ |
aoqi@0 | 107 | public class WebServiceWrapperGenerator extends WebServiceVisitor { |
aoqi@0 | 108 | private Set<String> wrapperNames; |
aoqi@0 | 109 | private Set<String> processedExceptions; |
aoqi@0 | 110 | private JCodeModel cm; |
aoqi@0 | 111 | private final MakeSafeTypeVisitor makeSafeVisitor; |
aoqi@0 | 112 | |
aoqi@0 | 113 | private static final FieldFactory FIELD_FACTORY = new FieldFactory(); |
aoqi@0 | 114 | |
aoqi@0 | 115 | private final AbstractWrapperBeanGenerator ap_generator = |
aoqi@0 | 116 | new ApWrapperBeanGenerator(InlineAnnotationReaderImpl.theInstance, |
aoqi@0 | 117 | new ApNavigator(builder.getProcessingEnvironment()), FIELD_FACTORY); |
aoqi@0 | 118 | |
aoqi@0 | 119 | private final class ApWrapperBeanGenerator extends AbstractWrapperBeanGenerator<TypeMirror, TypeElement, ExecutableElement, MemberInfo> { |
aoqi@0 | 120 | |
aoqi@0 | 121 | protected ApWrapperBeanGenerator( |
aoqi@0 | 122 | AnnotationReader<TypeMirror, TypeElement, ?, ExecutableElement> annReader, |
aoqi@0 | 123 | Navigator<TypeMirror, TypeElement, ?, ExecutableElement> nav, BeanMemberFactory<TypeMirror, MemberInfo> beanMemberFactory) { |
aoqi@0 | 124 | super(annReader, nav, beanMemberFactory); |
aoqi@0 | 125 | } |
aoqi@0 | 126 | |
aoqi@0 | 127 | @Override |
aoqi@0 | 128 | protected TypeMirror getSafeType(TypeMirror type) { |
aoqi@0 | 129 | return WebServiceWrapperGenerator.this.getSafeType(type); |
aoqi@0 | 130 | } |
aoqi@0 | 131 | |
aoqi@0 | 132 | @Override |
aoqi@0 | 133 | protected TypeMirror getHolderValueType(TypeMirror paramType) { |
aoqi@0 | 134 | return builder.getHolderValueType(paramType); |
aoqi@0 | 135 | } |
aoqi@0 | 136 | |
aoqi@0 | 137 | @Override |
aoqi@0 | 138 | protected boolean isVoidType(TypeMirror type) { |
aoqi@0 | 139 | return type != null && type.getKind().equals(TypeKind.VOID); |
aoqi@0 | 140 | } |
aoqi@0 | 141 | |
aoqi@0 | 142 | } |
aoqi@0 | 143 | |
aoqi@0 | 144 | private static final class FieldFactory implements AbstractWrapperBeanGenerator.BeanMemberFactory<TypeMirror, MemberInfo> { |
aoqi@0 | 145 | |
aoqi@0 | 146 | @Override |
aoqi@0 | 147 | public MemberInfo createWrapperBeanMember(TypeMirror paramType, |
aoqi@0 | 148 | String paramName, List<Annotation> jaxb) { |
aoqi@0 | 149 | return new MemberInfo(paramType, paramName, jaxb); |
aoqi@0 | 150 | } |
aoqi@0 | 151 | } |
aoqi@0 | 152 | |
aoqi@0 | 153 | public WebServiceWrapperGenerator(ModelBuilder builder, AnnotationProcessorContext context) { |
aoqi@0 | 154 | super(builder, context); |
aoqi@0 | 155 | makeSafeVisitor = new MakeSafeTypeVisitor(builder.getProcessingEnvironment()); |
aoqi@0 | 156 | } |
aoqi@0 | 157 | |
aoqi@0 | 158 | @Override |
aoqi@0 | 159 | protected void processWebService(WebService webService, TypeElement d) { |
aoqi@0 | 160 | cm = new JCodeModel(); |
aoqi@0 | 161 | wrapperNames = new HashSet<String>(); |
aoqi@0 | 162 | processedExceptions = new HashSet<String>(); |
aoqi@0 | 163 | } |
aoqi@0 | 164 | |
aoqi@0 | 165 | @Override |
aoqi@0 | 166 | protected void postProcessWebService(WebService webService, TypeElement d) { |
aoqi@0 | 167 | super.postProcessWebService(webService, d); |
aoqi@0 | 168 | doPostProcessWebService(webService, d); |
aoqi@0 | 169 | } |
aoqi@0 | 170 | |
aoqi@0 | 171 | @SuppressWarnings("CallToThreadDumpStack") |
aoqi@0 | 172 | protected void doPostProcessWebService(WebService webService, TypeElement d) { |
aoqi@0 | 173 | if (cm != null) { |
aoqi@0 | 174 | File sourceDir = builder.getSourceDir(); |
aoqi@0 | 175 | assert(sourceDir != null); |
aoqi@0 | 176 | WsgenOptions options = builder.getOptions(); |
aoqi@0 | 177 | try { |
aoqi@0 | 178 | CodeWriter cw = new FilerCodeWriter(sourceDir, options); |
aoqi@0 | 179 | if(options.verbose) |
aoqi@0 | 180 | cw = new ProgressCodeWriter(cw, System.out); |
aoqi@0 | 181 | cm.build(cw); |
aoqi@0 | 182 | } catch (IOException e) { |
aoqi@0 | 183 | e.printStackTrace(); |
aoqi@0 | 184 | } |
aoqi@0 | 185 | } |
aoqi@0 | 186 | } |
aoqi@0 | 187 | |
aoqi@0 | 188 | @Override |
aoqi@0 | 189 | protected void processMethod(ExecutableElement method, WebMethod webMethod) { |
aoqi@0 | 190 | builder.log("WrapperGen - method: "+method); |
aoqi@0 | 191 | builder.log("method.getDeclaringType(): " + method.asType()); |
aoqi@0 | 192 | if (wrapped && soapStyle.equals(SOAPStyle.DOCUMENT)) { |
aoqi@0 | 193 | generateWrappers(method, webMethod); |
aoqi@0 | 194 | } |
aoqi@0 | 195 | generateExceptionBeans(method); |
aoqi@0 | 196 | } |
aoqi@0 | 197 | |
aoqi@0 | 198 | private boolean generateExceptionBeans(ExecutableElement method) { |
aoqi@0 | 199 | String beanPackage = packageName + PD_JAXWS_PACKAGE_PD.getValue(); |
aoqi@0 | 200 | if (packageName.length() == 0) |
aoqi@0 | 201 | beanPackage = JAXWS_PACKAGE_PD.getValue(); |
aoqi@0 | 202 | boolean beanGenerated = false; |
aoqi@0 | 203 | for (TypeMirror thrownType : method.getThrownTypes()) { |
aoqi@0 | 204 | TypeElement typeDecl = (TypeElement) ((DeclaredType) thrownType).asElement(); |
aoqi@0 | 205 | if (typeDecl == null) { |
aoqi@0 | 206 | builder.processError(WebserviceapMessages.WEBSERVICEAP_COULD_NOT_FIND_TYPEDECL( |
aoqi@0 | 207 | thrownType.toString(), context.getRound())); |
aoqi@0 | 208 | return false; |
aoqi@0 | 209 | } |
aoqi@0 | 210 | boolean tmp = generateExceptionBean(typeDecl, beanPackage); |
aoqi@0 | 211 | beanGenerated = beanGenerated || tmp; |
aoqi@0 | 212 | } |
aoqi@0 | 213 | return beanGenerated; |
aoqi@0 | 214 | } |
aoqi@0 | 215 | |
aoqi@0 | 216 | private boolean duplicateName(String name) { |
aoqi@0 | 217 | for (String str : wrapperNames) { |
aoqi@0 | 218 | if (str.equalsIgnoreCase(name)) |
aoqi@0 | 219 | return true; |
aoqi@0 | 220 | } |
aoqi@0 | 221 | wrapperNames.add(name); |
aoqi@0 | 222 | return false; |
aoqi@0 | 223 | } |
aoqi@0 | 224 | |
aoqi@0 | 225 | private boolean generateWrappers(ExecutableElement method, WebMethod webMethod) { |
aoqi@0 | 226 | boolean isOneway = method.getAnnotation(Oneway.class) != null; |
aoqi@0 | 227 | String beanPackage = packageName + PD_JAXWS_PACKAGE_PD.getValue(); |
aoqi@0 | 228 | if (packageName.length() == 0) |
aoqi@0 | 229 | beanPackage = JAXWS_PACKAGE_PD.getValue(); |
aoqi@0 | 230 | Name methodName = method.getSimpleName(); |
aoqi@0 | 231 | String operationName = builder.getOperationName(methodName); |
aoqi@0 | 232 | operationName = webMethod != null && webMethod.operationName().length() > 0 ? |
aoqi@0 | 233 | webMethod.operationName() : operationName; |
aoqi@0 | 234 | String reqName = operationName; |
aoqi@0 | 235 | String resName = operationName + WebServiceConstants.RESPONSE.getValue(); |
aoqi@0 | 236 | String reqNamespace = typeNamespace; |
aoqi@0 | 237 | String resNamespace = typeNamespace; |
aoqi@0 | 238 | |
aoqi@0 | 239 | String requestClassName = beanPackage + StringUtils.capitalize(method.getSimpleName().toString()); |
aoqi@0 | 240 | RequestWrapper reqWrapper = method.getAnnotation(RequestWrapper.class); |
aoqi@0 | 241 | if (reqWrapper != null) { |
aoqi@0 | 242 | if (reqWrapper.className().length() > 0) |
aoqi@0 | 243 | requestClassName = reqWrapper.className(); |
aoqi@0 | 244 | if (reqWrapper.localName().length() > 0) |
aoqi@0 | 245 | reqName = reqWrapper.localName(); |
aoqi@0 | 246 | if (reqWrapper.targetNamespace().length() > 0) |
aoqi@0 | 247 | reqNamespace = reqWrapper.targetNamespace(); |
aoqi@0 | 248 | } |
aoqi@0 | 249 | builder.log("requestWrapper: "+requestClassName); |
aoqi@0 | 250 | ///// fix for wsgen CR 6442344 |
aoqi@0 | 251 | File file = new File(DirectoryUtil.getOutputDirectoryFor(requestClassName, builder.getSourceDir()), |
aoqi@0 | 252 | Names.stripQualifier(requestClassName) + GeneratorConstants.JAVA_SRC_SUFFIX.getValue()); |
aoqi@0 | 253 | builder.getOptions().addGeneratedFile(file); |
aoqi@0 | 254 | ////////// |
aoqi@0 | 255 | boolean canOverwriteRequest = builder.canOverWriteClass(requestClassName); |
aoqi@0 | 256 | if (!canOverwriteRequest) { |
aoqi@0 | 257 | builder.log("Class " + requestClassName + " exists. Not overwriting."); |
aoqi@0 | 258 | } |
aoqi@0 | 259 | if (duplicateName(requestClassName) && canOverwriteRequest) { |
aoqi@0 | 260 | builder.processError(WebserviceapMessages.WEBSERVICEAP_METHOD_REQUEST_WRAPPER_BEAN_NAME_NOT_UNIQUE( |
aoqi@0 | 261 | typeElement.getQualifiedName(), method.toString())); |
aoqi@0 | 262 | } |
aoqi@0 | 263 | |
aoqi@0 | 264 | String responseClassName = null; |
aoqi@0 | 265 | boolean canOverwriteResponse = canOverwriteRequest; |
aoqi@0 | 266 | if (!isOneway) { |
aoqi@0 | 267 | responseClassName = beanPackage+StringUtils.capitalize(method.getSimpleName().toString())+RESPONSE.getValue(); |
aoqi@0 | 268 | ResponseWrapper resWrapper = method.getAnnotation(ResponseWrapper.class); |
aoqi@0 | 269 | if(resWrapper != null) { |
aoqi@0 | 270 | if (resWrapper.className().length() > 0) |
aoqi@0 | 271 | responseClassName = resWrapper.className(); |
aoqi@0 | 272 | if (resWrapper.localName().length() > 0) |
aoqi@0 | 273 | resName = resWrapper.localName(); |
aoqi@0 | 274 | if (resWrapper.targetNamespace().length() > 0) |
aoqi@0 | 275 | resNamespace = resWrapper.targetNamespace(); |
aoqi@0 | 276 | } |
aoqi@0 | 277 | canOverwriteResponse = builder.canOverWriteClass(responseClassName); |
aoqi@0 | 278 | if (!canOverwriteResponse) { |
aoqi@0 | 279 | builder.log("Class " + responseClassName + " exists. Not overwriting."); |
aoqi@0 | 280 | } |
aoqi@0 | 281 | if (duplicateName(responseClassName) && canOverwriteResponse) { |
aoqi@0 | 282 | builder.processError(WebserviceapMessages.WEBSERVICEAP_METHOD_RESPONSE_WRAPPER_BEAN_NAME_NOT_UNIQUE( |
aoqi@0 | 283 | typeElement.getQualifiedName(), method.toString())); |
aoqi@0 | 284 | } |
aoqi@0 | 285 | file = new File(DirectoryUtil.getOutputDirectoryFor(responseClassName, builder.getSourceDir()), |
aoqi@0 | 286 | Names.stripQualifier(responseClassName) + GeneratorConstants.JAVA_SRC_SUFFIX.getValue()); |
aoqi@0 | 287 | builder.getOptions().addGeneratedFile(file); |
aoqi@0 | 288 | } |
aoqi@0 | 289 | //ArrayList<MemberInfo> reqMembers = new ArrayList<MemberInfo>(); |
aoqi@0 | 290 | //ArrayList<MemberInfo> resMembers = new ArrayList<MemberInfo>(); |
aoqi@0 | 291 | WrapperInfo reqWrapperInfo = new WrapperInfo(requestClassName); |
aoqi@0 | 292 | //reqWrapperInfo.setMembers(reqMembers); |
aoqi@0 | 293 | WrapperInfo resWrapperInfo = null; |
aoqi@0 | 294 | if (!isOneway) { |
aoqi@0 | 295 | resWrapperInfo = new WrapperInfo(responseClassName); |
aoqi@0 | 296 | //resWrapperInfo.setMembers(resMembers); |
aoqi@0 | 297 | } |
aoqi@0 | 298 | seiContext.setReqWrapperOperation(method, reqWrapperInfo); |
aoqi@0 | 299 | if (!isOneway) |
aoqi@0 | 300 | seiContext.setResWrapperOperation(method, resWrapperInfo); |
aoqi@0 | 301 | try { |
aoqi@0 | 302 | if (!canOverwriteRequest && !canOverwriteResponse) { |
aoqi@0 | 303 | return false; |
aoqi@0 | 304 | } |
aoqi@0 | 305 | |
aoqi@0 | 306 | JDefinedClass reqCls = null; |
aoqi@0 | 307 | if (canOverwriteRequest) { |
aoqi@0 | 308 | reqCls = getCMClass(requestClassName, CLASS); |
aoqi@0 | 309 | } |
aoqi@0 | 310 | |
aoqi@0 | 311 | JDefinedClass resCls = null; |
aoqi@0 | 312 | if (!isOneway && canOverwriteResponse) { |
aoqi@0 | 313 | resCls = getCMClass(responseClassName, CLASS); |
aoqi@0 | 314 | } |
aoqi@0 | 315 | |
aoqi@0 | 316 | // XMLElement Declarations |
aoqi@0 | 317 | writeXmlElementDeclaration(reqCls, reqName,reqNamespace); |
aoqi@0 | 318 | writeXmlElementDeclaration(resCls, resName, resNamespace); |
aoqi@0 | 319 | |
aoqi@0 | 320 | List<MemberInfo> reqMembers = ap_generator.collectRequestBeanMembers(method); |
aoqi@0 | 321 | List<MemberInfo> resMembers = ap_generator.collectResponseBeanMembers(method); |
aoqi@0 | 322 | |
aoqi@0 | 323 | // XmlType |
aoqi@0 | 324 | writeXmlTypeDeclaration(reqCls, reqName, reqNamespace, reqMembers); |
aoqi@0 | 325 | writeXmlTypeDeclaration(resCls, resName, resNamespace, resMembers); |
aoqi@0 | 326 | |
aoqi@0 | 327 | // class members |
aoqi@0 | 328 | writeMembers(reqCls, reqMembers); |
aoqi@0 | 329 | writeMembers(resCls, resMembers); |
aoqi@0 | 330 | |
aoqi@0 | 331 | } catch (Exception e) { |
aoqi@0 | 332 | throw new ModelerException("modeler.nestedGeneratorError",e); |
aoqi@0 | 333 | } |
aoqi@0 | 334 | return true; |
aoqi@0 | 335 | } |
aoqi@0 | 336 | |
aoqi@0 | 337 | // private List<Annotation> collectJAXBAnnotations(Declaration decl) { |
aoqi@0 | 338 | // List<Annotation> jaxbAnnotation = new ArrayList<Annotation>(); |
aoqi@0 | 339 | // for(Class jaxbClass : jaxbAnns) { |
aoqi@0 | 340 | // Annotation ann = decl.getAnnotation(jaxbClass); |
aoqi@0 | 341 | // if (ann != null) { |
aoqi@0 | 342 | // jaxbAnnotation.add(ann); |
aoqi@0 | 343 | // } |
aoqi@0 | 344 | // } |
aoqi@0 | 345 | // return jaxbAnnotation; |
aoqi@0 | 346 | // } |
aoqi@0 | 347 | |
aoqi@0 | 348 | private TypeMirror getSafeType(TypeMirror type) { |
aoqi@0 | 349 | return makeSafeVisitor.visit(type, builder.getProcessingEnvironment().getTypeUtils()); |
aoqi@0 | 350 | } |
aoqi@0 | 351 | |
aoqi@0 | 352 | private JType getType(TypeMirror typeMirror) { |
aoqi@0 | 353 | String type = typeMirror.toString(); |
aoqi@0 | 354 | try { |
aoqi@0 | 355 | // System.out.println("typeName: "+typeName); |
aoqi@0 | 356 | return cm.parseType(type); |
aoqi@0 | 357 | // System.out.println("type: "+type); |
aoqi@0 | 358 | } catch (ClassNotFoundException e) { |
aoqi@0 | 359 | return cm.ref(type); |
aoqi@0 | 360 | } |
aoqi@0 | 361 | } |
aoqi@0 | 362 | |
aoqi@0 | 363 | private void writeMembers(JDefinedClass cls, Collection<MemberInfo> members) { |
aoqi@0 | 364 | if (cls == null) |
aoqi@0 | 365 | return; |
aoqi@0 | 366 | for (MemberInfo memInfo : members) { |
aoqi@0 | 367 | JType type = getType(memInfo.getParamType()); |
aoqi@0 | 368 | JFieldVar field = cls.field(JMod.PRIVATE, type, memInfo.getParamName()); |
aoqi@0 | 369 | annotateParameterWithJaxbAnnotations(memInfo, field); |
aoqi@0 | 370 | } |
aoqi@0 | 371 | for (MemberInfo memInfo : members) { |
aoqi@0 | 372 | writeMember(cls, memInfo.getParamType(), |
aoqi@0 | 373 | memInfo.getParamName()); |
aoqi@0 | 374 | } |
aoqi@0 | 375 | } |
aoqi@0 | 376 | |
aoqi@0 | 377 | private void annotateParameterWithJaxbAnnotations(MemberInfo memInfo, JFieldVar field) { |
aoqi@0 | 378 | List<Annotation> jaxbAnnotations = memInfo.getJaxbAnnotations(); |
aoqi@0 | 379 | for(Annotation ann : jaxbAnnotations) { |
aoqi@0 | 380 | if (ann instanceof XmlMimeType) { |
aoqi@0 | 381 | JAnnotationUse jaxbAnn = field.annotate(XmlMimeType.class); |
aoqi@0 | 382 | jaxbAnn.param("value", ((XmlMimeType)ann).value()); |
aoqi@0 | 383 | } else if (ann instanceof XmlJavaTypeAdapter) { |
aoqi@0 | 384 | JAnnotationUse jaxbAnn = field.annotate(XmlJavaTypeAdapter.class); |
aoqi@0 | 385 | XmlJavaTypeAdapter ja = (XmlJavaTypeAdapter) ann; |
aoqi@0 | 386 | try { |
aoqi@0 | 387 | ja.value(); |
aoqi@0 | 388 | throw new AssertionError(); |
aoqi@0 | 389 | } catch (MirroredTypeException e) { |
aoqi@0 | 390 | jaxbAnn.param("value",getType(e.getTypeMirror())); |
aoqi@0 | 391 | } |
aoqi@0 | 392 | // XmlJavaTypeAdapter.type() is for package only. No need to copy. |
aoqi@0 | 393 | } else if (ann instanceof XmlAttachmentRef) { |
aoqi@0 | 394 | field.annotate(XmlAttachmentRef.class); |
aoqi@0 | 395 | } else if (ann instanceof XmlList){ |
aoqi@0 | 396 | field.annotate(XmlList.class); |
aoqi@0 | 397 | } else if (ann instanceof XmlElement) { |
aoqi@0 | 398 | XmlElement elemAnn = (XmlElement)ann; |
aoqi@0 | 399 | JAnnotationUse jAnn = field.annotate(XmlElement.class); |
aoqi@0 | 400 | jAnn.param("name", elemAnn.name()); |
aoqi@0 | 401 | jAnn.param("namespace", elemAnn.namespace()); |
aoqi@0 | 402 | if (elemAnn.nillable()) { |
aoqi@0 | 403 | jAnn.param("nillable", true); |
aoqi@0 | 404 | } |
aoqi@0 | 405 | if (elemAnn.required()) { |
aoqi@0 | 406 | jAnn.param("required", true); |
aoqi@0 | 407 | } |
aoqi@0 | 408 | } else { |
aoqi@0 | 409 | throw new WebServiceException("SEI Parameter cannot have this JAXB annotation: " + ann); |
aoqi@0 | 410 | } |
aoqi@0 | 411 | } |
aoqi@0 | 412 | } |
aoqi@0 | 413 | |
aoqi@0 | 414 | protected JDefinedClass getCMClass(String className, com.sun.codemodel.internal.ClassType type) { |
aoqi@0 | 415 | JDefinedClass cls; |
aoqi@0 | 416 | try { |
aoqi@0 | 417 | cls = cm._class(className, type); |
aoqi@0 | 418 | } catch (com.sun.codemodel.internal.JClassAlreadyExistsException e){ |
aoqi@0 | 419 | cls = cm._getClass(className); |
aoqi@0 | 420 | } |
aoqi@0 | 421 | return cls; |
aoqi@0 | 422 | } |
aoqi@0 | 423 | |
aoqi@0 | 424 | private boolean generateExceptionBean(TypeElement thrownDecl, String beanPackage) { |
aoqi@0 | 425 | if (!builder.isServiceException(thrownDecl.asType())) |
aoqi@0 | 426 | return false; |
aoqi@0 | 427 | |
aoqi@0 | 428 | String exceptionName = ClassNameInfo.getName(thrownDecl.getQualifiedName().toString()); |
aoqi@0 | 429 | if (processedExceptions.contains(exceptionName)) |
aoqi@0 | 430 | return false; |
aoqi@0 | 431 | processedExceptions.add(exceptionName); |
aoqi@0 | 432 | WebFault webFault = thrownDecl.getAnnotation(WebFault.class); |
aoqi@0 | 433 | String className = beanPackage+ exceptionName + BEAN.getValue(); |
aoqi@0 | 434 | |
aoqi@0 | 435 | Collection<MemberInfo> members = ap_generator.collectExceptionBeanMembers(thrownDecl); |
aoqi@0 | 436 | boolean isWSDLException = isWSDLException(members, thrownDecl); |
aoqi@0 | 437 | String namespace = typeNamespace; |
aoqi@0 | 438 | String name = exceptionName; |
aoqi@0 | 439 | FaultInfo faultInfo; |
aoqi@0 | 440 | if (isWSDLException) { |
aoqi@0 | 441 | TypeMirror beanType = getFaultInfoMember(members).getParamType(); |
aoqi@0 | 442 | faultInfo = new FaultInfo(TypeMonikerFactory.getTypeMoniker(beanType), true); |
aoqi@0 | 443 | namespace = webFault.targetNamespace().length()>0 ? |
aoqi@0 | 444 | webFault.targetNamespace() : namespace; |
aoqi@0 | 445 | name = webFault.name().length()>0 ? |
aoqi@0 | 446 | webFault.name() : name; |
aoqi@0 | 447 | faultInfo.setElementName(new QName(namespace, name)); |
aoqi@0 | 448 | seiContext.addExceptionBeanEntry(thrownDecl.getQualifiedName(), faultInfo, builder); |
aoqi@0 | 449 | return false; |
aoqi@0 | 450 | } |
aoqi@0 | 451 | if (webFault != null) { |
aoqi@0 | 452 | namespace = webFault.targetNamespace().length()>0 ? |
aoqi@0 | 453 | webFault.targetNamespace() : namespace; |
aoqi@0 | 454 | name = webFault.name().length()>0 ? |
aoqi@0 | 455 | webFault.name() : name; |
aoqi@0 | 456 | className = webFault.faultBean().length()>0 ? |
aoqi@0 | 457 | webFault.faultBean() : className; |
aoqi@0 | 458 | |
aoqi@0 | 459 | } |
aoqi@0 | 460 | JDefinedClass cls = getCMClass(className, CLASS); |
aoqi@0 | 461 | faultInfo = new FaultInfo(className, false); |
aoqi@0 | 462 | |
aoqi@0 | 463 | if (duplicateName(className)) { |
aoqi@0 | 464 | builder.processError(WebserviceapMessages.WEBSERVICEAP_METHOD_EXCEPTION_BEAN_NAME_NOT_UNIQUE( |
aoqi@0 | 465 | typeElement.getQualifiedName(), thrownDecl.getQualifiedName())); |
aoqi@0 | 466 | } |
aoqi@0 | 467 | |
aoqi@0 | 468 | boolean canOverWriteBean = builder.canOverWriteClass(className); |
aoqi@0 | 469 | if (!canOverWriteBean) { |
aoqi@0 | 470 | builder.log("Class " + className + " exists. Not overwriting."); |
aoqi@0 | 471 | seiContext.addExceptionBeanEntry(thrownDecl.getQualifiedName(), faultInfo, builder); |
aoqi@0 | 472 | return false; |
aoqi@0 | 473 | } |
aoqi@0 | 474 | if (seiContext.getExceptionBeanName(thrownDecl.getQualifiedName()) != null) |
aoqi@0 | 475 | return false; |
aoqi@0 | 476 | |
aoqi@0 | 477 | //write class comment - JAXWS warning |
aoqi@0 | 478 | JDocComment comment = cls.javadoc(); |
aoqi@0 | 479 | for (String doc : GeneratorBase.getJAXWSClassComment(ToolVersion.VERSION.MAJOR_VERSION)) { |
aoqi@0 | 480 | comment.add(doc); |
aoqi@0 | 481 | } |
aoqi@0 | 482 | |
aoqi@0 | 483 | // XmlElement Declarations |
aoqi@0 | 484 | writeXmlElementDeclaration(cls, name, namespace); |
aoqi@0 | 485 | |
aoqi@0 | 486 | // XmlType Declaration |
aoqi@0 | 487 | //members = sortMembers(members); |
aoqi@0 | 488 | XmlType xmlType = thrownDecl.getAnnotation(XmlType.class); |
aoqi@0 | 489 | String xmlTypeName = (xmlType != null && !xmlType.name().equals("##default")) ? xmlType.name() : exceptionName; |
aoqi@0 | 490 | String xmlTypeNamespace = (xmlType != null && !xmlType.namespace().equals("##default")) ? xmlType.namespace() : typeNamespace; |
aoqi@0 | 491 | writeXmlTypeDeclaration(cls, xmlTypeName, xmlTypeNamespace, members); |
aoqi@0 | 492 | |
aoqi@0 | 493 | writeMembers(cls, members); |
aoqi@0 | 494 | |
aoqi@0 | 495 | seiContext.addExceptionBeanEntry(thrownDecl.getQualifiedName(), faultInfo, builder); |
aoqi@0 | 496 | return true; |
aoqi@0 | 497 | } |
aoqi@0 | 498 | |
aoqi@0 | 499 | protected boolean isWSDLException(Collection<MemberInfo> members, TypeElement thrownDecl) { |
aoqi@0 | 500 | WebFault webFault = thrownDecl.getAnnotation(WebFault.class); |
aoqi@0 | 501 | return webFault != null && members.size() == 2 && getFaultInfoMember(members) != null; |
aoqi@0 | 502 | } |
aoqi@0 | 503 | |
aoqi@0 | 504 | /* |
aoqi@0 | 505 | * Returns the corresponding MemberInfo for getFaultInfo() |
aoqi@0 | 506 | * method of an exception. Returns null, if that method is not there. |
aoqi@0 | 507 | */ |
aoqi@0 | 508 | private MemberInfo getFaultInfoMember(Collection<MemberInfo> members) { |
aoqi@0 | 509 | for(MemberInfo member : members) { |
aoqi@0 | 510 | if (member.getParamName().equals(FAULT_INFO.getValue())) { |
aoqi@0 | 511 | return member; |
aoqi@0 | 512 | } |
aoqi@0 | 513 | } |
aoqi@0 | 514 | return null; |
aoqi@0 | 515 | } |
aoqi@0 | 516 | |
aoqi@0 | 517 | private void writeXmlElementDeclaration(JDefinedClass cls, String elementName, String namespaceUri) { |
aoqi@0 | 518 | |
aoqi@0 | 519 | if (cls == null) |
aoqi@0 | 520 | return; |
aoqi@0 | 521 | JAnnotationUse xmlRootElementAnn = cls.annotate(XmlRootElement.class); |
aoqi@0 | 522 | xmlRootElementAnn.param("name", elementName); |
aoqi@0 | 523 | if (namespaceUri.length() > 0) { |
aoqi@0 | 524 | xmlRootElementAnn.param("namespace", namespaceUri); |
aoqi@0 | 525 | } |
aoqi@0 | 526 | JAnnotationUse xmlAccessorTypeAnn = cls.annotate(cm.ref(XmlAccessorType.class)); |
aoqi@0 | 527 | xmlAccessorTypeAnn.param("value", XmlAccessType.FIELD); |
aoqi@0 | 528 | } |
aoqi@0 | 529 | |
aoqi@0 | 530 | private void writeXmlTypeDeclaration(JDefinedClass cls, String typeName, String namespaceUri, |
aoqi@0 | 531 | Collection<MemberInfo> members) { |
aoqi@0 | 532 | if (cls == null) |
aoqi@0 | 533 | return; |
aoqi@0 | 534 | JAnnotationUse xmlTypeAnn = cls.annotate(cm.ref(XmlType.class)); |
aoqi@0 | 535 | xmlTypeAnn.param("name", typeName); |
aoqi@0 | 536 | xmlTypeAnn.param("namespace", namespaceUri); |
aoqi@0 | 537 | if (members.size() > 1) { |
aoqi@0 | 538 | JAnnotationArrayMember paramArray = xmlTypeAnn.paramArray("propOrder"); |
aoqi@0 | 539 | for (MemberInfo memInfo : members) { |
aoqi@0 | 540 | paramArray.param(memInfo.getParamName()); |
aoqi@0 | 541 | } |
aoqi@0 | 542 | } |
aoqi@0 | 543 | } |
aoqi@0 | 544 | |
aoqi@0 | 545 | private void writeMember(JDefinedClass cls, TypeMirror paramType, |
aoqi@0 | 546 | String paramName) { |
aoqi@0 | 547 | |
aoqi@0 | 548 | if (cls == null) |
aoqi@0 | 549 | return; |
aoqi@0 | 550 | |
aoqi@0 | 551 | String accessorName =BindingHelper.mangleNameToPropertyName(paramName); |
aoqi@0 | 552 | String getterPrefix = paramType.toString().equals("boolean")? "is" : "get"; |
aoqi@0 | 553 | JType propType = getType(paramType); |
aoqi@0 | 554 | JMethod m = cls.method(JMod.PUBLIC, propType, getterPrefix+ accessorName); |
aoqi@0 | 555 | JDocComment methodDoc = m.javadoc(); |
aoqi@0 | 556 | JCommentPart ret = methodDoc.addReturn(); |
aoqi@0 | 557 | ret.add("returns "+propType.name()); |
aoqi@0 | 558 | JBlock body = m.body(); |
aoqi@0 | 559 | body._return( JExpr._this().ref(paramName) ); |
aoqi@0 | 560 | |
aoqi@0 | 561 | m = cls.method(JMod.PUBLIC, cm.VOID, "set"+accessorName); |
aoqi@0 | 562 | JVar param = m.param(propType, paramName); |
aoqi@0 | 563 | methodDoc = m.javadoc(); |
aoqi@0 | 564 | JCommentPart part = methodDoc.addParam(paramName); |
aoqi@0 | 565 | part.add("the value for the "+ paramName+" property"); |
aoqi@0 | 566 | body = m.body(); |
aoqi@0 | 567 | body.assign( JExpr._this().ref(paramName), param ); |
aoqi@0 | 568 | } |
aoqi@0 | 569 | } |