Results 1 to 6 of 6
Like Tree2Likes
  • 2 Post By JosAH

Thread: Regular expession that matchs a Java Method declaration and body

  1. #1
    rmp
    rmp is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default Regular expession that matchs a Java Method declaration and body

    Hi,

    I need a Regex that will match a java method declaration, as well as body.

    // Create a pattern to match the java methods
    Pattern p = Pattern.compile(Regex);

    thanks in advance
    ~parizi

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Regular expession that matchs a Java Method declaration and body

    Have you an idea?
    What says google?
    Regex to match a Java method signature - Stack Overflow
    Regex that Will Match a Java Method Declaration - Stack Overflow

    The method must have a return value?
    That could be be complicated (generics and so on) ....Java regex for a method signature with a Collection<GenericType> return type - Stack Overflow

    Why do you need that?

  3. #3
    rmp
    rmp is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default Re: Regular expession that matchs a Java Method declaration and body

    thanks for the reply. I have already checked out the links that you'd sent to me. I got some ideas but i am still not able to make out the pattern to match the methods. yeah, it might have return value. I need this for part of my testing approach's implementation, where it should automatically extract methods.

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

    Default Re: Regular expession that matchs a Java Method declaration and body

    Quote Originally Posted by rmp View Post
    I need a Regex that will match a java method declaration, as well as body.

    // Create a pattern to match the java methods
    Pattern p = Pattern.compile(Regex);
    Java is not a regular (type 3) language, it's a context free (type 2) language; regular expressions can only parse/match/recognize regular languages so it can't be done.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    rmp
    rmp is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default Re: Regular expession that matchs a Java Method declaration and body

    thanks Jos! you are right, but i believe it still can be done. The only matter is how to use a proper combination of Regular Expressions Constructs to come up with the desired pattern. Anyway, what would be your idea (other than regex) in order to help automated extracting of methods from a Java class?

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

    Default Re: Regular expession that matchs a Java Method declaration and body

    Quote Originally Posted by rmp View Post
    thanks Jos! you are right, but i believe it still can be done. The only matter is how to use a proper combination of Regular Expressions Constructs to come up with the desired pattern. Anyway, what would be your idea (other than regex) in order to help automated extracting of methods from a Java class?
    Belief only leads to religion and we all know the consequences of that ;-) I'd try to use javac itself for the task; it has a complete API (see tihe documentation); you can make it parse source text and not generate class files; the AST (Abstract Syntax Tree) can be read by the application too, so you can get the method definitions out of the text).

    kind regards,

    Jos
    DarrylBurke and masijade like this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. invalid method declaration
    By mcfer in forum New To Java
    Replies: 8
    Last Post: 09-22-2012, 01:26 PM
  2. Replies: 1
    Last Post: 10-17-2011, 11:45 AM
  3. Replies: 2
    Last Post: 09-27-2011, 03:59 PM
  4. invalid method declaration error
    By bsarules in forum Java Applets
    Replies: 7
    Last Post: 05-25-2010, 06:06 PM
  5. Error: invalid method declaration
    By silvia in forum New To Java
    Replies: 1
    Last Post: 07-27-2007, 12:10 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
  •