Many tools, especially IDEs, query the contents of .class files to determine a class's methods, fields, and constructors. In this assignment, you will write a Java program that uses Java Reflection to produce a database of the names of classes and their public non-static and public static members for all classes in a package. In MDE, mapping from Java .class files to a database is called a Text-2-Model (T2M) tool.
You may work alone or in groups of two. Start this assignment by reading the course web page on JUnit testing.
Write an application with the following command-line invocation:
> java introspect.Main <directory-of-classfiles>where directory-of-classfiles is the absolute path to a directory containing class files of a Java package. Here's an example call:
> java introspect.Main /java/otherclasses/p1packageThe output of introspect.Main goes to System.out. If you want to produce a file from the command line, use redirection:
> java introspect.Main /java/otherclasses/p1package > p1package.prog1.pl
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("qualifiedPackageName.className");
where c is a Java object from which you can extract the desired structural information and "qualifiedPackageName" 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 in this assignment 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 zip or jar files
for you to test your program. For your program to work, you will
have to unzip/unjar 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. You can always ask for clarifications.
dbase(prog1,[bcClass,bcMember]).
table(bcClass,[cid,"name","superName"]).
table(bcMember,[mid,cid,static,"type","sig"]).
Here's what the above means:
I provide you with a package with a single class, p1package.ReflectionTest. When you run your program on its directory (where /mydata/blahblah/p1package is this directory) and you have compiled ReflectionTest so that the directory has .class files:
> java introspect.Main /mydata/blahblah/p1packageYour program will output a database that will match this:
A zip file with all source code (including your Netbeans or Eclipse Project). The zip file must unzip into <yourLastName>/<YourFilesAndDirectories> if you work alone or <LastName1-LastName2>/<YourFilesAndDirectories> if you work in a group.
The Grader should be able to invoke your program
just like "java introspect.Main <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.
Your program outputs for the packages in
this zip file; a readme.txt file would be useful to show me where your results are.
Use the correct output file, above, as a basis for a JUnit test. Read the course web page on JUnit testing. It will help you.
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.