src/share/jaxws_classes/com/sun/tools/internal/ws/processor/modeler/annotation/WebServiceWrapperGenerator.java

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

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

mercurial