duke@1: /* jjg@1326: * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javac.jvm; duke@1: jjg@256: import com.sun.tools.javac.code.Type; vromero@1452: import com.sun.tools.javac.code.Types; vromero@1452: import com.sun.tools.javac.code.Types.UniqueType; jjg@256: import com.sun.tools.javac.util.Name; jjg@256: duke@1: duke@1: /** A JVM class file. duke@1: * duke@1: *

Generic Java classfiles have one additional attribute for classes, duke@1: * methods and fields: duke@1: *

duke@1:  *   "Signature" (u4 attr-length, u2 signature-index)
duke@1:  *  
duke@1: * duke@1: *

A signature gives the full Java type of a method or field. When duke@1: * used as a class attribute, it indicates type parameters, followed duke@1: * by supertype, followed by all interfaces. duke@1: *

duke@1:  *     methodOrFieldSignature ::= type
duke@1:  *     classSignature         ::= [ typeparams ] supertype { interfacetype }
duke@1:  *  
duke@1: *

The type syntax in signatures is extended as follows: jjg@1326: *

{@literal
duke@1:  *     type       ::= ... | classtype | methodtype | typevar
duke@1:  *     classtype  ::= classsig { '.' classsig }
duke@1:  *     classig    ::= 'L' name [typeargs] ';'
duke@1:  *     methodtype ::= [ typeparams ] '(' { type } ')' type
duke@1:  *     typevar    ::= 'T' name ';'
duke@1:  *     typeargs   ::= '<' type { type } '>'
duke@1:  *     typeparams ::= '<' typeparam { typeparam } '>'
duke@1:  *     typeparam  ::= name ':' type
jjg@1326:  *  }
duke@1: *

This class defines constants used in class files as well duke@1: * as routines to convert between internal ``.'' and external ``/'' duke@1: * separators in class names. duke@1: * jjg@581: *

This is NOT part of any supported API. jjg@581: * If you write code that depends on this, you do so at your own risk. duke@1: * This code and its internal interfaces are subject to change or duke@1: * deletion without notice. */ duke@1: public class ClassFile { duke@1: duke@1: public final static int JAVA_MAGIC = 0xCAFEBABE; duke@1: duke@1: // see Target duke@1: public final static int CONSTANT_Utf8 = 1; duke@1: public final static int CONSTANT_Unicode = 2; duke@1: public final static int CONSTANT_Integer = 3; duke@1: public final static int CONSTANT_Float = 4; duke@1: public final static int CONSTANT_Long = 5; duke@1: public final static int CONSTANT_Double = 6; duke@1: public final static int CONSTANT_Class = 7; duke@1: public final static int CONSTANT_String = 8; duke@1: public final static int CONSTANT_Fieldref = 9; duke@1: public final static int CONSTANT_Methodref = 10; duke@1: public final static int CONSTANT_InterfaceMethodref = 11; duke@1: public final static int CONSTANT_NameandType = 12; ksrini@826: public final static int CONSTANT_MethodHandle = 15; ksrini@826: public final static int CONSTANT_MethodType = 16; ksrini@826: public final static int CONSTANT_InvokeDynamic = 18; duke@1: mcimadamore@1336: public final static int REF_getField = 1; mcimadamore@1336: public final static int REF_getStatic = 2; mcimadamore@1336: public final static int REF_putField = 3; mcimadamore@1336: public final static int REF_putStatic = 4; mcimadamore@1336: public final static int REF_invokeVirtual = 5; mcimadamore@1336: public final static int REF_invokeStatic = 6; mcimadamore@1336: public final static int REF_invokeSpecial = 7; mcimadamore@1336: public final static int REF_newInvokeSpecial = 8; mcimadamore@1336: public final static int REF_invokeInterface = 9; mcimadamore@1336: duke@1: public final static int MAX_PARAMETERS = 0xff; duke@1: public final static int MAX_DIMENSIONS = 0xff; duke@1: public final static int MAX_CODE = 0xffff; duke@1: public final static int MAX_LOCALS = 0xffff; duke@1: public final static int MAX_STACK = 0xffff; duke@1: jjg@256: public enum Version { jjg@256: V45_3(45, 3), // base level for all attributes jjg@256: V49(49, 0), // JDK 1.5: enum, generics, annotations jjg@256: V50(50, 0), // JDK 1.6: stackmaps jjg@1418: V51(51, 0), // JDK 1.7 jjg@1418: V52(52, 0); // JDK 1.8: lambda, type annos, param names jjg@256: Version(int major, int minor) { jjg@256: this.major = major; jjg@256: this.minor = minor; jjg@256: } jjg@256: public final int major, minor; jjg@256: } jjg@256: duke@1: duke@1: /************************************************************************ duke@1: * String Translation Routines duke@1: ***********************************************************************/ duke@1: duke@1: /** Return internal representation of buf[offset..offset+len-1], duke@1: * converting '/' to '.'. duke@1: */ duke@1: public static byte[] internalize(byte[] buf, int offset, int len) { duke@1: byte[] translated = new byte[len]; duke@1: for (int j = 0; j < len; j++) { duke@1: byte b = buf[offset + j]; duke@1: if (b == '/') translated[j] = (byte) '.'; duke@1: else translated[j] = b; duke@1: } duke@1: return translated; duke@1: } duke@1: duke@1: /** Return internal representation of given name, duke@1: * converting '/' to '.'. duke@1: */ duke@1: public static byte[] internalize(Name name) { jjg@113: return internalize(name.getByteArray(), name.getByteOffset(), name.getByteLength()); duke@1: } duke@1: duke@1: /** Return external representation of buf[offset..offset+len-1], duke@1: * converting '.' to '/'. duke@1: */ duke@1: public static byte[] externalize(byte[] buf, int offset, int len) { duke@1: byte[] translated = new byte[len]; duke@1: for (int j = 0; j < len; j++) { duke@1: byte b = buf[offset + j]; duke@1: if (b == '.') translated[j] = (byte) '/'; duke@1: else translated[j] = b; duke@1: } duke@1: return translated; duke@1: } duke@1: duke@1: /** Return external representation of given name, duke@1: * converting '/' to '.'. duke@1: */ duke@1: public static byte[] externalize(Name name) { jjg@113: return externalize(name.getByteArray(), name.getByteOffset(), name.getByteLength()); duke@1: } duke@1: duke@1: /************************************************************************ duke@1: * Name-and-type duke@1: ***********************************************************************/ duke@1: duke@1: /** A class for the name-and-type signature of a method or field. duke@1: */ duke@1: public static class NameAndType { duke@1: Name name; vromero@1452: UniqueType uniqueType; vromero@1452: Types types; duke@1: vromero@1452: NameAndType(Name name, Type type, Types types) { duke@1: this.name = name; vromero@1452: this.uniqueType = new UniqueType(type, types); vromero@1452: this.types = types; duke@1: } duke@1: vromero@1452: void setType(Type type) { vromero@1452: this.uniqueType = new UniqueType(type, types); duke@1: } duke@1: vromero@1452: @Override vromero@1452: public boolean equals(Object other) { vromero@1452: return (other instanceof NameAndType && vromero@1452: name == ((NameAndType) other).name && vromero@1452: uniqueType.equals(((NameAndType) other).uniqueType)); vromero@1452: } vromero@1452: vromero@1452: @Override duke@1: public int hashCode() { vromero@1452: return name.hashCode() * uniqueType.hashCode(); duke@1: } duke@1: } duke@1: }