服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java class 映射及实用工具类完整源代码


  package com.elink.util;

/*
* <p>company: 凌科软件 www.elingke.com </p>
* @author liubaojun
* @version 1.0
* created on 2004-11-29
* 来源于 elinkbsp 部分源代码
*/

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
*/
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 );
}
}

扫描关注微信公众号