1 /*
   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 it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.  Oracle designates this
   7  * particular file as subject to the "Classpath" exception as provided
   8  * 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 WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 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 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /*
  26  * This file is available under and governed by the GNU General Public
  27  * License version 2 only, as published by the Free Software Foundation.
  28  * However, the following notice accompanied the original version of this
  29  * file:
  30  *
  31  * ASM: a very small and fast Java bytecode manipulation framework
  32  * Copyright (c) 2000-2011 INRIA, France Telecom
  33  * All rights reserved.
  34  *
  35  * Redistribution and use in source and binary forms, with or without
  36  * modification, are permitted provided that the following conditions
  37  * are met:
  38  * 1. Redistributions of source code must retain the above copyright
  39  *    notice, this list of conditions and the following disclaimer.
  40  * 2. Redistributions in binary form must reproduce the above copyright
  41  *    notice, this list of conditions and the following disclaimer in the
  42  *    documentation and/or other materials provided with the distribution.
  43  * 3. Neither the name of the copyright holders nor the names of its
  44  *    contributors may be used to endorse or promote products derived from
  45  *    this software without specific prior written permission.
  46  *
  47  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  48  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  50  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  51  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  52  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  53  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  54  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  55  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  56  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  57  * THE POSSIBILITY OF SUCH DAMAGE.
  58  */
  59 package jdk.internal.org.objectweb.asm.tree;
  60 
  61 import java.util.ArrayList;
  62 import java.util.List;
  63 
  64 import jdk.internal.org.objectweb.asm.ClassVisitor;
  65 import jdk.internal.org.objectweb.asm.ModuleVisitor;
  66 import jdk.internal.org.objectweb.asm.Opcodes;
  67 
  68 /**
  69  * A node that represents a module declaration.
  70  *
  71  * @author Remi Forax
  72  */
  73 public class ModuleNode extends ModuleVisitor {
  74     /**
  75      * Module name
  76      */
  77     public String name;
  78 
  79     /**
  80      * Module access flags, among {@code ACC_OPEN}, {@code ACC_SYNTHETIC}
  81      *            and {@code ACC_MANDATED}.
  82      */
  83     public int access;
  84 
  85     /**
  86      * Version of the module.
  87      * May be <tt>null</tt>.
  88      */
  89     public String version;
  90 
  91     /**
  92      * Name of the main class in internal form
  93      * May be <tt>null</tt>.
  94      */
  95     public String mainClass;
  96 
  97     /**
  98      * A list of packages that are declared by the current module.
  99      * May be <tt>null</tt>.
 100      */
 101     public List<String> packages;
 102 
 103     /**
 104      * A list of modules can are required by the current module.
 105      * May be <tt>null</tt>.
 106      */
 107     public List<ModuleRequireNode> requires;
 108 
 109     /**
 110      * A list of packages that are exported by the current module.
 111      * May be <tt>null</tt>.
 112      */
 113     public List<ModuleExportNode> exports;
 114 
 115     /**
 116      * A list of packages that are opened by the current module.
 117      * May be <tt>null</tt>.
 118      */
 119     public List<ModuleOpenNode> opens;
 120 
 121     /**
 122      * A list of classes in their internal forms that are used
 123      * as a service by the current module. May be <tt>null</tt>.
 124      */
 125     public List<String> uses;
 126 
 127     /**
 128      * A list of services along with their implementations provided
 129      * by the current module. May be <tt>null</tt>.
 130      */
 131     public List<ModuleProvideNode> provides;
 132 
 133     public ModuleNode(final String name, final int access,
 134             final String version) {
 135         super(Opcodes.ASM6);
 136         this.name = name;
 137         this.access = access;
 138         this.version = version;
 139     }
 140 
 141     public ModuleNode(final int api,
 142       final String name,
 143       final int access,
 144       final String version,
 145       final List<ModuleRequireNode> requires,
 146       final List<ModuleExportNode> exports,
 147       final List<ModuleOpenNode> opens,
 148       final List<String> uses,
 149       final List<ModuleProvideNode> provides) {
 150         super(api);
 151         this.name = name;
 152         this.access = access;
 153         this.version = version;
 154         this.requires = requires;
 155         this.exports = exports;
 156         this.opens = opens;
 157         this.uses = uses;
 158         this.provides = provides;
 159         if (getClass() != ModuleNode.class) {
 160             throw new IllegalStateException();
 161         }
 162     }
 163 
 164     @Override
 165     public void visitMainClass(String mainClass) {
 166         this.mainClass = mainClass;
 167     }
 168 
 169     @Override
 170     public void visitPackage(String packaze) {
 171         if (packages == null) {
 172             packages = new ArrayList<String>(5);
 173         }
 174         packages.add(packaze);
 175     }
 176 
 177     @Override
 178     public void visitRequire(String module, int access, String version) {
 179         if (requires == null) {
 180             requires = new ArrayList<ModuleRequireNode>(5);
 181         }
 182         requires.add(new ModuleRequireNode(module, access, version));
 183     }
 184 
 185     @Override
 186     public void visitExport(String packaze, int access, String... modules) {
 187         if (exports == null) {
 188             exports = new ArrayList<ModuleExportNode>(5);
 189         }
 190         List<String> moduleList = null;
 191         if (modules != null) {
 192             moduleList = new ArrayList<String>(modules.length);
 193             for (int i = 0; i < modules.length; i++) {
 194                 moduleList.add(modules[i]);
 195             }
 196         }
 197         exports.add(new ModuleExportNode(packaze, access, moduleList));
 198     }
 199 
 200     @Override
 201     public void visitOpen(String packaze, int access, String... modules) {
 202         if (opens == null) {
 203             opens = new ArrayList<ModuleOpenNode>(5);
 204         }
 205         List<String> moduleList = null;
 206         if (modules != null) {
 207             moduleList = new ArrayList<String>(modules.length);
 208             for (int i = 0; i < modules.length; i++) {
 209                 moduleList.add(modules[i]);
 210             }
 211         }
 212         opens.add(new ModuleOpenNode(packaze, access, moduleList));
 213     }
 214 
 215     @Override
 216     public void visitUse(String service) {
 217         if (uses == null) {
 218             uses = new ArrayList<String>(5);
 219         }
 220         uses.add(service);
 221     }
 222 
 223     @Override
 224     public void visitProvide(String service, String... providers) {
 225         if (provides == null) {
 226             provides = new ArrayList<ModuleProvideNode>(5);
 227         }
 228         ArrayList<String> providerList =
 229                 new ArrayList<String>(providers.length);
 230         for (int i = 0; i < providers.length; i++) {
 231                 providerList.add(providers[i]);
 232         }
 233         provides.add(new ModuleProvideNode(service, providerList));
 234     }
 235 
 236     @Override
 237     public void visitEnd() {
 238     }
 239 
 240     public void accept(final ClassVisitor cv) {
 241         ModuleVisitor mv = cv.visitModule(name, access, version);
 242         if (mv == null) {
 243             return;
 244         }
 245         if (mainClass != null) {
 246             mv.visitMainClass(mainClass);
 247         }
 248         if (packages != null) {
 249             for (int i = 0; i < packages.size(); i++) {
 250                 mv.visitPackage(packages.get(i));
 251             }
 252         }
 253 
 254         if (requires != null) {
 255             for (int i = 0; i < requires.size(); i++) {
 256                 requires.get(i).accept(mv);
 257             }
 258         }
 259         if (exports != null) {
 260             for (int i = 0; i < exports.size(); i++) {
 261                 exports.get(i).accept(mv);
 262             }
 263         }
 264         if (opens != null) {
 265             for (int i = 0; i < opens.size(); i++) {
 266                 opens.get(i).accept(mv);
 267             }
 268         }
 269         if (uses != null) {
 270             for (int i = 0; i < uses.size(); i++) {
 271                 mv.visitUse(uses.get(i));
 272             }
 273         }
 274         if (provides != null) {
 275             for (int i = 0; i < provides.size(); i++) {
 276                 provides.get(i).accept(mv);
 277             }
 278         }
 279     }
 280 }