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/generator/CustomExceptionGenerator.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,160 @@ 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.generator; 1.30 + 1.31 +import com.sun.codemodel.internal.ClassType; 1.32 +import com.sun.codemodel.internal.JAnnotationUse; 1.33 +import com.sun.codemodel.internal.JBlock; 1.34 +import com.sun.codemodel.internal.JClass; 1.35 +import com.sun.codemodel.internal.JClassAlreadyExistsException; 1.36 +import com.sun.codemodel.internal.JDefinedClass; 1.37 +import com.sun.codemodel.internal.JDocComment; 1.38 +import com.sun.codemodel.internal.JExpr; 1.39 +import com.sun.codemodel.internal.JFieldRef; 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.tools.internal.ws.processor.model.Fault; 1.46 +import com.sun.tools.internal.ws.processor.model.Model; 1.47 +import com.sun.tools.internal.ws.wscompile.ErrorReceiver; 1.48 +import com.sun.tools.internal.ws.wscompile.WsimportOptions; 1.49 + 1.50 +import javax.xml.ws.WebFault; 1.51 +import java.util.HashMap; 1.52 +import java.util.Map; 1.53 + 1.54 +/** 1.55 + * 1.56 + * @author WS Development Team 1.57 + */ 1.58 +public class CustomExceptionGenerator extends GeneratorBase { 1.59 + private Map<String, JClass> faults = new HashMap<String, JClass>(); 1.60 + 1.61 + public static void generate(Model model, 1.62 + WsimportOptions options, 1.63 + ErrorReceiver receiver){ 1.64 + CustomExceptionGenerator exceptionGen = new CustomExceptionGenerator(); 1.65 + exceptionGen.init(model, options, receiver); 1.66 + exceptionGen.doGeneration(); 1.67 + } 1.68 + 1.69 + public GeneratorBase getGenerator(Model model, WsimportOptions options, ErrorReceiver receiver) { 1.70 + GeneratorBase g = new CustomExceptionGenerator(); 1.71 + g.init(model, options, receiver); 1.72 + return g; 1.73 + } 1.74 + 1.75 + @Override 1.76 + public void visit(Fault fault) throws Exception { 1.77 + if (isRegistered(fault)) 1.78 + return; 1.79 + registerFault(fault); 1.80 + } 1.81 + 1.82 + private boolean isRegistered(Fault fault) { 1.83 + if(faults.keySet().contains(fault.getJavaException().getName())){ 1.84 + fault.setExceptionClass(faults.get(fault.getJavaException().getName())); 1.85 + return true; 1.86 + } 1.87 + return false; 1.88 + } 1.89 + 1.90 + private void registerFault(Fault fault) { 1.91 + try { 1.92 + write(fault); 1.93 + faults.put(fault.getJavaException().getName(), fault.getExceptionClass()); 1.94 + } catch (JClassAlreadyExistsException e) { 1.95 + throw new GeneratorException("generator.nestedGeneratorError",e); 1.96 + } 1.97 + } 1.98 + 1.99 + private void write(Fault fault) throws JClassAlreadyExistsException { 1.100 + String className = Names.customExceptionClassName(fault); 1.101 + 1.102 + JDefinedClass cls = cm._class(className, ClassType.CLASS); 1.103 + JDocComment comment = cls.javadoc(); 1.104 + if(fault.getJavaDoc() != null){ 1.105 + comment.add(fault.getJavaDoc()); 1.106 + comment.add("\n\n"); 1.107 + } 1.108 + 1.109 + for (String doc : getJAXWSClassComment()) { 1.110 + comment.add(doc); 1.111 + } 1.112 + 1.113 + cls._extends(java.lang.Exception.class); 1.114 + 1.115 + //@WebFault 1.116 + JAnnotationUse faultAnn = cls.annotate(WebFault.class); 1.117 + faultAnn.param("name", fault.getBlock().getName().getLocalPart()); 1.118 + faultAnn.param("targetNamespace", fault.getBlock().getName().getNamespaceURI()); 1.119 + 1.120 + JType faultBean = fault.getBlock().getType().getJavaType().getType().getType(); 1.121 + 1.122 + //faultInfo filed 1.123 + JFieldVar fi = cls.field(JMod.PRIVATE, faultBean, "faultInfo"); 1.124 + 1.125 + //add jaxb annotations 1.126 + fault.getBlock().getType().getJavaType().getType().annotate(fi); 1.127 + 1.128 + fi.javadoc().add("Java type that goes as soapenv:Fault detail element."); 1.129 + JFieldRef fr = JExpr.ref(JExpr._this(), fi); 1.130 + 1.131 + //Constructor 1.132 + JMethod constrc1 = cls.constructor(JMod.PUBLIC); 1.133 + JVar var1 = constrc1.param(String.class, "message"); 1.134 + JVar var2 = constrc1.param(faultBean, "faultInfo"); 1.135 + constrc1.javadoc().addParam(var1); 1.136 + constrc1.javadoc().addParam(var2); 1.137 + JBlock cb1 = constrc1.body(); 1.138 + cb1.invoke("super").arg(var1); 1.139 + 1.140 + cb1.assign(fr, var2); 1.141 + 1.142 + //constructor with Throwable 1.143 + JMethod constrc2 = cls.constructor(JMod.PUBLIC); 1.144 + var1 = constrc2.param(String.class, "message"); 1.145 + var2 = constrc2.param(faultBean, "faultInfo"); 1.146 + JVar var3 = constrc2.param(Throwable.class, "cause"); 1.147 + constrc2.javadoc().addParam(var1); 1.148 + constrc2.javadoc().addParam(var2); 1.149 + constrc2.javadoc().addParam(var3); 1.150 + JBlock cb2 = constrc2.body(); 1.151 + cb2.invoke("super").arg(var1).arg(var3); 1.152 + cb2.assign(fr, var2); 1.153 + 1.154 + 1.155 + //getFaultInfo() method 1.156 + JMethod fim = cls.method(JMod.PUBLIC, faultBean, "getFaultInfo"); 1.157 + fim.javadoc().addReturn().add("returns fault bean: "+faultBean.fullName()); 1.158 + JBlock fib = fim.body(); 1.159 + fib._return(fi); 1.160 + fault.setExceptionClass(cls); 1.161 + 1.162 + } 1.163 +}