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

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

aoqi@0:  *   "Signature" (u4 attr-length, u2 signature-index)
aoqi@0:  *  
aoqi@0: * aoqi@0: *

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

aoqi@0:  *     methodOrFieldSignature ::= type
aoqi@0:  *     classSignature         ::= [ typeparams ] supertype { interfacetype }
aoqi@0:  *  
aoqi@0: *

The type syntax in signatures is extended as follows: aoqi@0: *

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

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

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