0

i have an example simple code :

class Test {
    void method(boolean boo){
        String b;
        int a=0;
        try
        {
            java.awt.image.BufferedImage image=null;
            new Thread().sleep(1000);
            javax.swing.JOptionPane.showMessageDialog(null,"test");
            java.io.File file=new java.io.File("C:\\file.png");
            boolean boo=file.exists();
            if(boo==true)
                image=javax.imageio.ImageIO.read(file);
        }
        catch(InterruptedException e){}
    }
}

Basically i have to use BCEL to access the byte code and reach my objective. so, i have try create simple code :

import org.apache.bcel.Repository;
import org.apache.bcel.classfile.*;

class javap
{
    public static void main(String[]args)
    {
        try
        {
            JavaClass jc = Repository.lookupClass("Test");
            ConstantPool constantPool = jc.getConstantPool();
            Method [] method=jc.getMethods();
            for (Method m : method) 
            {
                LocalVariableTable lvt=m.getLocalVariableTable();
                LocalVariable[] lv=lvt.getLocalVariableTable();
                for(LocalVariable l : lv)
                {   
                    System.out.println(l.getName()+" : "+l.getSignature());
                }
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
}

Result :

local variable : this --> LTest;
local variable : image --> Ljava/awt/image/BufferedImage;
local variable : file --> Ljava/io/File;
local variable : boo --> Z
local variable : ex --> Ljava/lang/Exception;
local variable : this --> LTest;
local variable : a --> I

How to eliminate primitive types that have been retrieved like a as int and boo as boolean, and how to retrieve unused local variable like String b ?

-Thanks-

newbie
  • 929
  • 17
  • 35
  • That `catch` in `Test` throws nothing... sigh – mazhar islam Jun 25 '15 at 06:23
  • rakeb.void: considering the contents of the try block, I think this can be overlooked here. newbie: you can use Wrapper classes to get rid of the primitives, and the line with b, you can just delete or comment out. that way, your code (where you have two variables with the name b) might become compilable. It seems to me, you don't know naming conventions, and have quite a messy code. It might suit you best to read up on the basics before starting more complex code. – Stultuske Jun 25 '15 at 06:31
  • @Stultuske, OP was not asking for optimization tips on Test. he/she was asking for tips on usage of BCEL library. I believe the code in Test is messy on purpose and anyone who does bytecode parsing is hardly a newbie – Sharon Ben Asher Jun 25 '15 at 06:34
  • Please show us the expected output. – Tagir Valeev Jun 25 '15 at 06:40
  • @sharonbn "anyone who does bytecode parsing is hardly a newbie" .. first of all: his (user)name is newbie, which is why I addressed him like that. secondly, even a newbie can copy paste code he doesn't understand, even if that code is about bytecode parsing. – Stultuske Jun 25 '15 at 06:52
  • if you noticed his/her name, you might as well have noticed his/her reputation.... – Sharon Ben Asher Jun 25 '15 at 06:55
  • this is not only unhelpful, it is also spiteful and plain mean. I wish I could vote down a comment – Sharon Ben Asher Jun 25 '15 at 07:15
  • good for you. congratulations. – Stultuske Jun 25 '15 at 07:17
  • i have update my post. – newbie Jun 25 '15 at 07:24

1 Answers1

1

Regarding retrieval of unused local variable: I ran your code and I see that the LocalVariableTable doesn't seem to contain the unsued String. When I "use" it by initializing it, the variable will be added to the table. I searched and according to this thread the JIT (or perhaps the compiler? since it is the one creating the bytecode) optimizes the code and removes the unsued var completely from the bytecode.

Community
  • 1
  • 1
Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47