|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
你总不能说你写框架吧,那无疑会加大工作量,现在大多企业采取的是折中的办法,就是改别人写好的框架,可要改框架,前提是你对这个框架足够的了解,这就更难了。源代码packagecom.elink.util;
/*
*<p>Company:凌科软件www.elingke.com</p>
*@authorliubaojun
*@version1.0
*Createdon2004-11-29
*来历于elinkBSP部分源代码
*/
importjava.lang.reflect.*;
importjava.net.*;
publicclassClassUtil
{
/**
@paramstrClassName
@paramargsType
@paramargs
@returnObject
@throwsjava.lang.NoSuchMethodException
@throwsjava.lang.SecurityException
@throwsjava.lang.ClassNotFoundException
@throwsjava.lang.InstantiationException
@throwsjava.lang.IllegalAccessException
@throwsjava.lang.IllegalArgumentException
@throwsjava.lang.reflect.InvocationTargetException
*/
publicstaticObjectloadClass(StringstrClassName,Class[]argsType,
Object[]args)
throwsNoSuchMethodException,SecurityException,ClassNotFoundException,
InstantiationException,IllegalAccessException,
IllegalArgumentException,InvocationTargetException
{
ObjectreturnObj=null;
ClassclassName=null;
Constructorconstructor=null;
className=Class.forName(strClassName);
constructor=className.getConstructor(argsType);
returnObj=constructor.newInstance(args);
returnreturnObj;
}
/**
@paramstrClassName
@returnObject
@throwsjava.lang.NoSuchMethodException
@throwsjava.lang.SecurityException
@throwsjava.lang.ClassNotFoundException
@throwsjava.lang.InstantiationException
@throwsjava.lang.IllegalAccessException
@throwsjava.lang.IllegalArgumentException
@throwsjava.lang.reflect.InvocationTargetException
*/
publicstaticObjectloadClass(StringstrClassName)
throwsNoSuchMethodException,SecurityException,ClassNotFoundException,
InstantiationException,IllegalAccessException,
IllegalArgumentException,InvocationTargetException
{
returnloadClass(strClassName,null,null);
}
publicstaticObjectloadClass(StringstrClassName,ObjectdefObj)
{
try
{
returnloadClass(strClassName,null,null);
}
catch(Throwableex)
{
LogUtil.logException(ex);
}
returndefObj;
}
/**
@paramclassObject
@paramstrMethodName
@paramargsType
@paramargs
@returnObject
@throwsjava.lang.NoSuchMethodException
@throwsjava.lang.SecurityException
@throwsjava.lang.IllegalAccessException
@throwsjava.lang.IllegalArgumentException
@throwsjava.lang.reflect.InvocationTargetException
*/
publicstaticObjectinvokeMothod(ObjectclassObject,StringstrMethodName,
Class[]argsType,Object[]args)
throwsNoSuchMethodException,SecurityException,IllegalAccessException,
IllegalArgumentException,InvocationTargetException
{
MethodconcatMethod=classObject.getClass().getMethod(strMethodName,
argsType);
returnconcatMethod.invoke(classObject,args);
}
/**
@paramclassObject
@paramstrMethodName
@returnObject
@throwsjava.lang.NoSuchMethodException
@throwsjava.lang.SecurityException
@throwsjava.lang.IllegalAccessException
@throwsjava.lang.IllegalArgumentException
@throwsjava.lang.reflect.InvocationTargetException
*/
publicstaticObjectinvokeMothod(ObjectclassObject,StringstrMethodName)
throwsNoSuchMethodException,SecurityException,IllegalAccessException,
IllegalArgumentException,InvocationTargetException
{
returninvokeMothod(classObject,strMethodName,null,null);
}
/**
@paramclassObject
@paramstrFieldName
@returnObject
@throwsjava.lang.NoSuchFieldException
@throwsjava.lang.SecurityException
@throwsjava.lang.IllegalArgumentException
@throwsjava.lang.IllegalAccessException
*/
publicstaticObjectgetFieldValue(ObjectclassObject,StringstrFieldName)
throwsNoSuchFieldException,SecurityException,
IllegalArgumentException,IllegalAccessException
{
ObjectretObj=null;
Fieldfield;
field=classObject.getClass().getDeclaredField(strFieldName);
retObj=field.get(classObject);
returnretObj;
}
/**
@paramclassName
@returnString
*/
publicstaticStringwhereClass(StringclassName)
{
StringretStr=null;
if(!className.startsWith("/"))
{
className="/"+className;
}
className=className.replace(.,/);
className=className+".class";
java.net.URLclassUrl=getResource(className);
if(classUrl!=null)
{
Stringstr=classUrl.getFile();
retStr=str;
}
returnretStr;
}
/**
@paramname
@returnjava.net.URL
*/
publicstaticURLgetResource(Stringname)
{
returnClassUtil.class.getResource(name);
}
publicstaticvoidcompiler(StringtargetDir,Stringfilename,Stringclasspath)throwsException
{
//javac-dE:/test-classpathC:workWEB-INFlibss.jare:/test/TestVector.java
Stringstr="javac-d"+targetDir+"-classpath"+classpath+""+filename;
LogUtil.logInfo(str);
Runtimeruntime=Runtime.getRuntime();
Processp=runtime.exec(str);
}
publicstaticvoidcompiler(StringtargetDir,Stringfilename)throwsException
{
//javac-dE:/test-classpathC:workWEB-INFlibss.jare:/test/TestVector.java
Runtimeruntime=Runtime.getRuntime();
Processp=runtime.exec("javac-d"+targetDir+""+filename);
}
}
大型的应用一般不会用这些框架(因为性能考虑);开发人员根据需要选择用一些框架,也可以不选用框架;不用框架并不代表要自己写框架;修改框架的可能性更小。 |
|