• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

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

发布: 2007-7-04 13:34 | 作者: admin | 来源:  网友评论 | 查看: 37次 | 进入软件测试论坛讨论

领测软件测试网 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 );
}
}



延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网