Java Reflection

It is common in many tools, especially IDEs, to query the contents of .class files to determine a class's methods, fields, and constructors.  In this assignment, you will develop a Java application that uses Java reflection to output the names of classes and their public non-static and public static members for all classes in a package.

You may work alone or in groups of two but NOT 3 or more.

To begin, install JDK and Netbeans,  To do this assignment, you must know how to set a classpath.  And read the course web page on JUnit testing.

The Assignment

Write an application with the following command-line invocation:

> java  reflect.Main  <name-of-qualified-package> <directory>



> java  reflect.Main   jarreader  C:\Users\me\Documents\NetBeansProjects\JarReader\build\classes\jarreader

Your program will retrieve the names of all .class files in the given directory and use class reflection to examine the contents of each class file. The key Java statement to inspect a .class file is:

Class<?> c = Class.forName(""); 

where c is a Java object from which you can extract the desired structural information and "" is exactly that -- the qualified name of a Java class that is on your classpath (e.g., "java.util.Stack").  You must read JDK documents on the Class class (click here for more details) and the way Java encodes type names (click here for more details). Finding the names of public fields and methods using reflection is simple, once you have the Class<?> object for a given class.

Remember: to introspect a class means that it MUST be accessible via your class path AND it is not in a JAR file or zip file.  I will give you a zip file for you to test your program.  For your program to work, you will have to unzip it in a directory that is on your classpath.

Here are key limitations for this assignment to make your life easier:

The goal of this assignment is not to make this a complete tool, but rather to give you an idea of what reflective programs like this can do and how they work. Further, part of this assignment is to follow instructions  -- if you don't pay attention, you lose points.  Of course, you can always ask for clarifications.

To test your program on a specific packages, unzip this file and place its contents on your classpath, and execute the following command line, where "c:\blah\blah\yparser" is the path to the directory that contains the yparser package.   

Your output will be a set of 2 tables.  These tables and their attributes are:

Your output (file) should have the standard header given below:

:- discontiguous bcClass/3, bcMember/7.
:- dynamic bcClass/3, bcMember/7.

In a nutshell, you are generating a Prolog file:

This header is followed by tuples that you generate. The tuples produced for class yparser.connection are shown below.  This is the form of output that you should generate -- it is an executable Prolog file.  The full and correct output file is here.


/* public Constructors */

/* public Fields */

/* public Methods */



What to Submit to Canvas

A zip file with all source code.  The zip file must unzip into <yourName>/<YourFilesAndDirectories>.

  1. The Grader should be able to invoke your program just like "java reflect.Main <name-of-qualified-package> <directory>" exactly like the above from a command line in the unzipped directory. The Grader will reject any submission where this is not the case.

  2. Your program outputs for the 4 packages in this zip file; a readme.txt file would be useful to show me where your results are.

  3. Use the correct output file as a basis for a JUnit test.  Read the course web page on JUnit testing.  It will help you.

  4. Create a second unit test for your program --- Use your program to reflect on itself.

  5. A PDF file (in the required format) that the Grader should read to provide any information that is not obvious.  The contents of the PDF file can be minimal.

You should expect that other packages, known only to the grader, will be used to evaluate your program.

A critical part of any design is clarity and understandability.   Hence, you will be graded on the clarity of your project and its ability to work correctly.  Sloppy code, documentation, or anything that makes grading or understanding your program difficult will cost you points!  Beware, some of these "beauty" points are subjective. 

No late assignments/submissions will be accepted.