add getPosition() to LocalVariable

This commit is contained in:
Volker Berlin 2017-04-09 22:21:47 +02:00
parent 14dc89aacb
commit 68aaa5c589
2 changed files with 231 additions and 219 deletions

View File

@ -1,93 +1,103 @@
/* /*
Copyright 2011 - 2017 Volker Berlin (i-net software) Copyright 2011 - 2017 Volker Berlin (i-net software)
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
You may obtain a copy of the License at You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package de.inetsoftware.classparser; package de.inetsoftware.classparser;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
/** /**
* @author Volker Berlin * @author Volker Berlin
*/ */
public class LocalVariable { public class LocalVariable {
private int start_pc; private int start_pc;
private int length; private int length;
private int name_index; private int name_index;
private int descriptor_index; private int descriptor_index;
private int index; private int index;
private boolean declared; private int position;
protected LocalVariable() { private boolean declared;
// nothing
} /**
* http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.13
/** * http://docs.oracle.com/javase/specs/jvms/se5.0/html/ClassFile.doc.html#5956
* http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.13 *
* http://docs.oracle.com/javase/specs/jvms/se5.0/html/ClassFile.doc.html#5956 * @param input
* * the stream of the class
* @param input * @throws IOException
* the stream of the class * if any I/O error occurs.
* @throws IOException */
*/ LocalVariable( DataInputStream input, int position ) throws IOException {
LocalVariable( DataInputStream input ) throws IOException { start_pc = input.readUnsignedShort();
start_pc = input.readUnsignedShort(); length = input.readUnsignedShort();
length = input.readUnsignedShort(); name_index = input.readUnsignedShort();
name_index = input.readUnsignedShort(); descriptor_index = input.readUnsignedShort();
descriptor_index = input.readUnsignedShort(); index = input.readUnsignedShort();
index = input.readUnsignedShort(); this.position = position;
} }
/** /**
* Get the index in the local variable table. * Get the index in the local variable table (memory location/slot).
* @return the index. *
*/ * @return the index.
public int getIndex() { */
return index; public int getIndex() {
} return index;
}
/**
* Get the name of the variable /**
* @param constantPool ConstantPool of the current class * Get the position in the local variable table.
*/ *
public String getName( ConstantPool constantPool ) { * @return the position
return (String)constantPool.get( name_index ); */
} public int getPosition() {
return position;
public int getDescriptorIdx() { }
return descriptor_index;
} /**
* Get the name of the variable
/** * @param constantPool ConstantPool of the current class
* Was the declaration printed? */
* @return true if already declared public String getName( ConstantPool constantPool ) {
*/ return (String)constantPool.get( name_index );
public boolean isDeclared() { }
return declared;
} public int getDescriptorIdx() {
return descriptor_index;
/** }
* Mark this variable as declared.
*/ /**
public void setDeclared() { * Was the declaration printed?
declared = true; * @return true if already declared
} */
} public boolean isDeclared() {
return declared;
}
/**
* Mark this variable as declared.
*/
public void setDeclared() {
declared = true;
}
}

View File

@ -1,126 +1,128 @@
/* /*
Copyright 2011 - 2017 Volker Berlin (i-net software) Copyright 2011 - 2017 Volker Berlin (i-net software)
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
You may obtain a copy of the License at You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package de.inetsoftware.classparser; package de.inetsoftware.classparser;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
/** /**
* @author Volker Berlin * @author Volker Berlin
*/ */
public class LocalVariableTable { public class LocalVariableTable {
private LocalVariable[] tablePosition; private LocalVariable[] tablePosition;
private LocalVariable[] table; private LocalVariable[] table;
private int count; private int count;
/** /**
* @param maxLocals the count of local variables in the memory * @param maxLocals the count of local variables in the memory
*/ */
LocalVariableTable( int maxLocals ) { LocalVariableTable( int maxLocals ) {
table = new LocalVariable[maxLocals]; table = new LocalVariable[maxLocals];
tablePosition = new LocalVariable[maxLocals]; tablePosition = new LocalVariable[maxLocals];
} }
/** /**
* http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.13 * http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.13
* http://docs.oracle.com/javase/specs/jvms/se5.0/html/ClassFile.doc.html#5956 * http://docs.oracle.com/javase/specs/jvms/se5.0/html/ClassFile.doc.html#5956
* *
* @param input * @param input
* the stream of the class * the stream of the class
* @param withPositions a hack if we find a better solution to map the positions LocalVariableTypeTable * @param withPositions
* @throws IOException * a hack if we find a better solution to map the positions LocalVariableTypeTable
*/ * @throws IOException
void read( DataInputStream input, boolean withPositions ) throws IOException { * if any I/O error occurs.
count = input.readUnsignedShort(); */
if( count > table.length ) { void read( DataInputStream input, boolean withPositions ) throws IOException {
table = new LocalVariable[count]; count = input.readUnsignedShort();
tablePosition = new LocalVariable[count]; if( count > table.length ) {
} table = new LocalVariable[count];
for( int i = 0; i < count; i++ ) { tablePosition = new LocalVariable[count];
LocalVariable var = new LocalVariable( input ); }
int idx = var.getIndex(); for( int i = 0; i < count; i++ ) {
if( withPositions ) { LocalVariable var = new LocalVariable( input, i );
tablePosition[i] = var; int idx = var.getIndex();
} if( withPositions ) {
table[idx] = var; tablePosition[i] = var;
} }
} table[idx] = var;
}
/** }
* Get the count of variables.
* @return the count /**
*/ * Get the count of variables.
public int getPositionSize() { * @return the count
return count; */
} public int getPositionSize() {
return count;
/** }
* Get the count of storage places a 4 bytes for local variables. Double and long variables need 2 of this places.
* /**
* @return the local stack size * Get the count of storage places a 4 bytes for local variables. Double and long variables need 2 of this places.
*/ *
public int getSize() { * @return the local stack size
return table.length; */
} public int getSize() {
return table.length;
/** }
* Get the LocalVariable with it position. The position is continue also with double and long variables. Or if a variable is reused from a other block.
* /**
* @param pos * Get the LocalVariable with it position. The position is continue also with double and long variables. Or if a variable is reused from a other block.
* the position *
*/ * @param pos
@Nonnull * the position
public LocalVariable getPosition( int pos ) { */
return tablePosition[pos]; @Nonnull
} public LocalVariable getPosition( int pos ) {
return tablePosition[pos];
/** }
* Get the LocalVariable with its memory location. The index has empty places with double and long variables.
* /**
* @param idx * Get the LocalVariable with its memory location (slot). The index has empty places with double and long variables.
* the index in the memory *
*/ * @param idx
@Nonnull * the index in the memory
public LocalVariable get( int idx ) { */
return table[idx]; @Nonnull
} public LocalVariable get( int idx ) {
return table[idx];
/** }
* Find a LocalVariable with a given name.
* /**
* @param name * Find a LocalVariable with a given name.
* needed for evaluate the name. *
* @param constantPool * @param name
* current constant pool * needed for evaluate the name.
* @return the LocalVariable or null * @param constantPool
*/ * current constant pool
@Nullable * @return the LocalVariable or null
public LocalVariable get( String name, ConstantPool constantPool ) { */
for( int i=0; i<table.length; i++ ){ @Nullable
if( name.equals( table[i].getName( constantPool ) )) { public LocalVariable get( String name, ConstantPool constantPool ) {
return table[i]; for( int i=0; i<table.length; i++ ){
} if( name.equals( table[i].getName( constantPool ) )) {
} return table[i];
return null; }
} }
} return null;
}
}