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

Viewing 7 reply threads
  • Author
    Posts
    • #172156
      Participant
      Topics: 4
      Replies: 9
      Points: 28
      Rank: Member

      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>
    • #172165
      Participant
      Topics: 0
      Replies: 42
      Points: 403
      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: 512
      Points: 1,309
      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: 512
      Points: 1,309
      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…

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

      • #172489
        Participant
        Topics: 4
        Replies: 9
        Points: 28
        Rank: Member

        Thanks Joel . Will give it a try

    • #172486
      js
      Participant
      Topics: 28
      Replies: 751
      Points: 2,092
      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[]);
      }
      
Viewing 7 reply threads
  • The topic ‘To Get all the classes, methods and its sub methods from the java file’ is closed to new replies.