src/share/jaxws_classes/com/sun/tools/internal/ws/processor/generator/CustomExceptionGenerator.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.generator;
ohair@286 27
ohair@286 28 import com.sun.codemodel.internal.ClassType;
ohair@286 29 import com.sun.codemodel.internal.JAnnotationUse;
ohair@286 30 import com.sun.codemodel.internal.JBlock;
ohair@286 31 import com.sun.codemodel.internal.JClass;
ohair@286 32 import com.sun.codemodel.internal.JClassAlreadyExistsException;
ohair@286 33 import com.sun.codemodel.internal.JDefinedClass;
ohair@286 34 import com.sun.codemodel.internal.JDocComment;
ohair@286 35 import com.sun.codemodel.internal.JExpr;
ohair@286 36 import com.sun.codemodel.internal.JFieldRef;
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.tools.internal.ws.processor.model.Fault;
ohair@286 43 import com.sun.tools.internal.ws.processor.model.Model;
ohair@286 44 import com.sun.tools.internal.ws.wscompile.ErrorReceiver;
ohair@286 45 import com.sun.tools.internal.ws.wscompile.WsimportOptions;
ohair@286 46
ohair@286 47 import javax.xml.ws.WebFault;
ohair@286 48 import java.util.HashMap;
ohair@286 49 import java.util.Map;
ohair@286 50
ohair@286 51 /**
ohair@286 52 *
ohair@286 53 * @author WS Development Team
ohair@286 54 */
ohair@286 55 public class CustomExceptionGenerator extends GeneratorBase {
ohair@286 56 private Map<String, JClass> faults = new HashMap<String, JClass>();
ohair@286 57
ohair@286 58 public static void generate(Model model,
ohair@286 59 WsimportOptions options,
ohair@286 60 ErrorReceiver receiver){
ohair@286 61 CustomExceptionGenerator exceptionGen = new CustomExceptionGenerator();
ohair@286 62 exceptionGen.init(model, options, receiver);
ohair@286 63 exceptionGen.doGeneration();
ohair@286 64 }
ohair@286 65
ohair@286 66 public GeneratorBase getGenerator(Model model, WsimportOptions options, ErrorReceiver receiver) {
ohair@286 67 GeneratorBase g = new CustomExceptionGenerator();
ohair@286 68 g.init(model, options, receiver);
ohair@286 69 return g;
ohair@286 70 }
ohair@286 71
ohair@286 72 @Override
ohair@286 73 public void visit(Fault fault) throws Exception {
ohair@286 74 if (isRegistered(fault))
ohair@286 75 return;
ohair@286 76 registerFault(fault);
ohair@286 77 }
ohair@286 78
ohair@286 79 private boolean isRegistered(Fault fault) {
ohair@286 80 if(faults.keySet().contains(fault.getJavaException().getName())){
ohair@286 81 fault.setExceptionClass(faults.get(fault.getJavaException().getName()));
ohair@286 82 return true;
ohair@286 83 }
ohair@286 84 return false;
ohair@286 85 }
ohair@286 86
ohair@286 87 private void registerFault(Fault fault) {
ohair@286 88 try {
ohair@286 89 write(fault);
ohair@286 90 faults.put(fault.getJavaException().getName(), fault.getExceptionClass());
ohair@286 91 } catch (JClassAlreadyExistsException e) {
ohair@286 92 throw new GeneratorException("generator.nestedGeneratorError",e);
ohair@286 93 }
ohair@286 94 }
ohair@286 95
ohair@286 96 private void write(Fault fault) throws JClassAlreadyExistsException {
ohair@286 97 String className = Names.customExceptionClassName(fault);
ohair@286 98
ohair@286 99 JDefinedClass cls = cm._class(className, ClassType.CLASS);
ohair@286 100 JDocComment comment = cls.javadoc();
ohair@286 101 if(fault.getJavaDoc() != null){
ohair@286 102 comment.add(fault.getJavaDoc());
ohair@286 103 comment.add("\n\n");
ohair@286 104 }
ohair@286 105
ohair@286 106 for (String doc : getJAXWSClassComment()) {
ohair@286 107 comment.add(doc);
ohair@286 108 }
ohair@286 109
ohair@286 110 cls._extends(java.lang.Exception.class);
ohair@286 111
ohair@286 112 //@WebFault
ohair@286 113 JAnnotationUse faultAnn = cls.annotate(WebFault.class);
ohair@286 114 faultAnn.param("name", fault.getBlock().getName().getLocalPart());
ohair@286 115 faultAnn.param("targetNamespace", fault.getBlock().getName().getNamespaceURI());
ohair@286 116
ohair@286 117 JType faultBean = fault.getBlock().getType().getJavaType().getType().getType();
ohair@286 118
ohair@286 119 //faultInfo filed
ohair@286 120 JFieldVar fi = cls.field(JMod.PRIVATE, faultBean, "faultInfo");
ohair@286 121
ohair@286 122 //add jaxb annotations
ohair@286 123 fault.getBlock().getType().getJavaType().getType().annotate(fi);
ohair@286 124
ohair@286 125 fi.javadoc().add("Java type that goes as soapenv:Fault detail element.");
ohair@286 126 JFieldRef fr = JExpr.ref(JExpr._this(), fi);
ohair@286 127
ohair@286 128 //Constructor
ohair@286 129 JMethod constrc1 = cls.constructor(JMod.PUBLIC);
ohair@286 130 JVar var1 = constrc1.param(String.class, "message");
ohair@286 131 JVar var2 = constrc1.param(faultBean, "faultInfo");
ohair@286 132 constrc1.javadoc().addParam(var1);
ohair@286 133 constrc1.javadoc().addParam(var2);
ohair@286 134 JBlock cb1 = constrc1.body();
ohair@286 135 cb1.invoke("super").arg(var1);
ohair@286 136
ohair@286 137 cb1.assign(fr, var2);
ohair@286 138
ohair@286 139 //constructor with Throwable
ohair@286 140 JMethod constrc2 = cls.constructor(JMod.PUBLIC);
ohair@286 141 var1 = constrc2.param(String.class, "message");
ohair@286 142 var2 = constrc2.param(faultBean, "faultInfo");
ohair@286 143 JVar var3 = constrc2.param(Throwable.class, "cause");
ohair@286 144 constrc2.javadoc().addParam(var1);
ohair@286 145 constrc2.javadoc().addParam(var2);
ohair@286 146 constrc2.javadoc().addParam(var3);
ohair@286 147 JBlock cb2 = constrc2.body();
ohair@286 148 cb2.invoke("super").arg(var1).arg(var3);
ohair@286 149 cb2.assign(fr, var2);
ohair@286 150
ohair@286 151
ohair@286 152 //getFaultInfo() method
ohair@286 153 JMethod fim = cls.method(JMod.PUBLIC, faultBean, "getFaultInfo");
ohair@286 154 fim.javadoc().addReturn().add("returns fault bean: "+faultBean.fullName());
ohair@286 155 JBlock fib = fim.body();
ohair@286 156 fib._return(fi);
ohair@286 157 fault.setExceptionClass(cls);
ohair@286 158
ohair@286 159 }
ohair@286 160 }

mercurial