Java Class Mapping and utility class complete source code
Last Update:2017-02-28
Source: Internet
Author: User
source code package Com.elink.util;
/*
* <p>company: Linco software www.elingke.com </p>
* @author Liubaojun
* @version 1.0
* Created on 2004-11-29
* FROM ELINKBSP Partial source 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 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
*/
The 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
{
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
{
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.exec (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.exec ("javac-d" +targetdir+ "" +filename);
}
}