changeset 19439:1655ceeb9ba3

Instrumentation API needs to allow for non-modifiable modules
author alanb
date Mon, 03 Apr 2017 18:52:00 +0100
parents 4205f4220e41
children 50b26c0e7cd8
files src/java.instrument/share/classes/java/lang/instrument/Instrumentation.java src/java.instrument/share/classes/java/lang/instrument/UnmodifiableModuleException.java src/java.instrument/share/classes/sun/instrument/InstrumentationImpl.java test/java/lang/instrument/RedefineModuleAgent.java test/java/lang/instrument/RedefineModuleTest.java
diffstat 5 files changed, 105 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.instrument/share/classes/java/lang/instrument/Instrumentation.java	Mon Apr 03 14:57:56 2017 +0100
+++ b/src/java.instrument/share/classes/java/lang/instrument/Instrumentation.java	Mon Apr 03 18:52:00 2017 +0100
@@ -345,7 +345,7 @@
 
 
     /**
-     * Determines whether a class is modifiable by
+     * Tests whether a class is modifiable by
      * {@linkplain #retransformClasses retransformation}
      * or {@linkplain #redefineClasses redefinition}.
      * If a class is modifiable then this method returns <code>true</code>.
@@ -710,8 +710,11 @@
      *         {@code extraProvides} map contains a service provider type that
      *         is not a member of the module or an implementation of the service;
      *         or {@code extraProvides} maps a key to an empty list
+     * @throws UnmodifiableModuleException if the module cannot be modified
      * @throws NullPointerException if any of the arguments are {@code null} or
      *         any of the Sets or Maps contains a {@code null} key or value
+     *
+     * @see #isModifiableModule(Module)
      * @since 9
      * @spec JPMS
      */
@@ -721,4 +724,19 @@
                         Map<String, Set<Module>> extraOpens,
                         Set<Class<?>> extraUses,
                         Map<Class<?>, List<Class<?>>> extraProvides);
+
+    /**
+     * Tests whether a module can be modified with {@link #redefineModule
+     * redefineModule}. If a module is modifiable then this method returns
+     * {@code true}. If a module is not modifiable then this method returns
+     * {@code false}.
+     *
+     * @param module the class to check for being modifiable
+     * @return {@code true} if the module is modifiable, otherwise {@code false}
+     * @throws NullPointerException if the module is {@code null}
+     *
+     * @since 9
+     * @spec JPMS
+     */
+    boolean isModifiableModule(Module module);
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/java.instrument/share/classes/java/lang/instrument/UnmodifiableModuleException.java	Mon Apr 03 18:52:00 2017 +0100
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package java.lang.instrument;
+
+/**
+ * Thrown to indicate that a module cannot be modified.
+ *
+ * @see Instrumentation#redefineModule
+ * @since 9
+ * @spec JPMS
+ */
+
+public class UnmodifiableModuleException extends RuntimeException {
+    private static final long serialVersionUID = 6912511912351080644L;
+
+    /**
+     * Constructs an {@code UnmodifiableModuleException} with no
+     * detail message.
+     */
+    public UnmodifiableModuleException() {
+        super();
+    }
+
+    /**
+     * Constructs an {@code UnmodifiableModuleException} with the
+     * specified detail message.
+     *
+     * @param  msg the detail message.
+     */
+    public UnmodifiableModuleException(String msg) {
+        super(msg);
+    }
+}
--- a/src/java.instrument/share/classes/sun/instrument/InstrumentationImpl.java	Mon Apr 03 14:57:56 2017 +0100
+++ b/src/java.instrument/share/classes/sun/instrument/InstrumentationImpl.java	Mon Apr 03 18:52:00 2017 +0100
@@ -130,6 +130,13 @@
         return isModifiableClass0(mNativeAgent, theClass);
     }
 
+    public boolean isModifiableModule(Module module) {
+        if (module == null) {
+            throw new NullPointerException("'module' is null");
+        }
+        return true;
+    }
+
     public boolean
     isRetransformClassesSupported() {
         // ask lazily since there is some overhead
--- a/test/java/lang/instrument/RedefineModuleAgent.java	Mon Apr 03 14:57:56 2017 +0100
+++ b/test/java/lang/instrument/RedefineModuleAgent.java	Mon Apr 03 18:52:00 2017 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -46,4 +46,8 @@
                                Map<Class<?>, List<Class<?>>> extraProvides) {
         inst.redefineModule(module, extraReads, extraExports, extraOpens, extraUses, extraProvides);
     }
+
+    static boolean isModifiableModule(Module module) {
+        return inst.isModifiableModule(module);
+    }
 }
--- a/test/java/lang/instrument/RedefineModuleTest.java	Mon Apr 03 14:57:56 2017 +0100
+++ b/test/java/lang/instrument/RedefineModuleTest.java	Mon Apr 03 18:52:00 2017 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -66,6 +66,10 @@
                                            extraProvides);
     }
 
+    static boolean isModifiableModule(Module module) {
+        return RedefineModuleAgent.isModifiableModule(module);
+    }
+
 
     /**
      * Use redefineModule to update java.base to read java.instrument
@@ -277,6 +281,19 @@
     }
 
     /**
+     * Exercise IsModifiableModule
+     */
+    @Test
+    public void testIsModifiableModule() {
+        ClassLoader pcl = ClassLoader.getPlatformClassLoader();
+        ClassLoader scl = ClassLoader.getSystemClassLoader();
+        assertTrue(isModifiableModule(pcl.getUnnamedModule()));
+        assertTrue(isModifiableModule(scl.getUnnamedModule()));
+        assertTrue(isModifiableModule(RedefineModuleTest.class.getModule()));
+        assertTrue(isModifiableModule(Object.class.getModule()));
+    }
+
+    /**
      * Test redefineClass with null
      */
     public void testNulls() {