src/share/jaxws_classes/com/sun/tools/internal/jxc/SchemaGenerator.java

Fri, 16 Oct 2015 19:07:08 +0300

author
aefimov
date
Fri, 16 Oct 2015 19:07:08 +0300
changeset 1011
70d4d5435eb4
parent 368
0989ad8c0860
child 1288
f4ace6971570
permissions
-rw-r--r--

8073519: schemagen does not report errors while generating xsd files
Reviewed-by: dfuchs

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2013, 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.jxc;
ohair@286 27
ohair@286 28 import com.sun.tools.internal.jxc.ap.Options;
ohair@286 29 import com.sun.tools.internal.xjc.BadCommandLineException;
ohair@286 30 import com.sun.xml.internal.bind.util.Which;
ohair@286 31
ohair@286 32 import javax.lang.model.SourceVersion;
aefimov@1011 33 import javax.tools.Diagnostic;
ohair@286 34 import javax.tools.DiagnosticCollector;
ohair@286 35 import javax.tools.JavaCompiler;
ohair@286 36 import javax.tools.JavaFileObject;
ohair@286 37 import javax.tools.OptionChecker;
ohair@286 38 import javax.tools.StandardJavaFileManager;
ohair@286 39 import javax.tools.ToolProvider;
ohair@286 40 import javax.xml.bind.JAXBContext;
ohair@286 41 import java.io.File;
ohair@286 42 import java.lang.reflect.InvocationTargetException;
ohair@286 43 import java.lang.reflect.Method;
ohair@286 44 import java.net.MalformedURLException;
ohair@286 45 import java.net.URISyntaxException;
ohair@286 46 import java.net.URL;
alanb@368 47 import java.net.URLClassLoader;
ohair@286 48 import java.util.ArrayList;
alanb@368 49 import java.util.Collection;
ohair@286 50 import java.util.Collections;
ohair@286 51 import java.util.List;
ohair@286 52 import java.util.logging.Level;
ohair@286 53 import java.util.logging.Logger;
ohair@286 54
ohair@286 55 /**
ohair@286 56 * CLI entry-point to the schema generator.
ohair@286 57 *
ohair@286 58 * @author Bhakti Mehta
ohair@286 59 */
ohair@286 60 public class SchemaGenerator {
ohair@286 61 /**
ohair@286 62 * Runs the schema generator.
ohair@286 63 */
ohair@286 64 public static void main(String[] args) throws Exception {
ohair@286 65 System.exit(run(args));
ohair@286 66 }
ohair@286 67
ohair@286 68 public static int run(String[] args) throws Exception {
ohair@286 69 try {
ohair@286 70 ClassLoader cl = SecureLoader.getClassClassLoader(SchemaGenerator.class);
ohair@286 71 if (cl==null) {
ohair@286 72 cl = SecureLoader.getSystemClassLoader();
ohair@286 73 }
ohair@286 74 return run(args, cl);
ohair@286 75 } catch(Exception e) {
ohair@286 76 System.err.println(e.getMessage());
ohair@286 77 return -1;
ohair@286 78 }
ohair@286 79 }
ohair@286 80
ohair@286 81 /**
ohair@286 82 * Runs the schema generator.
ohair@286 83 *
ohair@286 84 * @param classLoader
ohair@286 85 * the schema generator will run in this classLoader.
ohair@286 86 * It needs to be able to load annotation processing and JAXB RI classes. Note that
ohair@286 87 * JAXB RI classes refer to annotation processing classes. Must not be null.
ohair@286 88 *
ohair@286 89 * @return
ohair@286 90 * exit code. 0 if success.
ohair@286 91 *
ohair@286 92 */
ohair@286 93 public static int run(String[] args, ClassLoader classLoader) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
ohair@286 94 final Options options = new Options();
ohair@286 95 if (args.length ==0) {
ohair@286 96 usage();
ohair@286 97 return -1;
ohair@286 98 }
ohair@286 99 for (String arg : args) {
ohair@286 100 if (arg.equals("-help")) {
ohair@286 101 usage();
ohair@286 102 return -1;
ohair@286 103 }
ohair@286 104
ohair@286 105 if (arg.equals("-version")) {
ohair@286 106 System.out.println(Messages.VERSION.format());
ohair@286 107 return -1;
ohair@286 108 }
ohair@286 109
ohair@286 110 if (arg.equals("-fullversion")) {
ohair@286 111 System.out.println(Messages.FULLVERSION.format());
ohair@286 112 return -1;
ohair@286 113 }
ohair@286 114
ohair@286 115 }
ohair@286 116
ohair@286 117 try {
ohair@286 118 options.parseArguments(args);
ohair@286 119 } catch (BadCommandLineException e) {
ohair@286 120 // there was an error in the command line.
ohair@286 121 // print usage and abort.
ohair@286 122 System.out.println(e.getMessage());
ohair@286 123 System.out.println();
ohair@286 124 usage();
ohair@286 125 return -1;
ohair@286 126 }
ohair@286 127
ohair@286 128 Class schemagenRunner = classLoader.loadClass(Runner.class.getName());
ohair@286 129 Method compileMethod = schemagenRunner.getDeclaredMethod("compile",String[].class,File.class);
ohair@286 130
ohair@286 131 List<String> aptargs = new ArrayList<String>();
ohair@286 132
ohair@286 133 if (options.encoding != null) {
ohair@286 134 aptargs.add("-encoding");
ohair@286 135 aptargs.add(options.encoding);
ohair@286 136 }
ohair@286 137
ohair@286 138 aptargs.add("-cp");
alanb@368 139 aptargs.add(setClasspath(options.classpath)); // set original classpath + jaxb-api to be visible to annotation processor
ohair@286 140
ohair@286 141 if(options.targetDir!=null) {
ohair@286 142 aptargs.add("-d");
ohair@286 143 aptargs.add(options.targetDir.getPath());
ohair@286 144 }
ohair@286 145
ohair@286 146 aptargs.addAll(options.arguments);
ohair@286 147
ohair@286 148 String[] argsarray = aptargs.toArray(new String[aptargs.size()]);
ohair@286 149 return ((Boolean) compileMethod.invoke(null, argsarray, options.episodeFile)) ? 0 : 1;
ohair@286 150 }
ohair@286 151
alanb@368 152 private static String setClasspath(String givenClasspath) {
alanb@368 153 StringBuilder cp = new StringBuilder();
alanb@368 154 appendPath(cp, givenClasspath);
alanb@368 155 ClassLoader cl = Thread.currentThread().getContextClassLoader();
alanb@368 156 while (cl != null) {
alanb@368 157 if (cl instanceof URLClassLoader) {
alanb@368 158 for (URL url : ((URLClassLoader) cl).getURLs()) {
alanb@368 159 appendPath(cp, url.getPath());
alanb@368 160 }
alanb@368 161 }
alanb@368 162 cl = cl.getParent();
alanb@368 163 }
alanb@368 164
alanb@368 165 appendPath(cp, findJaxbApiJar());
alanb@368 166 return cp.toString();
alanb@368 167 }
alanb@368 168
alanb@368 169 private static void appendPath(StringBuilder cp, String url) {
alanb@368 170 if (url == null || url.trim().isEmpty())
alanb@368 171 return;
alanb@368 172 if (cp.length() != 0)
alanb@368 173 cp.append(File.pathSeparatorChar);
alanb@368 174 cp.append(url);
alanb@368 175 }
alanb@368 176
ohair@286 177 /**
ohair@286 178 * Computes the file system path of <tt>jaxb-api.jar</tt> so that
ohair@286 179 * Annotation Processing will see them in the <tt>-cp</tt> option.
ohair@286 180 *
ohair@286 181 * <p>
ohair@286 182 * In Java, you can't do this reliably (for that matter there's no guarantee
ohair@286 183 * that such a jar file exists, such as in Glassfish), so we do the best we can.
ohair@286 184 *
ohair@286 185 * @return
ohair@286 186 * null if failed to locate it.
ohair@286 187 */
alanb@368 188 private static String findJaxbApiJar() {
ohair@286 189 String url = Which.which(JAXBContext.class);
ohair@286 190 if(url==null) return null; // impossible, but hey, let's be defensive
ohair@286 191
ohair@286 192 if(!url.startsWith("jar:") || url.lastIndexOf('!')==-1)
ohair@286 193 // no jar file
ohair@286 194 return null;
ohair@286 195
ohair@286 196 String jarFileUrl = url.substring(4,url.lastIndexOf('!'));
ohair@286 197 if(!jarFileUrl.startsWith("file:"))
ohair@286 198 return null; // not from file system
ohair@286 199
ohair@286 200 try {
ohair@286 201 File f = new File(new URL(jarFileUrl).toURI());
ohair@286 202 if (f.exists() && f.getName().endsWith(".jar")) { // see 6510966
alanb@368 203 return f.getPath();
ohair@286 204 }
ohair@286 205 f = new File(new URL(jarFileUrl).getFile());
ohair@286 206 if (f.exists() && f.getName().endsWith(".jar")) { // this is here for potential backw. compatibility issues
alanb@368 207 return f.getPath();
ohair@286 208 }
ohair@286 209 } catch (URISyntaxException ex) {
ohair@286 210 Logger.getLogger(SchemaGenerator.class.getName()).log(Level.SEVERE, null, ex);
ohair@286 211 } catch (MalformedURLException ex) {
ohair@286 212 Logger.getLogger(SchemaGenerator.class.getName()).log(Level.SEVERE, null, ex);
ohair@286 213 }
ohair@286 214 return null;
ohair@286 215 }
ohair@286 216
ohair@286 217 private static void usage( ) {
ohair@286 218 System.out.println(Messages.USAGE.format());
ohair@286 219 }
ohair@286 220
ohair@286 221 public static final class Runner {
ohair@286 222 public static boolean compile(String[] args, File episode) throws Exception {
ohair@286 223
ohair@286 224 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
ohair@286 225 DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
ohair@286 226 StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
ohair@286 227 JavacOptions options = JavacOptions.parse(compiler, fileManager, args);
ohair@286 228 List<String> unrecognizedOptions = options.getUnrecognizedOptions();
ohair@286 229 if (!unrecognizedOptions.isEmpty())
ohair@286 230 Logger.getLogger(SchemaGenerator.class.getName()).log(Level.WARNING, "Unrecognized options found: {0}", unrecognizedOptions);
ohair@286 231 Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(options.getFiles());
ohair@286 232 JavaCompiler.CompilationTask task = compiler.getTask(
ohair@286 233 null,
ohair@286 234 fileManager,
ohair@286 235 diagnostics,
ohair@286 236 options.getRecognizedOptions(),
ohair@286 237 options.getClassNames(),
ohair@286 238 compilationUnits);
ohair@286 239 com.sun.tools.internal.jxc.ap.SchemaGenerator r = new com.sun.tools.internal.jxc.ap.SchemaGenerator();
ohair@286 240 if (episode != null)
ohair@286 241 r.setEpisodeFile(episode);
ohair@286 242 task.setProcessors(Collections.singleton(r));
aefimov@1011 243 boolean res = task.call();
aefimov@1011 244 //Print messages generated by compiler
aefimov@1011 245 for (Diagnostic<? extends JavaFileObject> d : diagnostics.getDiagnostics()) {
aefimov@1011 246 System.err.println(d.toString());
aefimov@1011 247 }
aefimov@1011 248 return res;
ohair@286 249 }
ohair@286 250 }
ohair@286 251
ohair@286 252 /**
ohair@286 253 * @author Peter von der Ahe
ohair@286 254 */
ohair@286 255 private static final class JavacOptions {
ohair@286 256 private final List<String> recognizedOptions;
ohair@286 257 private final List<String> classNames;
ohair@286 258 private final List<File> files;
ohair@286 259 private final List<String> unrecognizedOptions;
ohair@286 260
ohair@286 261 private JavacOptions(List<String> recognizedOptions, List<String> classNames, List<File> files,
ohair@286 262 List<String> unrecognizedOptions) {
ohair@286 263 this.recognizedOptions = recognizedOptions;
ohair@286 264 this.classNames = classNames;
ohair@286 265 this.files = files;
ohair@286 266 this.unrecognizedOptions = unrecognizedOptions;
ohair@286 267 }
ohair@286 268
ohair@286 269 public static JavacOptions parse(OptionChecker primary, OptionChecker secondary, String... arguments) {
ohair@286 270 List<String> recognizedOptions = new ArrayList<String>();
ohair@286 271 List<String> unrecognizedOptions = new ArrayList<String>();
ohair@286 272 List<String> classNames = new ArrayList<String>();
ohair@286 273 List<File> files = new ArrayList<File>();
ohair@286 274 for (int i = 0; i < arguments.length; i++) {
ohair@286 275 String argument = arguments[i];
ohair@286 276 int optionCount = primary.isSupportedOption(argument);
ohair@286 277 if (optionCount < 0) {
ohair@286 278 optionCount = secondary.isSupportedOption(argument);
ohair@286 279 }
ohair@286 280 if (optionCount < 0) {
ohair@286 281 File file = new File(argument);
ohair@286 282 if (file.exists())
ohair@286 283 files.add(file);
ohair@286 284 else if (SourceVersion.isName(argument))
ohair@286 285 classNames.add(argument);
ohair@286 286 else
ohair@286 287 unrecognizedOptions.add(argument);
ohair@286 288 } else {
ohair@286 289 for (int j = 0; j < optionCount + 1; j++) {
ohair@286 290 int index = i + j;
ohair@286 291 if (index == arguments.length) throw new IllegalArgumentException(argument);
ohair@286 292 recognizedOptions.add(arguments[index]);
ohair@286 293 }
ohair@286 294 i += optionCount;
ohair@286 295 }
ohair@286 296 }
ohair@286 297 return new JavacOptions(recognizedOptions, classNames, files, unrecognizedOptions);
ohair@286 298 }
ohair@286 299
ohair@286 300 /**
ohair@286 301 * Returns the list of recognized options and their arguments.
ohair@286 302 *
ohair@286 303 * @return a list of options
ohair@286 304 */
ohair@286 305 public List<String> getRecognizedOptions() {
ohair@286 306 return Collections.unmodifiableList(recognizedOptions);
ohair@286 307 }
ohair@286 308
ohair@286 309 /**
ohair@286 310 * Returns the list of file names.
ohair@286 311 *
ohair@286 312 * @return a list of file names
ohair@286 313 */
ohair@286 314 public List<File> getFiles() {
ohair@286 315 return Collections.unmodifiableList(files);
ohair@286 316 }
ohair@286 317
ohair@286 318 /**
ohair@286 319 * Returns the list of class names.
ohair@286 320 *
ohair@286 321 * @return a list of class names
ohair@286 322 */
ohair@286 323 public List<String> getClassNames() {
ohair@286 324 return Collections.unmodifiableList(classNames);
ohair@286 325 }
ohair@286 326
ohair@286 327 /**
ohair@286 328 * Returns the list of unrecognized options.
ohair@286 329 *
ohair@286 330 * @return a list of unrecognized options
ohair@286 331 */
ohair@286 332 public List<String> getUnrecognizedOptions() {
ohair@286 333 return Collections.unmodifiableList(unrecognizedOptions);
ohair@286 334 }
ohair@286 335
ohair@286 336 @Override
ohair@286 337 public String toString() {
ohair@286 338 return String.format("recognizedOptions = %s; classNames = %s; " + "files = %s; unrecognizedOptions = %s", recognizedOptions, classNames, files, unrecognizedOptions);
ohair@286 339 }
ohair@286 340 }
ohair@286 341 }

mercurial