1 /*
   2  * Copyright (c) 1999, 2018, 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 com.sun.tools.javac.jvm;
  27 
  28 import com.sun.tools.javac.code.Type;
  29 import com.sun.tools.javac.code.Types;
  30 import com.sun.tools.javac.code.Types.UniqueType;
  31 import com.sun.tools.javac.util.Name;
  32 
  33 
  34 /** A JVM class file.
  35  *
  36  *  <p>Generic Java classfiles have one additional attribute for classes,
  37  *  methods and fields:
  38  *  <pre>
  39  *   "Signature" (u4 attr-length, u2 signature-index)
  40  *  </pre>
  41  *
  42  *  <p>A signature gives the full Java type of a method or field. When
  43  *  used as a class attribute, it indicates type parameters, followed
  44  *  by supertype, followed by all interfaces.
  45  *  <pre>
  46  *     methodOrFieldSignature ::= type
  47  *     classSignature         ::= [ typeparams ] supertype { interfacetype }
  48  *  </pre>
  49  *  <p>The type syntax in signatures is extended as follows:
  50  *  <pre>{@literal
  51  *     type       ::= ... | classtype | methodtype | typevar
  52  *     classtype  ::= classsig { '.' classsig }
  53  *     classig    ::= 'L' name [typeargs] ';'
  54  *     methodtype ::= [ typeparams ] '(' { type } ')' type
  55  *     typevar    ::= 'T' name ';'
  56  *     typeargs   ::= '<' type { type } '>'
  57  *     typeparams ::= '<' typeparam { typeparam } '>'
  58  *     typeparam  ::= name ':' type
  59  *  }</pre>
  60  *  <p>This class defines constants used in class files as well
  61  *  as routines to convert between internal ``.'' and external ``/''
  62  *  separators in class names.
  63  *
  64  *  <p><b>This is NOT part of any supported API.
  65  *  If you write code that depends on this, you do so at your own risk.
  66  *  This code and its internal interfaces are subject to change or
  67  *  deletion without notice.</b> */
  68 public class ClassFile {
  69 
  70     public final static int JAVA_MAGIC = 0xCAFEBABE;
  71 
  72     // see Target
  73     public final static int CONSTANT_Utf8 = 1;
  74     public final static int CONSTANT_Unicode = 2;
  75     public final static int CONSTANT_Integer = 3;
  76     public final static int CONSTANT_Float = 4;
  77     public final static int CONSTANT_Long = 5;
  78     public final static int CONSTANT_Double = 6;
  79     public final static int CONSTANT_Class = 7;
  80     public final static int CONSTANT_String = 8;
  81     public final static int CONSTANT_Fieldref = 9;
  82     public final static int CONSTANT_Methodref = 10;
  83     public final static int CONSTANT_InterfaceMethodref = 11;
  84     public final static int CONSTANT_NameandType = 12;
  85     public final static int CONSTANT_MethodHandle = 15;
  86     public final static int CONSTANT_MethodType = 16;
  87     public final static int CONSTANT_Dynamic = 17;
  88     public final static int CONSTANT_InvokeDynamic = 18;
  89     public final static int CONSTANT_Module = 19;
  90     public final static int CONSTANT_Package = 20;
  91 
  92     public final static int REF_getField = 1;
  93     public final static int REF_getStatic = 2;
  94     public final static int REF_putField = 3;
  95     public final static int REF_putStatic = 4;
  96     public final static int REF_invokeVirtual = 5;
  97     public final static int REF_invokeStatic = 6;
  98     public final static int REF_invokeSpecial = 7;
  99     public final static int REF_newInvokeSpecial = 8;
 100     public final static int REF_invokeInterface = 9;
 101 
 102     public final static int MAX_PARAMETERS = 0xff;
 103     public final static int MAX_DIMENSIONS = 0xff;
 104     public final static int MAX_CODE = 0xffff;
 105     public final static int MAX_LOCALS = 0xffff;
 106     public final static int MAX_STACK = 0xffff;
 107 
 108     public final static int PREVIEW_MINOR_VERSION = 0xffff;
 109 
 110     public enum Version {
 111         V45_3(45, 3), // base level for all attributes
 112         V49(49, 0),   // JDK 1.5: enum, generics, annotations
 113         V50(50, 0),   // JDK 1.6: stackmaps
 114         V51(51, 0),   // JDK 1.7
 115         V52(52, 0),   // JDK 1.8: lambda, type annos, param names
 116         V53(53, 0),   // JDK 1.9: modules, indy string concat
 117         V54(54, 0),   // JDK 10
 118         V55(55, 0),   // JDK 11: constant dynamic, nest mates
 119         V56(56, 0);   // JDK 12
 120         Version(int major, int minor) {
 121             this.major = major;
 122             this.minor = minor;
 123         }
 124         public final int major, minor;
 125 
 126         private static final Version MIN = values()[0];
 127         /** Return the least version supported, MIN */
 128         public static Version MIN() { return MIN; }
 129 
 130         private static final Version MAX = values()[values().length-1];
 131         /** Return the largest version supported, MAX */
 132         public static Version MAX() { return MAX; }
 133     }
 134 
 135 
 136 /************************************************************************
 137  * String Translation Routines
 138  ***********************************************************************/
 139 
 140     /**
 141      * Return internal representation of buf[offset..offset+len-1], converting '/' to '.'.
 142      *
 143      * Note: the naming is the inverse of that used by JVMS 4.2 The Internal Form Of Names,
 144      * which defines "internal name" to be the form using "/" instead of "."
 145      */
 146     public static byte[] internalize(byte[] buf, int offset, int len) {
 147         byte[] translated = new byte[len];
 148         for (int j = 0; j < len; j++) {
 149             byte b = buf[offset + j];
 150             if (b == '/') translated[j] = (byte) '.';
 151             else translated[j] = b;
 152         }
 153         return translated;
 154     }
 155 
 156     /**
 157      * Return internal representation of given name, converting '/' to '.'.
 158      *
 159      * Note: the naming is the inverse of that used by JVMS 4.2 The Internal Form Of Names,
 160      * which defines "internal name" to be the form using "/" instead of "."
 161      */
 162     public static byte[] internalize(Name name) {
 163         return internalize(name.getByteArray(), name.getByteOffset(), name.getByteLength());
 164     }
 165 
 166     /**
 167      * Return external representation of buf[offset..offset+len-1], converting '.' to '/'.
 168      *
 169      * Note: the naming is the inverse of that used by JVMS 4.2 The Internal Form Of Names,
 170      * which defines "internal name" to be the form using "/" instead of "."
 171      */
 172     public static byte[] externalize(byte[] buf, int offset, int len) {
 173         byte[] translated = new byte[len];
 174         for (int j = 0; j < len; j++) {
 175             byte b = buf[offset + j];
 176             if (b == '.') translated[j] = (byte) '/';
 177             else translated[j] = b;
 178         }
 179         return translated;
 180     }
 181 
 182     /**
 183      * Return external representation of given name, converting '/' to '.'.
 184      *
 185      * Note: the naming is the inverse of that used by JVMS 4.2 The Internal Form Of Names,
 186      * which defines "internal name" to be the form using "/" instead of "."
 187      */
 188     public static byte[] externalize(Name name) {
 189         return externalize(name.getByteArray(), name.getByteOffset(), name.getByteLength());
 190     }
 191 
 192 /************************************************************************
 193  * Name-and-type
 194  ***********************************************************************/
 195 
 196     /** A class for the name-and-type signature of a method or field.
 197      */
 198     public static class NameAndType {
 199         Name name;
 200         UniqueType uniqueType;
 201         Types types;
 202 
 203         NameAndType(Name name, Type type, Types types) {
 204             this.name = name;
 205             this.uniqueType = new UniqueType(type, types);
 206             this.types = types;
 207         }
 208 
 209         void setType(Type type) {
 210             this.uniqueType = new UniqueType(type, types);
 211         }
 212 
 213         @Override
 214         public boolean equals(Object other) {
 215             return (other instanceof NameAndType &&
 216                     name == ((NameAndType) other).name &&
 217                         uniqueType.equals(((NameAndType) other).uniqueType));
 218         }
 219 
 220         @Override
 221         public int hashCode() {
 222             return name.hashCode() * uniqueType.hashCode();
 223         }
 224     }
 225 }