0

As an example, I have a .Java file like this,

public class A {

   private void callData() {

       //There can be custom methods like this
       checkImage("A",true);
       checkObject("B","C",true);
   }
}

I want to read this methods name and parameters. I dont need to go inside those methods and take the values but I want to take the name and parameters. This A.java is a file located in my machine. Now I want to write a code to read this method names and parameters. I think this is clear :)

Thank you

Matt Way
  • 32,319
  • 10
  • 79
  • 85
  • Clear as mud. You labeled this javascript and java, which have absolutely nothing to do with each other, and failed to mention which language you want to read it with – charlietfl Nov 20 '16 at 12:38
  • Welcome to Stack Overflow! Please review our [SO Question Checklist](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) to help you to ask a good question, and thus get a good answer. – Joe C Nov 20 '16 at 12:56
  • Actually Its my bad because I m new to stackoverflow.com. I hope in future, I ll ask questions better. Thank you @charlietfl – Prabhath Perera Nov 20 '16 at 13:48

1 Answers1

0

Most questions should contain where possible a minimal example of things you have already tried as it helps us answers the question more efficiently in the problem you are having with your code. Anyway for picking out method headers and parameters you should probably be looking into using a regex if you dont want a more fully complete solution for code parsing:

Firstly read your file in, you can see examples for different version of java here from @Grimy:

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public String readFile(String filename)
{
    String content = null;
    File file = new File(filename); //for ex foo.txt
    FileReader reader = null;
    try {
        reader = new FileReader(file);
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(reader !=null){reader.close();}
    }
    return content;
}

Then theres a number of solutions for a possible regex you could use here and here

(?:(?:public)|(?:private)|(?:static)|(?:protected)\s+)*

Once you have designed a regex you could use any of the normal ways to group the matches and output what you need from the matches.

Its worth mentioning for this type of program solutions such as ANTLR can be useful (but overkill, thenless you are looking to extend beyond just method headers) as they can generate an entire parser for you to use.

D3181
  • 2,037
  • 5
  • 19
  • 44
  • 1
    Thank you very much, Actually I m new to stack overflow. I think i found my answer because I was finding a way to start. You have mentions lots of information regarding the details I m searching for. Thanks Dean219 – Prabhath Perera Nov 20 '16 at 13:51
  • No worires, i would recommend as i said focusing on firstly the regex solution as if you are learning java and your need is only for a header reader (method name, type and parameters) then focus on building a regex and using a pattern to match groups such as the individual arguments and then the method name separately so you can handle each particular event. I hope you find the answers you are looking for and comment back if you need any suggestions :) – D3181 Nov 20 '16 at 13:54