1 /*******************************************************************************
2  * Copyright (c) 2004, 2008 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.jdt.internal.core.util;
15 
16 import org.eclipse.jdt.core.util.ClassFormatException;
17 import org.eclipse.jdt.core.util.IConstantPool;
18 import org.eclipse.jdt.core.util.IStackMapFrame;
19 import org.eclipse.jdt.core.util.IVerificationTypeInfo;
20 
21 /**
22  * Default implementation of IStackMapFrame
23  */
24 public class DefaultStackMapFrame extends ClassFileStruct implements IStackMapFrame {
25 	private static final IVerificationTypeInfo[] EMPTY_LOCALS_OR_STACK_ITEMS = new IVerificationTypeInfo[0];
26 
27 	private int readOffset;
28 	private int numberOfLocals;
29 	private int numberOfStackItems;
30 	private IVerificationTypeInfo[] locals;
31 	private IVerificationTypeInfo[] stackItems;
32 	private int offsetDelta;
33 
34 	/**
35 	 * Constructor for StackMapFrame.
36 	 *
37 	 * @param classFileBytes
38 	 * @param constantPool
39 	 * @param offset
40 	 * @throws ClassFormatException
41 	 */
DefaultStackMapFrame( byte[] classFileBytes, IConstantPool constantPool, int offset)42 	public DefaultStackMapFrame(
43 			byte[] classFileBytes,
44 			IConstantPool constantPool,
45 			int offset) throws ClassFormatException {
46 		// FULL_FRAME
47 		this.offsetDelta = u2At(classFileBytes, 0, offset);
48 		int tempLocals = u2At(classFileBytes, 2, offset);
49 		this.numberOfLocals = tempLocals;
50 		this.readOffset = 4;
51 		if (tempLocals != 0) {
52 			this.locals = new IVerificationTypeInfo[tempLocals];
53 			for (int i = 0; i < tempLocals; i++) {
54 				VerificationInfo verificationInfo = new VerificationInfo(classFileBytes, constantPool, offset + this.readOffset);
55 				this.locals[i] = verificationInfo;
56 				this.readOffset += verificationInfo.sizeInBytes();
57 			}
58 		} else {
59 			this.locals = EMPTY_LOCALS_OR_STACK_ITEMS;
60 		}
61 		int tempStackItems = u2At(classFileBytes, this.readOffset, offset);
62 		this.readOffset += 2;
63 		this.numberOfStackItems = tempStackItems;
64 		if (tempStackItems != 0) {
65 			this.stackItems = new IVerificationTypeInfo[tempStackItems];
66 			for (int i = 0; i < tempStackItems; i++) {
67 				VerificationInfo verificationInfo = new VerificationInfo(classFileBytes, constantPool, offset + this.readOffset);
68 				this.stackItems[i] = verificationInfo;
69 				this.readOffset += verificationInfo.sizeInBytes();
70 			}
71 		} else {
72 			this.stackItems = EMPTY_LOCALS_OR_STACK_ITEMS;
73 		}
74 	}
sizeInBytes()75 	int sizeInBytes() {
76 		return this.readOffset;
77 	}
78 	@Override
getFrameType()79 	public int getFrameType() {
80 		return 255; // full_frame
81 	}
82 	@Override
getLocals()83 	public IVerificationTypeInfo[] getLocals() {
84 		return this.locals;
85 	}
86 	@Override
getNumberOfLocals()87 	public int getNumberOfLocals() {
88 		return this.numberOfLocals;
89 	}
90 	@Override
getNumberOfStackItems()91 	public int getNumberOfStackItems() {
92 		return this.numberOfStackItems;
93 	}
94 	@Override
getOffsetDelta()95 	public int getOffsetDelta() {
96 		return this.offsetDelta;
97 	}
98 	@Override
getStackItems()99 	public IVerificationTypeInfo[] getStackItems() {
100 		return this.stackItems;
101 	}
102 }
103