[ 
https://issues.apache.org/jira/browse/BCEL-123?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13511912#comment-13511912
 ] 

Emmanuel Bourg edited comment on BCEL-123 at 2/23/15 8:23 PM:
--------------------------------------------------------------

MethodGen mistakenly fills in LocalVariableTableGen from both 
LocalVariableTable and LocalVariableTypeTable.  In parsing 
LocalVariableTypeTable, it incorrectly reads the signatures with generics.  As 
a result, the LocalVariableTable is corrupted.

Here is an example:

{code}
public class VariableTableBug
{

   public String getFirstItem( final List< String > list )
   {
      int index = 0;
      String result = list.get( index );
      return result;
   }

   public static void main( final String[] args ) throws Exception
   {
      // find the resource that is our class
      Class< VariableTableBug > clazz = VariableTableBug.class;
      String resource = clazz.getName().replace( '.', '/' ) + ".class";
      
      // load it using BCEL
      JavaClass javaClass = new ClassParser( 
clazz.getClassLoader().getResourceAsStream( resource ),
                                             resource ).parse();
      
      // convert everything to a ClassGen
      ClassGen classGen = new ClassGen( javaClass );
      
      // find them getFirstItem method
      Method getFirstItemMethod = null;
      for ( Method method : classGen.getMethods() )
      {
         if ( method.getName().equals( "getFirstItem" ) )
         {
            getFirstItemMethod = method;
            break;
         }
      }
      
      // dump the LocalVariableTable attribute
      System.out.println( "LocalVariableTable of original" );
      System.out.println( "------------------------------" );
      for ( LocalVariable localVariable : 
getFirstItemMethod.getLocalVariableTable().getLocalVariableTable() )
      {
         System.out.println( localVariable.getName() + "\t" + 
localVariable.getIndex() + "\t" + localVariable.getSignature() );
      }
      System.out.println();

      // dump the LocalVariableTypeTable attribute
      for ( Attribute attribute : getFirstItemMethod.getCode().getAttributes() )
      {
         if ( attribute instanceof LocalVariableTypeTable )
         {
            System.out.println( "LocalVariableTypeTable of original" );
            System.out.println( "----------------------------------" );
            for ( LocalVariable localVariable : ( (LocalVariableTypeTable) 
attribute ).getLocalVariableTypeTable() )
            {
               System.out.println( localVariable.getName() + "\t" + 
localVariable.getIndex() + "\t" + localVariable.getSignature() );
            }
         }
      }
      System.out.println();
      
      // now convert to a MethodGen
      MethodGen methodGen = new MethodGen( getFirstItemMethod,
                                           classGen.getClassName(),
                                           classGen.getConstantPool() );
      
      // dump the LocalVariableTable
      System.out.println( "LocalVariableTable of MethodGen" );
      System.out.println( "-------------------------------" );
      for ( LocalVariableGen localVariableGen : methodGen.getLocalVariables() )
      {
         System.out.println( localVariableGen.getName() + "\t" + 
localVariableGen.getIndex() + "\t" + localVariableGen.getType() );
      }
   }
   
}
{code}


was (Author: [email protected]):
MethodGen mistakenly fills in LocalVariableTableGen from both 
LocalVariableTable and LocalVariableTypeTable.  In parsing 
LocalVariableTypeTable, it incorrectly reads the signatures with generics.  As 
a result, the LocalVariableTable is corrupted.

Here is an example:

public class VariableTableBug
{

   public String getFirstItem( final List< String > list )
   {
      int index = 0;
      String result = list.get( index );
      return result;
   }

   public static void main( final String[] args ) throws Exception
   {
      // find the resource that is our class
      Class< VariableTableBug > clazz = VariableTableBug.class;
      String resource = clazz.getName().replace( '.', '/' ) + ".class";
      
      // load it using BCEL
      JavaClass javaClass = new ClassParser( 
clazz.getClassLoader().getResourceAsStream( resource ),
                                             resource ).parse();
      
      // convert everything to a ClassGen
      ClassGen classGen = new ClassGen( javaClass );
      
      // find them getFirstItem method
      Method getFirstItemMethod = null;
      for ( Method method : classGen.getMethods() )
      {
         if ( method.getName().equals( "getFirstItem" ) )
         {
            getFirstItemMethod = method;
            break;
         }
      }
      
      // dump the LocalVariableTable attribute
      System.out.println( "LocalVariableTable of original" );
      System.out.println( "------------------------------" );
      for ( LocalVariable localVariable : 
getFirstItemMethod.getLocalVariableTable().getLocalVariableTable() )
      {
         System.out.println( localVariable.getName() + "\t" + 
localVariable.getIndex() + "\t" + localVariable.getSignature() );
      }
      System.out.println();

      // dump the LocalVariableTypeTable attribute
      for ( Attribute attribute : getFirstItemMethod.getCode().getAttributes() )
      {
         if ( attribute instanceof LocalVariableTypeTable )
         {
            System.out.println( "LocalVariableTypeTable of original" );
            System.out.println( "----------------------------------" );
            for ( LocalVariable localVariable : ( (LocalVariableTypeTable) 
attribute ).getLocalVariableTypeTable() )
            {
               System.out.println( localVariable.getName() + "\t" + 
localVariable.getIndex() + "\t" + localVariable.getSignature() );
            }
         }
      }
      System.out.println();
      
      // now convert to a MethodGen
      MethodGen methodGen = new MethodGen( getFirstItemMethod,
                                           classGen.getClassName(),
                                           classGen.getConstantPool() );
      
      // dump the LocalVariableTable
      System.out.println( "LocalVariableTable of MethodGen" );
      System.out.println( "-------------------------------" );
      for ( LocalVariableGen localVariableGen : methodGen.getLocalVariables() )
      {
         System.out.println( localVariableGen.getName() + "\t" + 
localVariableGen.getIndex() + "\t" + localVariableGen.getType() );
      }
   }
   
}

> MethodGen: LocalVariableTableGen issues
> ---------------------------------------
>
>                 Key: BCEL-123
>                 URL: https://issues.apache.org/jira/browse/BCEL-123
>             Project: Commons BCEL
>          Issue Type: Bug
>          Components: Main
>    Affects Versions: unspecified
>         Environment: Operating System: Linux
> Platform: PC
>            Reporter: Matthew Wilson
>            Assignee: Apache Commons Developers
>




--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to