< prev index next >

src/jdk.jextract/share/classes/com/sun/tools/jextract/Main.java

Print this page




  68         this.ctx = ctx;
  69     }
  70 
  71     private void processPackageMapping(Object arg) {
  72         String str = (String) arg;
  73         Path p = null;
  74         String pkgName;
  75         if (str.indexOf('=') == -1) {
  76             pkgName = str;
  77         } else {
  78             KeyValuePair kv = KeyValuePair.valueOf(str);
  79             p = Paths.get(kv.key);
  80             pkgName = kv.value;
  81 
  82             if (!Files.isDirectory(p)) {
  83                 throw new IllegalArgumentException(format("not.a.directory", kv.key));
  84             }
  85         }
  86 
  87         Utils.validPackageName(pkgName);
  88         ctx.usePackageForFolder(p, pkgName);
  89     }
  90 
  91     private void processHeader(Object header) {
  92         Path p = Paths.get((String) header);
  93         if (!Files.isReadable(p)) {
  94             throw new IllegalArgumentException(format("cannot.read.header.file", header));
  95         }
  96         p = p.toAbsolutePath();
  97         ctx.usePackageForFolder(p.getParent(), targetPackage);
  98         ctx.addSource(p);
  99     }
 100 
 101     private void setupLogging(Level level) {
 102         Logger logger = ctx.logger;
 103         logger.setUseParentHandlers(false);
 104         ConsoleHandler log = new ConsoleHandler();
 105         System.setProperty("java.util.logging.SimpleFormatter.format", "%4$s: %5$s%n");
 106         log.setFormatter(new SimpleFormatter());
 107         logger.setLevel(level);
 108         log.setLevel(level);
 109         logger.addHandler(log);
 110     }
 111 
 112     private void printHelp(OptionParser parser) {
 113         try {
 114             parser.printHelpOn(ctx.err);
 115         } catch (IOException ex) {
 116             if (Main.DEBUG) {
 117                 ex.printStackTrace(ctx.err);
 118             }


 234             } catch (PatternSyntaxException pse) {
 235                 ctx.err.println(format("exclude.symbols.pattern.error", pse.getMessage()));
 236             }
 237         }
 238 
 239         if (options.has("L")) {
 240             List<?> libpaths = options.valuesOf("L");
 241             // "L" with no "l" option!
 242             if (options.has("l")) {
 243                 libpaths.forEach(p -> ctx.addLinkCheckPath((String) p));
 244                 if (infer_rpath) {
 245                     libpaths.forEach(p -> ctx.addLibraryPath((String) p));
 246                 }
 247             } else {
 248                 ctx.err.println(format("warn.L.without.l"));
 249             }
 250         } else if (infer_rpath) {
 251             ctx.err.println(format("warn.rpath.auto.without.L"));
 252         }
 253 
 254         targetPackage = options.has("t") ? (String) options.valueOf("t") : "";
 255         if (!targetPackage.isEmpty()) {
 256             Utils.validPackageName(targetPackage);
 257         }
 258 
 259         if (options.has("m")) {
 260             options.valuesOf("m").forEach(this::processPackageMapping);
 261         }
 262 


 263         try {
 264             options.nonOptionArguments().stream().forEach(this::processHeader);
 265             ctx.parse();












 266         } catch (RuntimeException re) {
 267             ctx.err.println(re.getMessage());
 268             if (Main.DEBUG) {
 269                 re.printStackTrace(ctx.err);
 270             }
 271             return 2;
 272         }
 273 
 274         if (options.has("dry-run")) {
 275             return 0;
 276         }
 277 
 278         boolean hasOutput = false;
 279 
 280         if (options.has("d")) {
 281             hasOutput = true;
 282             Path dest = Paths.get((String) options.valueOf("d"));
 283             dest = dest.toAbsolutePath();
 284             try {
 285                 if (!Files.exists(dest)) {
 286                     Files.createDirectories(dest);
 287                 } else if (!Files.isDirectory(dest)) {
 288                     ctx.err.println(format("not.a.directory", dest));
 289                     return 4;
 290                 }
 291                 ctx.collectClassFiles(dest, args, targetPackage);
 292             } catch (IOException ex) {
 293                 ctx.err.println(format("cannot.write.class.file", dest, ex));
 294                 if (Main.DEBUG) {
 295                     ex.printStackTrace(ctx.err);
 296                 }
 297                 return 5;
 298             }
 299         }
 300 
 301         String outputName;
 302         if (options.has("o")) {
 303             outputName = (String) options.valueOf("o");
 304         } else if (hasOutput) {
 305             return 0;
 306         } else {
 307             outputName =  Paths.get((String)options.nonOptionArguments().get(0)).getFileName() + ".jar";
 308         }
 309 
 310         try {
 311             ctx.collectJarFile(Paths.get(outputName), args, targetPackage);
 312         } catch (IOException ex) {
 313             ctx.err.println(format("cannot.write.jar.file", outputName, ex));
 314             if (Main.DEBUG) {
 315                 ex.printStackTrace(ctx.err);
 316             }
 317             return 3;
 318         }
 319 
 320         return 0;
 321     }
 322 
 323     private static Path getBuiltinHeadersDir() {
 324         return Paths.get(System.getProperty("java.home"), "conf", "jextract");
 325     }
 326 
 327     public static void main(String... args) {
 328         Main instance = new Main(new Context());
 329 
 330         System.exit(instance.run(args));
 331     }
 332 
 333     public static class JextractToolProvider implements ToolProvider {
 334         @Override
 335         public String name() {
 336             return "jextract";
 337         }
 338 
 339         @Override
 340         public int run(PrintWriter out, PrintWriter err, String... args) {
 341             // defensive check to throw security exception early.
 342             // Note that the successful run of jextract under security
 343             // manager would require far more permissions like loading


  68         this.ctx = ctx;
  69     }
  70 
  71     private void processPackageMapping(Object arg) {
  72         String str = (String) arg;
  73         Path p = null;
  74         String pkgName;
  75         if (str.indexOf('=') == -1) {
  76             pkgName = str;
  77         } else {
  78             KeyValuePair kv = KeyValuePair.valueOf(str);
  79             p = Paths.get(kv.key);
  80             pkgName = kv.value;
  81 
  82             if (!Files.isDirectory(p)) {
  83                 throw new IllegalArgumentException(format("not.a.directory", kv.key));
  84             }
  85         }
  86 
  87         Utils.validPackageName(pkgName);
  88         ctx.addPackageMapping(p, pkgName);










  89     }
  90 
  91     private void setupLogging(Level level) {
  92         Logger logger = ctx.logger;
  93         logger.setUseParentHandlers(false);
  94         ConsoleHandler log = new ConsoleHandler();
  95         System.setProperty("java.util.logging.SimpleFormatter.format", "%4$s: %5$s%n");
  96         log.setFormatter(new SimpleFormatter());
  97         logger.setLevel(level);
  98         log.setLevel(level);
  99         logger.addHandler(log);
 100     }
 101 
 102     private void printHelp(OptionParser parser) {
 103         try {
 104             parser.printHelpOn(ctx.err);
 105         } catch (IOException ex) {
 106             if (Main.DEBUG) {
 107                 ex.printStackTrace(ctx.err);
 108             }


 224             } catch (PatternSyntaxException pse) {
 225                 ctx.err.println(format("exclude.symbols.pattern.error", pse.getMessage()));
 226             }
 227         }
 228 
 229         if (options.has("L")) {
 230             List<?> libpaths = options.valuesOf("L");
 231             // "L" with no "l" option!
 232             if (options.has("l")) {
 233                 libpaths.forEach(p -> ctx.addLinkCheckPath((String) p));
 234                 if (infer_rpath) {
 235                     libpaths.forEach(p -> ctx.addLibraryPath((String) p));
 236                 }
 237             } else {
 238                 ctx.err.println(format("warn.L.without.l"));
 239             }
 240         } else if (infer_rpath) {
 241             ctx.err.println(format("warn.rpath.auto.without.L"));
 242         }
 243 





 244         if (options.has("m")) {
 245             options.valuesOf("m").forEach(this::processPackageMapping);
 246         }
 247 
 248         final Writer writer;
 249 
 250         try {
 251             for (Object header : options.nonOptionArguments()) {
 252                 Path p = Paths.get((String)header);
 253                 if (!Files.isReadable(p)) {
 254                     throw new IllegalArgumentException(format("cannot.read.header.file", header));
 255                 }
 256                 p = p.normalize().toAbsolutePath();
 257                 ctx.addSource(p);
 258             }
 259             targetPackage = options.has("t") ? (String) options.valueOf("t") : "";
 260             if (!targetPackage.isEmpty()) {
 261                 Utils.validPackageName(targetPackage);
 262             }
 263             ctx.setTargetPackage(targetPackage);
 264             writer = new JextractTool(ctx).processHeaders();
 265         } catch (RuntimeException re) {
 266             ctx.err.println(re.getMessage());
 267             if (Main.DEBUG) {
 268                 re.printStackTrace(ctx.err);
 269             }
 270             return 2;
 271         }
 272 
 273         if (options.has("dry-run")) {
 274             return 0;
 275         }
 276 
 277         boolean hasOutput = false;
 278 
 279         if (options.has("d")) {
 280             hasOutput = true;
 281             Path dest = Paths.get((String) options.valueOf("d"));
 282             dest = dest.toAbsolutePath();
 283             try {
 284                 if (!Files.exists(dest)) {
 285                     Files.createDirectories(dest);
 286                 } else if (!Files.isDirectory(dest)) {
 287                     ctx.err.println(format("not.a.directory", dest));
 288                     return 4;
 289                 }
 290                 writer.writeClassFiles(dest, args);
 291             } catch (IOException ex) {
 292                 ctx.err.println(format("cannot.write.class.file", dest, ex));
 293                 if (Main.DEBUG) {
 294                     ex.printStackTrace(ctx.err);
 295                 }
 296                 return 5;
 297             }
 298         }
 299 
 300         String outputName;
 301         if (options.has("o")) {
 302             outputName = (String) options.valueOf("o");
 303         } else if (hasOutput) {
 304             return 0;
 305         } else {
 306             outputName =  Paths.get((String)options.nonOptionArguments().get(0)).getFileName() + ".jar";
 307         }
 308 
 309         try {
 310             writer.writeJarFile(Paths.get(outputName), args);
 311         } catch (IOException ex) {
 312             ctx.err.println(format("cannot.write.jar.file", outputName, ex));
 313             if (Main.DEBUG) {
 314                 ex.printStackTrace(ctx.err);
 315             }
 316             return 3;
 317         }
 318 
 319         return 0;
 320     }
 321 
 322     static Path getBuiltinHeadersDir() {
 323         return Paths.get(System.getProperty("java.home"), "conf", "jextract");
 324     }
 325 
 326     public static void main(String... args) {
 327         Main instance = new Main(new Context());
 328 
 329         System.exit(instance.run(args));
 330     }
 331 
 332     public static class JextractToolProvider implements ToolProvider {
 333         @Override
 334         public String name() {
 335             return "jextract";
 336         }
 337 
 338         @Override
 339         public int run(PrintWriter out, PrintWriter err, String... args) {
 340             // defensive check to throw security exception early.
 341             // Note that the successful run of jextract under security
 342             // manager would require far more permissions like loading
< prev index next >