< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  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.lang.module.ModuleDescriptor;

  29 import java.lang.module.ModuleDescriptor.Requires;
  30 import java.lang.module.ModuleDescriptor.Exports;
  31 import java.lang.module.ModuleDescriptor.Opens;
  32 import java.lang.module.ModuleDescriptor.Provides;
  33 import java.lang.module.ModuleDescriptor.Version;
  34 import java.util.ArrayList;
  35 import java.util.Collections;
  36 import java.util.HashMap;
  37 import java.util.HashSet;
  38 import java.util.List;
  39 import java.util.Map;
  40 import java.util.Set;
  41 
  42 import jdk.internal.misc.JavaLangModuleAccess;
  43 import jdk.internal.misc.SharedSecrets;
  44 import jdk.internal.org.objectweb.asm.Attribute;
  45 import jdk.internal.org.objectweb.asm.ByteVector;
  46 import jdk.internal.org.objectweb.asm.ClassReader;
  47 import jdk.internal.org.objectweb.asm.ClassWriter;
  48 import jdk.internal.org.objectweb.asm.Label;


  81         }
  82 
  83         public ModuleAttribute() {
  84             super(MODULE);
  85         }
  86 
  87         @Override
  88         protected Attribute read(ClassReader cr,
  89                                  int off,
  90                                  int len,
  91                                  char[] buf,
  92                                  int codeOff,
  93                                  Label[] labels)
  94         {
  95             // module_name (CONSTANT_Module_info)
  96             String mn = cr.readModule(off, buf);
  97             off += 2;
  98 
  99             // module_flags
 100             int module_flags = cr.readUnsignedShort(off);
 101             boolean open = ((module_flags & ACC_OPEN) != 0);
 102             boolean synthetic = ((module_flags & ACC_SYNTHETIC) != 0);
 103             off += 2;
 104 
 105             ModuleDescriptor.Builder builder = JLMA.newModuleBuilder(mn,
 106                                                                      false,
 107                                                                      open,
 108                                                                      synthetic);





 109 
 110             // module_version
 111             String module_version = cr.readUTF8(off, buf);
 112             off += 2;
 113             if (replacementVersion != null) {
 114                 builder.version(replacementVersion);
 115             } else if (module_version != null) {
 116                 builder.version(module_version);
 117             }
 118 
 119             // requires_count and requires[requires_count]
 120             int requires_count = cr.readUnsignedShort(off);
 121             off += 2;
 122             for (int i=0; i<requires_count; i++) {
 123                 // CONSTANT_Module_info
 124                 String dn = cr.readModule(off, buf);
 125                 off += 2;
 126 
 127                 // requires_flags
 128                 int requires_flags = cr.readUnsignedShort(off);
 129                 off += 2;
 130                 Set<Requires.Modifier> mods;
 131                 if (requires_flags == 0) {
 132                     mods = Collections.emptySet();
 133                 } else {
 134                     mods = new HashSet<>();
 135                     if ((requires_flags & ACC_TRANSITIVE) != 0)
 136                         mods.add(Requires.Modifier.TRANSITIVE);
 137                     if ((requires_flags & ACC_STATIC_PHASE) != 0)
 138                         mods.add(Requires.Modifier.STATIC);
 139                     if ((requires_flags & ACC_SYNTHETIC) != 0)
 140                         mods.add(Requires.Modifier.SYNTHETIC);
 141                     if ((requires_flags & ACC_MANDATED) != 0)
 142                         mods.add(Requires.Modifier.MANDATED);
 143                 }
 144 
 145 
 146                 // requires_version
 147                 Version compiledVersion = null;
 148                 String requires_version = cr.readUTF8(off, buf);
 149                 off += 2;
 150                 if (requires_version != null) {
 151                     compiledVersion = Version.parse(requires_version);
 152                 }
 153 
 154                 if (compiledVersion == null) {
 155                     builder.requires(mods, dn);
 156                 } else {
 157                     builder.requires(mods, dn, compiledVersion);
 158                 }
 159             }
 160 
 161             // exports_count and exports[exports_count]
 162             int exports_count = cr.readUnsignedShort(off);
 163             off += 2;
 164             if (exports_count > 0) {
 165                 for (int i=0; i<exports_count; i++) {
 166                     // CONSTANT_Package_info
 167                     String pkg = cr.readPackage(off, buf).replace('/', '.');
 168                     off += 2;
 169 
 170                     int exports_flags = cr.readUnsignedShort(off);
 171                     off += 2;
 172                     Set<Exports.Modifier> mods;
 173                     if (exports_flags == 0) {
 174                         mods = Collections.emptySet();
 175                     } else {
 176                         mods = new HashSet<>();
 177                         if ((exports_flags & ACC_SYNTHETIC) != 0)


 266 
 267             return new ModuleAttribute(builder.build());
 268         }
 269 
 270         @Override
 271         protected ByteVector write(ClassWriter cw,
 272                                    byte[] code,
 273                                    int len,
 274                                    int maxStack,
 275                                    int maxLocals)
 276         {
 277             assert descriptor != null;
 278             ByteVector attr = new ByteVector();
 279 
 280             // module_name
 281             String mn = descriptor.name();
 282             int module_name_index = cw.newModule(mn);
 283             attr.putShort(module_name_index);
 284 
 285             // module_flags

 286             int module_flags = 0;
 287             if (descriptor.isOpen())
 288                 module_flags |= ACC_OPEN;
 289             if (descriptor.isSynthetic())
 290                 module_flags |= ACC_SYNTHETIC;


 291             attr.putShort(module_flags);
 292 
 293             // module_version
 294             Version v = descriptor.version().orElse(null);
 295             if (v == null) {
 296                 attr.putShort(0);
 297             } else {
 298                 int module_version_index = cw.newUTF8(v.toString());
 299                 attr.putShort(module_version_index);
 300             }
 301 
 302             // requires_count
 303             attr.putShort(descriptor.requires().size());
 304 
 305             // requires[requires_count]
 306             for (Requires r : descriptor.requires()) {
 307                 int requires_index = cw.newModule(r.name());
 308                 attr.putShort(requires_index);
 309 
 310                 int requires_flags = 0;


   1 /*
   2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  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.lang.module.ModuleDescriptor;
  29 import java.lang.module.ModuleDescriptor.Builder;
  30 import java.lang.module.ModuleDescriptor.Requires;
  31 import java.lang.module.ModuleDescriptor.Exports;
  32 import java.lang.module.ModuleDescriptor.Opens;
  33 import java.lang.module.ModuleDescriptor.Provides;
  34 import java.lang.module.ModuleDescriptor.Version;
  35 import java.util.ArrayList;
  36 import java.util.Collections;
  37 import java.util.HashMap;
  38 import java.util.HashSet;
  39 import java.util.List;
  40 import java.util.Map;
  41 import java.util.Set;
  42 
  43 import jdk.internal.misc.JavaLangModuleAccess;
  44 import jdk.internal.misc.SharedSecrets;
  45 import jdk.internal.org.objectweb.asm.Attribute;
  46 import jdk.internal.org.objectweb.asm.ByteVector;
  47 import jdk.internal.org.objectweb.asm.ClassReader;
  48 import jdk.internal.org.objectweb.asm.ClassWriter;
  49 import jdk.internal.org.objectweb.asm.Label;


  82         }
  83 
  84         public ModuleAttribute() {
  85             super(MODULE);
  86         }
  87 
  88         @Override
  89         protected Attribute read(ClassReader cr,
  90                                  int off,
  91                                  int len,
  92                                  char[] buf,
  93                                  int codeOff,
  94                                  Label[] labels)
  95         {
  96             // module_name (CONSTANT_Module_info)
  97             String mn = cr.readModule(off, buf);
  98             off += 2;
  99 
 100             // module_flags
 101             int module_flags = cr.readUnsignedShort(off);


 102             off += 2;
 103 
 104             Set<ModuleDescriptor.Modifier> modifiers = new HashSet<>();
 105             if ((module_flags & ACC_OPEN) != 0)
 106                 modifiers.add(ModuleDescriptor.Modifier.OPEN);
 107             if ((module_flags & ACC_SYNTHETIC) != 0)
 108                 modifiers.add(ModuleDescriptor.Modifier.SYNTHETIC);
 109             if ((module_flags & ACC_MANDATED) != 0)
 110                 modifiers.add(ModuleDescriptor.Modifier.MANDATED);
 111 
 112             Builder builder = JLMA.newModuleBuilder(mn, false, modifiers);
 113 
 114             // module_version
 115             String module_version = cr.readUTF8(off, buf);
 116             off += 2;
 117             if (replacementVersion != null) {
 118                 builder.version(replacementVersion);
 119             } else if (module_version != null) {
 120                 builder.version(module_version);
 121             }
 122 
 123             // requires_count and requires[requires_count]
 124             int requires_count = cr.readUnsignedShort(off);
 125             off += 2;
 126             for (int i=0; i<requires_count; i++) {
 127                 // CONSTANT_Module_info
 128                 String dn = cr.readModule(off, buf);
 129                 off += 2;
 130 
 131                 // requires_flags
 132                 int requires_flags = cr.readUnsignedShort(off);
 133                 off += 2;
 134                 Set<Requires.Modifier> mods;
 135                 if (requires_flags == 0) {
 136                     mods = Collections.emptySet();
 137                 } else {
 138                     mods = new HashSet<>();
 139                     if ((requires_flags & ACC_TRANSITIVE) != 0)
 140                         mods.add(Requires.Modifier.TRANSITIVE);
 141                     if ((requires_flags & ACC_STATIC_PHASE) != 0)
 142                         mods.add(Requires.Modifier.STATIC);
 143                     if ((requires_flags & ACC_SYNTHETIC) != 0)
 144                         mods.add(Requires.Modifier.SYNTHETIC);
 145                     if ((requires_flags & ACC_MANDATED) != 0)
 146                         mods.add(Requires.Modifier.MANDATED);
 147                 }
 148 

 149                 // requires_version

 150                 String requires_version = cr.readUTF8(off, buf);
 151                 off += 2;
 152                 if (requires_version == null) {




 153                     builder.requires(mods, dn);
 154                 } else {
 155                     JLMA.requires(builder, mods, dn, requires_version);
 156                 }
 157             }
 158 
 159             // exports_count and exports[exports_count]
 160             int exports_count = cr.readUnsignedShort(off);
 161             off += 2;
 162             if (exports_count > 0) {
 163                 for (int i=0; i<exports_count; i++) {
 164                     // CONSTANT_Package_info
 165                     String pkg = cr.readPackage(off, buf).replace('/', '.');
 166                     off += 2;
 167 
 168                     int exports_flags = cr.readUnsignedShort(off);
 169                     off += 2;
 170                     Set<Exports.Modifier> mods;
 171                     if (exports_flags == 0) {
 172                         mods = Collections.emptySet();
 173                     } else {
 174                         mods = new HashSet<>();
 175                         if ((exports_flags & ACC_SYNTHETIC) != 0)


 264 
 265             return new ModuleAttribute(builder.build());
 266         }
 267 
 268         @Override
 269         protected ByteVector write(ClassWriter cw,
 270                                    byte[] code,
 271                                    int len,
 272                                    int maxStack,
 273                                    int maxLocals)
 274         {
 275             assert descriptor != null;
 276             ByteVector attr = new ByteVector();
 277 
 278             // module_name
 279             String mn = descriptor.name();
 280             int module_name_index = cw.newModule(mn);
 281             attr.putShort(module_name_index);
 282 
 283             // module_flags
 284             Set<ModuleDescriptor.Modifier> modifiers = descriptor.modifiers();
 285             int module_flags = 0;
 286             if (modifiers.contains(ModuleDescriptor.Modifier.OPEN))
 287                 module_flags |= ACC_OPEN;
 288             if (modifiers.contains(ModuleDescriptor.Modifier.SYNTHETIC))
 289                 module_flags |= ACC_SYNTHETIC;
 290             if (modifiers.contains(ModuleDescriptor.Modifier.MANDATED))
 291                 module_flags |= ACC_MANDATED;
 292             attr.putShort(module_flags);
 293 
 294             // module_version
 295             Version v = descriptor.version().orElse(null);
 296             if (v == null) {
 297                 attr.putShort(0);
 298             } else {
 299                 int module_version_index = cw.newUTF8(v.toString());
 300                 attr.putShort(module_version_index);
 301             }
 302 
 303             // requires_count
 304             attr.putShort(descriptor.requires().size());
 305 
 306             // requires[requires_count]
 307             for (Requires r : descriptor.requires()) {
 308                 int requires_index = cw.newModule(r.name());
 309                 attr.putShort(requires_index);
 310 
 311                 int requires_flags = 0;


< prev index next >