How can I recover .class references separate from other references to classes using BCEL?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How can I recover .class references separate from other references to classes using BCEL?

Showalter, Jim
The use case is, suppose you want to recover the source from a .class file.

In the original source, it said:

public class SomeExperiment {
     public static void main(String[] args) {
     System.out.println(SomeOtherClass.class); <<< WANT TO RECOVER THE ".class" FROM HERE
     }
}

In the visitor I wrote, there's an override:

     public void visitConstantClass(final ConstantClass obj) {
           String className = JavaUtils.dotsToSlashes(javaClass().getConstantPool().getConstant(obj.getNameIndex()).toString());
          System.out.println(class file name + ": " + className);
     }

Dumping out the data on a .class from a .java file that contains no occurrences of the string ".class", I get:

com/intuit/qbo/Misc.class: com/intuit/qbo/Misc$Reader
com/intuit/qbo/Misc.class: com/intuit/qbo/util/SomeException
com/intuit/qbo/Misc.class: com/intuit/qbo/Misc
com/intuit/qbo/Misc.class: com/intuit/qbo/api/MiscIf

This means ClassConstant isn't just for .class references. It's for all references to classes.

How can I use BCEL to recover the original ".class" reference in SomeExperiment?

Jim Showalter
Programmer
Intuit, 2003H-265Z
Garcia Avenue
Mountain View, CA 94043
(408) 353-4954
(408) 204-1661

Reply | Threaded
Open this post in threaded view
|

[BCEL] Re: How can I recover .class references separate from other references to classes using BCEL?

Stephan Herrmann
quick guess: check if the previous instruction is ldc.

HTH,
Stephan

On 10.09.2017 21:50, Showalter, Jim wrote:

> The use case is, suppose you want to recover the source from a .class file.
>
> In the original source, it said:
>
> public class SomeExperiment {
>       public static void main(String[] args) {
>       System.out.println(SomeOtherClass.class); <<< WANT TO RECOVER THE ".class" FROM HERE
>       }
> }
>
> In the visitor I wrote, there's an override:
>
>       public void visitConstantClass(final ConstantClass obj) {
>             String className = JavaUtils.dotsToSlashes(javaClass().getConstantPool().getConstant(obj.getNameIndex()).toString());
>            System.out.println(class file name + ": " + className);
>       }
>
> Dumping out the data on a .class from a .java file that contains no occurrences of the string ".class", I get:
>
> com/intuit/qbo/Misc.class: com/intuit/qbo/Misc$Reader
> com/intuit/qbo/Misc.class: com/intuit/qbo/util/SomeException
> com/intuit/qbo/Misc.class: com/intuit/qbo/Misc
> com/intuit/qbo/Misc.class: com/intuit/qbo/api/MiscIf
>
> This means ClassConstant isn't just for .class references. It's for all references to classes.
>
> How can I use BCEL to recover the original ".class" reference in SomeExperiment?
>
> Jim Showalter
> Programmer
> Intuit, 2003H-265Z
> Garcia Avenue
> Mountain View, CA 94043
> (408) 353-4954
> (408) 204-1661
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: [BCEL] Re: How can I recover .class references separate from other references to classes using BCEL?

Showalter, Jim
This works:

        public void visitCode(final Code obj) {
                ConstantPoolGen cpg = new ConstantPoolGen(javaClass.getConstantPool());
                InstructionList list = new InstructionList(obj.getCode());
                Iterator<InstructionHandle> iter = list.iterator();
                while (iter.hasNext()) {
                        Instruction instruction = ((InstructionHandle)iter.next()).getInstruction();
                        if (instruction instanceof LDC) {
                                LDC load = (LDC)instruction;
                                if (javaClass().getConstantPool().getConstant(load.getIndex()).getTag() == Const.CONSTANT_Class) {
                                        Object val = load.getValue(cpg);
                                        System.out.println(type.classFileName() + ": " + val);
                                }
                        }
                }
        }

-----Original Message-----
From: Stephan Herrmann [mailto:[hidden email]]
Sent: Sunday, September 10, 2017 1:12 PM
To: [hidden email]
Subject: [BCEL] Re: How can I recover .class references separate from other references to classes using BCEL?

quick guess: check if the previous instruction is ldc.

HTH,
Stephan

On 10.09.2017 21:50, Showalter, Jim wrote:

> The use case is, suppose you want to recover the source from a .class file.
>
> In the original source, it said:
>
> public class SomeExperiment {
>       public static void main(String[] args) {
>       System.out.println(SomeOtherClass.class); <<< WANT TO RECOVER THE ".class" FROM HERE
>       }
> }
>
> In the visitor I wrote, there's an override:
>
>       public void visitConstantClass(final ConstantClass obj) {
>             String className = JavaUtils.dotsToSlashes(javaClass().getConstantPool().getConstant(obj.getNameIndex()).toString());
>            System.out.println(class file name + ": " + className);
>       }
>
> Dumping out the data on a .class from a .java file that contains no occurrences of the string ".class", I get:
>
> com/intuit/qbo/Misc.class: com/intuit/qbo/Misc$Reader
> com/intuit/qbo/Misc.class: com/intuit/qbo/util/SomeException
> com/intuit/qbo/Misc.class: com/intuit/qbo/Misc
> com/intuit/qbo/Misc.class: com/intuit/qbo/api/MiscIf
>
> This means ClassConstant isn't just for .class references. It's for all references to classes.
>
> How can I use BCEL to recover the original ".class" reference in SomeExperiment?
>
> Jim Showalter
> Programmer
> Intuit, 2003H-265Z
> Garcia Avenue
> Mountain View, CA 94043
> (408) 353-4954
> (408) 204-1661
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]