Results 1 to 13 of 13
  1. #1
    Venny is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default ASM Bytecode ClassReader

    Hi All,

    Please see the code below:


    ClassReader cr;
    try {
    cr = new ClassReader("M");
    } catch (java.io.IOException e) {
    throw new RuntimeException("Can't open class M");
    }
    final ClassNode cn = new ClassNode();
    m = cn;
    cr.accept(cn, ClassReader.SKIP_DEBUG);


    My question is what does classReader actually do ? Am I supose to create a new class "M" myself or its self created. What does M actually refer to?

    Because evrytime I run this code am getting IOException that Can't open class M..

    Any help is appreciated.

    Thanks
    Venny

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Are we talking about this?

    According to the documentation a ClassReader instance is "A Java class parser to make a ClassVisitor visit an existing class". And the argument of that version of the constructor is "the fully qualified name of the class to be read".

    So basically you are supposed to already have the class and you supply its fully qualified name like com.my.package.M (self created classes aren't scheduled until Java Aleph 0).

    The home page links to tutorials for 1.5.x and 2.0.

  3. #3
    Venny is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    Hey thanks alot. I actually figured that out soon after posting the thread :)

    but i have another problem now. am running junit test case and m getting failure with this exception

    >> java.util.hashSet.add (Unknown Source)
    >> java.util.hashMap.put (unknown Source)

    Since am using these two methods in my code. do u know how to fix them ?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Venny View Post
    Hey thanks alot. I actually figured that out soon after posting the thread :)

    but i have another problem now. am running junit test case and m getting failure with this exception

    >> java.util.hashSet.add (Unknown Source)
    >> java.util.hashMap.put (unknown Source)

    Since am using these two methods in my code. do u know how to fix them ?
    What exception? I only see two class names and two method names. We are not psychic (especially not during the Weekend) so you have to give us as much (relevant) information as possible. Note that the ASM classes are not for the faint of heart and you really do have to know what you're doing.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Venny is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    Josah..
    Well I mentioned that I solved the problem of classes already. The next code was a part of some other problem. I was getting a junit failure with unkown source exception. so this part of the code is specifically for other problem .

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Venny View Post
    Josah..
    Well I mentioned that I solved the problem of classes already. The next code was a part of some other problem. I was getting a junit failure with unkown source exception. so this part of the code is specifically for other problem .
    Well, my remark in my previous reply still stands: you have to give us enough information to be able to help you.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Venny is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    Ok. Well I am not sure what information would you need for such exception. I can post the failure trace that i saw when I ran the junit test.
    Here it is, if it helps.



    java.lang.NullPointerException
    at CFG$Node.hashCode(CFG.java:32)
    at java.util.HashMap.put(Unknown Source)
    at java.util.HashSet.add(Unknown Source)
    at CFG.addNode(CFG.java:43)
    at CFGTest.addNode(CFGTest.java:48)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runRefle ctiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallabl e.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExpl osively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod .evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.e valuate(RunBefores.java:28)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunne r.java:274)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild( BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild( BlockJUnit4ClassRunner.java:48)
    at org.junit.runners.ParentRunner$3.run(ParentRunner. java:242)
    at org.junit.runners.ParentRunner$1.schedule(ParentRu nner.java:58)
    at org.junit.runners.ParentRunner.runChildren(ParentR unner.java:240)
    at org.junit.runners.ParentRunner.access$000(ParentRu nner.java:48)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRu nner.java:233)
    at org.junit.runners.ParentRunner.run(ParentRunner.ja va:303)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestR eference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecutio n.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:197)

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Venny View Post
    Ok. Well I am not sure what information would you need for such exception. I can post the failure trace that i saw when I ran the junit test.
    Here it is, if it helps.



    java.lang.NullPointerException
    at CFG$Node.hashCode(CFG.java:32)
    That's what we needed to know: your CFG$Node object is null. We don't know what it is so you have to put on your debug cap. The Node class is a nested class in the CFG class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Venny is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    Here is how it is: there is a CFG class in which i have to implement some methods and then there is CFGTest class where am testing those methods:

    for instance on of the method is addnode ():

    import java.util.Set;
    import java.util.Map;
    import java.util.HashSet;
    import java.util.HashMap;
    import java.util.Stack;
    import org.objectweb.asm.commons.*;
    import org.objectweb.asm.tree.*;

    public class CFG {
    Set<Node> nodes = new HashSet<Node>();
    Map<Node, Set<Node>> edges = new HashMap<Node, Set<Node>>();

    static class Node {
    int position;
    MethodNode method;
    ClassNode clazz;

    Node(int p, MethodNode m, ClassNode c) {
    position = p; method = m; clazz = c;
    }

    public boolean equals(Object o) {
    if (!(o instanceof Node)) return false;
    Node n = (Node)o;
    return (position == n.position) &&
    method.equals(n.method) && clazz.equals(n.clazz);
    }

    public int hashCode() {
    return position + method.hashCode() + clazz.hashCode();
    }

    public String toString() {
    return clazz.name + "." +
    method.name + method.signature + ": " + position;
    }
    }

    public void addNode(int p, MethodNode m, ClassNode c) {
    Node n1 = new Node(1000,m,c);
    nodes.add(n1);

    }


    CFGTest Class

    import org.junit.*;
    import static org.junit.Assert.*;
    import java.util.List;
    import org.objectweb.asm.*;
    import org.objectweb.asm.tree.*;
    import org.objectweb.asm.tree.analysis.*;

    public class CFGTest {
    private CFG cfg;
    private ClassNode m;
    private MethodNode m_m;

    @Before
    public void createCFG() {
    cfg = new CFG();

    ClassReader cr;
    try {
    cr = new ClassReader("CFGTest");
    } catch (java.io.IOException e) {
    throw new RuntimeException("Can't open class CFGTest");
    }
    final ClassNode cn = new ClassNode();
    m = cn;
    cr.accept(cn, ClassReader.SKIP_DEBUG);

    for (final MethodNode m : (List<MethodNode>)cn.methods) {
    if (m.name.equals("m")) m_m = m;
    int p = m.instructions.size();
    for (int i = 0; i < m.instructions.size(); i++)
    cfg.addNode(i, m, cn);

    Analyzer a = new Analyzer(new BasicInterpreter()) {
    protected void newControlFlowEdge(int insn, int succ) {
    cfg.addEdge(insn, m, cn,
    succ, m, cn);
    }
    };
    try {
    a.analyze(cn.name, m);
    } catch (AnalyzerException e) {}
    }
    }

    @Test
    public void addNode() {
    cfg.addNode(1000, m_m, m);
    CFG.Node n = new CFG.Node(1000, m_m, m);
    assertTrue(cfg.nodes.contains(n));
    }


    the value of p after debugging is shown as 8 but it should be 1000. not sure whats going wrong.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Venny View Post
    the value of p after debugging is shown as 8 but it should be 1000. not sure whats going wrong.
    Me neither; it's your code so you have to debug it, I'm not going to do it; just a tip: sprinkle in System.out.println( ... ) statements at all location you don't completely trust and see what is happening. I don't think throwing a bunch of unformatted code to us is going to help you much.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Venny is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    Can you tell me how to handle this null pointer exception ? because i am working on this since a long but not able to find a solution to this.

    Venny

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Venny View Post
    Can you tell me how to handle this null pointer exception ? because i am working on this since a long but not able to find a solution to this.
    First you have to find the method where the NPE is thrown (easy, just read the stack trace); next you have to find the variable that is null; System.out.println( ... ) every variable value and you'll find it. Now comes the hardest part: find all methods that call the method you found and put System.out.printl( ... ) statements there as well. Repeat the above steps until you've found the bug.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    Venny is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    Hi,

    I figured that out. there was an error with my code :(

    Another question: related to hashset. I think i shud post a new thread ..do answer :)

    Thanks

Similar Threads

  1. Decompilation of Bytecode to .java file
    By prassaad99 in forum New To Java
    Replies: 3
    Last Post: 07-13-2010, 08:06 AM
  2. Help with bytecode in java
    By coco in forum New To Java
    Replies: 3
    Last Post: 04-16-2009, 09:48 AM
  3. differences between bytecode and executable code
    By valery in forum New To Java
    Replies: 2
    Last Post: 04-16-2009, 09:13 AM
  4. [SOLVED] How to get the .class file which bytecode is in
    By raffaele181188 in forum New To Java
    Replies: 12
    Last Post: 04-16-2009, 08:41 AM
  5. JBE Java Bytecode Editor
    By lucnap in forum Advanced Java
    Replies: 3
    Last Post: 04-11-2009, 09:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •