P1: 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.


Before you begin, make sure that you can set your classpath properly.  Follow these iustructions, and don't proceed until you have them working.

The Assignment

You will write an application with the following command-line invocation:

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



> java -jar reflect.jar 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("qualified.package.name"); 

where c is a Java object from which you can extract the desired structural information and "qualified.package.name" 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: the classes that you want to introspect MUST be on your class path AND they must not be in JAR files.  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 7zip 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, bcMembers/7.
:- dynamic bcClass/3, bcMembers/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

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 -jar reflect.jar  <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.  You should see course URL for information 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.