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 Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,569 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2013, 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 + @Override 1.131 + protected TypeMirror getSafeType(TypeMirror type) { 1.132 + return WebServiceWrapperGenerator.this.getSafeType(type); 1.133 + } 1.134 + 1.135 + @Override 1.136 + protected TypeMirror getHolderValueType(TypeMirror paramType) { 1.137 + return builder.getHolderValueType(paramType); 1.138 + } 1.139 + 1.140 + @Override 1.141 + protected boolean isVoidType(TypeMirror type) { 1.142 + return type != null && type.getKind().equals(TypeKind.VOID); 1.143 + } 1.144 + 1.145 + } 1.146 + 1.147 + private static final class FieldFactory implements AbstractWrapperBeanGenerator.BeanMemberFactory<TypeMirror, MemberInfo> { 1.148 + 1.149 + @Override 1.150 + public MemberInfo createWrapperBeanMember(TypeMirror paramType, 1.151 + String paramName, List<Annotation> jaxb) { 1.152 + return new MemberInfo(paramType, paramName, jaxb); 1.153 + } 1.154 + } 1.155 + 1.156 + public WebServiceWrapperGenerator(ModelBuilder builder, AnnotationProcessorContext context) { 1.157 + super(builder, context); 1.158 + makeSafeVisitor = new MakeSafeTypeVisitor(builder.getProcessingEnvironment()); 1.159 + } 1.160 + 1.161 + @Override 1.162 + protected void processWebService(WebService webService, TypeElement d) { 1.163 + cm = new JCodeModel(); 1.164 + wrapperNames = new HashSet<String>(); 1.165 + processedExceptions = new HashSet<String>(); 1.166 + } 1.167 + 1.168 + @Override 1.169 + protected void postProcessWebService(WebService webService, TypeElement d) { 1.170 + super.postProcessWebService(webService, d); 1.171 + doPostProcessWebService(webService, d); 1.172 + } 1.173 + 1.174 + @SuppressWarnings("CallToThreadDumpStack") 1.175 + protected void doPostProcessWebService(WebService webService, TypeElement d) { 1.176 + if (cm != null) { 1.177 + File sourceDir = builder.getSourceDir(); 1.178 + assert(sourceDir != null); 1.179 + WsgenOptions options = builder.getOptions(); 1.180 + try { 1.181 + CodeWriter cw = new FilerCodeWriter(sourceDir, options); 1.182 + if(options.verbose) 1.183 + cw = new ProgressCodeWriter(cw, System.out); 1.184 + cm.build(cw); 1.185 + } catch (IOException e) { 1.186 + e.printStackTrace(); 1.187 + } 1.188 + } 1.189 + } 1.190 + 1.191 + @Override 1.192 + protected void processMethod(ExecutableElement method, WebMethod webMethod) { 1.193 + builder.log("WrapperGen - method: "+method); 1.194 + builder.log("method.getDeclaringType(): " + method.asType()); 1.195 + if (wrapped && soapStyle.equals(SOAPStyle.DOCUMENT)) { 1.196 + generateWrappers(method, webMethod); 1.197 + } 1.198 + generateExceptionBeans(method); 1.199 + } 1.200 + 1.201 + private boolean generateExceptionBeans(ExecutableElement method) { 1.202 + String beanPackage = packageName + PD_JAXWS_PACKAGE_PD.getValue(); 1.203 + if (packageName.length() == 0) 1.204 + beanPackage = JAXWS_PACKAGE_PD.getValue(); 1.205 + boolean beanGenerated = false; 1.206 + for (TypeMirror thrownType : method.getThrownTypes()) { 1.207 + TypeElement typeDecl = (TypeElement) ((DeclaredType) thrownType).asElement(); 1.208 + if (typeDecl == null) { 1.209 + builder.processError(WebserviceapMessages.WEBSERVICEAP_COULD_NOT_FIND_TYPEDECL( 1.210 + thrownType.toString(), context.getRound())); 1.211 + return false; 1.212 + } 1.213 + boolean tmp = generateExceptionBean(typeDecl, beanPackage); 1.214 + beanGenerated = beanGenerated || tmp; 1.215 + } 1.216 + return beanGenerated; 1.217 + } 1.218 + 1.219 + private boolean duplicateName(String name) { 1.220 + for (String str : wrapperNames) { 1.221 + if (str.equalsIgnoreCase(name)) 1.222 + return true; 1.223 + } 1.224 + wrapperNames.add(name); 1.225 + return false; 1.226 + } 1.227 + 1.228 + private boolean generateWrappers(ExecutableElement method, WebMethod webMethod) { 1.229 + boolean isOneway = method.getAnnotation(Oneway.class) != null; 1.230 + String beanPackage = packageName + PD_JAXWS_PACKAGE_PD.getValue(); 1.231 + if (packageName.length() == 0) 1.232 + beanPackage = JAXWS_PACKAGE_PD.getValue(); 1.233 + Name methodName = method.getSimpleName(); 1.234 + String operationName = builder.getOperationName(methodName); 1.235 + operationName = webMethod != null && webMethod.operationName().length() > 0 ? 1.236 + webMethod.operationName() : operationName; 1.237 + String reqName = operationName; 1.238 + String resName = operationName + WebServiceConstants.RESPONSE.getValue(); 1.239 + String reqNamespace = typeNamespace; 1.240 + String resNamespace = typeNamespace; 1.241 + 1.242 + String requestClassName = beanPackage + StringUtils.capitalize(method.getSimpleName().toString()); 1.243 + RequestWrapper reqWrapper = method.getAnnotation(RequestWrapper.class); 1.244 + if (reqWrapper != null) { 1.245 + if (reqWrapper.className().length() > 0) 1.246 + requestClassName = reqWrapper.className(); 1.247 + if (reqWrapper.localName().length() > 0) 1.248 + reqName = reqWrapper.localName(); 1.249 + if (reqWrapper.targetNamespace().length() > 0) 1.250 + reqNamespace = reqWrapper.targetNamespace(); 1.251 + } 1.252 + builder.log("requestWrapper: "+requestClassName); 1.253 +///// fix for wsgen CR 6442344 1.254 + File file = new File(DirectoryUtil.getOutputDirectoryFor(requestClassName, builder.getSourceDir()), 1.255 + Names.stripQualifier(requestClassName) + GeneratorConstants.JAVA_SRC_SUFFIX.getValue()); 1.256 + builder.getOptions().addGeneratedFile(file); 1.257 +////////// 1.258 + boolean canOverwriteRequest = builder.canOverWriteClass(requestClassName); 1.259 + if (!canOverwriteRequest) { 1.260 + builder.log("Class " + requestClassName + " exists. Not overwriting."); 1.261 + } 1.262 + if (duplicateName(requestClassName) && canOverwriteRequest) { 1.263 + builder.processError(WebserviceapMessages.WEBSERVICEAP_METHOD_REQUEST_WRAPPER_BEAN_NAME_NOT_UNIQUE( 1.264 + typeElement.getQualifiedName(), method.toString())); 1.265 + } 1.266 + 1.267 + String responseClassName = null; 1.268 + boolean canOverwriteResponse = canOverwriteRequest; 1.269 + if (!isOneway) { 1.270 + responseClassName = beanPackage+StringUtils.capitalize(method.getSimpleName().toString())+RESPONSE.getValue(); 1.271 + ResponseWrapper resWrapper = method.getAnnotation(ResponseWrapper.class); 1.272 + if(resWrapper != null) { 1.273 + if (resWrapper.className().length() > 0) 1.274 + responseClassName = resWrapper.className(); 1.275 + if (resWrapper.localName().length() > 0) 1.276 + resName = resWrapper.localName(); 1.277 + if (resWrapper.targetNamespace().length() > 0) 1.278 + resNamespace = resWrapper.targetNamespace(); 1.279 + } 1.280 + canOverwriteResponse = builder.canOverWriteClass(responseClassName); 1.281 + if (!canOverwriteResponse) { 1.282 + builder.log("Class " + responseClassName + " exists. Not overwriting."); 1.283 + } 1.284 + if (duplicateName(responseClassName) && canOverwriteResponse) { 1.285 + builder.processError(WebserviceapMessages.WEBSERVICEAP_METHOD_RESPONSE_WRAPPER_BEAN_NAME_NOT_UNIQUE( 1.286 + typeElement.getQualifiedName(), method.toString())); 1.287 + } 1.288 + file = new File(DirectoryUtil.getOutputDirectoryFor(responseClassName, builder.getSourceDir()), 1.289 + Names.stripQualifier(responseClassName) + GeneratorConstants.JAVA_SRC_SUFFIX.getValue()); 1.290 + builder.getOptions().addGeneratedFile(file); 1.291 + } 1.292 + //ArrayList<MemberInfo> reqMembers = new ArrayList<MemberInfo>(); 1.293 + //ArrayList<MemberInfo> resMembers = new ArrayList<MemberInfo>(); 1.294 + WrapperInfo reqWrapperInfo = new WrapperInfo(requestClassName); 1.295 + //reqWrapperInfo.setMembers(reqMembers); 1.296 + WrapperInfo resWrapperInfo = null; 1.297 + if (!isOneway) { 1.298 + resWrapperInfo = new WrapperInfo(responseClassName); 1.299 + //resWrapperInfo.setMembers(resMembers); 1.300 + } 1.301 + seiContext.setReqWrapperOperation(method, reqWrapperInfo); 1.302 + if (!isOneway) 1.303 + seiContext.setResWrapperOperation(method, resWrapperInfo); 1.304 + try { 1.305 + if (!canOverwriteRequest && !canOverwriteResponse) { 1.306 + return false; 1.307 + } 1.308 + 1.309 + JDefinedClass reqCls = null; 1.310 + if (canOverwriteRequest) { 1.311 + reqCls = getCMClass(requestClassName, CLASS); 1.312 + } 1.313 + 1.314 + JDefinedClass resCls = null; 1.315 + if (!isOneway && canOverwriteResponse) { 1.316 + resCls = getCMClass(responseClassName, CLASS); 1.317 + } 1.318 + 1.319 + // XMLElement Declarations 1.320 + writeXmlElementDeclaration(reqCls, reqName,reqNamespace); 1.321 + writeXmlElementDeclaration(resCls, resName, resNamespace); 1.322 + 1.323 + List<MemberInfo> reqMembers = ap_generator.collectRequestBeanMembers(method); 1.324 + List<MemberInfo> resMembers = ap_generator.collectResponseBeanMembers(method); 1.325 + 1.326 + // XmlType 1.327 + writeXmlTypeDeclaration(reqCls, reqName, reqNamespace, reqMembers); 1.328 + writeXmlTypeDeclaration(resCls, resName, resNamespace, resMembers); 1.329 + 1.330 + // class members 1.331 + writeMembers(reqCls, reqMembers); 1.332 + writeMembers(resCls, resMembers); 1.333 + 1.334 + } catch (Exception e) { 1.335 + throw new ModelerException("modeler.nestedGeneratorError",e); 1.336 + } 1.337 + return true; 1.338 + } 1.339 + 1.340 +// private List<Annotation> collectJAXBAnnotations(Declaration decl) { 1.341 +// List<Annotation> jaxbAnnotation = new ArrayList<Annotation>(); 1.342 +// for(Class jaxbClass : jaxbAnns) { 1.343 +// Annotation ann = decl.getAnnotation(jaxbClass); 1.344 +// if (ann != null) { 1.345 +// jaxbAnnotation.add(ann); 1.346 +// } 1.347 +// } 1.348 +// return jaxbAnnotation; 1.349 +// } 1.350 + 1.351 + private TypeMirror getSafeType(TypeMirror type) { 1.352 + return makeSafeVisitor.visit(type, builder.getProcessingEnvironment().getTypeUtils()); 1.353 + } 1.354 + 1.355 + private JType getType(TypeMirror typeMirror) { 1.356 + String type = typeMirror.toString(); 1.357 + try { 1.358 +// System.out.println("typeName: "+typeName); 1.359 + return cm.parseType(type); 1.360 +// System.out.println("type: "+type); 1.361 + } catch (ClassNotFoundException e) { 1.362 + return cm.ref(type); 1.363 + } 1.364 + } 1.365 + 1.366 + private void writeMembers(JDefinedClass cls, Collection<MemberInfo> members) { 1.367 + if (cls == null) 1.368 + return; 1.369 + for (MemberInfo memInfo : members) { 1.370 + JType type = getType(memInfo.getParamType()); 1.371 + JFieldVar field = cls.field(JMod.PRIVATE, type, memInfo.getParamName()); 1.372 + annotateParameterWithJaxbAnnotations(memInfo, field); 1.373 + } 1.374 + for (MemberInfo memInfo : members) { 1.375 + writeMember(cls, memInfo.getParamType(), 1.376 + memInfo.getParamName()); 1.377 + } 1.378 + } 1.379 + 1.380 + private void annotateParameterWithJaxbAnnotations(MemberInfo memInfo, JFieldVar field) { 1.381 + List<Annotation> jaxbAnnotations = memInfo.getJaxbAnnotations(); 1.382 + for(Annotation ann : jaxbAnnotations) { 1.383 + if (ann instanceof XmlMimeType) { 1.384 + JAnnotationUse jaxbAnn = field.annotate(XmlMimeType.class); 1.385 + jaxbAnn.param("value", ((XmlMimeType)ann).value()); 1.386 + } else if (ann instanceof XmlJavaTypeAdapter) { 1.387 + JAnnotationUse jaxbAnn = field.annotate(XmlJavaTypeAdapter.class); 1.388 + XmlJavaTypeAdapter ja = (XmlJavaTypeAdapter) ann; 1.389 + try { 1.390 + ja.value(); 1.391 + throw new AssertionError(); 1.392 + } catch (MirroredTypeException e) { 1.393 + jaxbAnn.param("value",getType(e.getTypeMirror())); 1.394 + } 1.395 + // XmlJavaTypeAdapter.type() is for package only. No need to copy. 1.396 + } else if (ann instanceof XmlAttachmentRef) { 1.397 + field.annotate(XmlAttachmentRef.class); 1.398 + } else if (ann instanceof XmlList){ 1.399 + field.annotate(XmlList.class); 1.400 + } else if (ann instanceof XmlElement) { 1.401 + XmlElement elemAnn = (XmlElement)ann; 1.402 + JAnnotationUse jAnn = field.annotate(XmlElement.class); 1.403 + jAnn.param("name", elemAnn.name()); 1.404 + jAnn.param("namespace", elemAnn.namespace()); 1.405 + if (elemAnn.nillable()) { 1.406 + jAnn.param("nillable", true); 1.407 + } 1.408 + if (elemAnn.required()) { 1.409 + jAnn.param("required", true); 1.410 + } 1.411 + } else { 1.412 + throw new WebServiceException("SEI Parameter cannot have this JAXB annotation: " + ann); 1.413 + } 1.414 + } 1.415 + } 1.416 + 1.417 + protected JDefinedClass getCMClass(String className, com.sun.codemodel.internal.ClassType type) { 1.418 + JDefinedClass cls; 1.419 + try { 1.420 + cls = cm._class(className, type); 1.421 + } catch (com.sun.codemodel.internal.JClassAlreadyExistsException e){ 1.422 + cls = cm._getClass(className); 1.423 + } 1.424 + return cls; 1.425 + } 1.426 + 1.427 + private boolean generateExceptionBean(TypeElement thrownDecl, String beanPackage) { 1.428 + if (!builder.isServiceException(thrownDecl.asType())) 1.429 + return false; 1.430 + 1.431 + String exceptionName = ClassNameInfo.getName(thrownDecl.getQualifiedName().toString()); 1.432 + if (processedExceptions.contains(exceptionName)) 1.433 + return false; 1.434 + processedExceptions.add(exceptionName); 1.435 + WebFault webFault = thrownDecl.getAnnotation(WebFault.class); 1.436 + String className = beanPackage+ exceptionName + BEAN.getValue(); 1.437 + 1.438 + Collection<MemberInfo> members = ap_generator.collectExceptionBeanMembers(thrownDecl); 1.439 + boolean isWSDLException = isWSDLException(members, thrownDecl); 1.440 + String namespace = typeNamespace; 1.441 + String name = exceptionName; 1.442 + FaultInfo faultInfo; 1.443 + if (isWSDLException) { 1.444 + TypeMirror beanType = getFaultInfoMember(members).getParamType(); 1.445 + faultInfo = new FaultInfo(TypeMonikerFactory.getTypeMoniker(beanType), true); 1.446 + namespace = webFault.targetNamespace().length()>0 ? 1.447 + webFault.targetNamespace() : namespace; 1.448 + name = webFault.name().length()>0 ? 1.449 + webFault.name() : name; 1.450 + faultInfo.setElementName(new QName(namespace, name)); 1.451 + seiContext.addExceptionBeanEntry(thrownDecl.getQualifiedName(), faultInfo, builder); 1.452 + return false; 1.453 + } 1.454 + if (webFault != null) { 1.455 + namespace = webFault.targetNamespace().length()>0 ? 1.456 + webFault.targetNamespace() : namespace; 1.457 + name = webFault.name().length()>0 ? 1.458 + webFault.name() : name; 1.459 + className = webFault.faultBean().length()>0 ? 1.460 + webFault.faultBean() : className; 1.461 + 1.462 + } 1.463 + JDefinedClass cls = getCMClass(className, CLASS); 1.464 + faultInfo = new FaultInfo(className, false); 1.465 + 1.466 + if (duplicateName(className)) { 1.467 + builder.processError(WebserviceapMessages.WEBSERVICEAP_METHOD_EXCEPTION_BEAN_NAME_NOT_UNIQUE( 1.468 + typeElement.getQualifiedName(), thrownDecl.getQualifiedName())); 1.469 + } 1.470 + 1.471 + boolean canOverWriteBean = builder.canOverWriteClass(className); 1.472 + if (!canOverWriteBean) { 1.473 + builder.log("Class " + className + " exists. Not overwriting."); 1.474 + seiContext.addExceptionBeanEntry(thrownDecl.getQualifiedName(), faultInfo, builder); 1.475 + return false; 1.476 + } 1.477 + if (seiContext.getExceptionBeanName(thrownDecl.getQualifiedName()) != null) 1.478 + return false; 1.479 + 1.480 + //write class comment - JAXWS warning 1.481 + JDocComment comment = cls.javadoc(); 1.482 + for (String doc : GeneratorBase.getJAXWSClassComment(ToolVersion.VERSION.MAJOR_VERSION)) { 1.483 + comment.add(doc); 1.484 + } 1.485 + 1.486 + // XmlElement Declarations 1.487 + writeXmlElementDeclaration(cls, name, namespace); 1.488 + 1.489 + // XmlType Declaration 1.490 + //members = sortMembers(members); 1.491 + XmlType xmlType = thrownDecl.getAnnotation(XmlType.class); 1.492 + String xmlTypeName = (xmlType != null && !xmlType.name().equals("##default")) ? xmlType.name() : exceptionName; 1.493 + String xmlTypeNamespace = (xmlType != null && !xmlType.namespace().equals("##default")) ? xmlType.namespace() : typeNamespace; 1.494 + writeXmlTypeDeclaration(cls, xmlTypeName, xmlTypeNamespace, members); 1.495 + 1.496 + writeMembers(cls, members); 1.497 + 1.498 + seiContext.addExceptionBeanEntry(thrownDecl.getQualifiedName(), faultInfo, builder); 1.499 + return true; 1.500 + } 1.501 + 1.502 + protected boolean isWSDLException(Collection<MemberInfo> members, TypeElement thrownDecl) { 1.503 + WebFault webFault = thrownDecl.getAnnotation(WebFault.class); 1.504 + return webFault != null && members.size() == 2 && getFaultInfoMember(members) != null; 1.505 + } 1.506 + 1.507 + /* 1.508 + * Returns the corresponding MemberInfo for getFaultInfo() 1.509 + * method of an exception. Returns null, if that method is not there. 1.510 + */ 1.511 + private MemberInfo getFaultInfoMember(Collection<MemberInfo> members) { 1.512 + for(MemberInfo member : members) { 1.513 + if (member.getParamName().equals(FAULT_INFO.getValue())) { 1.514 + return member; 1.515 + } 1.516 + } 1.517 + return null; 1.518 + } 1.519 + 1.520 + private void writeXmlElementDeclaration(JDefinedClass cls, String elementName, String namespaceUri) { 1.521 + 1.522 + if (cls == null) 1.523 + return; 1.524 + JAnnotationUse xmlRootElementAnn = cls.annotate(XmlRootElement.class); 1.525 + xmlRootElementAnn.param("name", elementName); 1.526 + if (namespaceUri.length() > 0) { 1.527 + xmlRootElementAnn.param("namespace", namespaceUri); 1.528 + } 1.529 + JAnnotationUse xmlAccessorTypeAnn = cls.annotate(cm.ref(XmlAccessorType.class)); 1.530 + xmlAccessorTypeAnn.param("value", XmlAccessType.FIELD); 1.531 + } 1.532 + 1.533 + private void writeXmlTypeDeclaration(JDefinedClass cls, String typeName, String namespaceUri, 1.534 + Collection<MemberInfo> members) { 1.535 + if (cls == null) 1.536 + return; 1.537 + JAnnotationUse xmlTypeAnn = cls.annotate(cm.ref(XmlType.class)); 1.538 + xmlTypeAnn.param("name", typeName); 1.539 + xmlTypeAnn.param("namespace", namespaceUri); 1.540 + if (members.size() > 1) { 1.541 + JAnnotationArrayMember paramArray = xmlTypeAnn.paramArray("propOrder"); 1.542 + for (MemberInfo memInfo : members) { 1.543 + paramArray.param(memInfo.getParamName()); 1.544 + } 1.545 + } 1.546 + } 1.547 + 1.548 + private void writeMember(JDefinedClass cls, TypeMirror paramType, 1.549 + String paramName) { 1.550 + 1.551 + if (cls == null) 1.552 + return; 1.553 + 1.554 + String accessorName =BindingHelper.mangleNameToPropertyName(paramName); 1.555 + String getterPrefix = paramType.toString().equals("boolean")? "is" : "get"; 1.556 + JType propType = getType(paramType); 1.557 + JMethod m = cls.method(JMod.PUBLIC, propType, getterPrefix+ accessorName); 1.558 + JDocComment methodDoc = m.javadoc(); 1.559 + JCommentPart ret = methodDoc.addReturn(); 1.560 + ret.add("returns "+propType.name()); 1.561 + JBlock body = m.body(); 1.562 + body._return( JExpr._this().ref(paramName) ); 1.563 + 1.564 + m = cls.method(JMod.PUBLIC, cm.VOID, "set"+accessorName); 1.565 + JVar param = m.param(propType, paramName); 1.566 + methodDoc = m.javadoc(); 1.567 + JCommentPart part = methodDoc.addParam(paramName); 1.568 + part.add("the value for the "+ paramName+" property"); 1.569 + body = m.body(); 1.570 + body.assign( JExpr._this().ref(paramName), param ); 1.571 + } 1.572 +}