< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java

Print this page
rev 2973 : JDK-8058150

@@ -23,33 +23,42 @@
  * questions.
  */
 package com.sun.tools.javac.main;
 
 import java.io.File;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.Map;
+import java.util.ServiceLoader;
 import java.util.Set;
 
 import javax.tools.JavaFileManager;
 import javax.tools.JavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.StandardLocation;
 
 import com.sun.tools.doclint.DocLint;
 import com.sun.tools.javac.code.Lint.LintCategory;
 import com.sun.tools.javac.code.Source;
+import com.sun.tools.javac.file.BaseFileManager;
 import com.sun.tools.javac.file.JavacFileManager;
 import com.sun.tools.javac.jvm.Profile;
 import com.sun.tools.javac.jvm.Target;
 import com.sun.tools.javac.main.OptionHelper.GrumpyHelper;
-import com.sun.tools.javac.file.BaseFileManager;
+import com.sun.tools.javac.platform.PlatformProvider;
+import com.sun.tools.javac.platform.PlatformProviderFactory;
 import com.sun.tools.javac.util.Context;
 import com.sun.tools.javac.util.List;
 import com.sun.tools.javac.util.ListBuffer;
 import com.sun.tools.javac.util.Log;
 import com.sun.tools.javac.util.Log.PrefixKind;
+import com.sun.tools.javac.util.Log.WriterKind;
 import com.sun.tools.javac.util.Options;
 import com.sun.tools.javac.util.PropagatedException;
 
 /**
  * Shared option and argument handling for command line and API usage of javac.

@@ -270,10 +279,67 @@
      *      ILLEGAL_ARGUMENT
      */
     private boolean processArgs(Iterable<String> args,
             Set<Option> allowableOpts, OptionHelper helper,
             boolean allowOperands, boolean checkFileManager) {
+        if (!doProcessArgs(args, allowableOpts, helper, allowOperands, checkFileManager))
+            return false;
+
+        String platformString = options.get(Option.PLATFORM);
+
+        checkOptionAllowed(platformString == null,
+                option -> error("err.platform.bootclasspath.conflict", option.getText()),
+                Option.BOOTCLASSPATH, Option.XBOOTCLASSPATH, Option.XBOOTCLASSPATH_APPEND,
+                Option.XBOOTCLASSPATH_PREPEND, Option.ENDORSEDDIRS, Option.EXTDIRS, Option.SOURCE,
+                Option.TARGET);
+
+        if (platformString != null) {
+            PlatformProvider platformProvider = lookupPlatformProvider(platformString);
+
+            if (platformProvider == null) {
+                error("err.unsupported.platform.version", platformString);
+                return false;
+            }
+
+            options.put(Option.SOURCE, platformProvider.getSourceVersion());
+            options.put(Option.TARGET, platformProvider.getTargetVersion());
+
+            context.put(PlatformProvider.class, platformProvider);
+
+            if (!doProcessArgs(platformProvider.getAdditionalOptions(), allowableOpts, helper, allowOperands, checkFileManager))
+                return false;
+
+            Collection<Path> platformCP = platformProvider.getPlatformPath();
+
+            if (platformCP != null) {
+                JavaFileManager fm = getFileManager();
+
+                if (!(fm instanceof StandardJavaFileManager)) {
+                    error("err.platform.not.standard.file.manager");
+                    return false;
+                }
+
+                try {
+                    StandardJavaFileManager sfm = (StandardJavaFileManager) fm;
+
+                    sfm.setLocationFromPaths(StandardLocation.PLATFORM_CLASS_PATH, platformCP);
+                } catch (IOException ex) {
+                    log.printLines(PrefixKind.JAVAC, "msg.io");
+                    ex.printStackTrace(log.getWriter(WriterKind.NOTICE));
+                    return false;
+                }
+            }
+        }
+
+        options.notifyListeners();
+
+        return true;
+    }
+
+    private boolean doProcessArgs(Iterable<String> args,
+            Set<Option> allowableOpts, OptionHelper helper,
+            boolean allowOperands, boolean checkFileManager) {
         JavaFileManager fm = checkFileManager ? getFileManager() : null;
         Iterator<String> argIter = args.iterator();
         while (argIter.hasNext()) {
             String arg = argIter.next();
             if (arg.isEmpty()) {

@@ -313,15 +379,12 @@
             } else {
                 if (option.process(helper, arg)) {
                     return false;
                 }
             }
-
         }
 
-        options.notifyListeners();
-
         return true;
     }
 
     /**
      * Validates the overall consistency of the options and operands

@@ -403,11 +466,11 @@
             }
         }
 
         boolean lintOptions = options.isUnset(Option.XLINT_CUSTOM, "-" + LintCategory.OPTIONS.option);
 
-        if (lintOptions && source.compareTo(Source.DEFAULT) < 0) {
+        if (lintOptions && source.compareTo(Source.DEFAULT) < 0 && !options.isSet(Option.PLATFORM)) {
             JavaFileManager fm = getFileManager();
             if (fm instanceof BaseFileManager) {
                 if (((BaseFileManager) fm).isDefaultBootClassPath())
                     log.warning(LintCategory.OPTIONS, "source.no.bootclasspath", source.name);
             }

@@ -433,10 +496,24 @@
             log.warning(LintCategory.OPTIONS, "option.obsolete.suppression");
 
         return !errors;
     }
 
+    private PlatformProvider lookupPlatformProvider(String platform) {
+        Iterable<PlatformProviderFactory> factories =
+                ServiceLoader.load(PlatformProviderFactory.class, Arguments.class.getClassLoader());
+        for (PlatformProviderFactory factory : factories) {
+            for (PlatformProvider provider : factory.createPlatformProviders()) {
+                if (provider.getName().equals(platform)) {
+                    return provider;
+                }
+            }
+        }
+
+        return null;
+    }
+
     /**
      * Returns true if there are no files or classes specified for use.
      * @return true if there are no files or classes specified for use
      */
     public boolean isEmpty() {

@@ -526,10 +603,23 @@
             return false;
         }
         return true;
     }
 
+    private interface ErrorReporter {
+        void report(Option o);
+    }
+
+    void checkOptionAllowed(boolean allowed, ErrorReporter r, Option... opts) {
+        if (!allowed) {
+            for (Option o: opts) {
+                if (options.isSet(o))
+                    r.report(o);
+            }
+        }
+    }
+
     void error(String key, Object... args) {
         errors = true;
         switch (errorMode) {
             case ILLEGAL_ARGUMENT: {
                 String msg = log.localize(PrefixKind.JAVAC, key, args);
< prev index next >