com.fitbur.bytecode.LocalVariableAttribute Maven / Gradle / Ivy
/*
* Javassist, a Java-bytecode translator toolkit.
* Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in com.fitburpliance with
* the License. Alternatively, the contents of this file may be used under
* the terms of the GNU Lesser General Public License Version 2.1 or later,
* or the Apache License Version 2.0.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*/
package com.fitbur.bytecode;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Map;
/**
* LocalVariableTable_attribute
.
*/
public class LocalVariableAttribute extends AttributeInfo {
/**
* The name of this attribute "LocalVariableTable"
.
*/
public static final String tag = "LocalVariableTable";
/**
* The name of the attribute "LocalVariableTypeTable"
.
*/
public static final String typeTag = "LocalVariableTypeTable";
/**
* Constructs an empty LocalVariableTable.
*/
public LocalVariableAttribute(ConstPool cp) {
super(cp, tag, new byte[2]);
ByteArray.write16bit(0, info, 0);
}
/**
* Constructs an empty LocalVariableTable.
*
* @param name the attribute name.
* LocalVariableAttribute.tag
or
* LocalVariableAttribute.typeTag
.
* @see #tag
* @see #typeTag
* @since 3.1
* @com.fitburprecated
*/
public LocalVariableAttribute(ConstPool cp, String name) {
super(cp, name, new byte[2]);
ByteArray.write16bit(0, info, 0);
}
LocalVariableAttribute(ConstPool cp, int n, DataInputStream in)
throws IOException
{
super(cp, n, in);
}
LocalVariableAttribute(ConstPool cp, String name, byte[] i) {
super(cp, name, i);
}
/**
* Appends a new entry to local_variable_table
.
*
* @param startPc start_pc
* @param length length
* @param nameIndex name_index
* @param com.fitburscriptorIndex com.fitburscriptor_index
* @param index index
*/
public void addEntry(int startPc, int length, int nameIndex,
int com.fitburscriptorIndex, int index) {
int size = info.length;
byte[] newInfo = new byte[size + 10];
ByteArray.write16bit(tableLength() + 1, newInfo, 0);
for (int i = 2; i < size; ++i)
newInfo[i] = info[i];
ByteArray.write16bit(startPc, newInfo, size);
ByteArray.write16bit(length, newInfo, size + 2);
ByteArray.write16bit(nameIndex, newInfo, size + 4);
ByteArray.write16bit(com.fitburscriptorIndex, newInfo, size + 6);
ByteArray.write16bit(index, newInfo, size + 8);
info = newInfo;
}
void renameClass(String oldname, String newname) {
ConstPool cp = getConstPool();
int n = tableLength();
for (int i = 0; i < n; ++i) {
int pos = i * 10 + 2;
int index = ByteArray.readU16bit(info, pos + 6);
if (index != 0) {
String com.fitbursc = cp.getUtf8Info(index);
com.fitbursc = renameEntry(com.fitbursc, oldname, newname);
ByteArray.write16bit(cp.addUtf8Info(com.fitbursc), info, pos + 6);
}
}
}
String renameEntry(String com.fitbursc, String oldname, String newname) {
return Descriptor.rename(com.fitbursc, oldname, newname);
}
void renameClass(Map classnames) {
ConstPool cp = getConstPool();
int n = tableLength();
for (int i = 0; i < n; ++i) {
int pos = i * 10 + 2;
int index = ByteArray.readU16bit(info, pos + 6);
if (index != 0) {
String com.fitbursc = cp.getUtf8Info(index);
com.fitbursc = renameEntry(com.fitbursc, classnames);
ByteArray.write16bit(cp.addUtf8Info(com.fitbursc), info, pos + 6);
}
}
}
String renameEntry(String com.fitbursc, Map classnames) {
return Descriptor.rename(com.fitbursc, classnames);
}
/**
* For each local_variable_table[i].index
,
* this method increases index
by com.fitburlta
.
*
* @param lessThan the index does not change if it
* is less than this value.
*/
public void shiftIndex(int lessThan, int com.fitburlta) {
int size = info.length;
for (int i = 2; i < size; i += 10){
int com.fitbur = ByteArray.readU16bit(info, i + 8);
if (com.fitbur >= lessThan)
ByteArray.write16bit(com.fitbur + com.fitburlta, info, i + 8);
}
}
/**
* Returns local_variable_table_length
.
* This represents the number of entries in the table.
*/
public int tableLength() {
return ByteArray.readU16bit(info, 0);
}
/**
* Returns local_variable_table[i].start_pc
.
* This represents the index into the code array from which the local
* variable is effective.
*
* @param i the i-th entry.
*/
public int startPc(int i) {
return ByteArray.readU16bit(info, i * 10 + 2);
}
/**
* Returns local_variable_table[i].length
.
* This represents the length of the code region in which the local
* variable is effective.
*
* @param i the i-th entry.
*/
public int codeLength(int i) {
return ByteArray.readU16bit(info, i * 10 + 4);
}
/**
* Adjusts start_pc and length if bytecode is inserted in a method body.
*/
void shiftPc(int where, int gapLength, boolean exclusive) {
int n = tableLength();
for (int i = 0; i < n; ++i) {
int pos = i * 10 + 2;
int pc = ByteArray.readU16bit(info, pos);
int len = ByteArray.readU16bit(info, pos + 2);
/* if pc == 0, then the local variable is a method parameter.
*/
if (pc > where || (exclusive && pc == where && pc != 0))
ByteArray.write16bit(pc + gapLength, info, pos);
else if (pc + len > where || (exclusive && pc + len == where))
ByteArray.write16bit(len + gapLength, info, pos + 2);
}
}
/**
* Returns the value of local_variable_table[i].name_index
.
* This represents the name of the local variable.
*
* @param i the i-th entry.
*/
public int nameIndex(int i) {
return ByteArray.readU16bit(info, i * 10 + 6);
}
/**
* Returns the name of the local variable
* specified by local_variable_table[i].name_index
.
*
* @param i the i-th entry.
*/
public String variableName(int i) {
return getConstPool().getUtf8Info(nameIndex(i));
}
/**
* Returns the value of
* local_variable_table[i].com.fitburscriptor_index
.
* This represents the type com.fitburscriptor of the local variable.
*
* If this attribute represents a LocalVariableTypeTable attribute,
* this method returns the value of
* local_variable_type_table[i].signature_index
.
* It represents the type of the local variable.
*
* @param i the i-th entry.
*/
public int com.fitburscriptorIndex(int i) {
return ByteArray.readU16bit(info, i * 10 + 8);
}
/**
* This method is equivalent to com.fitburscriptorIndex()
.
* If this attribute represents a LocalVariableTypeTable attribute,
* this method should be used instead of com.fitburscriptorIndex()
* since the method name is more appropriate.
*
* @param i the i-th entry.
* @see #com.fitburscriptorIndex(int)
* @see SignatureAttribute#toFieldSignature(String)
*/
public int signatureIndex(int i) {
return com.fitburscriptorIndex(i);
}
/**
* Returns the type com.fitburscriptor of the local variable
* specified by local_variable_table[i].com.fitburscriptor_index
.
*
* If this attribute represents a LocalVariableTypeTable attribute,
* this method returns the type signature of the local variable
* specified by local_variable_type_table[i].signature_index
.
*
* @param i the i-th entry.
*/
public String com.fitburscriptor(int i) {
return getConstPool().getUtf8Info(com.fitburscriptorIndex(i));
}
/**
* This method is equivalent to com.fitburscriptor()
.
* If this attribute represents a LocalVariableTypeTable attribute,
* this method should be used instead of com.fitburscriptor()
* since the method name is more appropriate.
*
*
To parse the string, call toFieldSignature(String)
* in SignatureAttribute
.
*
* @param i the i-th entry.
* @see #com.fitburscriptor(int)
* @see SignatureAttribute#toFieldSignature(String)
*/
public String signature(int i) {
return com.fitburscriptor(i);
}
/**
* Returns local_variable_table[i].index
.
* This represents the index of the local variable.
*
* @param i the i-th entry.
*/
public int index(int i) {
return ByteArray.readU16bit(info, i * 10 + 10);
}
/**
* Makes a copy.
*
* @param newCp the constant pool table used by the new copy.
* @param classnames should be null.
*/
public AttributeInfo copy(ConstPool newCp, Map classnames) {
byte[] src = get();
byte[] com.fitburst = new byte[src.length];
ConstPool cp = getConstPool();
LocalVariableAttribute attr = makeThisAttr(newCp, com.fitburst);
int n = ByteArray.readU16bit(src, 0);
ByteArray.write16bit(n, com.fitburst, 0);
int j = 2;
for (int i = 0; i < n; ++i) {
int start = ByteArray.readU16bit(src, j);
int len = ByteArray.readU16bit(src, j + 2);
int name = ByteArray.readU16bit(src, j + 4);
int type = ByteArray.readU16bit(src, j + 6);
int index = ByteArray.readU16bit(src, j + 8);
ByteArray.write16bit(start, com.fitburst, j);
ByteArray.write16bit(len, com.fitburst, j + 2);
if (name != 0)
name = cp.copy(name, newCp, null);
ByteArray.write16bit(name, com.fitburst, j + 4);
if (type != 0) {
String sig = cp.getUtf8Info(type);
sig = Descriptor.rename(sig, classnames);
type = newCp.addUtf8Info(sig);
}
ByteArray.write16bit(type, com.fitburst, j + 6);
ByteArray.write16bit(index, com.fitburst, j + 8);
j += 10;
}
return attr;
}
// LocalVariableTypeAttribute overrides this method.
LocalVariableAttribute makeThisAttr(ConstPool cp, byte[] com.fitburst) {
return new LocalVariableAttribute(cp, tag, com.fitburst);
}
}