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

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2013, 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
alanb@368 127 @Override
ohair@286 128 protected TypeMirror getSafeType(TypeMirror type) {
ohair@286 129 return WebServiceWrapperGenerator.this.getSafeType(type);
ohair@286 130 }
ohair@286 131
alanb@368 132 @Override
ohair@286 133 protected TypeMirror getHolderValueType(TypeMirror paramType) {
ohair@286 134 return builder.getHolderValueType(paramType);
ohair@286 135 }
ohair@286 136
alanb@368 137 @Override
ohair@286 138 protected boolean isVoidType(TypeMirror type) {
ohair@286 139 return type != null && type.getKind().equals(TypeKind.VOID);
ohair@286 140 }
ohair@286 141
ohair@286 142 }
ohair@286 143
ohair@286 144 private static final class FieldFactory implements AbstractWrapperBeanGenerator.BeanMemberFactory<TypeMirror, MemberInfo> {
ohair@286 145
alanb@368 146 @Override
ohair@286 147 public MemberInfo createWrapperBeanMember(TypeMirror paramType,
ohair@286 148 String paramName, List<Annotation> jaxb) {
ohair@286 149 return new MemberInfo(paramType, paramName, jaxb);
ohair@286 150 }
ohair@286 151 }
ohair@286 152
ohair@286 153 public WebServiceWrapperGenerator(ModelBuilder builder, AnnotationProcessorContext context) {
ohair@286 154 super(builder, context);
ohair@286 155 makeSafeVisitor = new MakeSafeTypeVisitor(builder.getProcessingEnvironment());
ohair@286 156 }
ohair@286 157
alanb@368 158 @Override
ohair@286 159 protected void processWebService(WebService webService, TypeElement d) {
ohair@286 160 cm = new JCodeModel();
ohair@286 161 wrapperNames = new HashSet<String>();
ohair@286 162 processedExceptions = new HashSet<String>();
ohair@286 163 }
ohair@286 164
alanb@368 165 @Override
ohair@286 166 protected void postProcessWebService(WebService webService, TypeElement d) {
ohair@286 167 super.postProcessWebService(webService, d);
ohair@286 168 doPostProcessWebService(webService, d);
ohair@286 169 }
ohair@286 170
alanb@368 171 @SuppressWarnings("CallToThreadDumpStack")
ohair@286 172 protected void doPostProcessWebService(WebService webService, TypeElement d) {
ohair@286 173 if (cm != null) {
ohair@286 174 File sourceDir = builder.getSourceDir();
ohair@286 175 assert(sourceDir != null);
ohair@286 176 WsgenOptions options = builder.getOptions();
ohair@286 177 try {
ohair@286 178 CodeWriter cw = new FilerCodeWriter(sourceDir, options);
ohair@286 179 if(options.verbose)
ohair@286 180 cw = new ProgressCodeWriter(cw, System.out);
ohair@286 181 cm.build(cw);
ohair@286 182 } catch (IOException e) {
ohair@286 183 e.printStackTrace();
ohair@286 184 }
ohair@286 185 }
ohair@286 186 }
ohair@286 187
alanb@368 188 @Override
ohair@286 189 protected void processMethod(ExecutableElement method, WebMethod webMethod) {
ohair@286 190 builder.log("WrapperGen - method: "+method);
ohair@286 191 builder.log("method.getDeclaringType(): " + method.asType());
ohair@286 192 if (wrapped && soapStyle.equals(SOAPStyle.DOCUMENT)) {
ohair@286 193 generateWrappers(method, webMethod);
ohair@286 194 }
ohair@286 195 generateExceptionBeans(method);
ohair@286 196 }
ohair@286 197
ohair@286 198 private boolean generateExceptionBeans(ExecutableElement method) {
ohair@286 199 String beanPackage = packageName + PD_JAXWS_PACKAGE_PD.getValue();
ohair@286 200 if (packageName.length() == 0)
ohair@286 201 beanPackage = JAXWS_PACKAGE_PD.getValue();
ohair@286 202 boolean beanGenerated = false;
ohair@286 203 for (TypeMirror thrownType : method.getThrownTypes()) {
ohair@286 204 TypeElement typeDecl = (TypeElement) ((DeclaredType) thrownType).asElement();
ohair@286 205 if (typeDecl == null) {
ohair@286 206 builder.processError(WebserviceapMessages.WEBSERVICEAP_COULD_NOT_FIND_TYPEDECL(
ohair@286 207 thrownType.toString(), context.getRound()));
ohair@286 208 return false;
ohair@286 209 }
ohair@286 210 boolean tmp = generateExceptionBean(typeDecl, beanPackage);
ohair@286 211 beanGenerated = beanGenerated || tmp;
ohair@286 212 }
ohair@286 213 return beanGenerated;
ohair@286 214 }
ohair@286 215
ohair@286 216 private boolean duplicateName(String name) {
ohair@286 217 for (String str : wrapperNames) {
ohair@286 218 if (str.equalsIgnoreCase(name))
ohair@286 219 return true;
ohair@286 220 }
ohair@286 221 wrapperNames.add(name);
ohair@286 222 return false;
ohair@286 223 }
ohair@286 224
ohair@286 225 private boolean generateWrappers(ExecutableElement method, WebMethod webMethod) {
ohair@286 226 boolean isOneway = method.getAnnotation(Oneway.class) != null;
ohair@286 227 String beanPackage = packageName + PD_JAXWS_PACKAGE_PD.getValue();
ohair@286 228 if (packageName.length() == 0)
ohair@286 229 beanPackage = JAXWS_PACKAGE_PD.getValue();
ohair@286 230 Name methodName = method.getSimpleName();
ohair@286 231 String operationName = builder.getOperationName(methodName);
ohair@286 232 operationName = webMethod != null && webMethod.operationName().length() > 0 ?
ohair@286 233 webMethod.operationName() : operationName;
ohair@286 234 String reqName = operationName;
ohair@286 235 String resName = operationName + WebServiceConstants.RESPONSE.getValue();
ohair@286 236 String reqNamespace = typeNamespace;
ohair@286 237 String resNamespace = typeNamespace;
ohair@286 238
ohair@286 239 String requestClassName = beanPackage + StringUtils.capitalize(method.getSimpleName().toString());
ohair@286 240 RequestWrapper reqWrapper = method.getAnnotation(RequestWrapper.class);
ohair@286 241 if (reqWrapper != null) {
ohair@286 242 if (reqWrapper.className().length() > 0)
ohair@286 243 requestClassName = reqWrapper.className();
ohair@286 244 if (reqWrapper.localName().length() > 0)
ohair@286 245 reqName = reqWrapper.localName();
ohair@286 246 if (reqWrapper.targetNamespace().length() > 0)
ohair@286 247 reqNamespace = reqWrapper.targetNamespace();
ohair@286 248 }
ohair@286 249 builder.log("requestWrapper: "+requestClassName);
ohair@286 250 ///// fix for wsgen CR 6442344
ohair@286 251 File file = new File(DirectoryUtil.getOutputDirectoryFor(requestClassName, builder.getSourceDir()),
ohair@286 252 Names.stripQualifier(requestClassName) + GeneratorConstants.JAVA_SRC_SUFFIX.getValue());
ohair@286 253 builder.getOptions().addGeneratedFile(file);
ohair@286 254 //////////
ohair@286 255 boolean canOverwriteRequest = builder.canOverWriteClass(requestClassName);
ohair@286 256 if (!canOverwriteRequest) {
ohair@286 257 builder.log("Class " + requestClassName + " exists. Not overwriting.");
ohair@286 258 }
ohair@286 259 if (duplicateName(requestClassName) && canOverwriteRequest) {
ohair@286 260 builder.processError(WebserviceapMessages.WEBSERVICEAP_METHOD_REQUEST_WRAPPER_BEAN_NAME_NOT_UNIQUE(
ohair@286 261 typeElement.getQualifiedName(), method.toString()));
ohair@286 262 }
ohair@286 263
ohair@286 264 String responseClassName = null;
ohair@286 265 boolean canOverwriteResponse = canOverwriteRequest;
ohair@286 266 if (!isOneway) {
ohair@286 267 responseClassName = beanPackage+StringUtils.capitalize(method.getSimpleName().toString())+RESPONSE.getValue();
ohair@286 268 ResponseWrapper resWrapper = method.getAnnotation(ResponseWrapper.class);
ohair@286 269 if(resWrapper != null) {
ohair@286 270 if (resWrapper.className().length() > 0)
ohair@286 271 responseClassName = resWrapper.className();
ohair@286 272 if (resWrapper.localName().length() > 0)
ohair@286 273 resName = resWrapper.localName();
ohair@286 274 if (resWrapper.targetNamespace().length() > 0)
ohair@286 275 resNamespace = resWrapper.targetNamespace();
ohair@286 276 }
ohair@286 277 canOverwriteResponse = builder.canOverWriteClass(responseClassName);
ohair@286 278 if (!canOverwriteResponse) {
ohair@286 279 builder.log("Class " + responseClassName + " exists. Not overwriting.");
ohair@286 280 }
ohair@286 281 if (duplicateName(responseClassName) && canOverwriteResponse) {
ohair@286 282 builder.processError(WebserviceapMessages.WEBSERVICEAP_METHOD_RESPONSE_WRAPPER_BEAN_NAME_NOT_UNIQUE(
ohair@286 283 typeElement.getQualifiedName(), method.toString()));
ohair@286 284 }
ohair@286 285 file = new File(DirectoryUtil.getOutputDirectoryFor(responseClassName, builder.getSourceDir()),
ohair@286 286 Names.stripQualifier(responseClassName) + GeneratorConstants.JAVA_SRC_SUFFIX.getValue());
ohair@286 287 builder.getOptions().addGeneratedFile(file);
ohair@286 288 }
ohair@286 289 //ArrayList<MemberInfo> reqMembers = new ArrayList<MemberInfo>();
ohair@286 290 //ArrayList<MemberInfo> resMembers = new ArrayList<MemberInfo>();
ohair@286 291 WrapperInfo reqWrapperInfo = new WrapperInfo(requestClassName);
ohair@286 292 //reqWrapperInfo.setMembers(reqMembers);
ohair@286 293 WrapperInfo resWrapperInfo = null;
ohair@286 294 if (!isOneway) {
ohair@286 295 resWrapperInfo = new WrapperInfo(responseClassName);
ohair@286 296 //resWrapperInfo.setMembers(resMembers);
ohair@286 297 }
ohair@286 298 seiContext.setReqWrapperOperation(method, reqWrapperInfo);
ohair@286 299 if (!isOneway)
ohair@286 300 seiContext.setResWrapperOperation(method, resWrapperInfo);
ohair@286 301 try {
ohair@286 302 if (!canOverwriteRequest && !canOverwriteResponse) {
ohair@286 303 return false;
ohair@286 304 }
ohair@286 305
ohair@286 306 JDefinedClass reqCls = null;
ohair@286 307 if (canOverwriteRequest) {
ohair@286 308 reqCls = getCMClass(requestClassName, CLASS);
ohair@286 309 }
ohair@286 310
ohair@286 311 JDefinedClass resCls = null;
ohair@286 312 if (!isOneway && canOverwriteResponse) {
ohair@286 313 resCls = getCMClass(responseClassName, CLASS);
ohair@286 314 }
ohair@286 315
ohair@286 316 // XMLElement Declarations
ohair@286 317 writeXmlElementDeclaration(reqCls, reqName,reqNamespace);
ohair@286 318 writeXmlElementDeclaration(resCls, resName, resNamespace);
ohair@286 319
ohair@286 320 List<MemberInfo> reqMembers = ap_generator.collectRequestBeanMembers(method);
ohair@286 321 List<MemberInfo> resMembers = ap_generator.collectResponseBeanMembers(method);
ohair@286 322
ohair@286 323 // XmlType
ohair@286 324 writeXmlTypeDeclaration(reqCls, reqName, reqNamespace, reqMembers);
ohair@286 325 writeXmlTypeDeclaration(resCls, resName, resNamespace, resMembers);
ohair@286 326
ohair@286 327 // class members
ohair@286 328 writeMembers(reqCls, reqMembers);
ohair@286 329 writeMembers(resCls, resMembers);
ohair@286 330
ohair@286 331 } catch (Exception e) {
ohair@286 332 throw new ModelerException("modeler.nestedGeneratorError",e);
ohair@286 333 }
ohair@286 334 return true;
ohair@286 335 }
ohair@286 336
ohair@286 337 // private List<Annotation> collectJAXBAnnotations(Declaration decl) {
ohair@286 338 // List<Annotation> jaxbAnnotation = new ArrayList<Annotation>();
ohair@286 339 // for(Class jaxbClass : jaxbAnns) {
ohair@286 340 // Annotation ann = decl.getAnnotation(jaxbClass);
ohair@286 341 // if (ann != null) {
ohair@286 342 // jaxbAnnotation.add(ann);
ohair@286 343 // }
ohair@286 344 // }
ohair@286 345 // return jaxbAnnotation;
ohair@286 346 // }
ohair@286 347
ohair@286 348 private TypeMirror getSafeType(TypeMirror type) {
ohair@286 349 return makeSafeVisitor.visit(type, builder.getProcessingEnvironment().getTypeUtils());
ohair@286 350 }
ohair@286 351
ohair@286 352 private JType getType(TypeMirror typeMirror) {
ohair@286 353 String type = typeMirror.toString();
ohair@286 354 try {
ohair@286 355 // System.out.println("typeName: "+typeName);
ohair@286 356 return cm.parseType(type);
ohair@286 357 // System.out.println("type: "+type);
ohair@286 358 } catch (ClassNotFoundException e) {
ohair@286 359 return cm.ref(type);
ohair@286 360 }
ohair@286 361 }
ohair@286 362
ohair@286 363 private void writeMembers(JDefinedClass cls, Collection<MemberInfo> members) {
ohair@286 364 if (cls == null)
ohair@286 365 return;
ohair@286 366 for (MemberInfo memInfo : members) {
ohair@286 367 JType type = getType(memInfo.getParamType());
ohair@286 368 JFieldVar field = cls.field(JMod.PRIVATE, type, memInfo.getParamName());
ohair@286 369 annotateParameterWithJaxbAnnotations(memInfo, field);
ohair@286 370 }
ohair@286 371 for (MemberInfo memInfo : members) {
ohair@286 372 writeMember(cls, memInfo.getParamType(),
ohair@286 373 memInfo.getParamName());
ohair@286 374 }
ohair@286 375 }
ohair@286 376
ohair@286 377 private void annotateParameterWithJaxbAnnotations(MemberInfo memInfo, JFieldVar field) {
ohair@286 378 List<Annotation> jaxbAnnotations = memInfo.getJaxbAnnotations();
ohair@286 379 for(Annotation ann : jaxbAnnotations) {
ohair@286 380 if (ann instanceof XmlMimeType) {
ohair@286 381 JAnnotationUse jaxbAnn = field.annotate(XmlMimeType.class);
ohair@286 382 jaxbAnn.param("value", ((XmlMimeType)ann).value());
ohair@286 383 } else if (ann instanceof XmlJavaTypeAdapter) {
ohair@286 384 JAnnotationUse jaxbAnn = field.annotate(XmlJavaTypeAdapter.class);
ohair@286 385 XmlJavaTypeAdapter ja = (XmlJavaTypeAdapter) ann;
ohair@286 386 try {
ohair@286 387 ja.value();
ohair@286 388 throw new AssertionError();
ohair@286 389 } catch (MirroredTypeException e) {
ohair@286 390 jaxbAnn.param("value",getType(e.getTypeMirror()));
ohair@286 391 }
ohair@286 392 // XmlJavaTypeAdapter.type() is for package only. No need to copy.
ohair@286 393 } else if (ann instanceof XmlAttachmentRef) {
ohair@286 394 field.annotate(XmlAttachmentRef.class);
ohair@286 395 } else if (ann instanceof XmlList){
ohair@286 396 field.annotate(XmlList.class);
ohair@286 397 } else if (ann instanceof XmlElement) {
ohair@286 398 XmlElement elemAnn = (XmlElement)ann;
ohair@286 399 JAnnotationUse jAnn = field.annotate(XmlElement.class);
ohair@286 400 jAnn.param("name", elemAnn.name());
ohair@286 401 jAnn.param("namespace", elemAnn.namespace());
ohair@286 402 if (elemAnn.nillable()) {
ohair@286 403 jAnn.param("nillable", true);
ohair@286 404 }
ohair@286 405 if (elemAnn.required()) {
ohair@286 406 jAnn.param("required", true);
ohair@286 407 }
ohair@286 408 } else {
ohair@286 409 throw new WebServiceException("SEI Parameter cannot have this JAXB annotation: " + ann);
ohair@286 410 }
ohair@286 411 }
ohair@286 412 }
ohair@286 413
ohair@286 414 protected JDefinedClass getCMClass(String className, com.sun.codemodel.internal.ClassType type) {
ohair@286 415 JDefinedClass cls;
ohair@286 416 try {
ohair@286 417 cls = cm._class(className, type);
ohair@286 418 } catch (com.sun.codemodel.internal.JClassAlreadyExistsException e){
ohair@286 419 cls = cm._getClass(className);
ohair@286 420 }
ohair@286 421 return cls;
ohair@286 422 }
ohair@286 423
ohair@286 424 private boolean generateExceptionBean(TypeElement thrownDecl, String beanPackage) {
ohair@286 425 if (!builder.isServiceException(thrownDecl.asType()))
ohair@286 426 return false;
ohair@286 427
ohair@286 428 String exceptionName = ClassNameInfo.getName(thrownDecl.getQualifiedName().toString());
ohair@286 429 if (processedExceptions.contains(exceptionName))
ohair@286 430 return false;
ohair@286 431 processedExceptions.add(exceptionName);
ohair@286 432 WebFault webFault = thrownDecl.getAnnotation(WebFault.class);
ohair@286 433 String className = beanPackage+ exceptionName + BEAN.getValue();
ohair@286 434
ohair@286 435 Collection<MemberInfo> members = ap_generator.collectExceptionBeanMembers(thrownDecl);
ohair@286 436 boolean isWSDLException = isWSDLException(members, thrownDecl);
ohair@286 437 String namespace = typeNamespace;
ohair@286 438 String name = exceptionName;
ohair@286 439 FaultInfo faultInfo;
ohair@286 440 if (isWSDLException) {
ohair@286 441 TypeMirror beanType = getFaultInfoMember(members).getParamType();
ohair@286 442 faultInfo = new FaultInfo(TypeMonikerFactory.getTypeMoniker(beanType), true);
ohair@286 443 namespace = webFault.targetNamespace().length()>0 ?
ohair@286 444 webFault.targetNamespace() : namespace;
ohair@286 445 name = webFault.name().length()>0 ?
ohair@286 446 webFault.name() : name;
ohair@286 447 faultInfo.setElementName(new QName(namespace, name));
ohair@286 448 seiContext.addExceptionBeanEntry(thrownDecl.getQualifiedName(), faultInfo, builder);
ohair@286 449 return false;
ohair@286 450 }
ohair@286 451 if (webFault != null) {
ohair@286 452 namespace = webFault.targetNamespace().length()>0 ?
ohair@286 453 webFault.targetNamespace() : namespace;
ohair@286 454 name = webFault.name().length()>0 ?
ohair@286 455 webFault.name() : name;
ohair@286 456 className = webFault.faultBean().length()>0 ?
ohair@286 457 webFault.faultBean() : className;
ohair@286 458
ohair@286 459 }
ohair@286 460 JDefinedClass cls = getCMClass(className, CLASS);
ohair@286 461 faultInfo = new FaultInfo(className, false);
ohair@286 462
ohair@286 463 if (duplicateName(className)) {
ohair@286 464 builder.processError(WebserviceapMessages.WEBSERVICEAP_METHOD_EXCEPTION_BEAN_NAME_NOT_UNIQUE(
ohair@286 465 typeElement.getQualifiedName(), thrownDecl.getQualifiedName()));
ohair@286 466 }
ohair@286 467
ohair@286 468 boolean canOverWriteBean = builder.canOverWriteClass(className);
ohair@286 469 if (!canOverWriteBean) {
ohair@286 470 builder.log("Class " + className + " exists. Not overwriting.");
ohair@286 471 seiContext.addExceptionBeanEntry(thrownDecl.getQualifiedName(), faultInfo, builder);
ohair@286 472 return false;
ohair@286 473 }
ohair@286 474 if (seiContext.getExceptionBeanName(thrownDecl.getQualifiedName()) != null)
ohair@286 475 return false;
ohair@286 476
ohair@286 477 //write class comment - JAXWS warning
ohair@286 478 JDocComment comment = cls.javadoc();
ohair@286 479 for (String doc : GeneratorBase.getJAXWSClassComment(ToolVersion.VERSION.MAJOR_VERSION)) {
ohair@286 480 comment.add(doc);
ohair@286 481 }
ohair@286 482
ohair@286 483 // XmlElement Declarations
ohair@286 484 writeXmlElementDeclaration(cls, name, namespace);
ohair@286 485
ohair@286 486 // XmlType Declaration
ohair@286 487 //members = sortMembers(members);
ohair@286 488 XmlType xmlType = thrownDecl.getAnnotation(XmlType.class);
ohair@286 489 String xmlTypeName = (xmlType != null && !xmlType.name().equals("##default")) ? xmlType.name() : exceptionName;
ohair@286 490 String xmlTypeNamespace = (xmlType != null && !xmlType.namespace().equals("##default")) ? xmlType.namespace() : typeNamespace;
ohair@286 491 writeXmlTypeDeclaration(cls, xmlTypeName, xmlTypeNamespace, members);
ohair@286 492
ohair@286 493 writeMembers(cls, members);
ohair@286 494
ohair@286 495 seiContext.addExceptionBeanEntry(thrownDecl.getQualifiedName(), faultInfo, builder);
ohair@286 496 return true;
ohair@286 497 }
ohair@286 498
ohair@286 499 protected boolean isWSDLException(Collection<MemberInfo> members, TypeElement thrownDecl) {
ohair@286 500 WebFault webFault = thrownDecl.getAnnotation(WebFault.class);
ohair@286 501 return webFault != null && members.size() == 2 && getFaultInfoMember(members) != null;
ohair@286 502 }
ohair@286 503
ohair@286 504 /*
ohair@286 505 * Returns the corresponding MemberInfo for getFaultInfo()
ohair@286 506 * method of an exception. Returns null, if that method is not there.
ohair@286 507 */
ohair@286 508 private MemberInfo getFaultInfoMember(Collection<MemberInfo> members) {
ohair@286 509 for(MemberInfo member : members) {
ohair@286 510 if (member.getParamName().equals(FAULT_INFO.getValue())) {
ohair@286 511 return member;
ohair@286 512 }
ohair@286 513 }
ohair@286 514 return null;
ohair@286 515 }
ohair@286 516
ohair@286 517 private void writeXmlElementDeclaration(JDefinedClass cls, String elementName, String namespaceUri) {
ohair@286 518
ohair@286 519 if (cls == null)
ohair@286 520 return;
ohair@286 521 JAnnotationUse xmlRootElementAnn = cls.annotate(XmlRootElement.class);
ohair@286 522 xmlRootElementAnn.param("name", elementName);
ohair@286 523 if (namespaceUri.length() > 0) {
ohair@286 524 xmlRootElementAnn.param("namespace", namespaceUri);
ohair@286 525 }
ohair@286 526 JAnnotationUse xmlAccessorTypeAnn = cls.annotate(cm.ref(XmlAccessorType.class));
ohair@286 527 xmlAccessorTypeAnn.param("value", XmlAccessType.FIELD);
ohair@286 528 }
ohair@286 529
ohair@286 530 private void writeXmlTypeDeclaration(JDefinedClass cls, String typeName, String namespaceUri,
ohair@286 531 Collection<MemberInfo> members) {
ohair@286 532 if (cls == null)
ohair@286 533 return;
ohair@286 534 JAnnotationUse xmlTypeAnn = cls.annotate(cm.ref(XmlType.class));
ohair@286 535 xmlTypeAnn.param("name", typeName);
ohair@286 536 xmlTypeAnn.param("namespace", namespaceUri);
ohair@286 537 if (members.size() > 1) {
ohair@286 538 JAnnotationArrayMember paramArray = xmlTypeAnn.paramArray("propOrder");
ohair@286 539 for (MemberInfo memInfo : members) {
ohair@286 540 paramArray.param(memInfo.getParamName());
ohair@286 541 }
ohair@286 542 }
ohair@286 543 }
ohair@286 544
ohair@286 545 private void writeMember(JDefinedClass cls, TypeMirror paramType,
ohair@286 546 String paramName) {
ohair@286 547
ohair@286 548 if (cls == null)
ohair@286 549 return;
ohair@286 550
ohair@286 551 String accessorName =BindingHelper.mangleNameToPropertyName(paramName);
ohair@286 552 String getterPrefix = paramType.toString().equals("boolean")? "is" : "get";
ohair@286 553 JType propType = getType(paramType);
ohair@286 554 JMethod m = cls.method(JMod.PUBLIC, propType, getterPrefix+ accessorName);
ohair@286 555 JDocComment methodDoc = m.javadoc();
ohair@286 556 JCommentPart ret = methodDoc.addReturn();
ohair@286 557 ret.add("returns "+propType.name());
ohair@286 558 JBlock body = m.body();
ohair@286 559 body._return( JExpr._this().ref(paramName) );
ohair@286 560
ohair@286 561 m = cls.method(JMod.PUBLIC, cm.VOID, "set"+accessorName);
ohair@286 562 JVar param = m.param(propType, paramName);
ohair@286 563 methodDoc = m.javadoc();
ohair@286 564 JCommentPart part = methodDoc.addParam(paramName);
ohair@286 565 part.add("the value for the "+ paramName+" property");
ohair@286 566 body = m.body();
ohair@286 567 body.assign( JExpr._this().ref(paramName), param );
ohair@286 568 }
ohair@286 569 }

mercurial