Reputation: 198418
JavaParser is a java source code parsing tool. I read the documents but found it only able to parse source of a full java class, like:
public class X {
public void show(String id) {
Question q = Quesiton.findById(id);
aaa.BBB.render(q);
}
}
But I want to parse only part of it, e.g. the method declaration:
public void show(String id) {
Question q = Quesiton.findById(id);
aaa.BBB.render(q);
}
How to do it or is it possible? If not, is there any other tool can do this?
Update
Actually I want to parse any valid expression in java, so I can do this easily with JavaParser:
CompilationUnit unit = JavaParser.parse(codeInputStream);
addField(unit, parseField("public String name")); // not possible now
Upvotes: 1
Views: 1230
Reputation: 453
I see you can include the method in a dummy class and parse it as usual. For the example you provided, enclose it inside:
public class DUMMY_CLASS_FOO {
// The string you're trying to parse (method or whatever)
}
Then you parse it as usual and neglect your dummy class while parsing.
UPDATE:
For the update you provided, you may try and catch
If the previous enclosure didn't do it, so enclose it into:
public class DUMMY_CLASS_FOO {
public void DUMMY_METHOD_FOO {
// Anything here
}
}
You might also search for Access Level Modifiers (public, private, ...etc), and if found, do the 1st solution. If not found, do the other one.
Upvotes: 2