·您的位置: 首页 » 资源教程 » 编程开发 » JAVA、JSP » Java Class 映射及实用工具类完整源代码

Java Class 映射及实用工具类完整源代码

类别: JAVA教程  评论数:0 总得分:0
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 );
}
}






-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1