changeset 55133:c431ab7f9e85

8218913: Rename --strip-debug jlink plugin Reviewed-by: alanb, mchung
author sgehwolf
date Wed, 13 Feb 2019 12:01:09 +0100
parents 00fcc1ef31e8
children 09cba396916f
files src/jdk.jlink/share/classes/jdk/tools/jlink/internal/TaskHelper.java src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/DefaultStripDebugPlugin.java src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/StripDebugPlugin.java src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/StripJavaDebugAttributesPlugin.java src/jdk.jlink/share/classes/jdk/tools/jlink/resources/plugins.properties src/jdk.jlink/share/classes/module-info.java test/jdk/tools/jlink/IntegrationTest.java test/jdk/tools/jlink/plugins/StripDebugPluginTest.java test/jdk/tools/jlink/plugins/StripJavaDebugAttributesPluginTest.java
diffstat 9 files changed, 317 insertions(+), 254 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/TaskHelper.java	Tue Feb 26 11:38:07 2019 +0100
+++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/TaskHelper.java	Wed Feb 13 12:01:09 2019 +0100
@@ -49,9 +49,9 @@
 import jdk.tools.jlink.builder.ImageBuilder;
 import jdk.tools.jlink.internal.Jlink.PluginsConfiguration;
 import jdk.tools.jlink.internal.plugins.DefaultCompressPlugin;
+import jdk.tools.jlink.internal.plugins.DefaultStripDebugPlugin;
 import jdk.tools.jlink.internal.plugins.ExcludeJmodSectionPlugin;
 import jdk.tools.jlink.internal.plugins.PluginsResourceBundle;
-import jdk.tools.jlink.internal.plugins.StripDebugPlugin;
 import jdk.tools.jlink.plugin.Plugin;
 import jdk.tools.jlink.plugin.Plugin.Category;
 import jdk.tools.jlink.plugin.PluginException;
@@ -375,7 +375,7 @@
                                 m.put(DefaultCompressPlugin.NAME, DefaultCompressPlugin.LEVEL_2);
                             }, false, "--compress", "-c");
                     mainOptions.add(plugOption);
