-3

is there any way to search all methods in Java class with regex?

Aditya Hadi
  • 364
  • 1
  • 4
  • 17
  • of course it is possible even it would be a complex regex. but what are you trying to achieve? you can get the list of methods with java reflection much more easily. – Juvanis Jul 26 '13 at 07:42
  • 1
    Are you trying to parse a Java code file or what? Regex certainly is the wrong tool for this job. – Daniel Hilgarth Jul 26 '13 at 07:43
  • Um, I am really impressed. It's a first time I see regex used as reflection tool. – Leri Jul 26 '13 at 07:43
  • You are probably looking for AspectJ Pointcut expressions: http://guptavikas.wordpress.com/2010/04/15/aspectj-pointcut-expressions/ – necromancer Jul 26 '13 at 07:47
  • I cannot compile the class due to some data not ready yet, that's why I cannot use java reflection. – Aditya Hadi Jul 26 '13 at 08:00
  • @user2621732 it's not clear that regex is the best solution to your problem. If you want more useful help please explain the actual underlying problem. – selig Jul 26 '13 at 08:04

2 Answers2

3
(public|protected|private|static|\s) +[\w\<\>\[\]]+\s+(\w+) *\([^\)]*\) *(\{?|[^;])

With this you can, but search before ask, because i only have used the search to find this answer ^^.

xlecoustillier
  • 16,183
  • 14
  • 60
  • 85
Distopic
  • 717
  • 3
  • 16
  • 31
  • 3
    That would not account for comments or strings. And if you copied that expression from somewhere, give the original author credit by linking to the source. – Daniel Hilgarth Jul 26 '13 at 07:44
  • http://stackoverflow.com/questions/68633/regex-that-will-match-a-java-method-declaration?rq=1 This is the link where this is discused. Thanks to Georgios Gousios. – Distopic Jul 26 '13 at 07:45
  • I still got ` else if (plane == REFLECT_PLANE_XZ)` in the results. Any idea? – Aditya Hadi Jul 26 '13 at 07:57
  • try to use only the first part oh the exreg, altaugh is a little suspicious that the string that you paste validate the expression. – Distopic Jul 26 '13 at 08:03
0

I think you're looking for reflection - see this tutorial for help. Only through reflection can you access information about loaded classes - unless you're thinking of loading in the .java file and analyzing its text.

selig
  • 4,834
  • 1
  • 20
  • 37