jdk/test/tools/jlink/plugins/StripDebugPluginTest.java
changeset 38320 e24c7029e8ba
parent 36511 9d0388c6b336
child 39321 c60f34e8c057
equal deleted inserted replaced
37948:caf97b37ebec 38320:e24c7029e8ba
    52 import com.sun.tools.classfile.Code_attribute;
    52 import com.sun.tools.classfile.Code_attribute;
    53 import com.sun.tools.classfile.ConstantPoolException;
    53 import com.sun.tools.classfile.ConstantPoolException;
    54 import com.sun.tools.classfile.Method;
    54 import com.sun.tools.classfile.Method;
    55 import java.util.HashMap;
    55 import java.util.HashMap;
    56 import java.util.Map;
    56 import java.util.Map;
    57 import jdk.tools.jlink.internal.PoolImpl;
    57 import jdk.tools.jlink.internal.ModulePoolImpl;
    58 import jdk.tools.jlink.internal.plugins.StripDebugPlugin;
    58 import jdk.tools.jlink.internal.plugins.StripDebugPlugin;
    59 import jdk.tools.jlink.plugin.Pool;
    59 import jdk.tools.jlink.plugin.ModuleEntry;
    60 import jdk.tools.jlink.plugin.Pool.ModuleData;
    60 import jdk.tools.jlink.plugin.ModulePool;
    61 import jdk.tools.jlink.plugin.TransformerPlugin;
    61 import jdk.tools.jlink.plugin.TransformerPlugin;
    62 import tests.Helper;
    62 import tests.Helper;
    63 
    63 
    64 public class StripDebugPluginTest {
    64 public class StripDebugPluginTest {
    65     public static void main(String[] args) throws Exception {
    65     public static void main(String[] args) throws Exception {
   104 
   104 
   105     private void check(String path, byte[] content, String infoPath, byte[] moduleInfo) throws Exception {
   105     private void check(String path, byte[] content, String infoPath, byte[] moduleInfo) throws Exception {
   106         path = path.replace('\\', '/');
   106         path = path.replace('\\', '/');
   107         StripDebugPlugin debug = new StripDebugPlugin();
   107         StripDebugPlugin debug = new StripDebugPlugin();
   108         debug.configure(new HashMap<>());
   108         debug.configure(new HashMap<>());
   109         ModuleData result1 = stripDebug(debug, Pool.newResource(path,content), path, infoPath, moduleInfo);
   109         ModuleEntry result1 = stripDebug(debug, ModuleEntry.create(path,content), path, infoPath, moduleInfo);
   110 
   110 
   111         if (!path.endsWith("module-info.class")) {
   111         if (!path.endsWith("module-info.class")) {
   112             if (result1.getLength() >= content.length) {
   112             if (result1.getLength() >= content.length) {
   113                 throw new AssertionError("Class size not reduced, debug info not "
   113                 throw new AssertionError("Class size not reduced, debug info not "
   114                         + "removed for " + path);
   114                         + "removed for " + path);
   115             }
   115             }
   116             checkDebugAttributes(result1.getBytes());
   116             checkDebugAttributes(result1.getBytes());
   117         }
   117         }
   118 
   118 
   119         ModuleData result2 = stripDebug(debug, result1, path, infoPath, moduleInfo);
   119         ModuleEntry result2 = stripDebug(debug, result1, path, infoPath, moduleInfo);
   120         if (result1.getLength() != result2.getLength()) {
   120         if (result1.getLength() != result2.getLength()) {
   121             throw new AssertionError("removing debug info twice reduces class size of "
   121             throw new AssertionError("removing debug info twice reduces class size of "
   122                     + path);
   122                     + path);
   123         }
   123         }
   124         checkDebugAttributes(result1.getBytes());
   124         checkDebugAttributes(result1.getBytes());
   125     }
   125     }
   126 
   126 
   127     private ModuleData stripDebug(TransformerPlugin debug, ModuleData classResource,
   127     private ModuleEntry stripDebug(TransformerPlugin debug, ModuleEntry classResource,
   128             String path, String infoPath, byte[] moduleInfo) throws Exception {
   128             String path, String infoPath, byte[] moduleInfo) throws Exception {
   129         Pool resources = new PoolImpl();
   129         ModulePool resources = new ModulePoolImpl();
   130         resources.add(classResource);
   130         resources.add(classResource);
   131         if (!path.endsWith("module-info.class")) {
   131         if (!path.endsWith("module-info.class")) {
   132             ModuleData res2 = Pool.newResource(infoPath, moduleInfo);
   132             ModuleEntry res2 = ModuleEntry.create(infoPath, moduleInfo);
   133             resources.add(res2);
   133             resources.add(res2);
   134         }
   134         }
   135         Pool results = new PoolImpl();
   135         ModulePool results = new ModulePoolImpl();
   136         debug.visit(resources, results);
   136         debug.visit(resources, results);
   137         System.out.println(classResource.getPath());
   137         System.out.println(classResource.getPath());
   138         return results.get(classResource.getPath());
   138         return results.findEntry(classResource.getPath()).get();
   139     }
   139     }
   140 
   140 
   141     private void checkDebugAttributes(byte[] strippedClassFile) throws IOException, ConstantPoolException {
   141     private void checkDebugAttributes(byte[] strippedClassFile) throws IOException, ConstantPoolException {
   142         ClassFile classFile = ClassFile.read(new ByteArrayInputStream(strippedClassFile));
   142         ClassFile classFile = ClassFile.read(new ByteArrayInputStream(strippedClassFile));
   143         String[] debugAttributes = new String[]{
   143         String[] debugAttributes = new String[]{