Task: Simple introspection shell

Develop a program that provides introspection-based functionalities for Java classes.  The interaction with the user should be provided in a shell that takes as input commands and prints as output the result returned by the execution of the command.

The commands to support are:

  • Class <name> : obtain instance of Class<name>
  • Set <name> : save object from last result with name <name>
  • Get <name> : select object previously saved with <name>
  • Index <int> : select object within an array. The array should have been obtained as the result of the previous command.

Whenever the user enters an unknown command, instead of immediately giving an error message, the shell should first check if the attempted command names a method that can be invoked on the result of the previous command.  In this case the following parameters given in the shell can be used as the actual parameters of the method to invoke.  An attempt is made to discover the best method via reflection and to convert the given parameters to the correct types. (Suggestion: start simple, with support only for primitive types).

As an example, consider the following possible interaction:

java -cp classes ap.Shell
Command:> Class java.lang.String
class java.lang.String
Command:> Set s
Saved name for object of type: class java.lang.Class
class java.lang.String
Command:> getDeclaredConstructors
Trying generic command: getDeclaredConstructors 
public java.lang.String()
public java.lang.String(java.lang.String)
public java.lang.String(char[])
public java.lang.String(char[],int,int)
public java.lang.String(int[],int,int)
public java.lang.String(byte[],int,int,int)
public java.lang.String(byte[],int)
public java.lang.String(byte[],int,int,java.lang.String) throws java.io.UnsupportedEncodingException
public java.lang.String(byte[],int,int,java.nio.charset.Charset)
public java.lang.String(byte[],java.lang.String) throws java.io.UnsupportedEncodingException
public java.lang.String(byte[],java.nio.charset.Charset)
public java.lang.String(byte[],int,int)
public java.lang.String(byte[])
public java.lang.String(java.lang.StringBuffer)
public java.lang.String(java.lang.StringBuilder)
java.lang.String(int,int,char[])
Command:> Index 1
public java.lang.String(java.lang.String)
Command:> Set c
Saved name for object of type: class java.lang.reflect.Constructor
public java.lang.String(java.lang.String)
Command:> newInstance Ola
Trying generic command: newInstance Ola 
Ola
Command:> equals Oi
Trying generic command: equals Oi
false
Command:> Get s
class java.lang.String
Command:> getPackage
Trying generic command: getPackage 
package java.lang, Java Platform API Specification, version 1.6