146 } 147 super.visitAttribute(attr); 148 } 149 150 /** 151 * Adds any remaining attributes that weren't replaced to the 152 * class file. 153 */ 154 void finish() { 155 attrs.values().forEach(a -> super.visitAttribute(a)); 156 attrs.clear(); 157 } 158 } 159 160 /** 161 * Outputs the modified module-info.class to the given output stream. 162 * Once this method has been called then the Extender object should 163 * be discarded. 164 */ 165 public void write(OutputStream out) throws IOException { 166 ClassWriter cw 167 = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES); 168 169 AttributeAddingClassVisitor cv 170 = new AttributeAddingClassVisitor(Opcodes.ASM5, cw); 171 172 ClassReader cr = new ClassReader(in); 173 174 if (conceals != null) 175 cv.addAttribute(new ConcealedPackagesAttribute(conceals)); 176 if (version != null) 177 cv.addAttribute(new VersionAttribute(version)); 178 if (mainClass != null) 179 cv.addAttribute(new MainClassAttribute(mainClass)); 180 if (osName != null || osArch != null || osVersion != null) 181 cv.addAttribute(new TargetPlatformAttribute(osName, osArch, osVersion)); 182 if (hashes != null) 183 cv.addAttribute(new HashesAttribute(hashes)); 184 185 List<Attribute> attrs = new ArrayList<>(); 186 187 // prototypes of attributes that should be parsed 188 attrs.add(new ModuleAttribute()); 189 attrs.add(new ConcealedPackagesAttribute()); 190 attrs.add(new VersionAttribute()); 191 attrs.add(new MainClassAttribute()); 192 attrs.add(new TargetPlatformAttribute()); 193 attrs.add(new HashesAttribute()); 194 195 cr.accept(cv, attrs.toArray(new Attribute[0]), 0); 196 197 // add any attributes that didn't replace previous attributes 198 cv.finish(); 199 200 // emit to the output stream 201 out.write(cw.toByteArray()); 202 } 203 204 /** 205 * Returns an {@code Extender} that may be used to add additional 206 * attributes to the module-info.class read from the given input 207 * stream. 208 */ 209 public static ModuleInfoExtender newExtender(InputStream in) { 210 return new ModuleInfoExtender(in); 211 } 212 213 } | 146 } 147 super.visitAttribute(attr); 148 } 149 150 /** 151 * Adds any remaining attributes that weren't replaced to the 152 * class file. 153 */ 154 void finish() { 155 attrs.values().forEach(a -> super.visitAttribute(a)); 156 attrs.clear(); 157 } 158 } 159 160 /** 161 * Outputs the modified module-info.class to the given output stream. 162 * Once this method has been called then the Extender object should 163 * be discarded. 164 */ 165 public void write(OutputStream out) throws IOException { 166 // emit to the output stream 167 out.write(getBytes()); 168 } 169 170 /** 171 * Returns the bytes of the modified module-info.class. 172 * Once this method has been called then the Extender object should 173 * be discarded. 174 */ 175 public byte[] getBytes() throws IOException { 176 ClassWriter cw 177 = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES); 178 179 AttributeAddingClassVisitor cv 180 = new AttributeAddingClassVisitor(Opcodes.ASM5, cw); 181 182 ClassReader cr = new ClassReader(in); 183 184 if (conceals != null) 185 cv.addAttribute(new ConcealedPackagesAttribute(conceals)); 186 if (version != null) 187 cv.addAttribute(new VersionAttribute(version)); 188 if (mainClass != null) 189 cv.addAttribute(new MainClassAttribute(mainClass)); 190 if (osName != null || osArch != null || osVersion != null) 191 cv.addAttribute(new TargetPlatformAttribute(osName, osArch, osVersion)); 192 if (hashes != null) 193 cv.addAttribute(new HashesAttribute(hashes)); 194 195 List<Attribute> attrs = new ArrayList<>(); 196 197 // prototypes of attributes that should be parsed 198 attrs.add(new ModuleAttribute()); 199 attrs.add(new ConcealedPackagesAttribute()); 200 attrs.add(new VersionAttribute()); 201 attrs.add(new MainClassAttribute()); 202 attrs.add(new TargetPlatformAttribute()); 203 attrs.add(new HashesAttribute()); 204 205 cr.accept(cv, attrs.toArray(new Attribute[0]), 0); 206 207 // add any attributes that didn't replace previous attributes 208 cv.finish(); 209 210 return cw.toByteArray(); 211 } 212 213 /** 214 * Returns an {@code Extender} that may be used to add additional 215 * attributes to the module-info.class read from the given input 216 * stream. 217 */ 218 public static ModuleInfoExtender newExtender(InputStream in) { 219 return new ModuleInfoExtender(in); 220 } 221 222 } |