To Get all the classes, methods and its sub methods from the java file

Hi All ,

<p class="_1qeIAgB0cPwnLhDF9XSiJM">I am trying to fetch all the classes , its methods and submethods from .java files using powershell scripting.</p> <p class="_1qeIAgB0cPwnLhDF9XSiJM">I have tried fetching the classes with the keyword "class" but for the methods I'm stuck .</p> <p class="_1qeIAgB0cPwnLhDF9XSiJM">Does anyone have any idea with this .. Thanks in advance :)</p>

Hey Ramya40.

Though I can see ways to interpret your question that work for this forum, can I just confirm that you are trying to do this in PowerShell?

This forum is quite specifically focused on PowerShell, and you may have more luck elsewhere if you’re trying to get help with something else.

-JPR

Hey James ,

Thats right !

I wanna get this using powershell scripting .

Thanks.

Example : 

ABC.java -- is the Java file

123 -- is the class name in ABC.Java file

MNO -- is the method of  Class 123

mno - is the sub method 

So basically i am trying to fetch the class names , methods and sub methods of the ABC.java file . I am having hundreds of java file . So have to fetch all of them . 



I have tried the below code : 

function Find_ClassAndItsMethods
{ 

  $List = get-childitem "C:\Programs\data\Git\tc-pdm\src\Customization\Source" -recurse -filter "*.java"
  foreach ($file in $list)

 {
   $Lists = Get-Content $file.FullName | Where-Object {$_ -like '*class*'} |Add-content "C:\Programs\java.txt"
 } 


}
Find_ClassAndItsMethods


Here i am able to fetch all the classes from the n number of java files . But for fetching methods i need some idea .

Thank u ...

I am trying this using powershell scripting

The only way to really do this is to re-parse the java file, probably with regex, to figure out what looks like a method.

Short of completely re-creating the java compiler logic from scratch, you won’t get a completely accurate result. Why are you trying to parse a completely different language using PowerShell? It seems like an odd thing to be doing to me.

Thanks for the response.

Yeah I’m supposed to do this using powershell. And I’m trying to fetch the methods using regex…

Ofcourse we need to know how a method looks like in java…

Class A

{

B() { }

}

So B is the method…

 

 

There’s a lot that could be involved here, but taking it at simplest you’re probably looking for a pattern like `\w+(.*?) *(?={)` (so, word characters followed by parentheses with maybe some things inside the parentheses and then followed by an open curly brace).

So what I might try is to look at each line of the file individually and see if it matches the pattern, then see if we can pull the pieces out so you can get some useful output. Something like…

https://gist.github.com/vexx32/bb2d4426942ae442cf1b4dbf168b7c20

The extra bits and pieces in the regex are just to make it easier to separate out the portions of the match for more useful data in case you need to play with it a bit. :slight_smile:

Using javac and javap:

/* This is a simple Java program. 
   FileName : "HelloWorld.java". */
class HelloWorld 
{ 
    // Your program begins with a call to main(). 
    // Prints "Hello, World" to the terminal window. 
    public static void main(String args[]) 
    { 
        System.out.println("Hello, World"); 
    } 
} 


javac HelloWorld.java
javap HelloWorld


Compiled from "HelloWorld.java"
class HelloWorld {
  HelloWorld();
  public static void main(java.lang.String[]);
}

Thanks Joel . Will give it a try