-1

I have a class file, and I just want to get the list of methods name in that class and print it o/p console.

Just assume that I have following methods in Test1 class

public class Test1{

public static void test1()
{
//some code
}

public static void test2()
{
//some code
}

public static void test3()
{
//some code here.
}
}

I just need to call all the above methods from another class in the specific order.

Like test1() first, and second test2() and followed by test3();

What I did like I just created

 Method[] methodarray=Test1.getMethods();
    if(methodarray.getName().startWith("test"))
    {
    sysout(methodarray.getName())
    }

The above code print the method in specific order first time but not always. some times it prints 3rd method first and 1method seconds, and finally 2 method.

Can anybody tell me the reason?, and how to resolve this?.

-Sasi

SKumar
  • 257
  • 2
  • 7
  • 19

3 Answers3

1

Quote from the JavaDoc: "The elements in the returned array are not sorted and are not in any particular order.".

First of all, it's always best to avoid reflection where possible in a project. Most of the time it's used for testing purposes, or if there really is no other way. That being said, if your methods are indeed called test1, test2 and test3 and you want to execute them in this order, you can use the following steps:

  1. Get all methods from the class (you've done that correctly with Method[] allMethods = Test1.getMethods();)
  2. Loop through them and save all which start with "test" in a seperate list
  3. Order that list with a Custom Sorting to sort the Methods of the seperate list by Method-Name. (See here for an example.)
  4. Invoke & Execute them

Still, it takes three loops (or some Java 8+ LINQ-queries) and it doesn't make the code very clear to anyone, including yourself. It's better to just execute them one by one manually, i.e.:

public void someMethod(){
    Test1.test1();
    Test1.test2();
    Test1.test3();
}

That's just my 2c. From the question it wasn't clear what the purpose of the methods are, or if there are more than three. I would suggest keeping away from reflection unless you really have no other way.

Community
  • 1
  • 1
Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
  • Hi folks, I don't want to sort the methods in alphabetical order. I just wanna get them as its in the Test1 class file. Like If test1() method is first in that class file and It always first when I called it through reflection, I just wanna maintain the order which is already present in the Test1 class file, and don't want to sort. – SKumar Sep 01 '15 at 04:50
1

As you can read in the javadoc for getDeclaredMethods(), the returned Method[] doesn't follow a particular order.

You can sort the methods easily by using a LinkedHashMap, or any other Map implementation, that maintains order of elements. For example:

public class MethodTest {

    private LinkedHashMap<String, Method> expectedMethodsInOrder;

    void testOne(){

    }

    void testEight(){

    }

    void beforeEight(){

    }

    @Before
    public void prepareMap(){
        expectedMethodsInOrder = new LinkedHashMap<>();
        expectedMethodsInOrder.put("testOne", null);
        expectedMethodsInOrder.put("beforeEight", null);
        expectedMethodsInOrder.put("testEight", null);
    }

    @Test
    public void test(){
        Method[] methods = MethodTest.class.getDeclaredMethods();

        for(Method m : methods){
            String name = m.getName();

            if(expectedMethodsInOrder.containsKey(name)){
                expectedMethodsInOrder.put(name, m);
            }
        }
        System.out.println(expectedMethodsInOrder.values().toString());
    }
}

Output:

[void Main.testOne(), void Main.beforeEight(), void Main.testEight()]

SME_Dev
  • 1,880
  • 13
  • 23
0

It's working as expected in Windows 7 but not in Windows 8. I have same setup on both environments, like JAVA JDK7, and Eclipse IDE JEE build. But reflection returns the method as Its mentioned in respective class file. But not in Windows 8. It seems to be environment issues.

I just wanna update my things which I observed. Please correct me If there is any other root cause.

-Sasi

SKumar
  • 257
  • 2
  • 7
  • 19