From 0b5fe06157be8d2275850c40a4c83c7494b2c7b9 Mon Sep 17 00:00:00 2001 From: sjfink Date: Wed, 10 Jan 2007 14:45:42 +0000 Subject: [PATCH] support Java 5.0 Signature attribute and small refactorings git-svn-id: https://wala.svn.sourceforge.net/svnroot/wala/trunk@546 f5eafffb-2e1d-0410-98e4-8ec43c5233c4 --- .../ibm/wala/shrikeCT/AttributeReader.java | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 com.ibm.wala.shrike/src/com/ibm/wala/shrikeCT/AttributeReader.java diff --git a/com.ibm.wala.shrike/src/com/ibm/wala/shrikeCT/AttributeReader.java b/com.ibm.wala.shrike/src/com/ibm/wala/shrikeCT/AttributeReader.java new file mode 100644 index 000000000..a0c8f6ed5 --- /dev/null +++ b/com.ibm.wala.shrike/src/com/ibm/wala/shrikeCT/AttributeReader.java @@ -0,0 +1,91 @@ +/******************************************************************************* + * Copyright (c) 2002,2006 IBM Corporation. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package com.ibm.wala.shrikeCT; + +/** + * This is a base class for "attribute readers", the classes which provide + * access to the contents of attributes. + */ +public abstract class AttributeReader { + protected ClassReader cr; + protected int attr; + protected int length; + + /** + * Construct a reader for a particular attribute. + * + * @param attr + * a valid attribute iterator pointing at the attribute to read + * @param expectedName + * the name the attribute must have + */ + protected AttributeReader(ClassReader.AttrIterator attr, String expectedName) throws InvalidClassFileException { + attr.verifyValid(); + this.cr = attr.cr; + this.attr = attr.offset; + this.length = attr.size; + + String n = attr.getName(); + if (expectedName != n && !expectedName.equals(n)) { + throw new IllegalArgumentException("Attribute " + n + " is not a " + expectedName + " attribute"); + } + } + + /** + * @return the class reader the attribute belongs to + */ + public final ClassReader getClassReader() { + return cr; + } + + /** + * @return the offset of the raw attribute data (including the attribute + * header) + */ + public final int getRawOffset() { + return attr; + } + + /** + * @return the size of the raw attribute data (including the attribute header) + */ + public final int getRawSize() { + return length; + } + + /** + * Ensure that the len bytes starting at offset fall within the attribute + * data. + * + * @throws InvalidClassFileException + * if the bytes fall outside the data + */ + protected final void checkSize(int offset, int len) throws InvalidClassFileException { + if (length < offset - attr + len) { + throw new InvalidClassFileException(offset, "Attribute data too short, expected " + len + " bytes, got " + + (length + attr - offset)); + } + } + + /** + * Ensure that the len bytes starting at offset end at the end of the + * attribute data. + * + * @throws InvalidClassFileException + * if the bytes do not end at the end of the attribute + */ + protected final void checkSizeEquals(int offset, int len) throws InvalidClassFileException { + if (length != offset - attr + len) { + throw new InvalidClassFileException(offset, "Attribute data invalid length, expected " + len + " bytes, got " + + (length + attr - offset)); + } + } +} \ No newline at end of file