groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [groovy] 02/03: Handle resources robustly
Date Fri, 29 Nov 2019 05:36:21 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit 35d5a6ce505d2a9680d69a4007576e9bfeff37f7
Author: Daniel.Sun <realbluesun@hotmail.com>
AuthorDate: Fri Nov 29 12:19:41 2019 +0800

    Handle resources robustly
---
 .../codehaus/groovy/reflection/SunClassLoader.java | 12 +++++------
 .../runtime/callsite/GroovySunClassLoader.java     | 23 +++++++++++-----------
 2 files changed, 17 insertions(+), 18 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/reflection/SunClassLoader.java b/src/main/java/org/codehaus/groovy/reflection/SunClassLoader.java
index 6914585..b8e9c1c 100644
--- a/src/main/java/org/codehaus/groovy/reflection/SunClassLoader.java
+++ b/src/main/java/org/codehaus/groovy/reflection/SunClassLoader.java
@@ -79,12 +79,12 @@ public class SunClassLoader extends ClassLoader implements Opcodes {
     }
 
     protected void loadFromRes(String name) throws IOException {
-        final InputStream asStream = SunClassLoader.class.getClassLoader().getResourceAsStream(resName(name));
-        ClassReader reader = new ClassReader(asStream);
-        final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
-        reader.accept(cw, ClassWriter.COMPUTE_MAXS);
-        asStream.close();
-        define(cw.toByteArray(), name);
+        try (final InputStream asStream = SunClassLoader.class.getClassLoader().getResourceAsStream(resName(name)))
{
+            ClassReader reader = new ClassReader(asStream);
+            final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
+            reader.accept(cw, ClassWriter.COMPUTE_MAXS);
+            define(cw.toByteArray(), name);
+        }
     }
 
     protected static String resName(String s) {
diff --git a/src/main/java/org/codehaus/groovy/runtime/callsite/GroovySunClassLoader.java
b/src/main/java/org/codehaus/groovy/runtime/callsite/GroovySunClassLoader.java
index 7b9e9ca..07a39d1 100644
--- a/src/main/java/org/codehaus/groovy/runtime/callsite/GroovySunClassLoader.java
+++ b/src/main/java/org/codehaus/groovy/runtime/callsite/GroovySunClassLoader.java
@@ -57,17 +57,16 @@ public class GroovySunClassLoader extends SunClassLoader {
     }
 
     private void loadAbstract() throws IOException {
-        final InputStream asStream = GroovySunClassLoader.class.getClassLoader().getResourceAsStream(resName("org.codehaus.groovy.runtime.callsite.AbstractCallSite"));
-        ClassReader reader = new ClassReader(asStream);
-        final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
-        final ClassVisitor cv = new ClassVisitor(CompilerConfiguration.ASM_API_VERSION, cw)
{
-            public void visit(int version, int access, String name, String signature, String
superName, String[] interfaces) {
-                super.visit(version, access, name, signature, "sun/reflect/GroovyMagic",
interfaces);
-            }            
-        };
-        reader.accept(cv, ClassWriter.COMPUTE_MAXS);
-        asStream.close();
-        define(cw.toByteArray(), "org.codehaus.groovy.runtime.callsite.AbstractCallSite");
+        try (final InputStream asStream = GroovySunClassLoader.class.getClassLoader().getResourceAsStream(resName("org.codehaus.groovy.runtime.callsite.AbstractCallSite")))
{
+            ClassReader reader = new ClassReader(asStream);
+            final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
+            final ClassVisitor cv = new ClassVisitor(CompilerConfiguration.ASM_API_VERSION,
cw) {
+                public void visit(int version, int access, String name, String signature,
String superName, String[] interfaces) {
+                    super.visit(version, access, name, signature, "sun/reflect/GroovyMagic",
interfaces);
+                }
+            };
+            reader.accept(cv, ClassWriter.COMPUTE_MAXS);
+            define(cw.toByteArray(), "org.codehaus.groovy.runtime.callsite.AbstractCallSite");
+        }
     }
-
 }


Mime
View raw message