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

Welcome Forums General PowerShell Q&A To Get all the classes, methods and its sub methods from the java file

This topic contains 9 replies, has 4 voices, and was last updated by

 
Participant
3 weeks, 5 days ago.

  • Author
    Posts
  • #172156

    Participant
    Topics: 4
    Replies: 9
    Points: 28
    Rank: Member

    Hi All ,

    I am trying to fetch all the classes , its methods and submethods from .java files using powershell scripting.

    I have tried fetching the classes with the keyword "class"  but for the methods I'm stuck .

    Does anyone have any idea with this .. Thanks in advance 🙂

  • #172165

    Moderator
    Topics: 0
    Replies: 34
    Points: 321
    Team MemberHelping Hand
    Rank: Contributor

    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

    • #172168

      Participant
      Topics: 4
      Replies: 9
      Points: 28
      Rank: Member

      Hey James ,

      Thats right !

      I wanna get this using powershell scripting .

      Thanks.

  • #172174

    Participant
    Topics: 4
    Replies: 9
    Points: 28
    Rank: Member
    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 ...
  • #172312

    Participant
    Topics: 4
    Replies: 9
    Points: 28
    Rank: Member

    I am trying this using powershell scripting

  • #172315

    Participant
    Topics: 2
    Replies: 482
    Points: 1,146
    Helping Hand
    Rank: Community Hero

    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.

  • #172318

    Participant
    Topics: 4
    Replies: 9
    Points: 28
    Rank: Member

    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..

     

     

  • #172477

    Participant
    Topics: 2
    Replies: 482
    Points: 1,146
    Helping Hand
    Rank: Community Hero

    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...

    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. 🙂

    • #172489

      Participant
      Topics: 4
      Replies: 9
      Points: 28
      Rank: Member

      Thanks Joel . Will give it a try

  • #172486
    js

    Participant
    Topics: 24
    Replies: 677
    Points: 1,613
    Helping Hand
    Rank: Community Hero

    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[]);
    }
    

You must be logged in to reply to this topic.