Monday, 15 June 2015

metaprogramming - Find the start and end of a Java function -



metaprogramming - Find the start and end of a Java function -

in java, possible generate corresponding curly brace sample of java source code, given source code string, start of curly brace?

public class findfunctionstartandend{ public static object findcorrespondingcurlybrace(string sourcecode, string startingposition){ //return string position corresponds matching curly brace //the input string should source code of java class //if sourcecode not valid java code, homecoming false } }

you need run through string , count open , closing brackets origin @ starting position , match them up. problem be, there several cases in need ignore them (here couple of examples):

in line comment (between // , "new line") in block comment (between /* (not hidden line comment) , */) in string literals (between 2 matching "'s (ignoring escapes (\", watch \\" or \\\"!))) in char literals (between 2 matching ''s) possibly more

you can either maintain counters of these run through string, or come regular look match entire function body (including these cases in list) , apply 1 time , see matching index ends after found body.

either 1 should bit involved right in cases, recommend hitting google see if can find examples of pre-made regular expressions, example.

java metaprogramming syntax-highlighting

No comments:

Post a Comment