1

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
durron597
  • 31,968
  • 17
  • 99
  • 158
Freewind
  • 193,756
  • 157
  • 432
  • 708
  • MethodVisitor does not serve your purpose? Please check the link http://code.google.com/p/javaparser/wiki/UsingThisParser (Section -->Visiting class methods) – sreemanth pulagam Nov 26 '12 at 05:54

1 Answers1

2

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.

Hazem Elraffiee
  • 453
  • 7
  • 16