groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [groovy] 02/05: Improve robustness
Date Thu, 19 Dec 2019 03:09:31 GMT
This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 73cbb4d41f218deadcf830b929c2f9544795e8e2
Author: Daniel Sun <sunlan@apache.org>
AuthorDate: Thu Dec 19 08:23:46 2019 +0800

    Improve robustness
    
    (cherry picked from commit 2737292fcc9b0a16e1fdd90d7a0347e083bd7cec)
---
 .../codehaus/groovy/vmplugin/VMPluginFactory.java  | 38 +++++++++++++++-------
 .../java/org/codehaus/groovy/ant/VerifyClass.java  | 30 ++++++++++-------
 2 files changed, 46 insertions(+), 22 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/vmplugin/VMPluginFactory.java b/src/main/java/org/codehaus/groovy/vmplugin/VMPluginFactory.java
index f6cf64a..679c1b6 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/VMPluginFactory.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/VMPluginFactory.java
@@ -18,14 +18,21 @@
  */
 package org.codehaus.groovy.vmplugin;
 
+import org.codehaus.groovy.runtime.DefaultGroovyMethods;
 import org.codehaus.groovy.vmplugin.v7.Java7;
 
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
 /**
  * Factory class to get functionality based on the VM version.
  * The usage of this class is not for public use, only for the
  * runtime.
  */
 public class VMPluginFactory {
+    private static final Logger LOGGER = Logger.getLogger(VMPluginFactory.class.getName());
 
     private static final String JDK8_CLASSNAME_CHECK = "java.util.Optional";
     private static final String JDK9_CLASSNAME_CHECK = "java.lang.Module";
@@ -33,7 +40,7 @@ public class VMPluginFactory {
     private static final String JDK8_PLUGIN_NAME = "org.codehaus.groovy.vmplugin.v8.Java8";
     private static final String JDK9_PLUGIN_NAME = "org.codehaus.groovy.vmplugin.v9.Java9";
 
-    private static final VMPlugin plugin;
+    private static final VMPlugin PLUGIN;
 
     static {
         VMPlugin target = createPlugin(JDK9_CLASSNAME_CHECK, JDK9_PLUGIN_NAME);
@@ -43,20 +50,29 @@ public class VMPluginFactory {
                 target = new Java7();
             }
         }
-        plugin = target;
+
+        PLUGIN = target;
     }
 
     public static VMPlugin getPlugin() {
-        return plugin;
+        return PLUGIN;
     }
 
-    private static VMPlugin createPlugin(String classNameCheck, String pluginName) {
-        try {
-            ClassLoader loader = VMPluginFactory.class.getClassLoader();
-            loader.loadClass(classNameCheck);
-            return (VMPlugin) loader.loadClass(pluginName).getDeclaredConstructor().newInstance();
-        } catch (Throwable ex) {
-            return null;
-        }
+    private static VMPlugin createPlugin(final String classNameCheck, final String pluginName)
{
+        return AccessController.doPrivileged((PrivilegedAction<VMPlugin>) () ->
{
+            try {
+                ClassLoader loader = VMPluginFactory.class.getClassLoader();
+                loader.loadClass(classNameCheck);
+                return (VMPlugin) loader.loadClass(pluginName).getDeclaredConstructor().newInstance();
+            } catch (Throwable t) {
+                if (LOGGER.isLoggable(Level.FINE)) {
+                    LOGGER.fine("Trying to create VM plugin `" + pluginName + "` by checking
`" + classNameCheck
+                            + "`, but failed:\n" + DefaultGroovyMethods.asString(t)
+                    );
+                }
+
+                return null;
+            }
+        });
     }
 }
diff --git a/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/VerifyClass.java
b/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/VerifyClass.java
index f660d40..975c4c5 100644
--- a/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/VerifyClass.java
+++ b/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/VerifyClass.java
@@ -31,9 +31,11 @@ import org.objectweb.asm.tree.analysis.SimpleVerifier;
 import org.objectweb.asm.util.CheckClassAdapter;
 import org.objectweb.asm.util.TraceMethodVisitor;
 
+import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.List;
 
 /**
@@ -94,18 +96,24 @@ public class VerifyClass extends MatchingTask {
     }
 
     private boolean readClass(String clazz) throws IOException {
-        ClassReader cr = new ClassReader(new FileInputStream(clazz));
-        ClassNode ca = new ClassNode() {
-            public void visitEnd() {
-                //accept(cv);
-            }
-        };
-        cr.accept(new CheckClassAdapter(ca), ClassWriter.COMPUTE_MAXS);
-        boolean failed = false;
+        ClassNode ca;
+        try (final InputStream inputStream =
+                     new BufferedInputStream(
+                             new FileInputStream(clazz))) {
+            ClassReader cr = new ClassReader(inputStream);
+            ca = new ClassNode() {
+                @Override
+                public void visitEnd() {
+                    //accept(cv);
+                }
+            };
+            cr.accept(new CheckClassAdapter(ca), ClassWriter.COMPUTE_MAXS);
+        }
 
-        List methods = ca.methods;
-        for (int i = 0; i < methods.size(); ++i) {
-            MethodNode method = (MethodNode) methods.get(i);
+        boolean failed = false;
+        List<MethodNode> methods = ca.methods;
+        for (int i = 0, n = methods.size(); i < n; ++i) {
+            MethodNode method = methods.get(i);
             if (method.instructions.size() > 0) {
                 Analyzer a = new Analyzer(new SimpleVerifier());
                 try {


Mime
View raw message