no message

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@967 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Don Leckie
2002-09-20 17:05:55 +00:00
parent 29f7de2d36
commit 5a6986ac33
2 changed files with 589 additions and 0 deletions

@ -0,0 +1,158 @@
package net.sourceforge.pmd;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.MessageFormat;
/**
* Provides special functionality for PMD class loading.
*
* @author Donald A. Leckie
* @since September 19, 2002
* @version $Revision$, $Date$
*/
public class PMDClassLoader extends ClassLoader
{
/**
********************************************************************************
*/
public PMDClassLoader()
{
super();
}
/**
********************************************************************************
*
* @param classFileName
*
* @return
*
* @throws PMDException
*/
public Class loadRuleClass(String classFileName, String className)
throws PMDException
{
classFileName = trim(classFileName);
className = trim(className);
if (classFileName == null)
{
String message = "Missing the class file name.";
PMDException pmdException = new PMDException(message);
pmdException.fillInStackTrace();
throw pmdException;
}
if (className == null)
{
String message = "Missing the class name.";
PMDException pmdException = new PMDException(message);
pmdException.fillInStackTrace();
throw pmdException;
}
FileInputStream inputStream = null;
Class ruleClass = null;
File file = new File(classFileName);
if (file.exists() == false)
{
String template = "Could not open file \"{0}\" for class \"{1}\".";
String[] parameters = {classFileName, className};
String message = MessageFormat.format(template, parameters);
PMDException pmdException = new PMDException(message);
pmdException.fillInStackTrace();
throw pmdException;
}
try
{
byte[] data;
inputStream = new FileInputStream(classFileName);
data = new byte[(int) file.length()];
inputStream.read(data);
ruleClass = defineClass(className, data, 0, data.length);
resolveClass(ruleClass);
}
catch (IOException exception)
{
String template = "Error while reading file \"{0}\" for class \"{1}\".";
String[] parameters = {classFileName, className};
String message = MessageFormat.format(template, parameters);
PMDException pmdException = new PMDException(message);
pmdException.fillInStackTrace();
throw pmdException;
}
finally
{
if (inputStream != null)
{
try
{
inputStream.close();
}
catch (IOException exception)
{
inputStream = null;
}
}
}
return ruleClass;
}
/**
********************************************************************************
*
* @param text
*
* @return
*/
private String trim(String text)
{
if (text != null)
{
text = text.trim();
if (text.length() == 0)
{
text = null;
}
}
return text;
}
/**
********************************************************************************
*
* @param args
*/
public static void main(String[] args)
{
try
{
PMDClassLoader classLoader = new PMDClassLoader();
String fileName = "c:\\cvs\\pmd\\classes\\net\\sourceforge\\pmd\\rules\\BracesRule.class";
String className = "net.sourceforge.pmd.rules.BracesRule";
Class theClass = classLoader.loadRuleClass(fileName, className);
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
}

File diff suppressed because it is too large Load Diff