You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2018/04/24 06:42:08 UTC
groovy git commit: Add DGM `whichJar` to get the url of the jar
containing the specified class(closes #690)
Repository: groovy
Updated Branches:
refs/heads/master fc65d78d8 -> bf8c65517
Add DGM `whichJar` to get the url of the jar containing the specified class(closes #690)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/bf8c6551
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/bf8c6551
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/bf8c6551
Branch: refs/heads/master
Commit: bf8c655179471c0a703ce9f9c13a880bb06e9a4a
Parents: fc65d78
Author: Daniel Sun <re...@hotmail.com>
Authored: Tue Apr 24 12:58:31 2018 +0800
Committer: sunlan <su...@apache.org>
Committed: Tue Apr 24 14:42:04 2018 +0800
----------------------------------------------------------------------
.../groovy/runtime/DefaultGroovyMethods.java | 18 ++++++++++++++++++
.../runtime/DefaultGroovyMethodsTest.groovy | 4 ++++
2 files changed, 22 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/bf8c6551/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 4d4e3f6..1ef341f 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -108,7 +108,9 @@ import java.lang.reflect.Proxy;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
+import java.net.URL;
import java.security.AccessController;
+import java.security.CodeSource;
import java.security.PrivilegedAction;
import java.text.MessageFormat;
import java.util.AbstractCollection;
@@ -598,6 +600,22 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
}
/**
+ * Gets the url of the jar containing the specified class
+ *
+ * @param self the class
+ * @return the url of the jar, {@code null} if the specified class is from JDK
+ */
+ public static URL whichJar(Class self) {
+ CodeSource codeSource = self.getProtectionDomain().getCodeSource();
+
+ if (null == codeSource) {
+ return null;
+ }
+
+ return codeSource.getLocation();
+ }
+
+ /**
* Extend class globally with category methods.
*
* @param self any Class
http://git-wip-us.apache.org/repos/asf/groovy/blob/bf8c6551/src/test/org/codehaus/groovy/runtime/DefaultGroovyMethodsTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/runtime/DefaultGroovyMethodsTest.groovy b/src/test/org/codehaus/groovy/runtime/DefaultGroovyMethodsTest.groovy
index e332407..8ad57c4 100644
--- a/src/test/org/codehaus/groovy/runtime/DefaultGroovyMethodsTest.groovy
+++ b/src/test/org/codehaus/groovy/runtime/DefaultGroovyMethodsTest.groovy
@@ -308,4 +308,8 @@ class DefaultGroovyMethodsTest extends GroovyTestCase {
assertFalse(DefaultGroovyMethods.implies(true, null))
}
+ void testWhichJar() {
+ assert DefaultGroovyMethods.whichJar(org.objectweb.asm.Opcodes).getFile().matches(/(.+\/)?asm[-].+\.jar/)
+ assert null == DefaultGroovyMethods.whichJar(String)
+ }
}