Path: blob/master/src/java.base/share/classes/jdk/internal/module/ModuleInfoExtender.java
41159 views
/*1* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.internal.module;2627import java.io.IOException;28import java.io.InputStream;29import java.io.OutputStream;30import java.lang.module.ModuleDescriptor.Version;31import java.util.ArrayList;32import java.util.Collections;33import java.util.List;34import java.util.Set;3536import jdk.internal.org.objectweb.asm.Attribute;37import jdk.internal.org.objectweb.asm.ClassReader;38import jdk.internal.org.objectweb.asm.ClassVisitor;39import jdk.internal.org.objectweb.asm.ClassWriter;40import jdk.internal.org.objectweb.asm.ModuleVisitor;41import jdk.internal.org.objectweb.asm.Opcodes;42import jdk.internal.org.objectweb.asm.commons.ModuleHashesAttribute;43import jdk.internal.org.objectweb.asm.commons.ModuleResolutionAttribute;44import jdk.internal.org.objectweb.asm.commons.ModuleTargetAttribute;4546/**47* Utility class to extend a module-info.class with additional attributes.48*/4950public final class ModuleInfoExtender {5152// the input stream to read the original module-info.class53private final InputStream in;5455// the packages in the ModulePackages attribute56private Set<String> packages;5758// the value for the module version in the Module attribute59private Version version;6061// the value of the ModuleMainClass attribute62private String mainClass;6364// the value for the ModuleTarget attribute65private String targetPlatform;6667// the hashes for the ModuleHashes attribute68private ModuleHashes hashes;6970// the value of the ModuleResolution attribute71private ModuleResolution moduleResolution;7273private ModuleInfoExtender(InputStream in) {74this.in = in;75}7677/**78* Sets the packages for the ModulePackages attribute79*80* @apiNote This method does not check that the package names are legal81* package names or that the set of packages is a super set of the82* packages in the module.83*/84public ModuleInfoExtender packages(Set<String> packages) {85this.packages = Collections.unmodifiableSet(packages);86return this;87}8889/**90* Sets the value for the module version in the Module attribute91*/92public ModuleInfoExtender version(Version version) {93this.version = version;94return this;95}9697/**98* Sets the value of the ModuleMainClass attribute.99*100* @apiNote This method does not check that the main class is a legal101* class name in a named package.102*/103public ModuleInfoExtender mainClass(String mainClass) {104this.mainClass = mainClass;105return this;106}107108/**109* Sets the value for the ModuleTarget attribute.110*/111public ModuleInfoExtender targetPlatform(String targetPlatform) {112this.targetPlatform = targetPlatform;113return this;114}115116/**117* The ModuleHashes attribute will be emitted to the module-info with118* the hashes encapsulated in the given {@code ModuleHashes}119* object.120*/121public ModuleInfoExtender hashes(ModuleHashes hashes) {122this.hashes = hashes;123return this;124}125126/**127* Sets the value for the ModuleResolution attribute.128*/129public ModuleInfoExtender moduleResolution(ModuleResolution mres) {130this.moduleResolution = mres;131return this;132}133134/**135* Outputs the modified module-info.class to the given output stream.136* Once this method has been called then the Extender object should137* be discarded.138*/139public void write(OutputStream out) throws IOException {140// emit to the output stream141out.write(toByteArray());142}143144/**145* Returns the bytes of the modified module-info.class.146* Once this method has been called then the Extender object should147* be discarded.148*/149public byte[] toByteArray() throws IOException {150ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS151+ ClassWriter.COMPUTE_FRAMES);152153ClassReader cr = new ClassReader(in);154155ClassVisitor cv = new ClassVisitor(Opcodes.ASM7, cw) {156@Override157public ModuleVisitor visitModule(String name, int flags, String version) {158Version v = ModuleInfoExtender.this.version;159String vs = (v != null) ? v.toString() : version;160ModuleVisitor mv = super.visitModule(name, flags, vs);161162// ModuleMainClass attribute163if (mainClass != null) {164mv.visitMainClass(mainClass.replace('.', '/'));165}166167// ModulePackages attribute168if (packages != null) {169packages.stream()170.sorted()171.forEach(pn -> mv.visitPackage(pn.replace('.', '/')));172}173174return new ModuleVisitor(Opcodes.ASM7, mv) {175public void visitMainClass(String existingMainClass) {176// skip main class if there is a new value177if (mainClass == null) {178super.visitMainClass(existingMainClass);179}180}181public void visitPackage(String existingPackage) {182// skip packages if there is a new set of packages183if (packages == null) {184super.visitPackage(existingPackage);185}186}187};188}189@Override190public void visitAttribute(Attribute attr) {191String name = attr.type;192// drop existing attributes if there are replacements193if (name.equals(ClassFileConstants.MODULE_TARGET)194&& targetPlatform != null)195return;196if (name.equals(ClassFileConstants.MODULE_RESOLUTION)197&& moduleResolution != null)198return;199if (name.equals(ClassFileConstants.MODULE_HASHES)200&& hashes != null)201return;202203super.visitAttribute(attr);204205}206};207208List<Attribute> attrs = new ArrayList<>();209attrs.add(new ModuleTargetAttribute());210attrs.add(new ModuleResolutionAttribute());211attrs.add(new ModuleHashesAttribute());212cr.accept(cv, attrs.toArray(new Attribute[0]), 0);213214// add ModuleTarget, ModuleResolution and ModuleHashes attributes215if (targetPlatform != null) {216cw.visitAttribute(new ModuleTargetAttribute(targetPlatform));217}218if (moduleResolution != null) {219int flags = moduleResolution.value();220cw.visitAttribute(new ModuleResolutionAttribute(flags));221}222if (hashes != null) {223String algorithm = hashes.algorithm();224List<String> names = new ArrayList<>();225List<byte[]> values = new ArrayList<>();226for (String name : hashes.names()) {227names.add(name);228values.add(hashes.hashFor(name));229}230cw.visitAttribute(new ModuleHashesAttribute(algorithm, names, values));231}232233return cw.toByteArray();234}235236/**237* Returns an {@code Extender} that may be used to add additional238* attributes to the module-info.class read from the given input239* stream.240*/241public static ModuleInfoExtender newExtender(InputStream in) {242return new ModuleInfoExtender(in);243}244245}246247248