Java Class 映射及工具 + 生產力類完整原始碼

來源:互聯網
上載者:User
 

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

 }

}


作者Blog:http://blog.csdn.net/netcom19/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.