langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/file/ModuleNameReader.java
changeset 41411 31882abda8b5
parent 41410 ef26c8e40f1e
parent 41378 62969f0fb9c5
child 41412 4d1d88a4f815
equal deleted inserted replaced
41410:ef26c8e40f1e 41411:31882abda8b5
     1 /*
       
     2  * Copyright (c) 1999, 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 package com.sun.tools.javac.file;
       
    26 
       
    27 import java.io.IOException;
       
    28 import java.io.InputStream;
       
    29 import java.nio.file.Files;
       
    30 import java.nio.file.Path;
       
    31 
       
    32 import com.sun.tools.javac.jvm.ClassFile;
       
    33 
       
    34 import static com.sun.tools.javac.jvm.ClassFile.*;
       
    35 
       
    36 
       
    37 /**
       
    38  * Stripped down ClassReader, just sufficient to read module names from module-info.class files
       
    39  * while analyzing the module path.
       
    40  *
       
    41  * <p>
       
    42  * <b>This is NOT part of any supported API. If you write code that depends on this, you do so at
       
    43  * your own risk. This code and its internal interfaces are subject to change or deletion without
       
    44  * notice.</b>
       
    45  */
       
    46 public class ModuleNameReader {
       
    47     static class BadClassFile extends Exception {
       
    48         private static final long serialVersionUID = 0;
       
    49         BadClassFile(String msg) {
       
    50             super(msg);
       
    51         }
       
    52     }
       
    53 
       
    54     private static final int INITIAL_BUFFER_SIZE = 0x0fff0;
       
    55 
       
    56     /** The buffer containing the currently read class file.
       
    57      */
       
    58     private byte[] buf = new byte[INITIAL_BUFFER_SIZE];
       
    59 
       
    60     /** The current input pointer.
       
    61      */
       
    62     private int bp;
       
    63 
       
    64     /** The objects of the constant pool.
       
    65      */
       
    66     private Object[] poolObj;
       
    67 
       
    68     /** For every constant pool entry, an index into buf where the
       
    69      *  defining section of the entry is found.
       
    70      */
       
    71     private int[] poolIdx;
       
    72 
       
    73     ModuleNameReader() {
       
    74     }
       
    75 
       
    76     String readModuleName(Path p) throws IOException, BadClassFile {
       
    77         try (InputStream in = Files.newInputStream(p)) {
       
    78             bp = 0;
       
    79             buf = readInputStream(buf, in);
       
    80 
       
    81             int magic = nextInt();
       
    82             if (magic != JAVA_MAGIC)
       
    83                 throw new BadClassFile("illegal.start.of.class.file");
       
    84 
       
    85             int minorVersion = nextChar();
       
    86             int majorVersion = nextChar();
       
    87 
       
    88             indexPool();
       
    89 
       
    90             int accessflags = nextChar();
       
    91             return readModuleInfoName(nextChar());
       
    92         }
       
    93     }
       
    94 
       
    95     /** Extract a character at position bp from buf.
       
    96      */
       
    97     char getChar(int bp) {
       
    98         return
       
    99             (char)(((buf[bp] & 0xFF) << 8) + (buf[bp+1] & 0xFF));
       
   100     }
       
   101 
       
   102     /** Read a character.
       
   103      */
       
   104     char nextChar() {
       
   105         return (char)(((buf[bp++] & 0xFF) << 8) + (buf[bp++] & 0xFF));
       
   106     }
       
   107 
       
   108     /** Read an integer.
       
   109      */
       
   110     int nextInt() {
       
   111         return
       
   112             ((buf[bp++] & 0xFF) << 24) +
       
   113             ((buf[bp++] & 0xFF) << 16) +
       
   114             ((buf[bp++] & 0xFF) << 8) +
       
   115             (buf[bp++] & 0xFF);
       
   116     }
       
   117 
       
   118     /** Index all constant pool entries, writing their start addresses into
       
   119      *  poolIdx.
       
   120      */
       
   121     void indexPool() throws BadClassFile {
       
   122         poolIdx = new int[nextChar()];
       
   123         poolObj = new Object[poolIdx.length];
       
   124         int i = 1;
       
   125         while (i < poolIdx.length) {
       
   126             poolIdx[i++] = bp;
       
   127             byte tag = buf[bp++];
       
   128             switch (tag) {
       
   129             case CONSTANT_Utf8: case CONSTANT_Unicode: {
       
   130                 int len = nextChar();
       
   131                 bp = bp + len;
       
   132                 break;
       
   133             }
       
   134             case CONSTANT_Class:
       
   135             case CONSTANT_String:
       
   136             case CONSTANT_MethodType:
       
   137                 bp = bp + 2;
       
   138                 break;
       
   139             case CONSTANT_MethodHandle:
       
   140                 bp = bp + 3;
       
   141                 break;
       
   142             case CONSTANT_Fieldref:
       
   143             case CONSTANT_Methodref:
       
   144             case CONSTANT_InterfaceMethodref:
       
   145             case CONSTANT_NameandType:
       
   146             case CONSTANT_Integer:
       
   147             case CONSTANT_Float:
       
   148             case CONSTANT_InvokeDynamic:
       
   149                 bp = bp + 4;
       
   150                 break;
       
   151             case CONSTANT_Long:
       
   152             case CONSTANT_Double:
       
   153                 bp = bp + 8;
       
   154                 i++;
       
   155                 break;
       
   156             default:
       
   157                 throw new BadClassFile("malformed constant pool");
       
   158             }
       
   159         }
       
   160     }
       
   161 
       
   162     /** Read the class name of a module-info.class file.
       
   163      * The name is stored in a CONSTANT_Class entry, where the
       
   164      * class name is of the form module-name/module-info.
       
   165      */
       
   166     String readModuleInfoName(int i) throws BadClassFile {
       
   167         int classIndex = poolIdx[i];
       
   168         if (buf[classIndex] == CONSTANT_Class) {
       
   169             int utf8Index = poolIdx[getChar(classIndex + 1)];
       
   170             if (buf[utf8Index] == CONSTANT_Utf8) {
       
   171                 int len = getChar(utf8Index + 1);
       
   172                 int start = utf8Index + 3;
       
   173                 String suffix = "/module-info";
       
   174                 if (endsWith(buf, start, len, suffix))
       
   175                     return new String(ClassFile.internalize(buf, start, len - suffix.length()));
       
   176             }
       
   177         }
       
   178         throw new BadClassFile("bad module-info name");
       
   179     }
       
   180 
       
   181     private boolean endsWith(byte[] buf, int start, int len, String suffix) {
       
   182         if (len <= suffix.length())
       
   183             return false;
       
   184         for (int i = 0; i < suffix.length(); i++) {
       
   185             if (buf[start + len - suffix.length() + i] != suffix.charAt(i))
       
   186                 return false;
       
   187         }
       
   188         return true;
       
   189     }
       
   190 
       
   191     private static byte[] readInputStream(byte[] buf, InputStream s) throws IOException {
       
   192         try {
       
   193             buf = ensureCapacity(buf, s.available());
       
   194             int r = s.read(buf);
       
   195             int bp = 0;
       
   196             while (r != -1) {
       
   197                 bp += r;
       
   198                 buf = ensureCapacity(buf, bp);
       
   199                 r = s.read(buf, bp, buf.length - bp);
       
   200             }
       
   201             return buf;
       
   202         } finally {
       
   203             try {
       
   204                 s.close();
       
   205             } catch (IOException e) {
       
   206                 /* Ignore any errors, as this stream may have already
       
   207                  * thrown a related exception which is the one that
       
   208                  * should be reported.
       
   209                  */
       
   210             }
       
   211         }
       
   212     }
       
   213 
       
   214     /*
       
   215      * ensureCapacity will increase the buffer as needed, taking note that
       
   216      * the new buffer will always be greater than the needed and never
       
   217      * exactly equal to the needed size or bp. If equal then the read (above)
       
   218      * will infinitely loop as buf.length - bp == 0.
       
   219      */
       
   220     private static byte[] ensureCapacity(byte[] buf, int needed) {
       
   221         if (buf.length <= needed) {
       
   222             byte[] old = buf;
       
   223             buf = new byte[Integer.highestOneBit(needed) << 1];
       
   224             System.arraycopy(old, 0, buf, 0, old.length);
       
   225         }
       
   226         return buf;
       
   227     }
       
   228 }