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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * Asm plugin testing.
  26  * @test
  27  * @summary Test basic functionality.
  28  * @author Jean-Francois Denise
  29  * @modules java.base/jdk.internal.org.objectweb.asm
  30  *          jdk.jlink/jdk.tools.jlink.internal
  31  *          jdk.jlink/jdk.tools.jlink.internal.plugins.asm
  32  * @build AsmPluginTestBase
  33  * @run main IdentityPluginTest
  34  */
  35 
  36 import java.io.ByteArrayInputStream;
  37 import java.io.IOException;
  38 import java.io.UncheckedIOException;
  39 
  40 import jdk.internal.org.objectweb.asm.ClassReader;
  41 import jdk.internal.org.objectweb.asm.ClassVisitor;
  42 import jdk.internal.org.objectweb.asm.ClassWriter;
  43 import jdk.internal.org.objectweb.asm.Opcodes;
  44 import jdk.tools.jlink.internal.plugins.asm.AsmPool.WritableClassPool;
  45 import jdk.tools.jlink.plugin.ModuleEntry;
  46 import jdk.tools.jlink.plugin.ModulePool;
  47 
  48 public class IdentityPluginTest extends AsmPluginTestBase {
  49 
  50     public static void main(String[] args) throws Exception {
  51         if (!isImageBuild()) {
  52             System.err.println("Test not run. Not image build.");
  53             return;
  54         }
  55         new IdentityPluginTest().test();
  56     }
  57 
  58     public void test() throws Exception {
  59         IdentityPlugin asm = new IdentityPlugin();
  60         ModulePool resourcePool = asm.visit(getPool());
  61         asm.test(getPool(), resourcePool);
  62     }
  63 
  64     private class IdentityPlugin extends TestPlugin {
  65 
  66         @Override
  67         public void visit() {
  68             for (ModuleEntry res : getPools().getGlobalPool().getClasses()) {
  69                 if (res.getPath().endsWith("module-info.class")) {
  70                     continue;
  71                 }
  72                 ClassReader reader = getPools().getGlobalPool().getClassReader(res);
  73                 ClassWriter writer = new ClassWriter(reader, ClassWriter.COMPUTE_FRAMES);
  74                 IdentityClassVisitor visitor = new IdentityClassVisitor(writer);
  75                 reader.accept(visitor, ClassReader.EXPAND_FRAMES);
  76                 getPools().getGlobalPool().getTransformedClasses().addClass(writer);
  77             }
  78         }
  79 
  80         @Override
  81         public void test(ModulePool inResources, ModulePool outResources) throws IOException {
  82             if (outResources.isEmpty()) {
  83                 throw new AssertionError("Empty result");
  84             }
  85             if (!isVisitCalled()) {
  86                 throw new AssertionError("Resources not visited");
  87             }
  88             WritableClassPool transformedClasses = getPools().getGlobalPool().getTransformedClasses();
  89             if (transformedClasses.getClasses().size() != getClasses().size()) {
  90                 throw new AssertionError("Number of transformed classes not equal to expected");
  91             }
  92             for (String className : getClasses()) {
  93                 if (transformedClasses.getClassReader(className) == null) {
  94                     throw new AssertionError("Class not transformed " + className);
  95                 }
  96             }
  97             outResources.entries().forEach(r -> {
  98                 if (r.getPath().endsWith(".class") && !r.getPath().endsWith("module-info.class")) {
  99                     try {
 100                         ClassReader reader = new ClassReader(new ByteArrayInputStream(r.getBytes()));
 101                         ClassWriter w = new ClassWriter(reader, ClassWriter.COMPUTE_FRAMES);
 102                         reader.accept(w, ClassReader.EXPAND_FRAMES);
 103                     } catch (IOException exp) {
 104                         throw new UncheckedIOException(exp);
 105                     }
 106                 }
 107             });
 108         }
 109 
 110         @Override
 111         public String getName() {
 112             return "identity-plugin";
 113         }
 114     }
 115 
 116     private static class IdentityClassVisitor extends ClassVisitor {
 117         public IdentityClassVisitor(ClassWriter cv) {
 118             super(Opcodes.ASM5, cv);
 119         }
 120     }
 121 }