Results 1 to 13 of 13
Thread: ASM Bytecode ClassReader
- 01-30-2011, 07:41 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
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
- 01-30-2011, 09:26 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 18
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.
- 01-30-2011, 10:34 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
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 ?
- 01-30-2011, 10:58 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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,
JosBuild a wall around Donald Trump; I'll pay for it.
- 01-30-2011, 11:37 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
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 .
- 01-30-2011, 11:44 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 01-30-2011, 11:57 AM #7
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
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)
- 01-30-2011, 12:22 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 01-30-2011, 12:46 PM #9
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
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.
- 01-30-2011, 12:55 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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,
JosBuild a wall around Donald Trump; I'll pay for it.
- 01-31-2011, 06:57 AM #11
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
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
- 01-31-2011, 08:31 AM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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,
JosBuild a wall around Donald Trump; I'll pay for it.
- 02-02-2011, 04:21 AM #13
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
Decompilation of Bytecode to .java file
By prassaad99 in forum New To JavaReplies: 3Last Post: 07-13-2010, 08:06 AM -
Help with bytecode in java
By coco in forum New To JavaReplies: 3Last Post: 04-16-2009, 09:48 AM -
differences between bytecode and executable code
By valery in forum New To JavaReplies: 2Last Post: 04-16-2009, 09:13 AM -
[SOLVED] How to get the .class file which bytecode is in
By raffaele181188 in forum New To JavaReplies: 12Last Post: 04-16-2009, 08:41 AM -
JBE Java Bytecode Editor
By lucnap in forum Advanced JavaReplies: 3Last Post: 04-11-2009, 09:00 PM
Bookmarks