1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/ws/processor/modeler/annotation/WebServiceWrapperGenerator.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,561 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.internal.ws.processor.modeler.annotation; 1.30 + 1.31 +import com.sun.codemodel.internal.CodeWriter; 1.32 +import com.sun.codemodel.internal.JAnnotationArrayMember; 1.33 +import com.sun.codemodel.internal.JAnnotationUse; 1.34 +import com.sun.codemodel.internal.JBlock; 1.35 +import com.sun.codemodel.internal.JCodeModel; 1.36 +import com.sun.codemodel.internal.JCommentPart; 1.37 +import com.sun.codemodel.internal.JDefinedClass; 1.38 +import com.sun.codemodel.internal.JDocComment; 1.39 +import com.sun.codemodel.internal.JExpr; 1.40 +import com.sun.codemodel.internal.JFieldVar; 1.41 +import com.sun.codemodel.internal.JMethod; 1.42 +import com.sun.codemodel.internal.JMod; 1.43 +import com.sun.codemodel.internal.JType; 1.44 +import com.sun.codemodel.internal.JVar; 1.45 +import com.sun.codemodel.internal.writer.ProgressCodeWriter; 1.46 +import com.sun.tools.internal.jxc.ap.InlineAnnotationReaderImpl; 1.47 +import com.sun.tools.internal.jxc.model.nav.ApNavigator; 1.48 +import com.sun.tools.internal.ws.ToolVersion; 1.49 +import com.sun.tools.internal.ws.processor.generator.GeneratorBase; 1.50 +import com.sun.tools.internal.ws.processor.generator.GeneratorConstants; 1.51 +import com.sun.tools.internal.ws.processor.generator.Names; 1.52 +import com.sun.tools.internal.ws.processor.modeler.ModelerException; 1.53 +import com.sun.tools.internal.ws.processor.util.DirectoryUtil; 1.54 +import com.sun.tools.internal.ws.resources.WebserviceapMessages; 1.55 +import com.sun.tools.internal.ws.util.ClassNameInfo; 1.56 +import com.sun.tools.internal.ws.wscompile.FilerCodeWriter; 1.57 +import com.sun.tools.internal.ws.wscompile.WsgenOptions; 1.58 +import com.sun.tools.internal.ws.wsdl.document.soap.SOAPStyle; 1.59 +import com.sun.xml.internal.bind.v2.model.annotation.AnnotationReader; 1.60 +import com.sun.xml.internal.bind.v2.model.nav.Navigator; 1.61 +import com.sun.xml.internal.ws.model.AbstractWrapperBeanGenerator; 1.62 +import com.sun.xml.internal.ws.spi.db.BindingHelper; 1.63 +import com.sun.xml.internal.ws.util.StringUtils; 1.64 + 1.65 +import javax.jws.Oneway; 1.66 +import javax.jws.WebMethod; 1.67 +import javax.jws.WebService; 1.68 +import javax.lang.model.element.ExecutableElement; 1.69 +import javax.lang.model.element.Name; 1.70 +import javax.lang.model.element.TypeElement; 1.71 +import javax.lang.model.type.DeclaredType; 1.72 +import javax.lang.model.type.MirroredTypeException; 1.73 +import javax.lang.model.type.TypeKind; 1.74 +import javax.lang.model.type.TypeMirror; 1.75 +import javax.xml.bind.annotation.XmlAccessType; 1.76 +import javax.xml.bind.annotation.XmlAccessorType; 1.77 +import javax.xml.bind.annotation.XmlAttachmentRef; 1.78 +import javax.xml.bind.annotation.XmlElement; 1.79 +import javax.xml.bind.annotation.XmlList; 1.80 +import javax.xml.bind.annotation.XmlMimeType; 1.81 +import javax.xml.bind.annotation.XmlRootElement; 1.82 +import javax.xml.bind.annotation.XmlType; 1.83 +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 1.84 +import javax.xml.namespace.QName; 1.85 +import javax.xml.ws.RequestWrapper; 1.86 +import javax.xml.ws.ResponseWrapper; 1.87 +import javax.xml.ws.WebFault; 1.88 +import javax.xml.ws.WebServiceException; 1.89 +import java.io.File; 1.90 +import java.io.IOException; 1.91 +import java.lang.annotation.Annotation; 1.92 +import java.util.Collection; 1.93 +import java.util.HashSet; 1.94 +import java.util.List; 1.95 +import java.util.Set; 1.96 + 1.97 +import static com.sun.codemodel.internal.ClassType.CLASS; 1.98 +import static com.sun.tools.internal.ws.processor.modeler.annotation.WebServiceConstants.BEAN; 1.99 +import static com.sun.tools.internal.ws.processor.modeler.annotation.WebServiceConstants.FAULT_INFO; 1.100 +import static com.sun.tools.internal.ws.processor.modeler.annotation.WebServiceConstants.JAXWS_PACKAGE_PD; 1.101 +import static com.sun.tools.internal.ws.processor.modeler.annotation.WebServiceConstants.PD_JAXWS_PACKAGE_PD; 1.102 +import static com.sun.tools.internal.ws.processor.modeler.annotation.WebServiceConstants.RESPONSE; 1.103 + 1.104 +/** 1.105 + * This class generates the request/response and Exception Beans 1.106 + * used by the JAX-WS runtime. 1.107 + * 1.108 + * @author WS Development Team 1.109 + */ 1.110 +public class WebServiceWrapperGenerator extends WebServiceVisitor { 1.111 + private Set<String> wrapperNames; 1.112 + private Set<String> processedExceptions; 1.113 + private JCodeModel cm; 1.114 + private final MakeSafeTypeVisitor makeSafeVisitor; 1.115 + 1.116 + private static final FieldFactory FIELD_FACTORY = new FieldFactory(); 1.117 + 1.118 + private final AbstractWrapperBeanGenerator ap_generator = 1.119 + new ApWrapperBeanGenerator(InlineAnnotationReaderImpl.theInstance, 1.120 + new ApNavigator(builder.getProcessingEnvironment()), FIELD_FACTORY); 1.121 + 1.122 + private final class ApWrapperBeanGenerator extends AbstractWrapperBeanGenerator<TypeMirror, TypeElement, ExecutableElement, MemberInfo> { 1.123 + 1.124 + protected ApWrapperBeanGenerator( 1.125 + AnnotationReader<TypeMirror, TypeElement, ?, ExecutableElement> annReader, 1.126 + Navigator<TypeMirror, TypeElement, ?, ExecutableElement> nav, BeanMemberFactory<TypeMirror, MemberInfo> beanMemberFactory) { 1.127 + super(annReader, nav, beanMemberFactory); 1.128 + } 1.129 + 1.130 + protected TypeMirror getSafeType(TypeMirror type) { 1.131 + return WebServiceWrapperGenerator.this.getSafeType(type); 1.132 + } 1.133 + 1.134 + protected TypeMirror getHolderValueType(TypeMirror paramType) { 1.135 + return builder.getHolderValueType(paramType); 1.136 + } 1.137 + 1.138 + protected boolean isVoidType(TypeMirror type) { 1.139 + return type != null && type.getKind().equals(TypeKind.VOID); 1.140 + } 1.141 + 1.142 + } 1.143 + 1.144 + private static final class FieldFactory implements AbstractWrapperBeanGenerator.BeanMemberFactory<TypeMirror, MemberInfo> { 1.145 + 1.146 + public MemberInfo createWrapperBeanMember(TypeMirror paramType, 1.147 + String paramName, List<Annotation> jaxb) { 1.148 + return new MemberInfo(paramType, paramName, jaxb); 1.149 + } 1.150 + } 1.151 + 1.152 + public WebServiceWrapperGenerator(ModelBuilder builder, AnnotationProcessorContext context) { 1.153 + super(builder, context); 1.154 + makeSafeVisitor = new MakeSafeTypeVisitor(builder.getProcessingEnvironment()); 1.155 + } 1.156 + 1.157 + protected void processWebService(WebService webService, TypeElement d) { 1.158 + cm = new JCodeModel(); 1.159 + wrapperNames = new HashSet<String>(); 1.160 + processedExceptions = new HashSet<String>(); 1.161 + } 1.162 + 1.163 + protected void postProcessWebService(WebService webService, TypeElement d) { 1.164 + super.postProcessWebService(webService, d); 1.165 + doPostProcessWebService(webService, d); 1.166 + } 1.167 + 1.168 + protected void doPostProcessWebService(WebService webService, TypeElement d) { 1.169 + if (cm != null) { 1.170 + File sourceDir = builder.getSourceDir(); 1.171 + assert(sourceDir != null); 1.172 + WsgenOptions options = builder.getOptions(); 1.173 + try { 1.174 + CodeWriter cw = new FilerCodeWriter(sourceDir, options); 1.175 + if(options.verbose) 1.176 + cw = new ProgressCodeWriter(cw, System.out); 1.177 + cm.build(cw); 1.178 + } catch (IOException e) { 1.179 + e.printStackTrace(); 1.180 + } 1.181 + } 1.182 + } 1.183 + 1.184 + protected void processMethod(ExecutableElement method, WebMethod webMethod) { 1.185 + builder.log("WrapperGen - method: "+method); 1.186 + builder.log("method.getDeclaringType(): " + method.asType()); 1.187 + if (wrapped && soapStyle.equals(SOAPStyle.DOCUMENT)) { 1.188 + generateWrappers(method, webMethod); 1.189 + } 1.190 + generateExceptionBeans(method); 1.191 + } 1.192 + 1.193 + private boolean generateExceptionBeans(ExecutableElement method) { 1.194 + String beanPackage = packageName + PD_JAXWS_PACKAGE_PD.getValue(); 1.195 + if (packageName.length() == 0) 1.196 + beanPackage = JAXWS_PACKAGE_PD.getValue(); 1.197 + boolean beanGenerated = false; 1.198 + for (TypeMirror thrownType : method.getThrownTypes()) { 1.199 + TypeElement typeDecl = (TypeElement) ((DeclaredType) thrownType).asElement(); 1.200 + if (typeDecl == null) { 1.201 + builder.processError(WebserviceapMessages.WEBSERVICEAP_COULD_NOT_FIND_TYPEDECL( 1.202 + thrownType.toString(), context.getRound())); 1.203 + return false; 1.204 + } 1.205 + boolean tmp = generateExceptionBean(typeDecl, beanPackage); 1.206 + beanGenerated = beanGenerated || tmp; 1.207 + } 1.208 + return beanGenerated; 1.209 + } 1.210 + 1.211 + private boolean duplicateName(String name) { 1.212 + for (String str : wrapperNames) { 1.213 + if (str.equalsIgnoreCase(name)) 1.214 + return true; 1.215 + } 1.216 + wrapperNames.add(name); 1.217 + return false; 1.218 + } 1.219 + 1.220 + private boolean generateWrappers(ExecutableElement method, WebMethod webMethod) { 1.221 + boolean isOneway = method.getAnnotation(Oneway.class) != null; 1.222 + String beanPackage = packageName + PD_JAXWS_PACKAGE_PD.getValue(); 1.223 + if (packageName.length() == 0) 1.224 + beanPackage = JAXWS_PACKAGE_PD.getValue(); 1.225 + Name methodName = method.getSimpleName(); 1.226 + String operationName = builder.getOperationName(methodName); 1.227 + operationName = webMethod != null && webMethod.operationName().length() > 0 ? 1.228 + webMethod.operationName() : operationName; 1.229 + String reqName = operationName; 1.230 + String resName = operationName + WebServiceConstants.RESPONSE.getValue(); 1.231 + String reqNamespace = typeNamespace; 1.232 + String resNamespace = typeNamespace; 1.233 + 1.234 + String requestClassName = beanPackage + StringUtils.capitalize(method.getSimpleName().toString()); 1.235 + RequestWrapper reqWrapper = method.getAnnotation(RequestWrapper.class); 1.236 + if (reqWrapper != null) { 1.237 + if (reqWrapper.className().length() > 0) 1.238 + requestClassName = reqWrapper.className(); 1.239 + if (reqWrapper.localName().length() > 0) 1.240 + reqName = reqWrapper.localName(); 1.241 + if (reqWrapper.targetNamespace().length() > 0) 1.242 + reqNamespace = reqWrapper.targetNamespace(); 1.243 + } 1.244 + builder.log("requestWrapper: "+requestClassName); 1.245 +///// fix for wsgen CR 6442344 1.246 + File file = new File(DirectoryUtil.getOutputDirectoryFor(requestClassName, builder.getSourceDir()), 1.247 + Names.stripQualifier(requestClassName) + GeneratorConstants.JAVA_SRC_SUFFIX.getValue()); 1.248 + builder.getOptions().addGeneratedFile(file); 1.249 +////////// 1.250 + boolean canOverwriteRequest = builder.canOverWriteClass(requestClassName); 1.251 + if (!canOverwriteRequest) { 1.252 + builder.log("Class " + requestClassName + " exists. Not overwriting."); 1.253 + } 1.254 + if (duplicateName(requestClassName) && canOverwriteRequest) { 1.255 + builder.processError(WebserviceapMessages.WEBSERVICEAP_METHOD_REQUEST_WRAPPER_BEAN_NAME_NOT_UNIQUE( 1.256 + typeElement.getQualifiedName(), method.toString())); 1.257 + } 1.258 + 1.259 + String responseClassName = null; 1.260 + boolean canOverwriteResponse = canOverwriteRequest; 1.261 + if (!isOneway) { 1.262 + responseClassName = beanPackage+StringUtils.capitalize(method.getSimpleName().toString())+RESPONSE.getValue(); 1.263 + ResponseWrapper resWrapper = method.getAnnotation(ResponseWrapper.class); 1.264 + if(resWrapper != null) { 1.265 + if (resWrapper.className().length() > 0) 1.266 + responseClassName = resWrapper.className(); 1.267 + if (resWrapper.localName().length() > 0) 1.268 + resName = resWrapper.localName(); 1.269 + if (resWrapper.targetNamespace().length() > 0) 1.270 + resNamespace = resWrapper.targetNamespace(); 1.271 + } 1.272 + canOverwriteResponse = builder.canOverWriteClass(responseClassName); 1.273 + if (!canOverwriteResponse) { 1.274 + builder.log("Class " + responseClassName + " exists. Not overwriting."); 1.275 + } 1.276 + if (duplicateName(responseClassName) && canOverwriteResponse) { 1.277 + builder.processError(WebserviceapMessages.WEBSERVICEAP_METHOD_RESPONSE_WRAPPER_BEAN_NAME_NOT_UNIQUE( 1.278 + typeElement.getQualifiedName(), method.toString())); 1.279 + } 1.280 + file = new File(DirectoryUtil.getOutputDirectoryFor(responseClassName, builder.getSourceDir()), 1.281 + Names.stripQualifier(responseClassName) + GeneratorConstants.JAVA_SRC_SUFFIX.getValue()); 1.282 + builder.getOptions().addGeneratedFile(file); 1.283 + } 1.284 + //ArrayList<MemberInfo> reqMembers = new ArrayList<MemberInfo>(); 1.285 + //ArrayList<MemberInfo> resMembers = new ArrayList<MemberInfo>(); 1.286 + WrapperInfo reqWrapperInfo = new WrapperInfo(requestClassName); 1.287 + //reqWrapperInfo.setMembers(reqMembers); 1.288 + WrapperInfo resWrapperInfo = null; 1.289 + if (!isOneway) { 1.290 + resWrapperInfo = new WrapperInfo(responseClassName); 1.291 + //resWrapperInfo.setMembers(resMembers); 1.292 + } 1.293 + seiContext.setReqWrapperOperation(method, reqWrapperInfo); 1.294 + if (!isOneway) 1.295 + seiContext.setResWrapperOperation(method, resWrapperInfo); 1.296 + try { 1.297 + if (!canOverwriteRequest && !canOverwriteResponse) { 1.298 + return false; 1.299 + } 1.300 + 1.301 + JDefinedClass reqCls = null; 1.302 + if (canOverwriteRequest) { 1.303 + reqCls = getCMClass(requestClassName, CLASS); 1.304 + } 1.305 + 1.306 + JDefinedClass resCls = null; 1.307 + if (!isOneway && canOverwriteResponse) { 1.308 + resCls = getCMClass(responseClassName, CLASS); 1.309 + } 1.310 + 1.311 + // XMLElement Declarations 1.312 + writeXmlElementDeclaration(reqCls, reqName,reqNamespace); 1.313 + writeXmlElementDeclaration(resCls, resName, resNamespace); 1.314 + 1.315 + List<MemberInfo> reqMembers = ap_generator.collectRequestBeanMembers(method); 1.316 + List<MemberInfo> resMembers = ap_generator.collectResponseBeanMembers(method); 1.317 + 1.318 + // XmlType 1.319 + writeXmlTypeDeclaration(reqCls, reqName, reqNamespace, reqMembers); 1.320 + writeXmlTypeDeclaration(resCls, resName, resNamespace, resMembers); 1.321 + 1.322 + // class members 1.323 + writeMembers(reqCls, reqMembers); 1.324 + writeMembers(resCls, resMembers); 1.325 + 1.326 + } catch (Exception e) { 1.327 + throw new ModelerException("modeler.nestedGeneratorError",e); 1.328 + } 1.329 + return true; 1.330 + } 1.331 + 1.332 +// private List<Annotation> collectJAXBAnnotations(Declaration decl) { 1.333 +// List<Annotation> jaxbAnnotation = new ArrayList<Annotation>(); 1.334 +// for(Class jaxbClass : jaxbAnns) { 1.335 +// Annotation ann = decl.getAnnotation(jaxbClass); 1.336 +// if (ann != null) { 1.337 +// jaxbAnnotation.add(ann); 1.338 +// } 1.339 +// } 1.340 +// return jaxbAnnotation; 1.341 +// } 1.342 + 1.343 + private TypeMirror getSafeType(TypeMirror type) { 1.344 + return makeSafeVisitor.visit(type, builder.getProcessingEnvironment().getTypeUtils()); 1.345 + } 1.346 + 1.347 + private JType getType(TypeMirror typeMirror) { 1.348 + String type = typeMirror.toString(); 1.349 + try { 1.350 +// System.out.println("typeName: "+typeName); 1.351 + return cm.parseType(type); 1.352 +// System.out.println("type: "+type); 1.353 + } catch (ClassNotFoundException e) { 1.354 + return cm.ref(type); 1.355 + } 1.356 + } 1.357 + 1.358 + private void writeMembers(JDefinedClass cls, Collection<MemberInfo> members) { 1.359 + if (cls == null) 1.360 + return; 1.361 + for (MemberInfo memInfo : members) { 1.362 + JType type = getType(memInfo.getParamType()); 1.363 + JFieldVar field = cls.field(JMod.PRIVATE, type, memInfo.getParamName()); 1.364 + annotateParameterWithJaxbAnnotations(memInfo, field); 1.365 + } 1.366 + for (MemberInfo memInfo : members) { 1.367 + writeMember(cls, memInfo.getParamType(), 1.368 + memInfo.getParamName()); 1.369 + } 1.370 + } 1.371 + 1.372 + private void annotateParameterWithJaxbAnnotations(MemberInfo memInfo, JFieldVar field) { 1.373 + List<Annotation> jaxbAnnotations = memInfo.getJaxbAnnotations(); 1.374 + for(Annotation ann : jaxbAnnotations) { 1.375 + if (ann instanceof XmlMimeType) { 1.376 + JAnnotationUse jaxbAnn = field.annotate(XmlMimeType.class); 1.377 + jaxbAnn.param("value", ((XmlMimeType)ann).value()); 1.378 + } else if (ann instanceof XmlJavaTypeAdapter) { 1.379 + JAnnotationUse jaxbAnn = field.annotate(XmlJavaTypeAdapter.class); 1.380 + XmlJavaTypeAdapter ja = (XmlJavaTypeAdapter) ann; 1.381 + try { 1.382 + ja.value(); 1.383 + throw new AssertionError(); 1.384 + } catch (MirroredTypeException e) { 1.385 + jaxbAnn.param("value",getType(e.getTypeMirror())); 1.386 + } 1.387 + // XmlJavaTypeAdapter.type() is for package only. No need to copy. 1.388 + } else if (ann instanceof XmlAttachmentRef) { 1.389 + field.annotate(XmlAttachmentRef.class); 1.390 + } else if (ann instanceof XmlList){ 1.391 + field.annotate(XmlList.class); 1.392 + } else if (ann instanceof XmlElement) { 1.393 + XmlElement elemAnn = (XmlElement)ann; 1.394 + JAnnotationUse jAnn = field.annotate(XmlElement.class); 1.395 + jAnn.param("name", elemAnn.name()); 1.396 + jAnn.param("namespace", elemAnn.namespace()); 1.397 + if (elemAnn.nillable()) { 1.398 + jAnn.param("nillable", true); 1.399 + } 1.400 + if (elemAnn.required()) { 1.401 + jAnn.param("required", true); 1.402 + } 1.403 + } else { 1.404 + throw new WebServiceException("SEI Parameter cannot have this JAXB annotation: " + ann); 1.405 + } 1.406 + } 1.407 + } 1.408 + 1.409 + protected JDefinedClass getCMClass(String className, com.sun.codemodel.internal.ClassType type) { 1.410 + JDefinedClass cls; 1.411 + try { 1.412 + cls = cm._class(className, type); 1.413 + } catch (com.sun.codemodel.internal.JClassAlreadyExistsException e){ 1.414 + cls = cm._getClass(className); 1.415 + } 1.416 + return cls; 1.417 + } 1.418 + 1.419 + private boolean generateExceptionBean(TypeElement thrownDecl, String beanPackage) { 1.420 + if (!builder.isServiceException(thrownDecl.asType())) 1.421 + return false; 1.422 + 1.423 + String exceptionName = ClassNameInfo.getName(thrownDecl.getQualifiedName().toString()); 1.424 + if (processedExceptions.contains(exceptionName)) 1.425 + return false; 1.426 + processedExceptions.add(exceptionName); 1.427 + WebFault webFault = thrownDecl.getAnnotation(WebFault.class); 1.428 + String className = beanPackage+ exceptionName + BEAN.getValue(); 1.429 + 1.430 + Collection<MemberInfo> members = ap_generator.collectExceptionBeanMembers(thrownDecl); 1.431 + boolean isWSDLException = isWSDLException(members, thrownDecl); 1.432 + String namespace = typeNamespace; 1.433 + String name = exceptionName; 1.434 + FaultInfo faultInfo; 1.435 + if (isWSDLException) { 1.436 + TypeMirror beanType = getFaultInfoMember(members).getParamType(); 1.437 + faultInfo = new FaultInfo(TypeMonikerFactory.getTypeMoniker(beanType), true); 1.438 + namespace = webFault.targetNamespace().length()>0 ? 1.439 + webFault.targetNamespace() : namespace; 1.440 + name = webFault.name().length()>0 ? 1.441 + webFault.name() : name; 1.442 + faultInfo.setElementName(new QName(namespace, name)); 1.443 + seiContext.addExceptionBeanEntry(thrownDecl.getQualifiedName(), faultInfo, builder); 1.444 + return false; 1.445 + } 1.446 + if (webFault != null) { 1.447 + namespace = webFault.targetNamespace().length()>0 ? 1.448 + webFault.targetNamespace() : namespace; 1.449 + name = webFault.name().length()>0 ? 1.450 + webFault.name() : name; 1.451 + className = webFault.faultBean().length()>0 ? 1.452 + webFault.faultBean() : className; 1.453 + 1.454 + } 1.455 + JDefinedClass cls = getCMClass(className, CLASS); 1.456 + faultInfo = new FaultInfo(className, false); 1.457 + 1.458 + if (duplicateName(className)) { 1.459 + builder.processError(WebserviceapMessages.WEBSERVICEAP_METHOD_EXCEPTION_BEAN_NAME_NOT_UNIQUE( 1.460 + typeElement.getQualifiedName(), thrownDecl.getQualifiedName())); 1.461 + } 1.462 + 1.463 + boolean canOverWriteBean = builder.canOverWriteClass(className); 1.464 + if (!canOverWriteBean) { 1.465 + builder.log("Class " + className + " exists. Not overwriting."); 1.466 + seiContext.addExceptionBeanEntry(thrownDecl.getQualifiedName(), faultInfo, builder); 1.467 + return false; 1.468 + } 1.469 + if (seiContext.getExceptionBeanName(thrownDecl.getQualifiedName()) != null) 1.470 + return false; 1.471 + 1.472 + //write class comment - JAXWS warning 1.473 + JDocComment comment = cls.javadoc(); 1.474 + for (String doc : GeneratorBase.getJAXWSClassComment(ToolVersion.VERSION.MAJOR_VERSION)) { 1.475 + comment.add(doc); 1.476 + } 1.477 + 1.478 + // XmlElement Declarations 1.479 + writeXmlElementDeclaration(cls, name, namespace); 1.480 + 1.481 + // XmlType Declaration 1.482 + //members = sortMembers(members); 1.483 + XmlType xmlType = thrownDecl.getAnnotation(XmlType.class); 1.484 + String xmlTypeName = (xmlType != null && !xmlType.name().equals("##default")) ? xmlType.name() : exceptionName; 1.485 + String xmlTypeNamespace = (xmlType != null && !xmlType.namespace().equals("##default")) ? xmlType.namespace() : typeNamespace; 1.486 + writeXmlTypeDeclaration(cls, xmlTypeName, xmlTypeNamespace, members); 1.487 + 1.488 + writeMembers(cls, members); 1.489 + 1.490 + seiContext.addExceptionBeanEntry(thrownDecl.getQualifiedName(), faultInfo, builder); 1.491 + return true; 1.492 + } 1.493 + 1.494 + protected boolean isWSDLException(Collection<MemberInfo> members, TypeElement thrownDecl) { 1.495 + WebFault webFault = thrownDecl.getAnnotation(WebFault.class); 1.496 + return webFault != null && members.size() == 2 && getFaultInfoMember(members) != null; 1.497 + } 1.498 + 1.499 + /* 1.500 + * Returns the corresponding MemberInfo for getFaultInfo() 1.501 + * method of an exception. Returns null, if that method is not there. 1.502 + */ 1.503 + private MemberInfo getFaultInfoMember(Collection<MemberInfo> members) { 1.504 + for(MemberInfo member : members) { 1.505 + if (member.getParamName().equals(FAULT_INFO.getValue())) { 1.506 + return member; 1.507 + } 1.508 + } 1.509 + return null; 1.510 + } 1.511 + 1.512 + private void writeXmlElementDeclaration(JDefinedClass cls, String elementName, String namespaceUri) { 1.513 + 1.514 + if (cls == null) 1.515 + return; 1.516 + JAnnotationUse xmlRootElementAnn = cls.annotate(XmlRootElement.class); 1.517 + xmlRootElementAnn.param("name", elementName); 1.518 + if (namespaceUri.length() > 0) { 1.519 + xmlRootElementAnn.param("namespace", namespaceUri); 1.520 + } 1.521 + JAnnotationUse xmlAccessorTypeAnn = cls.annotate(cm.ref(XmlAccessorType.class)); 1.522 + xmlAccessorTypeAnn.param("value", XmlAccessType.FIELD); 1.523 + } 1.524 + 1.525 + private void writeXmlTypeDeclaration(JDefinedClass cls, String typeName, String namespaceUri, 1.526 + Collection<MemberInfo> members) { 1.527 + if (cls == null) 1.528 + return; 1.529 + JAnnotationUse xmlTypeAnn = cls.annotate(cm.ref(XmlType.class)); 1.530 + xmlTypeAnn.param("name", typeName); 1.531 + xmlTypeAnn.param("namespace", namespaceUri); 1.532 + if (members.size() > 1) { 1.533 + JAnnotationArrayMember paramArray = xmlTypeAnn.paramArray("propOrder"); 1.534 + for (MemberInfo memInfo : members) { 1.535 + paramArray.param(memInfo.getParamName()); 1.536 + } 1.537 + } 1.538 + } 1.539 + 1.540 + private void writeMember(JDefinedClass cls, TypeMirror paramType, 1.541 + String paramName) { 1.542 + 1.543 + if (cls == null) 1.544 + return; 1.545 + 1.546 + String accessorName =BindingHelper.mangleNameToPropertyName(paramName); 1.547 + String getterPrefix = paramType.toString().equals("boolean")? "is" : "get"; 1.548 + JType propType = getType(paramType); 1.549 + JMethod m = cls.method(JMod.PUBLIC, propType, getterPrefix+ accessorName); 1.550 + JDocComment methodDoc = m.javadoc(); 1.551 + JCommentPart ret = methodDoc.addReturn(); 1.552 + ret.add("returns "+propType.name()); 1.553 + JBlock body = m.body(); 1.554 + body._return( JExpr._this().ref(paramName) ); 1.555 + 1.556 + m = cls.method(JMod.PUBLIC, cm.VOID, "set"+accessorName); 1.557 + JVar param = m.param(propType, paramName); 1.558 + methodDoc = m.javadoc(); 1.559 + JCommentPart part = methodDoc.addParam(paramName); 1.560 + part.add("the value for the "+ paramName+" property"); 1.561 + body = m.body(); 1.562 + body.assign( JExpr._this().ref(paramName), param ); 1.563 + } 1.564 +}