java filed类源码_Java Class 映射及实用工具类完整源代码

package com.elink.util;

/*

*

Company: 凌科软件 www.elingke.com

* @author liubaojun

* @version 1.0

http://www.gaodaima.com/41445.htmljava Class 映射及实用工具类完整源代码

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

}

}

欢迎大家阅读《Java Class 映射及实用工具类完整源代码》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码

e7ce419cf2d6ad34d01da2ceb8829eed.png

微信 赏一包辣条吧~

023a57327877fb4402bcc76911ec18ea.png

支付宝 赏一听可乐吧~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值