Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.tools.internal.ws.processor.modeler.annotation; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.istack.internal.logging.Logger; |
aoqi@0 | 29 | import com.sun.tools.internal.ws.processor.generator.GeneratorUtil; |
aoqi@0 | 30 | import com.sun.tools.internal.ws.processor.modeler.ModelerException; |
aoqi@0 | 31 | import com.sun.tools.internal.ws.resources.WebserviceapMessages; |
aoqi@0 | 32 | import com.sun.tools.internal.ws.wscompile.AbortException; |
aoqi@0 | 33 | import com.sun.tools.internal.ws.wscompile.WsgenOptions; |
aoqi@0 | 34 | |
aoqi@0 | 35 | import javax.annotation.processing.AbstractProcessor; |
aoqi@0 | 36 | import javax.annotation.processing.ProcessingEnvironment; |
aoqi@0 | 37 | import javax.annotation.processing.RoundEnvironment; |
aoqi@0 | 38 | import javax.annotation.processing.SupportedAnnotationTypes; |
aoqi@0 | 39 | import javax.annotation.processing.SupportedOptions; |
aoqi@0 | 40 | import javax.jws.WebService; |
aoqi@0 | 41 | import javax.lang.model.SourceVersion; |
aoqi@0 | 42 | import javax.lang.model.element.Element; |
aoqi@0 | 43 | import javax.lang.model.element.ElementKind; |
aoqi@0 | 44 | import javax.lang.model.element.Name; |
aoqi@0 | 45 | import javax.lang.model.element.TypeElement; |
aoqi@0 | 46 | import javax.lang.model.type.TypeMirror; |
aoqi@0 | 47 | import javax.lang.model.util.ElementFilter; |
aoqi@0 | 48 | import javax.tools.Diagnostic; |
aoqi@0 | 49 | import javax.xml.ws.Holder; |
aoqi@0 | 50 | import javax.xml.ws.WebServiceProvider; |
aoqi@0 | 51 | import java.io.ByteArrayOutputStream; |
aoqi@0 | 52 | import java.io.File; |
aoqi@0 | 53 | import java.io.PrintStream; |
aoqi@0 | 54 | import java.lang.reflect.Method; |
aoqi@0 | 55 | import java.rmi.Remote; |
aoqi@0 | 56 | import java.rmi.RemoteException; |
aoqi@0 | 57 | import java.util.ArrayList; |
aoqi@0 | 58 | import java.util.Collection; |
aoqi@0 | 59 | import java.util.HashSet; |
aoqi@0 | 60 | import java.util.Scanner; |
aoqi@0 | 61 | import java.util.Set; |
aoqi@0 | 62 | import java.util.logging.Level; |
aoqi@0 | 63 | |
aoqi@0 | 64 | /** |
aoqi@0 | 65 | * WebServiceAp is a AnnotationProcessor for processing javax.jws.* and |
aoqi@0 | 66 | * javax.xml.ws.* annotations. This class is used either by the WsGen (CompileTool) tool or |
aoqi@0 | 67 | * indirectly when invoked by javac. |
aoqi@0 | 68 | * |
aoqi@0 | 69 | * @author WS Development Team |
aoqi@0 | 70 | */ |
aoqi@0 | 71 | @SupportedAnnotationTypes({ |
aoqi@0 | 72 | "javax.jws.HandlerChain", |
aoqi@0 | 73 | "javax.jws.Oneway", |
aoqi@0 | 74 | "javax.jws.WebMethod", |
aoqi@0 | 75 | "javax.jws.WebParam", |
aoqi@0 | 76 | "javax.jws.WebResult", |
aoqi@0 | 77 | "javax.jws.WebService", |
aoqi@0 | 78 | "javax.jws.soap.InitParam", |
aoqi@0 | 79 | "javax.jws.soap.SOAPBinding", |
aoqi@0 | 80 | "javax.jws.soap.SOAPMessageHandler", |
aoqi@0 | 81 | "javax.jws.soap.SOAPMessageHandlers", |
aoqi@0 | 82 | "javax.xml.ws.BindingType", |
aoqi@0 | 83 | "javax.xml.ws.RequestWrapper", |
aoqi@0 | 84 | "javax.xml.ws.ResponseWrapper", |
aoqi@0 | 85 | "javax.xml.ws.ServiceMode", |
aoqi@0 | 86 | "javax.xml.ws.WebEndpoint", |
aoqi@0 | 87 | "javax.xml.ws.WebFault", |
aoqi@0 | 88 | "javax.xml.ws.WebServiceClient", |
aoqi@0 | 89 | "javax.xml.ws.WebServiceProvider", |
aoqi@0 | 90 | "javax.xml.ws.WebServiceRef" |
aoqi@0 | 91 | }) |
aoqi@0 | 92 | @SupportedOptions({WebServiceAp.DO_NOT_OVERWRITE, WebServiceAp.IGNORE_NO_WEB_SERVICE_FOUND_WARNING}) |
aoqi@0 | 93 | public class WebServiceAp extends AbstractProcessor implements ModelBuilder { |
aoqi@0 | 94 | |
aoqi@0 | 95 | private static final Logger LOGGER = Logger.getLogger(WebServiceAp.class); |
aoqi@0 | 96 | |
aoqi@0 | 97 | public static final String DO_NOT_OVERWRITE = "doNotOverWrite"; |
aoqi@0 | 98 | public static final String IGNORE_NO_WEB_SERVICE_FOUND_WARNING = "ignoreNoWebServiceFoundWarning"; |
aoqi@0 | 99 | |
aoqi@0 | 100 | private WsgenOptions options; |
aoqi@0 | 101 | protected AnnotationProcessorContext context; |
aoqi@0 | 102 | private File sourceDir; |
aoqi@0 | 103 | private boolean doNotOverWrite; |
aoqi@0 | 104 | private boolean ignoreNoWebServiceFoundWarning = false; |
aoqi@0 | 105 | private TypeElement remoteElement; |
aoqi@0 | 106 | private TypeMirror remoteExceptionElement; |
aoqi@0 | 107 | private TypeMirror exceptionElement; |
aoqi@0 | 108 | private TypeMirror runtimeExceptionElement; |
aoqi@0 | 109 | private TypeElement defHolderElement; |
aoqi@0 | 110 | private boolean isCommandLineInvocation; |
aoqi@0 | 111 | private PrintStream out; |
aoqi@0 | 112 | private Collection<TypeElement> processedTypeElements = new HashSet<TypeElement>(); |
aoqi@0 | 113 | |
aoqi@0 | 114 | public WebServiceAp() { |
aoqi@0 | 115 | this.context = new AnnotationProcessorContext(); |
aoqi@0 | 116 | } |
aoqi@0 | 117 | |
aoqi@0 | 118 | public WebServiceAp(WsgenOptions options, PrintStream out) { |
aoqi@0 | 119 | this.options = options; |
aoqi@0 | 120 | this.sourceDir = (options != null) ? options.sourceDir : null; |
aoqi@0 | 121 | this.doNotOverWrite = (options != null) && options.doNotOverWrite; |
aoqi@0 | 122 | this.context = new AnnotationProcessorContext(); |
aoqi@0 | 123 | this.out = out; |
aoqi@0 | 124 | } |
aoqi@0 | 125 | |
aoqi@0 | 126 | @Override |
aoqi@0 | 127 | public synchronized void init(ProcessingEnvironment processingEnv) { |
aoqi@0 | 128 | super.init(processingEnv); |
aoqi@0 | 129 | remoteElement = processingEnv.getElementUtils().getTypeElement(Remote.class.getName()); |
aoqi@0 | 130 | remoteExceptionElement = processingEnv.getElementUtils().getTypeElement(RemoteException.class.getName()).asType(); |
aoqi@0 | 131 | exceptionElement = processingEnv.getElementUtils().getTypeElement(Exception.class.getName()).asType(); |
aoqi@0 | 132 | runtimeExceptionElement = processingEnv.getElementUtils().getTypeElement(RuntimeException.class.getName()).asType(); |
aoqi@0 | 133 | defHolderElement = processingEnv.getElementUtils().getTypeElement(Holder.class.getName()); |
aoqi@0 | 134 | if (options == null) { |
aoqi@0 | 135 | options = new WsgenOptions(); |
aoqi@0 | 136 | |
aoqi@0 | 137 | out = new PrintStream(new ByteArrayOutputStream()); |
aoqi@0 | 138 | |
aoqi@0 | 139 | doNotOverWrite = getOption(DO_NOT_OVERWRITE); |
aoqi@0 | 140 | ignoreNoWebServiceFoundWarning = getOption(IGNORE_NO_WEB_SERVICE_FOUND_WARNING); |
aoqi@0 | 141 | |
aoqi@0 | 142 | String classDir = parseArguments(); |
aoqi@0 | 143 | String property = System.getProperty("java.class.path"); |
aoqi@0 | 144 | options.classpath = classDir + File.pathSeparator + (property != null ? property : ""); |
aoqi@0 | 145 | isCommandLineInvocation = true; |
aoqi@0 | 146 | } |
aoqi@0 | 147 | options.filer = processingEnv.getFiler(); |
aoqi@0 | 148 | } |
aoqi@0 | 149 | |
aoqi@0 | 150 | private String parseArguments() { |
aoqi@0 | 151 | // let's try to parse JavacOptions |
aoqi@0 | 152 | |
aoqi@0 | 153 | String classDir = null; |
aoqi@0 | 154 | try { |
aoqi@0 | 155 | ClassLoader cl = WebServiceAp.class.getClassLoader(); |
aoqi@0 | 156 | Class javacProcessingEnvironmentClass = Class.forName("com.sun.tools.javac.processing.JavacProcessingEnvironment", false, cl); |
aoqi@0 | 157 | if (javacProcessingEnvironmentClass.isInstance(processingEnv)) { |
aoqi@0 | 158 | Method getContextMethod = javacProcessingEnvironmentClass.getDeclaredMethod("getContext"); |
aoqi@0 | 159 | Object tmpContext = getContextMethod.invoke(processingEnv); |
aoqi@0 | 160 | Class optionsClass = Class.forName("com.sun.tools.javac.util.Options", false, cl); |
aoqi@0 | 161 | Class contextClass = Class.forName("com.sun.tools.javac.util.Context", false, cl); |
aoqi@0 | 162 | Method instanceMethod = optionsClass.getDeclaredMethod("instance", new Class[]{contextClass}); |
aoqi@0 | 163 | Object tmpOptions = instanceMethod.invoke(null, tmpContext); |
aoqi@0 | 164 | if (tmpOptions != null) { |
aoqi@0 | 165 | Method getMethod = optionsClass.getDeclaredMethod("get", new Class[]{String.class}); |
aoqi@0 | 166 | Object result = getMethod.invoke(tmpOptions, "-s"); // todo: we have to check for -d also |
aoqi@0 | 167 | if (result != null) { |
aoqi@0 | 168 | classDir = (String) result; |
aoqi@0 | 169 | } |
aoqi@0 | 170 | this.options.verbose = getMethod.invoke(tmpOptions, "-verbose") != null; |
aoqi@0 | 171 | } |
aoqi@0 | 172 | } |
aoqi@0 | 173 | } catch (Exception e) { |
aoqi@0 | 174 | /// some Error was here - problems with reflection or security |
aoqi@0 | 175 | processWarning(WebserviceapMessages.WEBSERVICEAP_PARSING_JAVAC_OPTIONS_ERROR()); |
aoqi@0 | 176 | report(e.getMessage()); |
aoqi@0 | 177 | } |
aoqi@0 | 178 | |
aoqi@0 | 179 | if (classDir == null) { // some error within reflection block |
aoqi@0 | 180 | String property = System.getProperty("sun.java.command"); |
aoqi@0 | 181 | if (property != null) { |
aoqi@0 | 182 | Scanner scanner = new Scanner(property); |
aoqi@0 | 183 | boolean sourceDirNext = false; |
aoqi@0 | 184 | while (scanner.hasNext()) { |
aoqi@0 | 185 | String token = scanner.next(); |
aoqi@0 | 186 | if (sourceDirNext) { |
aoqi@0 | 187 | classDir = token; |
aoqi@0 | 188 | sourceDirNext = false; |
aoqi@0 | 189 | } else if ("-verbose".equals(token)) { |
aoqi@0 | 190 | options.verbose = true; |
aoqi@0 | 191 | } else if ("-s".equals(token)) { |
aoqi@0 | 192 | sourceDirNext = true; |
aoqi@0 | 193 | } |
aoqi@0 | 194 | } |
aoqi@0 | 195 | } |
aoqi@0 | 196 | } |
aoqi@0 | 197 | if (classDir != null) { |
aoqi@0 | 198 | sourceDir = new File(classDir); |
aoqi@0 | 199 | } |
aoqi@0 | 200 | return classDir; |
aoqi@0 | 201 | } |
aoqi@0 | 202 | |
aoqi@0 | 203 | private boolean getOption(String key) { |
aoqi@0 | 204 | String value = processingEnv.getOptions().get(key); |
aoqi@0 | 205 | if (value != null) { |
aoqi@0 | 206 | return Boolean.valueOf(value); |
aoqi@0 | 207 | } |
aoqi@0 | 208 | return false; |
aoqi@0 | 209 | } |
aoqi@0 | 210 | |
aoqi@0 | 211 | @Override |
aoqi@0 | 212 | public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
aoqi@0 | 213 | if (context.getRound() != 1) { |
aoqi@0 | 214 | return true; |
aoqi@0 | 215 | } |
aoqi@0 | 216 | context.incrementRound(); |
aoqi@0 | 217 | WebService webService; |
aoqi@0 | 218 | WebServiceProvider webServiceProvider; |
aoqi@0 | 219 | WebServiceVisitor webServiceVisitor = new WebServiceWrapperGenerator(this, context); |
aoqi@0 | 220 | boolean processedEndpoint = false; |
aoqi@0 | 221 | Collection<TypeElement> classes = new ArrayList<TypeElement>(); |
aoqi@0 | 222 | filterClasses(classes, roundEnv.getRootElements()); |
aoqi@0 | 223 | for (TypeElement element : classes) { |
aoqi@0 | 224 | webServiceProvider = element.getAnnotation(WebServiceProvider.class); |
aoqi@0 | 225 | webService = element.getAnnotation(WebService.class); |
aoqi@0 | 226 | if (webServiceProvider != null) { |
aoqi@0 | 227 | if (webService != null) { |
aoqi@0 | 228 | processError(WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_AND_WEBSERVICEPROVIDER(element.getQualifiedName())); |
aoqi@0 | 229 | } |
aoqi@0 | 230 | processedEndpoint = true; |
aoqi@0 | 231 | } |
aoqi@0 | 232 | |
aoqi@0 | 233 | if (webService == null) { |
aoqi@0 | 234 | continue; |
aoqi@0 | 235 | } |
aoqi@0 | 236 | |
aoqi@0 | 237 | element.accept(webServiceVisitor, null); |
aoqi@0 | 238 | processedEndpoint = true; |
aoqi@0 | 239 | } |
aoqi@0 | 240 | if (!processedEndpoint) { |
aoqi@0 | 241 | if (isCommandLineInvocation) { |
aoqi@0 | 242 | if (!ignoreNoWebServiceFoundWarning) { |
aoqi@0 | 243 | processWarning(WebserviceapMessages.WEBSERVICEAP_NO_WEBSERVICE_ENDPOINT_FOUND()); |
aoqi@0 | 244 | } |
aoqi@0 | 245 | } else { |
aoqi@0 | 246 | processError(WebserviceapMessages.WEBSERVICEAP_NO_WEBSERVICE_ENDPOINT_FOUND()); |
aoqi@0 | 247 | } |
aoqi@0 | 248 | } |
aoqi@0 | 249 | return true; |
aoqi@0 | 250 | } |
aoqi@0 | 251 | |
aoqi@0 | 252 | private void filterClasses(Collection<TypeElement> classes, Collection<? extends Element> elements) { |
aoqi@0 | 253 | for (Element element : elements) { |
aoqi@0 | 254 | if (element.getKind().equals(ElementKind.CLASS)) { |
aoqi@0 | 255 | classes.add((TypeElement) element); |
aoqi@0 | 256 | filterClasses(classes, ElementFilter.typesIn(element.getEnclosedElements())); |
aoqi@0 | 257 | } |
aoqi@0 | 258 | } |
aoqi@0 | 259 | } |
aoqi@0 | 260 | |
aoqi@0 | 261 | @Override |
aoqi@0 | 262 | public void processWarning(String message) { |
aoqi@0 | 263 | if (isCommandLineInvocation) { |
aoqi@0 | 264 | processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, message); |
aoqi@0 | 265 | } else { |
aoqi@0 | 266 | report(message); |
aoqi@0 | 267 | } |
aoqi@0 | 268 | } |
aoqi@0 | 269 | |
aoqi@0 | 270 | protected void report(String msg) { |
aoqi@0 | 271 | if (out == null) { |
aoqi@0 | 272 | if (LOGGER.isLoggable(Level.FINE)) { |
aoqi@0 | 273 | LOGGER.log(Level.FINE, "No output set for web service annotation processor reporting."); |
aoqi@0 | 274 | } |
aoqi@0 | 275 | return; |
aoqi@0 | 276 | } |
aoqi@0 | 277 | out.println(msg); |
aoqi@0 | 278 | out.flush(); |
aoqi@0 | 279 | } |
aoqi@0 | 280 | |
aoqi@0 | 281 | @Override |
aoqi@0 | 282 | public void processError(String message) { |
aoqi@0 | 283 | if (isCommandLineInvocation) { |
aoqi@0 | 284 | processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, message); |
aoqi@0 | 285 | throw new AbortException(); |
aoqi@0 | 286 | } else { |
aoqi@0 | 287 | throw new ModelerException(message); |
aoqi@0 | 288 | } |
aoqi@0 | 289 | } |
aoqi@0 | 290 | |
aoqi@0 | 291 | @Override |
aoqi@0 | 292 | public void processError(String message, Element element) { |
aoqi@0 | 293 | if (isCommandLineInvocation) { |
aoqi@0 | 294 | processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, message, element); |
aoqi@0 | 295 | } else { |
aoqi@0 | 296 | throw new ModelerException(message); |
aoqi@0 | 297 | } |
aoqi@0 | 298 | } |
aoqi@0 | 299 | |
aoqi@0 | 300 | @Override |
aoqi@0 | 301 | public boolean canOverWriteClass(String className) { |
aoqi@0 | 302 | return !((doNotOverWrite && GeneratorUtil.classExists(options, className))); |
aoqi@0 | 303 | } |
aoqi@0 | 304 | |
aoqi@0 | 305 | @Override |
aoqi@0 | 306 | public File getSourceDir() { |
aoqi@0 | 307 | return sourceDir; |
aoqi@0 | 308 | } |
aoqi@0 | 309 | |
aoqi@0 | 310 | @Override |
aoqi@0 | 311 | public boolean isRemote(TypeElement typeElement) { |
aoqi@0 | 312 | return processingEnv.getTypeUtils().isSubtype(typeElement.asType(), remoteElement.asType()); |
aoqi@0 | 313 | } |
aoqi@0 | 314 | |
aoqi@0 | 315 | @Override |
aoqi@0 | 316 | public boolean isServiceException(TypeMirror typeMirror) { |
aoqi@0 | 317 | return processingEnv.getTypeUtils().isSubtype(typeMirror, exceptionElement) |
aoqi@0 | 318 | && !processingEnv.getTypeUtils().isSubtype(typeMirror, runtimeExceptionElement) |
aoqi@0 | 319 | && !processingEnv.getTypeUtils().isSubtype(typeMirror, remoteExceptionElement); |
aoqi@0 | 320 | } |
aoqi@0 | 321 | |
aoqi@0 | 322 | @Override |
aoqi@0 | 323 | public TypeMirror getHolderValueType(TypeMirror type) { |
aoqi@0 | 324 | return TypeModeler.getHolderValueType(type, defHolderElement, processingEnv); |
aoqi@0 | 325 | } |
aoqi@0 | 326 | |
aoqi@0 | 327 | @Override |
aoqi@0 | 328 | public boolean checkAndSetProcessed(TypeElement typeElement) { |
aoqi@0 | 329 | if (!processedTypeElements.contains(typeElement)) { |
aoqi@0 | 330 | processedTypeElements.add(typeElement); |
aoqi@0 | 331 | return false; |
aoqi@0 | 332 | } |
aoqi@0 | 333 | return true; |
aoqi@0 | 334 | } |
aoqi@0 | 335 | |
aoqi@0 | 336 | @Override |
aoqi@0 | 337 | public void log(String message) { |
aoqi@0 | 338 | if (options != null && options.verbose) { |
aoqi@0 | 339 | message = new StringBuilder().append('[').append(message).append(']').toString(); // "[%s]" |
aoqi@0 | 340 | processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, message); |
aoqi@0 | 341 | } |
aoqi@0 | 342 | } |
aoqi@0 | 343 | |
aoqi@0 | 344 | @Override |
aoqi@0 | 345 | public WsgenOptions getOptions() { |
aoqi@0 | 346 | return options; |
aoqi@0 | 347 | } |
aoqi@0 | 348 | |
aoqi@0 | 349 | @Override |
aoqi@0 | 350 | public ProcessingEnvironment getProcessingEnvironment() { |
aoqi@0 | 351 | return processingEnv; |
aoqi@0 | 352 | } |
aoqi@0 | 353 | |
aoqi@0 | 354 | @Override |
aoqi@0 | 355 | public String getOperationName(Name messageName) { |
aoqi@0 | 356 | return messageName != null ? messageName.toString() : null; |
aoqi@0 | 357 | } |
aoqi@0 | 358 | |
aoqi@0 | 359 | @Override |
aoqi@0 | 360 | public SourceVersion getSupportedSourceVersion() { |
aoqi@0 | 361 | return SourceVersion.latest(); |
aoqi@0 | 362 | } |
aoqi@0 | 363 | } |