|
Packit |
5e46da |
/***
|
|
Packit |
5e46da |
* ASM: a very small and fast Java bytecode manipulation framework
|
|
Packit |
5e46da |
* Copyright (c) 2000-2011 INRIA, France Telecom
|
|
Packit |
5e46da |
* All rights reserved.
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* Redistribution and use in source and binary forms, with or without
|
|
Packit |
5e46da |
* modification, are permitted provided that the following conditions
|
|
Packit |
5e46da |
* are met:
|
|
Packit |
5e46da |
* 1. Redistributions of source code must retain the above copyright
|
|
Packit |
5e46da |
* notice, this list of conditions and the following disclaimer.
|
|
Packit |
5e46da |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
Packit |
5e46da |
* notice, this list of conditions and the following disclaimer in the
|
|
Packit |
5e46da |
* documentation and/or other materials provided with the distribution.
|
|
Packit |
5e46da |
* 3. Neither the name of the copyright holders nor the names of its
|
|
Packit |
5e46da |
* contributors may be used to endorse or promote products derived from
|
|
Packit |
5e46da |
* this software without specific prior written permission.
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
Packit |
5e46da |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
5e46da |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
5e46da |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
Packit |
5e46da |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
Packit |
5e46da |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
Packit |
5e46da |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
Packit |
5e46da |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
Packit |
5e46da |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
Packit |
5e46da |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
|
Packit |
5e46da |
* THE POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
5e46da |
*/
|
|
Packit |
5e46da |
package org.objectweb.asm;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
/**
|
|
Packit |
5e46da |
* A non standard class, field, method or code attribute.
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* @author Eric Bruneton
|
|
Packit |
5e46da |
* @author Eugene Kuleshov
|
|
Packit |
5e46da |
*/
|
|
Packit |
5e46da |
public class Attribute {
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
/**
|
|
Packit |
5e46da |
* The type of this attribute.
|
|
Packit |
5e46da |
*/
|
|
Packit |
5e46da |
public final String type;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
/**
|
|
Packit |
5e46da |
* The raw value of this attribute, used only for unknown attributes.
|
|
Packit |
5e46da |
*/
|
|
Packit |
5e46da |
byte[] value;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
/**
|
|
Packit |
5e46da |
* The next attribute in this attribute list. May be <tt>null</tt>.
|
|
Packit |
5e46da |
*/
|
|
Packit |
5e46da |
Attribute next;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
/**
|
|
Packit |
5e46da |
* Constructs a new empty attribute.
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* @param type
|
|
Packit |
5e46da |
* the type of the attribute.
|
|
Packit |
5e46da |
*/
|
|
Packit |
5e46da |
protected Attribute(final String type) {
|
|
Packit |
5e46da |
this.type = type;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
/**
|
|
Packit |
5e46da |
* Returns <tt>true</tt> if this type of attribute is unknown. The default
|
|
Packit |
5e46da |
* implementation of this method always returns <tt>true</tt>.
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* @return <tt>true</tt> if this type of attribute is unknown.
|
|
Packit |
5e46da |
*/
|
|
Packit |
5e46da |
public boolean isUnknown() {
|
|
Packit |
5e46da |
return true;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
/**
|
|
Packit |
5e46da |
* Returns <tt>true</tt> if this type of attribute is a code attribute.
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* @return <tt>true</tt> if this type of attribute is a code attribute.
|
|
Packit |
5e46da |
*/
|
|
Packit |
5e46da |
public boolean isCodeAttribute() {
|
|
Packit |
5e46da |
return false;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
/**
|
|
Packit |
5e46da |
* Returns the labels corresponding to this attribute.
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* @return the labels corresponding to this attribute, or <tt>null</tt> if
|
|
Packit |
5e46da |
* this attribute is not a code attribute that contains labels.
|
|
Packit |
5e46da |
*/
|
|
Packit |
5e46da |
protected Label[] getLabels() {
|
|
Packit |
5e46da |
return null;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
/**
|
|
Packit |
5e46da |
* Reads a {@link #type type} attribute. This method must return a
|
|
Packit |
5e46da |
* new {@link Attribute} object, of type {@link #type type},
|
|
Packit |
5e46da |
* corresponding to the <tt>len</tt> bytes starting at the given offset, in
|
|
Packit |
5e46da |
* the given class reader.
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* @param cr
|
|
Packit |
5e46da |
* the class that contains the attribute to be read.
|
|
Packit |
5e46da |
* @param off
|
|
Packit |
5e46da |
* index of the first byte of the attribute's content in
|
|
Packit |
5e46da |
* {@link ClassReader#b cr.b}. The 6 attribute header bytes,
|
|
Packit |
5e46da |
* containing the type and the length of the attribute, are not
|
|
Packit |
5e46da |
* taken into account here.
|
|
Packit |
5e46da |
* @param len
|
|
Packit |
5e46da |
* the length of the attribute's content.
|
|
Packit |
5e46da |
* @param buf
|
|
Packit |
5e46da |
* buffer to be used to call {@link ClassReader#readUTF8
|
|
Packit |
5e46da |
* readUTF8}, {@link ClassReader#readClass(int,char[]) readClass}
|
|
Packit |
5e46da |
* or {@link ClassReader#readConst readConst}.
|
|
Packit |
5e46da |
* @param codeOff
|
|
Packit |
5e46da |
* index of the first byte of code's attribute content in
|
|
Packit |
5e46da |
* {@link ClassReader#b cr.b}, or -1 if the attribute to be read
|
|
Packit |
5e46da |
* is not a code attribute. The 6 attribute header bytes,
|
|
Packit |
5e46da |
* containing the type and the length of the attribute, are not
|
|
Packit |
5e46da |
* taken into account here.
|
|
Packit |
5e46da |
* @param labels
|
|
Packit |
5e46da |
* the labels of the method's code, or <tt>null</tt> if the
|
|
Packit |
5e46da |
* attribute to be read is not a code attribute.
|
|
Packit |
5e46da |
* @return a new {@link Attribute} object corresponding to the given
|
|
Packit |
5e46da |
* bytes.
|
|
Packit |
5e46da |
*/
|
|
Packit |
5e46da |
protected Attribute read(final ClassReader cr, final int off,
|
|
Packit |
5e46da |
final int len, final char[] buf, final int codeOff,
|
|
Packit |
5e46da |
final Label[] labels) {
|
|
Packit |
5e46da |
Attribute attr = new Attribute(type);
|
|
Packit |
5e46da |
attr.value = new byte[len];
|
|
Packit |
5e46da |
System.arraycopy(cr.b, off, attr.value, 0, len);
|
|
Packit |
5e46da |
return attr;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
/**
|
|
Packit |
5e46da |
* Returns the byte array form of this attribute.
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* @param cw
|
|
Packit |
5e46da |
* the class to which this attribute must be added. This
|
|
Packit |
5e46da |
* parameter can be used to add to the constant pool of this
|
|
Packit |
5e46da |
* class the items that corresponds to this attribute.
|
|
Packit |
5e46da |
* @param code
|
|
Packit |
5e46da |
* the bytecode of the method corresponding to this code
|
|
Packit |
5e46da |
* attribute, or <tt>null</tt> if this attribute is not a code
|
|
Packit |
5e46da |
* attributes.
|
|
Packit |
5e46da |
* @param len
|
|
Packit |
5e46da |
* the length of the bytecode of the method corresponding to this
|
|
Packit |
5e46da |
* code attribute, or <tt>null</tt> if this attribute is not a
|
|
Packit |
5e46da |
* code attribute.
|
|
Packit |
5e46da |
* @param maxStack
|
|
Packit |
5e46da |
* the maximum stack size of the method corresponding to this
|
|
Packit |
5e46da |
* code attribute, or -1 if this attribute is not a code
|
|
Packit |
5e46da |
* attribute.
|
|
Packit |
5e46da |
* @param maxLocals
|
|
Packit |
5e46da |
* the maximum number of local variables of the method
|
|
Packit |
5e46da |
* corresponding to this code attribute, or -1 if this attribute
|
|
Packit |
5e46da |
* is not a code attribute.
|
|
Packit |
5e46da |
* @return the byte array form of this attribute.
|
|
Packit |
5e46da |
*/
|
|
Packit |
5e46da |
protected ByteVector write(final ClassWriter cw, final byte[] code,
|
|
Packit |
5e46da |
final int len, final int maxStack, final int maxLocals) {
|
|
Packit |
5e46da |
ByteVector v = new ByteVector();
|
|
Packit |
5e46da |
v.data = value;
|
|
Packit |
5e46da |
v.length = value.length;
|
|
Packit |
5e46da |
return v;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
/**
|
|
Packit |
5e46da |
* Returns the length of the attribute list that begins with this attribute.
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* @return the length of the attribute list that begins with this attribute.
|
|
Packit |
5e46da |
*/
|
|
Packit |
5e46da |
final int getCount() {
|
|
Packit |
5e46da |
int count = 0;
|
|
Packit |
5e46da |
Attribute attr = this;
|
|
Packit |
5e46da |
while (attr != null) {
|
|
Packit |
5e46da |
count += 1;
|
|
Packit |
5e46da |
attr = attr.next;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
return count;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
/**
|
|
Packit |
5e46da |
* Returns the size of all the attributes in this attribute list.
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* @param cw
|
|
Packit |
5e46da |
* the class writer to be used to convert the attributes into
|
|
Packit |
5e46da |
* byte arrays, with the {@link #write write} method.
|
|
Packit |
5e46da |
* @param code
|
|
Packit |
5e46da |
* the bytecode of the method corresponding to these code
|
|
Packit |
5e46da |
* attributes, or <tt>null</tt> if these attributes are not code
|
|
Packit |
5e46da |
* attributes.
|
|
Packit |
5e46da |
* @param len
|
|
Packit |
5e46da |
* the length of the bytecode of the method corresponding to
|
|
Packit |
5e46da |
* these code attributes, or <tt>null</tt> if these attributes
|
|
Packit |
5e46da |
* are not code attributes.
|
|
Packit |
5e46da |
* @param maxStack
|
|
Packit |
5e46da |
* the maximum stack size of the method corresponding to these
|
|
Packit |
5e46da |
* code attributes, or -1 if these attributes are not code
|
|
Packit |
5e46da |
* attributes.
|
|
Packit |
5e46da |
* @param maxLocals
|
|
Packit |
5e46da |
* the maximum number of local variables of the method
|
|
Packit |
5e46da |
* corresponding to these code attributes, or -1 if these
|
|
Packit |
5e46da |
* attributes are not code attributes.
|
|
Packit |
5e46da |
* @return the size of all the attributes in this attribute list. This size
|
|
Packit |
5e46da |
* includes the size of the attribute headers.
|
|
Packit |
5e46da |
*/
|
|
Packit |
5e46da |
final int getSize(final ClassWriter cw, final byte[] code, final int len,
|
|
Packit |
5e46da |
final int maxStack, final int maxLocals) {
|
|
Packit |
5e46da |
Attribute attr = this;
|
|
Packit |
5e46da |
int size = 0;
|
|
Packit |
5e46da |
while (attr != null) {
|
|
Packit |
5e46da |
cw.newUTF8(attr.type);
|
|
Packit |
5e46da |
size += attr.write(cw, code, len, maxStack, maxLocals).length + 6;
|
|
Packit |
5e46da |
attr = attr.next;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
return size;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
/**
|
|
Packit |
5e46da |
* Writes all the attributes of this attribute list in the given byte
|
|
Packit |
5e46da |
* vector.
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* @param cw
|
|
Packit |
5e46da |
* the class writer to be used to convert the attributes into
|
|
Packit |
5e46da |
* byte arrays, with the {@link #write write} method.
|
|
Packit |
5e46da |
* @param code
|
|
Packit |
5e46da |
* the bytecode of the method corresponding to these code
|
|
Packit |
5e46da |
* attributes, or <tt>null</tt> if these attributes are not code
|
|
Packit |
5e46da |
* attributes.
|
|
Packit |
5e46da |
* @param len
|
|
Packit |
5e46da |
* the length of the bytecode of the method corresponding to
|
|
Packit |
5e46da |
* these code attributes, or <tt>null</tt> if these attributes
|
|
Packit |
5e46da |
* are not code attributes.
|
|
Packit |
5e46da |
* @param maxStack
|
|
Packit |
5e46da |
* the maximum stack size of the method corresponding to these
|
|
Packit |
5e46da |
* code attributes, or -1 if these attributes are not code
|
|
Packit |
5e46da |
* attributes.
|
|
Packit |
5e46da |
* @param maxLocals
|
|
Packit |
5e46da |
* the maximum number of local variables of the method
|
|
Packit |
5e46da |
* corresponding to these code attributes, or -1 if these
|
|
Packit |
5e46da |
* attributes are not code attributes.
|
|
Packit |
5e46da |
* @param out
|
|
Packit |
5e46da |
* where the attributes must be written.
|
|
Packit |
5e46da |
*/
|
|
Packit |
5e46da |
final void put(final ClassWriter cw, final byte[] code, final int len,
|
|
Packit |
5e46da |
final int maxStack, final int maxLocals, final ByteVector out) {
|
|
Packit |
5e46da |
Attribute attr = this;
|
|
Packit |
5e46da |
while (attr != null) {
|
|
Packit |
5e46da |
ByteVector b = attr.write(cw, code, len, maxStack, maxLocals);
|
|
Packit |
5e46da |
out.putShort(cw.newUTF8(attr.type)).putInt(b.length);
|
|
Packit |
5e46da |
out.putByteArray(b.data, 0, b.length);
|
|
Packit |
5e46da |
attr = attr.next;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|