Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can anyone help, I am trying to write a program that takes a class name as a com

ID: 3779562 • Letter: C

Question

Can anyone help, I am trying to write a program that takes a class name as a command line arguement and in that class get all the public static methods that have no parameters and return booleans and whose name start a test, and invoke these methods. All the method that it envokes should do is return true;

The command line should look like this: java main "className"

This is what I have so far. I am able to take in a class name and find the constructors of that class but I am not able to find the methods that I need. Or how to envoke them once found.

import static java.lang.System.out;
import static java.lang.System.err;

import java.util.*;

import java.lang.reflect.*;

public class main
{
   public static void main(String[] args)
   {
       Class cls = null;
       try{
       cls = Class.forName(args[0]);
       }catch(ClassNotFoundException e){
           System.out.println(Not found);
       }
       System.out.println("Constructors:");
       Constructor[] constructors = cls.getDeclaredConstructors();
       for (Constructor m : constructors)
System.out.println(" " + m);
         
       System.out.println("Methods:");
       Method[] methods = cls.getDeclaredMethods();
       for (Method m : methods)
       {
           System.out.println("In");
           if (Modifier.isPublic(m.getModifiers()))
System.out.println(" " + m.toString());
       }
   }
}

public class MyClass
{

public MyClass()
   {
  
   }
   public static boolean test()
   {
       return true;
   }
}

Explanation / Answer

import static java.lang.System.out;
import static java.lang.System.err;
import java.util.*;
import java.lang.reflect.*;
public class main
{
public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
Class cls = null;
try{
cls = Class.forName(args[0]);
}catch(ClassNotFoundException e){
System.out.println("Not found");
}
System.out.println("Constructors:");
Constructor[] constructors = cls.getDeclaredConstructors();
for (Constructor m : constructors)
System.out.println(" " + m);

System.out.println("Methods:");
Method[] methods = cls.getDeclaredMethods();
for (Method m : methods)
{
System.out.println("In");
if (Modifier.isPublic(m.getModifiers()))
System.out.println(" " + m.toString());
System.out.println("Invoking method: "+m.invoke(cls, null) );// invoking method through reflection
}
}
}
class MyClass
{
public MyClass()
{
  
}
public static boolean test()
{
return true;
}
}

---------------

output:

Constructors:
public MyClass()
Methods:
In
public static boolean MyClass.test()
Invoking method: true

//Note: feel free to ask doubts/question. God bless you !

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote