diff --git a/src/de/inetsoftware/classparser/Code.java b/src/de/inetsoftware/classparser/Code.java index 1ffa3bc..9e16e22 100644 --- a/src/de/inetsoftware/classparser/Code.java +++ b/src/de/inetsoftware/classparser/Code.java @@ -97,7 +97,7 @@ public class Code { } AttributeInfo data = attributes.get( "LocalVariableTable" ); if( data != null ) { - localVariableTable = new LocalVariableTable( maxLocals ); + localVariableTable = new LocalVariableTable( maxLocals, constantPool ); localVariableTable.read( data.getDataInputStream(), true ); data = attributes.get( "LocalVariableTypeTable" ); if( data != null ) { diff --git a/src/de/inetsoftware/classparser/LocalVariable.java b/src/de/inetsoftware/classparser/LocalVariable.java index 47bf730..719cfc2 100644 --- a/src/de/inetsoftware/classparser/LocalVariable.java +++ b/src/de/inetsoftware/classparser/LocalVariable.java @@ -1,5 +1,5 @@ /* - Copyright 2011 - 2017 Volker Berlin (i-net software) + Copyright 2011 - 2018 Volker Berlin (i-net software) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -24,17 +24,19 @@ import java.io.IOException; */ public class LocalVariable { - private int start_pc; + private final int start_pc; - private int length; + private final int length; - private int name_index; + private final int name_index; - private int descriptor_index; + private final int descriptor_index; - private int index; + private final int index; - private int position; + private final int position; + + private final ConstantPool constantPool; private boolean declared; @@ -44,16 +46,21 @@ public class LocalVariable { * * @param input * the stream of the class + * @param position + * the position in the LocalVariableTable + * @param constantPool + * Reference to the current ConstantPool * @throws IOException * if any I/O error occurs. */ - LocalVariable( DataInputStream input, int position ) throws IOException { + LocalVariable( DataInputStream input, int position, ConstantPool constantPool ) throws IOException { start_pc = input.readUnsignedShort(); length = input.readUnsignedShort(); name_index = input.readUnsignedShort(); descriptor_index = input.readUnsignedShort(); index = input.readUnsignedShort(); this.position = position; + this.constantPool = constantPool; } /** @@ -76,9 +83,10 @@ public class LocalVariable { /** * Get the name of the variable - * @param constantPool ConstantPool of the current class + * + * @return the name */ - public String getName( ConstantPool constantPool ) { + public String getName() { return (String)constantPool.get( name_index ); } diff --git a/src/de/inetsoftware/classparser/LocalVariableTable.java b/src/de/inetsoftware/classparser/LocalVariableTable.java index 516526c..e211fc4 100644 --- a/src/de/inetsoftware/classparser/LocalVariableTable.java +++ b/src/de/inetsoftware/classparser/LocalVariableTable.java @@ -27,6 +27,8 @@ import javax.annotation.Nullable; */ public class LocalVariableTable { + private final ConstantPool constantPool; + private LocalVariable[] tablePosition; private LocalVariable[] table; @@ -34,11 +36,17 @@ public class LocalVariableTable { private int count; /** - * @param maxLocals the count of local variables in the memory + * Create a new instance of the code attribute "LocalVariableTable". + * + * @param maxLocals + * the count of local variables in the memory + * @param constantPool + * Reference to the current ConstantPool */ - LocalVariableTable( int maxLocals ) { + LocalVariableTable( int maxLocals, ConstantPool constantPool ) { table = new LocalVariable[maxLocals]; tablePosition = new LocalVariable[maxLocals]; + this.constantPool = constantPool; } /** @@ -60,7 +68,7 @@ public class LocalVariableTable { } boolean[] wasSet = new boolean[table.length]; for( int i = 0; i < count; i++ ) { - LocalVariable var = new LocalVariable( input, i ); + LocalVariable var = new LocalVariable( input, i, constantPool ); int idx = var.getIndex(); if( withPositions ) { tablePosition[i] = var; @@ -105,6 +113,7 @@ public class LocalVariableTable { * * @param idx * the index in the memory + * @return the LocalVariable */ @Nonnull public LocalVariable get( int idx ) { @@ -116,14 +125,12 @@ public class LocalVariableTable { * * @param name * needed for evaluate the name. - * @param constantPool - * current constant pool * @return the LocalVariable or null */ @Nullable - public LocalVariable get( String name, ConstantPool constantPool ) { + public LocalVariable get( String name ) { for( int i=0; i