src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/StripDebugPlugin.java
branchniosocketimpl-branch
changeset 57249 3e39753ed05b
parent 57248 48d523dfbdc9
parent 53950 cf47efcf7771
child 57250 b38f280d2114
equal deleted inserted replaced
57248:48d523dfbdc9 57249:3e39753ed05b
     1 /*
       
     2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 package jdk.tools.jlink.internal.plugins;
       
    26 
       
    27 import java.util.function.Predicate;
       
    28 import jdk.internal.org.objectweb.asm.ClassReader;
       
    29 import jdk.internal.org.objectweb.asm.ClassWriter;
       
    30 import jdk.tools.jlink.plugin.ResourcePool;
       
    31 import jdk.tools.jlink.plugin.ResourcePoolBuilder;
       
    32 import jdk.tools.jlink.plugin.ResourcePoolEntry;
       
    33 import jdk.tools.jlink.plugin.Plugin;
       
    34 
       
    35 /**
       
    36  *
       
    37  * Strip debug attributes plugin
       
    38  */
       
    39 public final class StripDebugPlugin implements Plugin {
       
    40     public static final String NAME = "strip-debug";
       
    41     private final Predicate<String> predicate;
       
    42 
       
    43     public StripDebugPlugin() {
       
    44         this((path) -> false);
       
    45     }
       
    46 
       
    47     StripDebugPlugin(Predicate<String> predicate) {
       
    48         this.predicate = predicate;
       
    49     }
       
    50 
       
    51     @Override
       
    52     public String getName() {
       
    53         return NAME;
       
    54     }
       
    55 
       
    56     @Override
       
    57     public String getDescription() {
       
    58         return PluginsResourceBundle.getDescription(NAME);
       
    59     }
       
    60 
       
    61     @Override
       
    62     public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {
       
    63         //remove *.diz files as well as debug attributes.
       
    64         in.transformAndCopy((resource) -> {
       
    65             ResourcePoolEntry res = resource;
       
    66             if (resource.type().equals(ResourcePoolEntry.Type.CLASS_OR_RESOURCE)) {
       
    67                 String path = resource.path();
       
    68                 if (path.endsWith(".class")) {
       
    69                     if (path.endsWith("module-info.class")) {
       
    70                         // XXX. Do we have debug info? Is Asm ready for module-info?
       
    71                     } else {
       
    72                         ClassReader reader = new ClassReader(resource.contentBytes());
       
    73                         ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
       
    74                         reader.accept(writer, ClassReader.SKIP_DEBUG);
       
    75                         byte[] content = writer.toByteArray();
       
    76                         res = resource.copyWithContent(content);
       
    77                     }
       
    78                 }
       
    79             } else if (predicate.test(res.path())) {
       
    80                 res = null;
       
    81             }
       
    82             return res;
       
    83         }, out);
       
    84 
       
    85         return out.build();
       
    86     }
       
    87 }