Java Class Mapping and utility class complete source code

Source: Internet
Author: User
Tags constructor
source code package Com.elink.util;

/*
* <p>company: Linco software www.elingke.com </p>
* @author Liubaojun
* @version 1.0
* Created on 2004-11-29
* FROM ELINKBSP Partial source code
*/

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





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.