-                } else if (plugin instanceof StripDebugPlugin) {
+                } else if (plugin instanceof DefaultStripDebugPlugin) {
                     plugOption
                         = new PluginOption(false,
                             (task, opt, arg) -> {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/DefaultStripDebugPlugin.java	Wed Feb 13 12:01:09 2019 +0100
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2019, Red Hat, Inc.
+ * 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 jdk.tools.jlink.internal.plugins;
+
+import jdk.tools.jlink.plugin.Plugin;
+import jdk.tools.jlink.plugin.ResourcePool;
+import jdk.tools.jlink.plugin.ResourcePoolBuilder;
+
+/**
+ * Combined debug stripping plugin: Java debug attributes and native debug
+ * symbols.
+ *
+ */
+public final class DefaultStripDebugPlugin implements Plugin {
+
+    public static final String NAME = "strip-debug";
+
+    private final Plugin javaStripPlugin = new StripJavaDebugAttributesPlugin();
+
+    @Override
+    public String getName() {
+        return NAME;
+    }
+
+    @Override
+    public String getDescription() {
+        return PluginsResourceBundle.getDescription(NAME);
+    }
+
+    @Override
+    public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {
+        return javaStripPlugin.transform(in, out);
+    }
+
+}
--- a/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/StripDebugPlugin.java	Tue Feb 26 11:38:07 2019 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,87 +0,0 @@
-/*
- * Copyright (c) 2015, 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 jdk.tools.jlink.internal.plugins;
-
-import java.util.function.Predicate;
-import jdk.internal.org.objectweb.asm.ClassReader;
-import jdk.internal.org.objectweb.asm.ClassWriter;
-import jdk.tools.jlink.plugin.ResourcePool;
-import jdk.tools.jlink.plugin.ResourcePoolBuilder;
-import jdk.tools.jlink.plugin.ResourcePoolEntry;
-import jdk.tools.jlink.plugin.Plugin;
-
-/**
- *
- * Strip debug attributes plugin
- */
-public final class StripDebugPlugin implements Plugin {
-    public static final String NAME = "strip-debug";
-    private final Predicate<String> predicate;
-
-    public StripDebugPlugin() {
-        this((path) -> false);
-    }
-
-    StripDebugPlugin(Predicate<String> predicate) {
-        this.predicate = predicate;
-    }
-
-    @Override
-    public String getName() {
-        return NAME;
-    }
-
-    @Override
-    public String getDescription() {
-        return PluginsResourceBundle.getDescription(NAME);
-    }
-
-    @Override
-    public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {
-        //remove *.diz files as well as debug attributes.
-        in.transformAndCopy((resource) -> {
-            ResourcePoolEntry res = resource;
-            if (resource.type().equals(ResourcePoolEntry.Type.CLASS_OR_RESOURCE)) {
-                String path = resource.path();
-                if (path.endsWith(".class")) {
-                    if (path.endsWith("module-info.class")) {
-                        // XXX. Do we have debug info? Is Asm ready for module-info?
-                    } else {
-                        ClassReader reader = new ClassReader(resource.contentBytes());
-                        ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
-                        reader.accept(writer, ClassReader.SKIP_DEBUG);
-                        byte[] content = writer.toByteArray();
-                        res = resource.copyWithContent(content);
-                    }
-                }
-            } else if (predicate.test(res.path())) {
-                res = null;
-            }
-            return res;
-        }, out);
-
-        return out.build();
-    }
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/StripJavaDebugAttributesPlugin.java	Wed Feb 13 12:01:09 2019 +0100
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2015, 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 jdk.tools.jlink.internal.plugins;
+
+import java.util.function.Predicate;
+
+import jdk.internal.org.objectweb.asm.ClassReader;
+import jdk.internal.org.objectweb.asm.ClassWriter;
+import jdk.tools.jlink.plugin.Plugin;
+import jdk.tools.jlink.plugin.ResourcePool;
+import jdk.tools.jlink.plugin.ResourcePoolBuilder;
+import jdk.tools.jlink.plugin.ResourcePoolEntry;
+
+/**
+ *
+ * Strip java debug attributes plugin
+ */
+public final class StripJavaDebugAttributesPlugin implements Plugin {
+    public static final String NAME = "strip-java-debug-attributes";
+    private final Predicate<String> predicate;
+
+    public StripJavaDebugAttributesPlugin() {
+        this((path) -> false);
+    }
+
+    StripJavaDebugAttributesPlugin(Predicate<String> predicate) {
+        this.predicate = predicate;
+    }
+
+    @Override
+    public String getName() {
+        return NAME;
+    }
+
+    @Override
+    public String getDescription() {
+        return PluginsResourceBundle.getDescription(NAME);
+    }
+
+    @Override
+    public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {
+        //remove *.diz files as well as debug attributes.
+        in.transformAndCopy((resource) -> {
+            ResourcePoolEntry res = resource;
+            if (resource.type().equals(ResourcePoolEntry.Type.CLASS_OR_RESOURCE)) {
+                String path = resource.path();
+                if (path.endsWith(".class")) {
+                    if (path.endsWith("module-info.class")) {
+                        // XXX. Do we have debug info? Is Asm ready for module-info?
+                    } else {
+                        ClassReader reader = new ClassReader(resource.contentBytes());
+                        ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
+                        reader.accept(writer, ClassReader.SKIP_DEBUG);
+                        byte[] content = writer.toByteArray();
+                        res = resource.copyWithContent(content);
+                    }
+                }
+            } else if (predicate.test(res.path())) {
+                res = null;
+            }
+            return res;
+        }, out);
+
+        return out.build();
+    }
+}
--- a/src/jdk.jlink/share/classes/jdk/tools/jlink/resources/plugins.properties	Tue Feb 26 11:38:07 2019 +0100
+++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/resources/plugins.properties	Wed Feb 13 12:01:09 2019 +0100
@@ -99,6 +99,9 @@
 strip-debug.description=\
 Strip debug information from the output image
 
+strip-java-debug-attributes.description=\
+Strip Java debug attributes from classes in the output image
+
 strip-native-commands.description=\
 Exclude native commands (such as java/java.exe) from the image
 
--- a/src/jdk.jlink/share/classes/module-info.java	Tue Feb 26 11:38:07 2019 +0100
+++ b/src/jdk.jlink/share/classes/module-info.java	Wed Feb 13 12:01:09 2019 +0100
@@ -63,7 +63,8 @@
         jdk.tools.jlink.internal.Main.JlinkToolProvider;
 
     provides jdk.tools.jlink.plugin.Plugin with
-        jdk.tools.jlink.internal.plugins.StripDebugPlugin,
+        jdk.tools.jlink.internal.plugins.DefaultStripDebugPlugin,
+        jdk.tools.jlink.internal.plugins.StripJavaDebugAttributesPlugin,
         jdk.tools.jlink.internal.plugins.ExcludePlugin,
         jdk.tools.jlink.internal.plugins.ExcludeFilesPlugin,
         jdk.tools.jlink.internal.plugins.ExcludeJmodSectionPlugin,
--- a/test/jdk/tools/jlink/IntegrationTest.java	Tue Feb 26 11:38:07 2019 +0100
+++ b/test/jdk/tools/jlink/IntegrationTest.java	Wed Feb 13 12:01:09 2019 +0100
@@ -49,7 +49,7 @@
 import jdk.tools.jlink.internal.Jlink.PluginsConfiguration;
 import jdk.tools.jlink.internal.PostProcessor;
 import jdk.tools.jlink.internal.plugins.DefaultCompressPlugin;
-import jdk.tools.jlink.internal.plugins.StripDebugPlugin;
+import jdk.tools.jlink.internal.plugins.DefaultStripDebugPlugin;
 
 import tests.Helper;
 import tests.JImageGenerator;
@@ -168,7 +168,7 @@
         //Strip debug
         {
             Map<String, String> config1 = new HashMap<>();
-            config1.put(StripDebugPlugin.NAME, "");
+            config1.put(DefaultStripDebugPlugin.NAME, "");
             Plugin strip = Jlink.newPlugin("strip-debug", config1, null);
             lst.add(strip);
         }
--- a/test/jdk/tools/jlink/plugins/StripDebugPluginTest.java	Tue Feb 26 11:38:07 2019 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,162 +0,0 @@
-/*
- * Copyright (c) 2015, 2018, 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.
- *
- * 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.
- */
-
-/*
- * @test
- * @summary Test StripDebugPlugin
- * @author Jean-Francois Denise
- * @library ../../lib
- * @build tests.*
- * @modules java.base/jdk.internal.jimage
- *          jdk.jlink/jdk.tools.jlink.internal
- *          jdk.jlink/jdk.tools.jlink.internal.plugins
- *          jdk.jlink/jdk.tools.jlink.plugin
- *          jdk.jlink/jdk.tools.jimage
- *          jdk.jlink/jdk.tools.jmod
- *          jdk.jdeps/com.sun.tools.classfile
- *          jdk.compiler
- * @run main StripDebugPluginTest
- */
-
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Iterator;
-import java.util.List;
-import java.util.stream.Stream;
-
-import com.sun.tools.classfile.Attribute;
-import com.sun.tools.classfile.ClassFile;
-import com.sun.tools.classfile.Code_attribute;
-import com.sun.tools.classfile.ConstantPoolException;
-import com.sun.tools.classfile.Method;
-import java.util.HashMap;
-import java.util.Map;
-import jdk.tools.jlink.internal.ResourcePoolManager;
-import jdk.tools.jlink.internal.plugins.StripDebugPlugin;
-import jdk.tools.jlink.plugin.ResourcePoolEntry;
-import jdk.tools.jlink.plugin.ResourcePool;
-import jdk.tools.jlink.plugin.Plugin;
-import tests.Helper;
-
-public class StripDebugPluginTest {
-    public static void main(String[] args) throws Exception {
-        new StripDebugPluginTest().test();
-    }
-
-    public void test() throws Exception {
-        Helper helper = Helper.newHelper();
-        if (helper == null) {
-            // Skip test if the jmods directory is missing (e.g. exploded image)
-            System.err.println("Test not run, NO jmods directory");
-            return;
-        }
-
-        List<String> classes = Arrays.asList("toto.Main", "toto.com.foo.bar.X");
-        Path moduleFile = helper.generateModuleCompiledClasses(
-                helper.getJmodSrcDir(), helper.getJmodClassesDir(), "leaf1", classes);
-        Path moduleInfo = moduleFile.resolve("module-info.class");
-
-        // Classes have been compiled in debug.
-        List<Path> covered = new ArrayList<>();
-        byte[] infoContent = Files.readAllBytes(moduleInfo);
-        try (Stream<Path> stream = Files.walk(moduleFile)) {
-            for (Iterator<Path> iterator = stream.iterator(); iterator.hasNext(); ) {
-                Path p = iterator.next();
-                if (Files.isRegularFile(p) && p.toString().endsWith(".class")) {
-                    byte[] content = Files.readAllBytes(p);
-                    String path = "/" + helper.getJmodClassesDir().relativize(p).toString();
-                    String moduleInfoPath = path + "/module-info.class";
-                    check(path, content, moduleInfoPath, infoContent);
-                    covered.add(p);
-                }
-            }
-        }
-        if (covered.isEmpty()) {
-            throw new AssertionError("No class to compress");
-        } else {
-            System.err.println("removed debug attributes from "
-                    + covered.size() + " classes");
-        }
-    }
-
-    private void check(String path, byte[] content, String infoPath, byte[] moduleInfo) throws Exception {
-        path = path.replace('\\', '/');
-        StripDebugPlugin debug = new StripDebugPlugin();
-        debug.configure(new HashMap<>());
-        ResourcePoolEntry result1 = stripDebug(debug, ResourcePoolEntry.create(path,content), path, infoPath, moduleInfo);
-
-        if (!path.endsWith("module-info.class")) {
-            if (result1.contentLength() >= content.length) {
-                throw new AssertionError("Class size not reduced, debug info not "
-                        + "removed for " + path);
-            }
-            checkDebugAttributes(result1.contentBytes());
-        }
-
-        ResourcePoolEntry result2 = stripDebug(debug, result1, path, infoPath, moduleInfo);
-        if (result1.contentLength() != result2.contentLength()) {
-            throw new AssertionError("removing debug info twice reduces class size of "
-                    + path);
-        }
-        checkDebugAttributes(result1.contentBytes());
-    }
-
-    private ResourcePoolEntry stripDebug(Plugin debug, ResourcePoolEntry classResource,
-            String path, String infoPath, byte[] moduleInfo) throws Exception {
-        ResourcePoolManager resources = new ResourcePoolManager();
-        resources.add(classResource);
-        if (!path.endsWith("module-info.class")) {
-            ResourcePoolEntry res2 = ResourcePoolEntry.create(infoPath, moduleInfo);
-            resources.add(res2);
-        }
-        ResourcePoolManager results = new ResourcePoolManager();
-        ResourcePool resPool = debug.transform(resources.resourcePool(),
-                results.resourcePoolBuilder());
-        System.out.println(classResource.path());
-
-        return resPool.findEntry(classResource.path()).get();
-    }
-
-    private void checkDebugAttributes(byte[] strippedClassFile) throws IOException, ConstantPoolException {
-        ClassFile classFile = ClassFile.read(new ByteArrayInputStream(strippedClassFile));
-        String[] debugAttributes = new String[]{
-                Attribute.LineNumberTable,
-                Attribute.LocalVariableTable,
-                Attribute.LocalVariableTypeTable
-        };
-        for (Method method : classFile.methods) {
-            String methodName = method.getName(classFile.constant_pool);
-            Code_attribute code = (Code_attribute) method.attributes.get(Attribute.Code);
-            for (String attr : debugAttributes) {
-                if (code.attributes.get(attr) != null) {
-                    throw new AssertionError("Debug attribute was not removed: " + attr +
-                            " from method " + classFile.getName() + "#" + methodName);
-                }
-            }
-        }
-    }
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/tools/jlink/plugins/StripJavaDebugAttributesPluginTest.java	Wed Feb 13 12:01:09 2019 +0100
@@ -0,0 +1,162 @@
+/*
+ * Copyright (c) 2015, 2018, 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.
+ *
+ * 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.
+ */
+
+/*
+ * @test
+ * @summary Test StripJavaDebugAttributesPlugin
+ * @author Jean-Francois Denise
+ * @library ../../lib
+ * @build tests.*
+ * @modules java.base/jdk.internal.jimage
+ *          jdk.jlink/jdk.tools.jlink.internal
+ *          jdk.jlink/jdk.tools.jlink.internal.plugins
+ *          jdk.jlink/jdk.tools.jlink.plugin
+ *          jdk.jlink/jdk.tools.jimage
+ *          jdk.jlink/jdk.tools.jmod
+ *          jdk.jdeps/com.sun.tools.classfile
+ *          jdk.compiler
+ * @run main StripJavaDebugAttributesPluginTest
+ */
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.stream.Stream;
+
+import com.sun.tools.classfile.Attribute;
+import com.sun.tools.classfile.ClassFile;
+import com.sun.tools.classfile.Code_attribute;
+import com.sun.tools.classfile.ConstantPoolException;
+import com.sun.tools.classfile.Method;
+
+import jdk.tools.jlink.internal.ResourcePoolManager;
+import jdk.tools.jlink.internal.plugins.StripJavaDebugAttributesPlugin;
+import jdk.tools.jlink.plugin.Plugin;
+import jdk.tools.jlink.plugin.ResourcePool;
+import jdk.tools.jlink.plugin.ResourcePoolEntry;
+import tests.Helper;
+
+public class StripJavaDebugAttributesPluginTest {
+    public static void main(String[] args) throws Exception {
+        new StripJavaDebugAttributesPluginTest().test();
+    }
+
+    public void test() throws Exception {
+        Helper helper = Helper.newHelper();
+        if (helper == null) {
+            // Skip test if the jmods directory is missing (e.g. exploded image)
+            System.err.println("Test not run, NO jmods directory");
+            return;
+        }
+
+        List<String> classes = Arrays.asList("toto.Main", "toto.com.foo.bar.X");
+        Path moduleFile = helper.generateModuleCompiledClasses(
+                helper.getJmodSrcDir(), helper.getJmodClassesDir(), "leaf1", classes);
+        Path moduleInfo = moduleFile.resolve("module-info.class");
+
+        // Classes have been compiled in debug.
+        List<Path> covered = new ArrayList<>();
+        byte[] infoContent = Files.readAllBytes(moduleInfo);
+        try (Stream<Path> stream = Files.walk(moduleFile)) {
+            for (Iterator<Path> iterator = stream.iterator(); iterator.hasNext(); ) {
+                Path p = iterator.next();
+                if (Files.isRegularFile(p) && p.toString().endsWith(".class")) {
+                    byte[] content = Files.readAllBytes(p);
+                    String path = "/" + helper.getJmodClassesDir().relativize(p).toString();
+                    String moduleInfoPath = path + "/module-info.class";
+                    check(path, content, moduleInfoPath, infoContent);
+                    covered.add(p);
+                }
+            }
+        }
+        if (covered.isEmpty()) {
+            throw new AssertionError("No class to compress");
+        } else {
+            System.err.println("removed debug attributes from "
+                    + covered.size() + " classes");
+        }
+    }
+
+    private void check(String path, byte[] content, String infoPath, byte[] moduleInfo) throws Exception {
+        path = path.replace('\\', '/');
+        StripJavaDebugAttributesPlugin debug = new StripJavaDebugAttributesPlugin();
+        debug.configure(new HashMap<>());
+        ResourcePoolEntry result1 = stripDebug(debug, ResourcePoolEntry.create(path,content), path, infoPath, moduleInfo);
+
+        if (!path.endsWith("module-info.class")) {
+            if (result1.contentLength() >= content.length) {
+                throw new AssertionError("Class size not reduced, debug info not "
+                        + "removed for " + path);
+            }
+            checkDebugAttributes(result1.contentBytes());
+        }
+
+        ResourcePoolEntry result2 = stripDebug(debug, result1, path, infoPath, moduleInfo);
+        if (result1.contentLength() != result2.contentLength()) {
+            throw new AssertionError("removing debug info twice reduces class size of "
+                    + path);
+        }
+        checkDebugAttributes(result1.contentBytes());
+    }
+
+    private ResourcePoolEntry stripDebug(Plugin debug, ResourcePoolEntry classResource,
+            String path, String infoPath, byte[] moduleInfo) throws Exception {
+        ResourcePoolManager resources = new ResourcePoolManager();
+        resources.add(classResource);
+        if (!path.endsWith("module-info.class")) {
+            ResourcePoolEntry res2 = ResourcePoolEntry.create(infoPath, moduleInfo);
+            resources.add(res2);
+        }
+        ResourcePoolManager results = new ResourcePoolManager();
+        ResourcePool resPool = debug.transform(resources.resourcePool(),
+                results.resourcePoolBuilder());
+        System.out.println(classResource.path());
+
+        return resPool.findEntry(classResource.path()).get();
+    }
+
+    private void checkDebugAttributes(byte[] strippedClassFile) throws IOException, ConstantPoolException {
+        ClassFile classFile = ClassFile.read(new ByteArrayInputStream(strippedClassFile));
+        String[] debugAttributes = new String[]{
+                Attribute.LineNumberTable,
+                Attribute.LocalVariableTable,
+                Attribute.LocalVariableTypeTable
+        };
+        for (Method method : classFile.methods) {
+            String methodName = method.getName(classFile.constant_pool);
+            Code_attribute code = (Code_attribute) method.attributes.get(Attribute.Code);
+            for (String attr : debugAttributes) {
+                if (code.attributes.get(attr) != null) {
+                    throw new AssertionError("Debug attribute was not removed: " + attr +
+                            " from method " + classFile.getName() + "#" + methodName);
+                }
+            }
+        }
+    }
+}