Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracting all Methods in a Java File (i.e. myFile.Java) #85

Open
cyberjj999 opened this issue Nov 19, 2020 · 1 comment
Open

Extracting all Methods in a Java File (i.e. myFile.Java) #85

cyberjj999 opened this issue Nov 19, 2020 · 1 comment

Comments

@cyberjj999
Copy link

Hi everyone, I would love like to extract all Java Methods in a Java File as mentioned in the title of this question.
To give an example of what I mean

i.e.. the file myFile.java contains the following code

System.out.print("test123")
public static List<String> getLinks(String url) throws IOException {
    List<String> result = new ArrayList<String>();
    Document doc = Jsoup.connect(url).get();
    Elements links = doc.select("a");
    for (Element link : links) {
      result.add(link.absUrl("href"));
    }
    return result;
  }

  public static List<String> getLinksV2(String url) throws BadRequest {
    try {
      URL aUrl= new URL(url);
      String host = aUrl.getHost();
      System.out.println(host);
      if (host.startsWith("172.") || host.startsWith("192.168") || host.startsWith("10.")){
        throw new BadRequest("Use of Private IP");
      } else {
        return getLinks(url);
      }
    } catch(Exception e) {
      throw new BadRequest(e.getMessage());
    }
  }

Given the following code, I should be able to get back the 2 methods entirely (including the method header, body and return statement etc)

P.S. I am not sure if Javalang is the best tool for this, but I am certainly open to other tools. I have tried using REGEX and python script but it is not very accurate. I am open to any other command-line tools as well.
The primary goal I have is to be able to have the list of methods that is within a java file.
P.S. Please suggest any tools to me for this purpose! I would really appreciate it!

Thanks in advanced!

@lyriccoder
Copy link

lyriccoder commented Dec 4, 2020

@cyberjj999 I've answered in your previous question. Hope, it's what you need.
There also CDT framework (it's written on Java, Eclipse's framework). IT can do much more that the current framework.
Or you can find Java parser https://javaparser.org/.

I believe there is no Python framework which can parse Java code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants