jtulach commented on code in PR #5609:
URL: https://github.com/apache/netbeans/pull/5609#discussion_r1141265451


##########
nbbuild/antsrc/org/netbeans/nbbuild/CustomJavac.java:
##########
@@ -252,4 +276,144 @@ private static boolean isIgnored(
         return false;
     }
 
+    private static final class NbJavacLoader extends URLClassLoader {
+        private static final String MAIN_COMPILER_CP = "nbjavac.class.path";
+        private static final String MAIN_COMPILER_CLASS = 
"com.sun.tools.javac.Main";
+        private final Map<String, Class<?>> priorityLoaded;
+
+        private NbJavacLoader(URL[] urls, ClassLoader parent) {
+            super(urls, parent);
+            this.priorityLoaded = new HashMap<>();
+        }
+
+        private static synchronized Class<?> findMainCompilerClass(
+                Project prj
+        ) throws MalformedURLException, ClassNotFoundException, 
URISyntaxException {
+            String cp = prj.getProperty(MAIN_COMPILER_CP);
+            if (cp == null) {
+                return null;
+            }
+
+            Object c = System.getProperties().get(MAIN_COMPILER_CLASS);
+            if (!(c instanceof Class<?>)) {
+                FileSet fs = new FileSet();
+                final File cpPath = new File(cp);
+                if (cpPath.isAbsolute()) {
+                    fs.setDir(cpPath.getParentFile());
+                    fs.setIncludes(cpPath.getName());
+                } else {
+                    String nball = prj.getProperty("nb_all");
+                    if (nball != null) {
+                        fs.setDir(new File(nball));
+                    } else {
+                        fs.setDir(prj.getBaseDir());
+                    }
+                    fs.setIncludes(cp);
+                }
+                List<URL> urls = new ArrayList<>();
+                final DirectoryScanner scan = fs.getDirectoryScanner(prj);
+                File base = scan.getBasedir();
+                for (String relative : scan.getIncludedFiles()) {
+                    File file = new File(base, relative);
+                    URL url = FileUtils.getFileUtils().getFileURL(file);
+                    urls.add(url);
+                }
+                if (urls.isEmpty()) {
+                    throw new BuildException("Cannot find nb-javac JAR 
libraries in " + base + " and " + cp);
+                }
+                URLClassLoader loader = new NbJavacLoader(urls.toArray(new 
URL[0]), CustomJavac.class.getClassLoader().getParent());
+                final Class<?> newCompilerClass = 
Class.forName(MAIN_COMPILER_CLASS, true, loader);
+                assertIsolatedClassLoader(newCompilerClass, loader);
+                System.getProperties().put(MAIN_COMPILER_CLASS, 
newCompilerClass);
+                c = newCompilerClass;
+            }
+            return (Class<?>) c;
+        }
+
+        private static void assertIsolatedClassLoader(Class<?> c, 
URLClassLoader loader) throws ClassNotFoundException, BuildException {
+            if (c.getClassLoader() != loader) {
+                throw new BuildException("Class " + c + " loaded by " + 
c.getClassLoader() + " and not " + loader);
+            }
+            Class<?> stdLoc = 
c.getClassLoader().loadClass(StandardLocation.class.getName());
+            if (stdLoc.getClassLoader() != loader) {
+                throw new BuildException("Class " + stdLoc + " loaded by " + 
stdLoc.getClassLoader() + " and not " + loader);
+            }
+        }
+
+        @Override
+        protected Class<?> loadClass(String name, boolean resolve) throws 
ClassNotFoundException {
+            if (isNbJavacClass(name)) {
+                Class<?> clazz = priorityLoaded.get(name);
+                if (clazz == null) {
+                    clazz = findClass(name);
+                    priorityLoaded.put(name, clazz);
+                }
+                return clazz;
+            }
+            return super.loadClass(name, resolve);
+        }
+
+        @Override
+        public Class<?> loadClass(String name) throws ClassNotFoundException {
+            return loadClass(name, false);
+        }
+
+        private static boolean isNbJavacClass(String name) {
+            return name.startsWith("javax.annotation.")
+                    || name.startsWith("javax.tools.")
+                    || name.startsWith("javax.lang.model.")
+                    || name.startsWith("com.sun.source.")
+                    || name.startsWith("com.sun.tools.");
+        }
+    }
+
+    private static final class NbJavacCompiler extends Javac13 {
+
+        private final Class<?> mainClazz;
+
+        NbJavacCompiler(Class<?> mainClazz) {
+            this.mainClazz = mainClazz;
+        }
+
+        @Override
+        public boolean execute() throws BuildException {
+            attributes.log("Using modern compiler", Project.MSG_VERBOSE);
+            Commandline cmd = setupModernJavacCommand();
+            final String[] args = cmd.getArguments();
+            boolean bootClasspath = false;
+            for (int i = 0; i < args.length; i++) {
+                if (args[i].startsWith("-Xbootclasspath/p:")) { // ide/html
+                    bootClasspath = true;
+                }
+                if (args[i].startsWith("-J")) {
+                    args[i] = "-Xlint:none"; // webcommon/javascript2.editor
+                }
+            }
+            for (int i = 0; i < args.length; i++) {
+                if (!bootClasspath) {
+                    if ("-target".equals(args[i]) || 
"-source".equals(args[i])) {
+                        args[i] = "--release";
+                        if (args[i + 1].startsWith("1.")) {
+                            args[i + 1] = "8";

Review Comment:
   No problem with that, but in order to keep this PR small and focused, I 
suggest to fix all the occurrences of `1.8`
   ```
   netbeans$ grep 1.8 */*/nbproject/project.properties | wc -l
   826
   ```
   in some different PR.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to