• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // ASM: a very small and fast Java bytecode manipulation framework
2 // Copyright (c) 2000-2011 INRIA, France Telecom
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions
7 // are met:
8 // 1. Redistributions of source code must retain the above copyright
9 //    notice, this list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright
11 //    notice, this list of conditions and the following disclaimer in the
12 //    documentation and/or other materials provided with the distribution.
13 // 3. Neither the name of the copyright holders nor the names of its
14 //    contributors may be used to endorse or promote products derived from
15 //    this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 // THE POSSIBILITY OF SUCH DAMAGE.
28 
29 package org.objectweb.asm.commons;
30 
31 import org.objectweb.asm.ModuleVisitor;
32 import org.objectweb.asm.Opcodes;
33 
34 /**
35  * A {@link ModuleVisitor} that remaps types with a {@link Remapper}.
36  *
37  * @author Remi Forax
38  */
39 public class ModuleRemapper extends ModuleVisitor {
40 
41   /** The remapper used to remap the types in the visited module. */
42   protected final Remapper remapper;
43 
44   /**
45    * Constructs a new {@link ModuleRemapper}. <i>Subclasses must not use this constructor</i>.
46    * Instead, they must use the {@link #ModuleRemapper(int,ModuleVisitor,Remapper)} version.
47    *
48    * @param moduleVisitor the module visitor this remapper must delegate to.
49    * @param remapper the remapper to use to remap the types in the visited module.
50    */
ModuleRemapper(final ModuleVisitor moduleVisitor, final Remapper remapper)51   public ModuleRemapper(final ModuleVisitor moduleVisitor, final Remapper remapper) {
52     this(/* latest api = */ Opcodes.ASM9, moduleVisitor, remapper);
53   }
54 
55   /**
56    * Constructs a new {@link ModuleRemapper}.
57    *
58    * @param api the ASM API version supported by this remapper. Must be one of the {@code
59    *     ASM}<i>x</i> values in {@link Opcodes}.
60    * @param moduleVisitor the module visitor this remapper must delegate to.
61    * @param remapper the remapper to use to remap the types in the visited module.
62    */
ModuleRemapper( final int api, final ModuleVisitor moduleVisitor, final Remapper remapper)63   protected ModuleRemapper(
64       final int api, final ModuleVisitor moduleVisitor, final Remapper remapper) {
65     super(api, moduleVisitor);
66     this.remapper = remapper;
67   }
68 
69   @Override
visitMainClass(final String mainClass)70   public void visitMainClass(final String mainClass) {
71     super.visitMainClass(remapper.mapType(mainClass));
72   }
73 
74   @Override
visitPackage(final String packaze)75   public void visitPackage(final String packaze) {
76     super.visitPackage(remapper.mapPackageName(packaze));
77   }
78 
79   @Override
visitRequire(final String module, final int access, final String version)80   public void visitRequire(final String module, final int access, final String version) {
81     super.visitRequire(remapper.mapModuleName(module), access, version);
82   }
83 
84   @Override
visitExport(final String packaze, final int access, final String... modules)85   public void visitExport(final String packaze, final int access, final String... modules) {
86     String[] remappedModules = null;
87     if (modules != null) {
88       remappedModules = new String[modules.length];
89       for (int i = 0; i < modules.length; ++i) {
90         remappedModules[i] = remapper.mapModuleName(modules[i]);
91       }
92     }
93     super.visitExport(remapper.mapPackageName(packaze), access, remappedModules);
94   }
95 
96   @Override
visitOpen(final String packaze, final int access, final String... modules)97   public void visitOpen(final String packaze, final int access, final String... modules) {
98     String[] remappedModules = null;
99     if (modules != null) {
100       remappedModules = new String[modules.length];
101       for (int i = 0; i < modules.length; ++i) {
102         remappedModules[i] = remapper.mapModuleName(modules[i]);
103       }
104     }
105     super.visitOpen(remapper.mapPackageName(packaze), access, remappedModules);
106   }
107 
108   @Override
visitUse(final String service)109   public void visitUse(final String service) {
110     super.visitUse(remapper.mapType(service));
111   }
112 
113   @Override
visitProvide(final String service, final String... providers)114   public void visitProvide(final String service, final String... providers) {
115     String[] remappedProviders = new String[providers.length];
116     for (int i = 0; i < providers.length; ++i) {
117       remappedProviders[i] = remapper.mapType(providers[i]);
118     }
119     super.visitProvide(remapper.mapType(service), remappedProviders);
120   }
121 }
122