Package com. elink. util;
/*
* <P> company: lingke software www.elingke.com </P>
* @ Author liubaojun
* @ Version 1.0
* Created on 2004-11-29
* ElinkbspSource code
*/
Import java. Lang. Reflect .*;
Import java.net .*;
Public class classutil
{
/**
@ Param strclassname
@ Param argstype
@ Param ARGs
@ Return object
@ Throws java. Lang. nosuchmethodexception
@ Throws java. Lang. securityexception
@ Throws java. Lang. classnotfoundexception
@ Throws java. Lang. instantiationexception
@ Throws java. Lang. illegalaccessexception
@ Throws java. Lang. illegalargumentexception
@ Throws java. Lang. Reflect. invocationtargetexception
*/
Public static object loadclass (string strclassname, class [] argstype,
Object [] ARGs)
Throws nosuchmethodexception, securityexception, classnotfoundexception,
Instantiationexception, illegalaccessexception,
Illegalargumentexception, invocationtargetexception
{
Object returnobj = NULL;
Class classname = NULL;
Constructor = NULL;
Classname = Class. forname (strclassname );
Constructor = classname. getconstructor (argstype );
Returnobj = constructor. newinstance (ARGs );
Return returnobj;
}
/**
@ Param strclassname
@ Return object
@ Throws java. Lang. nosuchmethodexception
@ Throws java. Lang. securityexception
@ Throws java. Lang. classnotfoundexception
@ Throws java. Lang. instantiationexception
@ Throws java. Lang. illegalaccessexception
@ Throws java. Lang. illegalargumentexception
@ Throws java. Lang. Reflect. invocationtargetexception
*/
Public static object loadclass (string strclassname)
Throws nosuchmethodexception, securityexception, classnotfoundexception,
Instantiationexception, illegalaccessexception,
Illegalargumentexception, invocationtargetexception
{
Return loadclass (strclassname, null, null );
}
Public static object loadclass (string strclassname, object defobj)
{
Try
{
Return loadclass (strclassname, null, null );
}
Catch (throwable ex)
{
Logutil. logexception (Ex );
}
Return defobj;
}
/**
@ Param classobject
@ Param strmethodname
@ Param argstype
@ Param ARGs
@ Return object
@ Throws java. Lang. nosuchmethodexception
@ Throws java. Lang. securityexception
@ Throws java. Lang. illegalaccessexception
@ Throws java. Lang. illegalargumentexception
@ Throws java. Lang. Reflect. invocationtargetexception
*/
Public static object invokemothod (Object classobject, string strmethodname,
Class [] argstype, object [] ARGs)
Throws nosuchmethodexception, securityexception, illegalaccessexception,
Illegalargumentexception, invocationtargetexception
{
Method concatmethod = classobject. getclass (). getmethod (strmethodname,
Argstype );
Return concatmethod. Invoke (classobject, argS );
}
/**
@ Param classobject
@ Param strmethodname
@ return object
@ throws Java. lang. nosuchmethodexception
@ throws Java. lang. securityexception
@ throws Java. lang. illegalaccessexception
@ throws Java. lang. illegalargumentexception
@ throws Java. lang. reflect. invocationtargetexception
*/
Public static object invokemothod (Object classobject, string strmethodname)
throws nosuchmethodexception, securityexception, illegalaccessexception,
illegalargumentexception, invocationtargetexception
{< br>
return invokemothod (classobject, strmethodname, null, null );
}
/**
@ Param classobject
@ Param strfieldname
@ return object
@ throws Java. lang. nosuchfieldexception
@ throws Java. lang. securityexception
@ throws Java. lang. illegalargumentexception
@ throws Java. lang. illegalaccessexception
*/
Public static object getfieldvalue (Object classobject, string strfieldname)
throws nosuchfieldexception, securityexception,
illegalargumentexception, illegalaccessexception
{< br>
Object retobj = NULL;
Field field;
Field = classobject. getclass (). getdeclaredfield (strfieldname);
retobj = field. get (classobject);
return retobj;
}
/**
@ Param classname
@ Return string
*/
Public static string whereclass (string classname)
{
String retstr = NULL;
If (! Classname. startswith ("/"))
{
Classname = "/" + classname;
}
Classname = classname. Replace ('.','/');
Classname = classname + ". Class ";
Java.net. url classurl = getresource (classname );
If (classurl! = NULL)
{
String STR = classurl. GetFile ();
Retstr = STR;
}
Return retstr;
}
/**
@ Param name
@ Return java.net. url
*/
Public static URL getresource (string name)
{
Return classutil. Class. getresource (name );
}
Public static void Compiler (string targetdir, string filename, string classpath) throws exception
{
// Javac-d e:/test-classpath c: \ work \ WEB-INF \ Lib \ BSS. Jar E:/test/testvector. Java
String STR = "javac-d" + targetdir + "-classpath" + classpath + "" + filename;
Logutil. loginfo (STR );
Runtime runtime = runtime. getruntime ();
PROCESS p = runtime.exe C (STR );
}
Public static void Compiler (string targetdir, string filename) throws exception
{
// Javac-d e:/test-classpath c: \ work \ WEB-INF \ Lib \ BSS. Jar E:/test/testvector. Java
Runtime runtime = runtime. getruntime ();
PROCESS p = runtime.exe C ("javac-d" + targetdir + "" + filename );
}
}
Author's blog:Http://blog.csdn.net/netcom19/