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
This commit is contained in:
sjfink 2007-01-10 14:45:42 +00:00
parent eba382fe08
commit 0b5fe06157
1 changed files with 91 additions and 0 deletions

View File

@ -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));
}
}
}