refactor
This commit is contained in:
coderfengyun 2014-03-28 10:56:56 +08:00
parent 212646d9cf
commit d8321996f3
4 changed files with 16 additions and 18 deletions

View File

@ -98,7 +98,7 @@ public class ParamCoordinator implements SessionObject {
private boolean createObj(String className) { private boolean createObj(String className) {
try { try {
MyFileClassLoader classLoader = new MyFileClassLoader( UserDefinedClassLoader classLoader = new UserDefinedClassLoader(
Main.USER_DEFINED_PARAMS_FOLDER); Main.USER_DEFINED_PARAMS_FOLDER);
Class<?> cls = classLoader.loadClass(className); Class<?> cls = classLoader.loadClass(className);
Object instance = cls.newInstance(); Object instance = cls.newInstance();

View File

@ -54,7 +54,7 @@ public class ParametersFactory {
private boolean createObj(String className) { private boolean createObj(String className) {
try { try {
MyFileClassLoader cl = new MyFileClassLoader( UserDefinedClassLoader cl = new UserDefinedClassLoader(
Main.USER_DEFINED_PARAMS_FOLDER); Main.USER_DEFINED_PARAMS_FOLDER);
Class<?> cls = cl.loadClass(className); Class<?> cls = cl.loadClass(className);
Constructor<?> contructor = cls Constructor<?> contructor = cls

View File

@ -5,7 +5,7 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
public class MyFileClassLoader extends ClassLoader { public class UserDefinedClassLoader extends ClassLoader {
private String classPath; private String classPath;
@ -13,16 +13,16 @@ public class MyFileClassLoader extends ClassLoader {
this.classPath = classPath; this.classPath = classPath;
} }
private MyFileClassLoader() { private UserDefinedClassLoader() {
} }
public MyFileClassLoader(String classPath) { public UserDefinedClassLoader(String classPath) {
this(); this();
this.setClassPath(classPath); this.setClassPath(classPath);
} }
/** /**
* 锟斤拷锟斤拷锟斤拷锟斤拷址锟斤拷指锟斤拷锟斤拷目录锟斤拷锟斤拷锟洁锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟<EFBFBD> *
*/ */
@Override @Override
protected Class<?> findClass(String name) throws ClassNotFoundException { protected Class<?> findClass(String name) throws ClassNotFoundException {
@ -36,15 +36,15 @@ public class MyFileClassLoader extends ClassLoader {
} }
/** /**
* 锟斤拷锟斤拷锟斤拷锟斤拷址锟斤拷锟斤拷锟斤拷 byte 锟斤拷锟斤拷锟<EFBFBD> *
* *
* @param name * @param name
* 锟斤拷锟斤拷锟街凤拷 锟斤拷锟界 com.cmw.entity.SysEntity *
* @return 锟斤拷锟斤拷锟斤拷锟侥硷拷 byte 锟斤拷锟斤拷锟<EFBFBD> * @return
* @throws IOException * @throws IOException
*/ */
private byte[] loadClassData(String name) throws IOException { private byte[] loadClassData(String name) throws IOException {
File file = getFile(name); File file = getClassFile(name);
@SuppressWarnings("resource") @SuppressWarnings("resource")
FileInputStream fis = new FileInputStream(file); FileInputStream fis = new FileInputStream(file);
byte[] arrData = new byte[(int) file.length()]; byte[] arrData = new byte[(int) file.length()];
@ -53,17 +53,15 @@ public class MyFileClassLoader extends ClassLoader {
} }
/** /**
* 锟斤拷锟斤拷锟斤拷锟斤拷址锟斤拷锟揭伙拷锟<EFBFBD>File 锟斤拷锟斤拷
* *
* @param name * @param name
* 锟斤拷锟斤拷锟街凤拷 * @return
* @return File 锟斤拷锟斤拷
* @throws FileNotFoundException * @throws FileNotFoundException
*/ */
private File getFile(String name) throws FileNotFoundException { private File getClassFile(String name) throws FileNotFoundException {
File dir = new File(classPath); File dir = new File(classPath);
if (!dir.exists()) if (!dir.exists())
throw new FileNotFoundException(classPath + " 目录锟斤拷锟斤拷锟节o拷"); throw new FileNotFoundException(classPath + " not exists!");
String _classPath = classPath.replaceAll("[\\\\]", "/"); String _classPath = classPath.replaceAll("[\\\\]", "/");
int offset = _classPath.lastIndexOf("/"); int offset = _classPath.lastIndexOf("/");
name = name.replaceAll("[.]", "/"); name = name.replaceAll("[.]", "/");
@ -73,7 +71,7 @@ public class MyFileClassLoader extends ClassLoader {
_classPath += name + ".class"; _classPath += name + ".class";
dir = new File(_classPath); dir = new File(_classPath);
if (!dir.exists()) if (!dir.exists())
throw new FileNotFoundException(dir + " 锟斤拷锟斤拷锟节o拷"); throw new FileNotFoundException(dir + " not exists!");
return dir; return dir;
} }

View File

@ -4,7 +4,7 @@ import static org.junit.Assert.*;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import org.bench4q.agent.parameterization.impl.MyFileClassLoader; import org.bench4q.agent.parameterization.impl.UserDefinedClassLoader;
import org.junit.Test; import org.junit.Test;
public class Test_MyFileClassLoader { public class Test_MyFileClassLoader {
@ -13,7 +13,7 @@ public class Test_MyFileClassLoader {
@Test @Test
public void test() throws ClassNotFoundException, InstantiationException, public void test() throws ClassNotFoundException, InstantiationException,
IllegalAccessException { IllegalAccessException {
MyFileClassLoader fileClsLoader = new MyFileClassLoader(Test_ClassPath); UserDefinedClassLoader fileClsLoader = new UserDefinedClassLoader(Test_ClassPath);
Class<?> cls = fileClsLoader Class<?> cls = fileClsLoader
.loadClass("com.cmw.entity.sys.AccordionEntity"); .loadClass("com.cmw.entity.sys.AccordionEntity");