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


##########
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

Review Comment:
   This looks strange. I see the "javac.compilerargs=-J-Xmx512m" and 
`build.compiler=extJavac` in `project.properties` from 
`webcommon/javascript2.editor`. Both additions look doubious and most probably 
should be dropped instead of adding this hack (they are identically present in 
`php/php.editor`).



##########
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:
   We have this code at various locations. Would it make sense to go once 
through the codebase and switch all usesages of `1.7`/`1.8`  to 7/8? The old 
numbering is dead.



##########
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<>();

Review Comment:
   Two questions: Does the JVM really not cache the class object? I would have 
expected the VM to hold the class definition in memory while the class is still 
in use. If it is indeed necessary, this should be a `ConcurrentHashMap` or be 
wrapped with `Collections#synchronizedMap`, as `loadClass` is not synchronized 
and there is no explicit synchronization provided there.



##########
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";
+                        }
+                    }
+                }
+                if ("-Werror".equals(args[i])) {
+                    args[i] = "-Xlint:none";

Review Comment:
   If I understand this correctly this turn a "fail build on error" into an 
"suppress errors". This sounds strange to me.



-- 
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