Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By srbytpl

Thread: how to display all the method and parameter names in java using JAVA

  1. #1
    AnandVijay is offline Member
    Join Date
    Oct 2013
    Posts
    15
    Rep Power
    0

    Default how to display all the method and parameter names in java using JAVA

    Hi Friends can anyone help me to find ?
    how to display all the method and parameter names in java file using JAVA

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: how to display all the method and parameter names in java using JAVA

    Very vague. Are you asking to get all the methods from a Java source file (as in - parse the code itself), or do you want to get all the methods with their parameters from a compiled class when you run your java application?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: how to display all the method and parameter names in java using JAVA

    Assuming you don't mean by using reflection, then you need to parse the file. And considering that there are numerous ways to
    declare a method and the declaration may span several lines, it is non-trivial. There may be some existing packages that will
    facilitate the process but I am not aware of any.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    AnandVijay is offline Member
    Join Date
    Oct 2013
    Posts
    15
    Rep Power
    0

    Default Re: how to display all the method and parameter names in java using JAVA

    ex:
    class a{
    void m1()
    {
    }
    void m2()
    {
    }
    }
    get all the methods from a Java source file
    output like
    m1
    m2

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: how to display all the method and parameter names in java using JAVA

    You did not answer my question, you just repeated your original vague post with more text.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default Re: how to display all the method and parameter names in java using JAVA

    The API for javac (the compiler) is public now as well as the API for the AST (Abstract Syntax Tree); you can make the compiler parse your source while you grab the methods from the AST; and no, I am not supplying any example code here, but here's a nice article: https://today.java.net/pub/a/today/2...iler-apis.html

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    srbytpl is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Default Re: how to display all the method and parameter names in java using JAVA

    public class MyClass{

    String field1;
    String field2;
    String field3;

    public static void main(String[] args) {

    writeClassMethods(MyClass.class);
    }

    private static void writeClassMethods(Class<MyClass> o){

    Method[] methods = o.getDeclaredMethods();
    Field[] fields = o.getDeclaredFields();

    System.out.println("Methods :\n");
    for (Method method : methods) {

    System.out.println(method.getName());
    }
    System.out.println("***************************\n" );

    System.out.println("Fields :\n");
    for (Field field : fields) {

    System.out.println(field.getName());
    }
    }

    public void method1(){

    }
    public void method2(){

    }
    AnandVijay likes this.

  8. #8
    srbytpl is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Default Re: how to display all the method and parameter names in java using JAVA

    output :

    Methods :

    main
    writeClassMethods
    method1
    method2
    ***************************

    Fields :

    field1
    field2
    field3

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default Re: how to display all the method and parameter names in java using JAVA

    Sigh, please read the thread before you reply: the OP wants to read source code, not reflected class code ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    AnandVijay is offline Member
    Join Date
    Oct 2013
    Posts
    15
    Rep Power
    0

    Default Re: how to display all the method and parameter names in java using JAVA

    thanks

  11. #11
    AnandVijay is offline Member
    Join Date
    Oct 2013
    Posts
    15
    Rep Power
    0

    Default Re: how to display all the method and parameter names in java using JAVA

    thanks for your reply...

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default Re: how to display all the method and parameter names in java using JAVA

    @AnandVijay: I'm wondering why you like reply #7; it doesn't use any source code to extract the methods etc. from a class (as you asked). It simply doesn't do what you wanted; do you know what you want?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 3
    Last Post: 01-20-2012, 10:34 PM
  2. Replies: 18
    Last Post: 04-18-2011, 05:39 AM
  3. Replies: 2
    Last Post: 10-15-2010, 05:09 AM
  4. Java database/method display problem
    By thelinuxguy in forum Advanced Java
    Replies: 3
    Last Post: 02-20-2009, 07:56 PM
  5. Getting method names using Reflection
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-24-2008, 03:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •