< prev index next >

src/java.base/share/classes/jdk/internal/module/ModuleInfoExtender.java

Print this page
rev 47453 : imported patch jdk-new-asm-update.patch


  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 
  26 package jdk.internal.module;
  27 
  28 import java.io.IOException;
  29 import java.io.InputStream;
  30 import java.io.OutputStream;
  31 import java.lang.module.ModuleDescriptor.Version;
  32 import java.util.ArrayList;
  33 import java.util.Collections;
  34 import java.util.HashMap;
  35 import java.util.List;
  36 import java.util.Map;
  37 import java.util.Set;
  38 
  39 import jdk.internal.org.objectweb.asm.Attribute;
  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 
  45 import static jdk.internal.module.ClassFileAttributes.*;
  46 
  47 /**
  48  * Utility class to extend a module-info.class with additional attributes.
  49  */
  50 
  51 public final class ModuleInfoExtender {
  52 
  53     // the input stream to read the original module-info.class
  54     private final InputStream in;
  55 
  56     // the packages in the ModulePackages attribute
  57     private Set<String> packages;
  58 
  59     // the value for the module version in the Module attribute
  60     private Version version;
  61 
  62     // the value of the ModuleMainClass attribute
  63     private String mainClass;
  64 
  65     // the value for the ModuleTarget attribute
  66     private String targetPlatform;


 116 
 117     /**
 118      * The ModuleHashes attribute will be emitted to the module-info with
 119      * the hashes encapsulated in the given {@code ModuleHashes}
 120      * object.
 121      */
 122     public ModuleInfoExtender hashes(ModuleHashes hashes) {
 123         this.hashes = hashes;
 124         return this;
 125     }
 126 
 127     /**
 128      * Sets the value for the ModuleResolution attribute.
 129      */
 130     public ModuleInfoExtender moduleResolution(ModuleResolution mres) {
 131         this.moduleResolution = mres;
 132         return this;
 133     }
 134 
 135     /**
 136      * A ClassVisitor that supports adding class file attributes. If an
 137      * attribute already exists then the first occurrence of the attribute
 138      * is replaced.
 139      */
 140     private static class AttributeAddingClassVisitor extends ClassVisitor {
 141         private Map<String, Attribute> attrs = new HashMap<>();
 142 
 143         AttributeAddingClassVisitor(int api, ClassVisitor cv) {
 144             super(api, cv);
 145         }
 146 
 147         void addAttribute(Attribute attr) {
 148             attrs.put(attr.type, attr);
 149         }
 150 
 151         @Override
 152         public void visitAttribute(Attribute attr) {
 153             String name = attr.type;
 154             Attribute replacement = attrs.get(name);
 155             if (replacement != null) {
 156                 attr = replacement;
 157                 attrs.remove(name);
 158             }
 159             super.visitAttribute(attr);
 160         }
 161 
 162         /**
 163          * Adds any remaining attributes that weren't replaced to the
 164          * class file.
 165          */
 166         void finish() {
 167             attrs.values().forEach(a -> super.visitAttribute(a));
 168             attrs.clear();
 169         }
 170     }
 171 
 172     /**
 173      * Outputs the modified module-info.class to the given output stream.
 174      * Once this method has been called then the Extender object should
 175      * be discarded.
 176      */
 177     public void write(OutputStream out) throws IOException {
 178         // emit to the output stream
 179         out.write(toByteArray());
 180     }
 181 
 182     /**
 183      * Returns the bytes of the modified module-info.class.
 184      * Once this method has been called then the Extender object should
 185      * be discarded.
 186      */
 187     public byte[] toByteArray() throws IOException {
 188         ClassWriter cw
 189             = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
 190 
 191         AttributeAddingClassVisitor cv
 192             = new AttributeAddingClassVisitor(Opcodes.ASM5, cw);
 193 
 194         ClassReader cr = new ClassReader(in);
 195 
 196         if (packages != null)
 197             cv.addAttribute(new ModulePackagesAttribute(packages));
 198         if (mainClass != null)
 199             cv.addAttribute(new ModuleMainClassAttribute(mainClass));
 200         if (targetPlatform != null)
 201             cv.addAttribute(new ModuleTargetAttribute(targetPlatform));
 202         if (hashes != null)
 203             cv.addAttribute(new ModuleHashesAttribute(hashes));
 204         if (moduleResolution != null)
 205             cv.addAttribute(new ModuleResolutionAttribute(moduleResolution.value()));
 206 
 207         List<Attribute> attrs = new ArrayList<>();



 208 
 209         // prototypes of attributes that should be parsed
 210         attrs.add(new ModuleAttribute(version));
 211         attrs.add(new ModulePackagesAttribute());
 212         attrs.add(new ModuleMainClassAttribute());




































 213         attrs.add(new ModuleTargetAttribute());

 214         attrs.add(new ModuleHashesAttribute());
 215 
 216         cr.accept(cv, attrs.toArray(new Attribute[0]), 0);
 217 
 218         // add any attributes that didn't replace previous attributes
 219         cv.finish();
















 220 
 221         return cw.toByteArray();
 222     }
 223 
 224     /**
 225      * Returns an {@code Extender} that may be used to add additional
 226      * attributes to the module-info.class read from the given input
 227      * stream.
 228      */
 229     public static ModuleInfoExtender newExtender(InputStream in) {
 230         return new ModuleInfoExtender(in);
 231     }
 232 
 233 }


  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 
  26 package jdk.internal.module;
  27 
  28 import java.io.IOException;
  29 import java.io.InputStream;
  30 import java.io.OutputStream;
  31 import java.lang.module.ModuleDescriptor.Version;
  32 import java.util.ArrayList;
  33 import java.util.Collections;

  34 import java.util.List;

  35 import java.util.Set;
  36 
  37 import jdk.internal.org.objectweb.asm.Attribute;
  38 import jdk.internal.org.objectweb.asm.ClassReader;
  39 import jdk.internal.org.objectweb.asm.ClassVisitor;
  40 import jdk.internal.org.objectweb.asm.ClassWriter;
  41 import jdk.internal.org.objectweb.asm.ModuleVisitor;
  42 import jdk.internal.org.objectweb.asm.Opcodes;
  43 import jdk.internal.org.objectweb.asm.commons.ModuleHashesAttribute;
  44 import jdk.internal.org.objectweb.asm.commons.ModuleResolutionAttribute;
  45 import jdk.internal.org.objectweb.asm.commons.ModuleTargetAttribute;
  46 


  47 /**
  48  * Utility class to extend a module-info.class with additional attributes.
  49  */
  50 
  51 public final class ModuleInfoExtender {
  52 
  53     // the input stream to read the original module-info.class
  54     private final InputStream in;
  55 
  56     // the packages in the ModulePackages attribute
  57     private Set<String> packages;
  58 
  59     // the value for the module version in the Module attribute
  60     private Version version;
  61 
  62     // the value of the ModuleMainClass attribute
  63     private String mainClass;
  64 
  65     // the value for the ModuleTarget attribute
  66     private String targetPlatform;


 116 
 117     /**
 118      * The ModuleHashes attribute will be emitted to the module-info with
 119      * the hashes encapsulated in the given {@code ModuleHashes}
 120      * object.
 121      */
 122     public ModuleInfoExtender hashes(ModuleHashes hashes) {
 123         this.hashes = hashes;
 124         return this;
 125     }
 126 
 127     /**
 128      * Sets the value for the ModuleResolution attribute.
 129      */
 130     public ModuleInfoExtender moduleResolution(ModuleResolution mres) {
 131         this.moduleResolution = mres;
 132         return this;
 133     }
 134 
 135     /**





































 136      * Outputs the modified module-info.class to the given output stream.
 137      * Once this method has been called then the Extender object should
 138      * be discarded.
 139      */
 140     public void write(OutputStream out) throws IOException {
 141         // emit to the output stream
 142         out.write(toByteArray());
 143     }
 144 
 145     /**
 146      * Returns the bytes of the modified module-info.class.
 147      * Once this method has been called then the Extender object should
 148      * be discarded.
 149      */
 150     public byte[] toByteArray() throws IOException {
 151         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
 152                                          + ClassWriter.COMPUTE_FRAMES);
 153 



 154         ClassReader cr = new ClassReader(in);
 155 
 156         ClassVisitor cv = new ClassVisitor(Opcodes.ASM6, cw) {
 157             @Override
 158             public ModuleVisitor visitModule(String name, int flags, String version) {
 159                 Version v = ModuleInfoExtender.this.version;
 160                 String vs = (v != null) ? v.toString() : version;
 161                 ModuleVisitor mv = super.visitModule(name, flags, vs);




 162 
 163                 // ModuleMainClass attribute
 164                 if (mainClass != null) {
 165                     mv.visitMainClass(mainClass.replace('.', '/'));
 166                 }
 167 
 168                 // ModulePackages attribute
 169                 if (packages != null) {
 170                     packages.forEach(pn -> mv.visitPackage(pn.replace('.', '/')));
 171                 }
 172 
 173                 return new ModuleVisitor(Opcodes.ASM6, mv) {
 174                     public void visitMainClass(String existingMainClass) {
 175                         // skip main class if there is a new value
 176                         if (mainClass == null) {
 177                             super.visitMainClass(existingMainClass);
 178                         }
 179                     }
 180                     public void visitPackage(String existingPackage) {
 181                         // skip packages if there is a new set of packages
 182                         if (packages == null) {
 183                             super.visitPackage(existingPackage);
 184                         }
 185                     }
 186                 };
 187             }
 188             @Override
 189             public void visitAttribute(Attribute attr) {
 190                 String name = attr.type;
 191                 // drop existing attributes if there are replacements
 192                 if (name.equals(ClassFileConstants.MODULE_TARGET)
 193                     && targetPlatform != null)
 194                     return;
 195                 if (name.equals(ClassFileConstants.MODULE_RESOLUTION)
 196                     && moduleResolution != null)
 197                     return;
 198                 if (name.equals(ClassFileConstants.MODULE_HASHES)
 199                     && hashes != null)
 200                     return;
 201 
 202                 super.visitAttribute(attr);
 203 
 204             }
 205         };
 206 
 207         List<Attribute> attrs = new ArrayList<>();
 208         attrs.add(new ModuleTargetAttribute());
 209         attrs.add(new ModuleResolutionAttribute());
 210         attrs.add(new ModuleHashesAttribute());

 211         cr.accept(cv, attrs.toArray(new Attribute[0]), 0);
 212 
 213         // add ModuleTarget, ModuleResolution and ModuleHashes attributes
 214         if (targetPlatform != null) {
 215             cw.visitAttribute(new ModuleTargetAttribute(targetPlatform));
 216         }
 217         if (moduleResolution != null) {
 218             int flags = moduleResolution.value();
 219             cw.visitAttribute(new ModuleResolutionAttribute(flags));
 220         }
 221         if (hashes != null) {
 222             String algorithm = hashes.algorithm();
 223             List<String> names = new ArrayList<>();
 224             List<byte[]> values = new ArrayList<>();
 225             for (String name : hashes.names()) {
 226                 names.add(name);
 227                 values.add(hashes.hashFor(name));
 228             }
 229             cw.visitAttribute(new ModuleHashesAttribute(algorithm, names, values));
 230         }
 231 
 232         return cw.toByteArray();
 233     }
 234 
 235     /**
 236      * Returns an {@code Extender} that may be used to add additional
 237      * attributes to the module-info.class read from the given input
 238      * stream.
 239      */
 240     public static ModuleInfoExtender newExtender(InputStream in) {
 241         return new ModuleInfoExtender(in);
 242     }
 243 
 244 }
< prev index next >