< prev index next >

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

Print this page
rev 52456 : imported patch asm.additional


 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)




 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.ASM7, 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.ASM7, 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)


< prev index next >