1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.classfmt;
13 abstract public class ClassFileStruct implements ClassFileConstants {
16 public ClassFileStruct(byte classFileBytes[], int off) {
17 reference = classFileBytes;
20 public ClassFileStruct (byte classFileBytes[], int off, boolean verifyStructure) {
21 reference = classFileBytes;
24 public double doubleAt(int relativeOffset) {
25 return (Double.longBitsToDouble(this.i8At(relativeOffset)));
27 public float floatAt(int relativeOffset) {
28 return (Float.intBitsToFloat(this.i4At(relativeOffset)));
30 public int i1At(int relativeOffset) {
31 return reference[relativeOffset + structOffset];
33 public int i2At(int relativeOffset) {
34 int position = relativeOffset + structOffset;
35 return (reference[position++] << 8) + (reference[position] & 0xFF);
37 public int i4At(int relativeOffset) {
38 int position = relativeOffset + structOffset;
39 return ((reference[position++] & 0xFF) << 24) + ((reference[position++] & 0xFF) << 16) + ((reference[position++] & 0xFF) << 8) + (reference[position] & 0xFF);
41 public long i8At(int relativeOffset) {
42 int position = relativeOffset + structOffset;
43 return (((long) (reference[position++] & 0xFF)) << 56)
44 + (((long) (reference[position++] & 0xFF)) << 48)
45 + (((long) (reference[position++] & 0xFF)) << 40)
46 + (((long) (reference[position++] & 0xFF)) << 32)
47 + (((long) (reference[position++] & 0xFF)) << 24)
48 + (((long) (reference[position++] & 0xFF)) << 16)
49 + (((long) (reference[position++] & 0xFF)) << 8)
50 + (reference[position++] & 0xFF);
52 public static String printTypeModifiers(int modifiers) {
54 java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
55 java.io.PrintWriter print = new java.io.PrintWriter(out);
57 if ((modifiers & AccPublic) != 0) print.print("public "); //$NON-NLS-1$
58 if ((modifiers & AccPrivate) != 0) print.print("private "); //$NON-NLS-1$
59 if ((modifiers & AccFinal) != 0) print.print("final "); //$NON-NLS-1$
60 if ((modifiers & AccSuper) != 0) print.print("super "); //$NON-NLS-1$
61 if ((modifiers & AccInterface) != 0) print.print("interface "); //$NON-NLS-1$
62 if ((modifiers & AccAbstract) != 0) print.print("abstract "); //$NON-NLS-1$
64 return out.toString();
66 public int u1At(int relativeOffset) {
67 return (reference[relativeOffset + structOffset] & 0xFF);
69 public int u2At(int relativeOffset) {
70 int position = relativeOffset + structOffset;
71 return ((reference[position++] & 0xFF) << 8) + (reference[position] & 0xFF);
73 public long u4At(int relativeOffset) {
74 int position = relativeOffset + structOffset;
75 return (((reference[position++] & 0xFFL) << 24) + ((reference[position++] & 0xFF) << 16) + ((reference[position++] & 0xFF) << 8) + (reference[position] & 0xFF));
77 public char[] utf8At(int relativeOffset, int bytesAvailable) {
78 int length = bytesAvailable;
79 char outputBuf[] = new char[bytesAvailable];
81 int readOffset = this.structOffset + relativeOffset;
84 int x = this.reference[readOffset++] & 0xFF;
86 if ((0x80 & x) != 0) {
87 if ((x & 0x20) != 0) {
89 x = ((x & 0xF) << 12) | ((this.reference[readOffset++] & 0x3F) << 6) | (this.reference[readOffset++] & 0x3F);
92 x = ((x & 0x1F) << 6) | (this.reference[readOffset++] & 0x3F);
95 outputBuf[outputPos++] = (char) x;
98 if (outputPos != bytesAvailable) {
99 System.arraycopy(outputBuf, 0, (outputBuf = new char[outputPos]), 0, outputPos);
104 protected void reset() {
105 this.reference = null;
108 public char[] utf8At(int relativeOffset, int bytesAvailable, boolean testValidity) throws ClassFormatException {
110 int length = bytesAvailable;
111 char outputBuf[] = new char[bytesAvailable];
113 int readOffset = structOffset + relativeOffset;
115 while (length != 0) {
116 x = reference[readOffset++] & 0xFF;
118 if ((0x80 & x) != 0) {
120 if ((0x40 & x) == 0) {
121 throw new ClassFormatException(ClassFormatException.ErrMalformedUtf8);
124 throw new ClassFormatException(ClassFormatException.ErrMalformedUtf8);
127 y = this.reference[readOffset++] & 0xFF;
130 if ((y & 0xC0) != 0x80) {
131 throw new ClassFormatException(ClassFormatException.ErrMalformedUtf8);
134 if ((x & 0x20) != 0) {
135 if (testValidity && (length < 1)) {
136 throw new ClassFormatException(ClassFormatException.ErrMalformedUtf8);
138 z = this.reference[readOffset++] & 0xFF;
140 if (testValidity && ((z & 0xC0) != 0x80)) {
141 throw new ClassFormatException(ClassFormatException.ErrMalformedUtf8);
143 x = ((x & 0x1F) << 12) + ((y & 0x3F) << 6) + (z & 0x3F);
144 if (testValidity && (x < 0x0800)) {
145 throw new ClassFormatException(ClassFormatException.ErrMalformedUtf8);
148 x = ((x & 0x1F) << 6) + (y & 0x3F);
149 if (testValidity && !((x == 0) || (x >= 0x80))) {
150 throw new ClassFormatException(ClassFormatException.ErrMalformedUtf8);
154 if (testValidity && x == 0) {
155 throw new ClassFormatException(ClassFormatException.ErrMalformedUtf8);
158 outputBuf[outputPos++] = (char) x;
161 if (outputPos != bytesAvailable) {
162 System.arraycopy(outputBuf, 0, (outputBuf = new char[outputPos]), 0, outputPos);
166 public static void verifyMethodNameAndSignature(char[] name, char[] signature) throws ClassFormatException {
168 // ensure name is not empty
169 if (name.length == 0) {
170 throw new ClassFormatException(ClassFormatException.ErrInvalidMethodName);
173 // if name begins with the < character it must be clinit or init
174 if (name[0] == '<') {
175 if (new String(name).equals("<clinit>") || new String(name).equals("<init>")) { //$NON-NLS-2$ //$NON-NLS-1$
176 int signatureLength = signature.length;
177 if (!((signatureLength > 2)
178 && (signature[0] == '(')
179 && (signature[signatureLength - 2] == ')')
180 && (signature[signatureLength - 1] == 'V'))) {
181 throw new ClassFormatException(ClassFormatException.ErrInvalidMethodSignature);
184 throw new ClassFormatException(ClassFormatException.ErrInvalidMethodName);