 |
|
|
CheckClasses.java
|
/*
* Author: Havard Rast Blok
* E-mail: 
* Web : www.rememberjava.com
*/
import java.io.File;
import java.util.*;
import com.rememberjava.io.SimpleFileFilter;
/**
* Show all the class-files refered to by the current
* CLASSPATH and try to load them.
*/
public class CheckClasses
{
private SimpleFileFilter fileFilter;
//Position where the OK/FAILED message should be set
private final int msgStart = 70;
public CheckClasses()
{
String paths[];
//make a file filter witch accpets class files
fileFilter = new SimpleFileFilter( "class" );
//get the paths of the CLASSPATH variable
paths = getPaths();
//show the class files
for(int i = 0; i < paths.length; i++)
{
showClasses( new File(paths[i]), null );
}
}
/**
* Returns the directories of the CLASSPATH as elements of a
* String array.
*
* @return array containing the directories
*/
private String[] getPaths()
{
String cp;
StringTokenizer st;
int tokens;
int counter;
String ans[];
//get classpath
cp = System.getProperty("java.class.path");
System.out.println("CLASSPATH=" + cp + "\n");
//applay StringTokenizer and use the underlying OS' pathSeparator (; or :)
st = new StringTokenizer( cp, File.pathSeparator );
tokens = st.countTokens();
ans = new String[tokens];
counter = 0;
//put each path in an array element
while (st.hasMoreTokens())
{
ans[counter] = st.nextToken();
counter++;
}
return ans;
}
/**
* Gets the sub directories of the input directory.
*
* @param path the directory to retreive sub directories from.
* @return array of sub directories. The array is of type Object[],
* however each element of the array may be casted to a File object.
*/
private Object[] getDirectories( File path )
{
Vector tmpVec;
File fileList[];
Object ans[];
tmpVec = new Vector();
//get all the files in the current directory
fileList = path.listFiles();
//add the directories in the list to the vector
for(int i = 0; i < fileList.length; i++)
{
if( fileList[i].isDirectory() )
{
tmpVec.add( fileList[i] );
}
}
//convert the Vector to an array
ans = tmpVec.toArray();
return ans;
}
/**
* Shows the classes of the input directory and its sub directories.
* If the directory does not exist or may not be read, a text message
* will say so.
* @param pathName directory to search for class files
*/
private void showClasses( File pathName, String packageName )
{
File classes[];
Object directories[];
String newPackageName;
String firstName;
String className;
//Get file names with extension .class in the current directory
classes = pathName.listFiles( fileFilter );
//check if pathName is a directory and readable
if( classes != null )
{
//System.out.println("pathName="+pathName +" packageName="+packageName);
//get name of package
if( packageName == null )
{
newPackageName = "";
}
else
{
newPackageName = packageName + pathName.getName() + ".";
}
//show the classes and if the may be loaded
for(int i = 0; i < classes.length; i++)
{
firstName = getFirstName( classes[i].getName() );
className = newPackageName + firstName;
System.out.println(className + getSpaces( className ) + getLoadStatus( className ) );
}
//get the sub directories of the current directory
directories = getDirectories( pathName );
//show the class files in the sub directories
//RECURSIVE CALL TO THIS METHOD
for(int i = 0; i < directories.length; i++)
{
showClasses( (File)directories[i], newPackageName );
}
}
else
{
System.out.println( "\n" + pathName + "\n was not found, or error reading.\n");
}
}
/**
* Gets the first name of a file name.
* The extension after the last dot (.) is truncated.
* @param fileName Name of the file
* @return First name of file, before the last dot (.)
*/
private String getFirstName( String fileName )
{
int dotIndex = fileName.lastIndexOf( '.' );
return fileName.substring( 0, dotIndex );
}
/**
* Gets the status of the class-loading.
* Class.forName(className) is run to see if the class could be loaded.
* Be aware of the the loading will fail if depentent classes are not present.
* Please refere to the Java API doc. for further documentation.
*
* @param className class name, with full package name
* @return String showing the status [ OK ] or [ FAILED ]
*/
private String getLoadStatus( String className )
{
String ans = "[ OK ]";
try
{
Class.forName(className);
}
catch(Throwable e)
{
ans = "[ FAILED ]";
}
return ans;
}
/**
* Gets the right number of spaces to align the status messages.
* The the length of the class name is substracted from the constant msgStart
* to find the right number of spaces.
*
* @param className name of the class to align the message to
* @return String of spaces
*/
private String getSpaces( String className )
{
String ans;
int spaces;
spaces = msgStart - className.length();
ans = "";
for(int i = 0; i < spaces; i++)
{
ans = ans + " ";
}
return ans;
}
public static void main( String [] arg)
{
new CheckClasses();
}
}
|
|
|
 |