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