1.首先获得方法A所在类的Class object,即通过Class.forName(ClassName)以及 object.newInstance()获得。
注意这里的ClassName包含类所在的包名。
2.利用刚才得到的Class object调用getDeclaredMethod(String name, Class>... parameterTypes)方法。前一个参数是nonpublic方法A的名字,后面的参数是包含方法A的各个参数的类型的Class Object。这样就可以得到方法A的object,原文描述为:Returns a Method object that reflects the specified declared method of the class or interface represented by this Class object。
Method A’= getDeclaredMethod(“A”,Class>... parameterTypesOf”A”) 也可以保留方法A原名,这里为了区分命名为A’。
3.设置方法A’的可见性A’.setAccessible(true);
4.调用A’方法,注意一定要invoke()====>A’.invoke(A所在类的Classobject, A的参数)。
1.和私有方法的反射类似
2.利用刚才得到的Class object调用getDeclaredField(String fieldName)获取字段对象fieldObj。3.设置字段可见性fieldObj.setAccessible(true);
4.设置字段fieldObj.set(object, value)
获取字段的值fieldObj.get(object)
(1)写个类
package xxx.yyy.zzz
public class Simple {
private void displayMessage(String strMsg) {
System.out.println("message is:" + strMsg);
}
}
(2)反射调用
import java.lang.reflect.*;
public class Test {
public static void main(String[] args) throws ClassNotFoundException,
SecurityException, NoSuchMethodException, InstantiationException,
IllegalAccessException, IllegalArgumentException,
InvocationTargetException {
Class simpleClass = Class.forName("xxx.yyy.zzz.Simple");
Object simpelObject = simpleClass.newInstance();
Class[] args1 = new Class[1];
args1[0] = String.class;
Method simpleMethod = simpleClass.getDeclaredMethod("displayMessage",arg1);
simpleMethod.setAccessible(true);
String[] argments = new String[1];
argments[0] = "Hello,world";
simpleMethod.invoke(simpelObject, argments);
}
}
package org.apache.nutch.parse.html;
public class TestOnly {
private String[] array= new String[4];
private String[] getStringArray(){
return array;
}
private void setStringArray(String[] values){
array=values;
}
}
package org.apache.nutch.parse.html;
import org.junit.Test;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import static org.junit.Assert.*;
public class TestOnlyTest {
@Test
public void testGetStringArray() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException,
IllegalAccessException, InstantiationException, MalformedURLException, NoSuchFieldException {
Class testObj = Class.forName("org.apache.nutch.parse.html.TestOnly");
Object obj = testObj.newInstance();
/*test set method.*/
Method setArray = testObj.getDeclaredMethod("setStringArray", new Class[]{String[].class});
setArray.setAccessible(true);
String[] name = {"hello", "world"};
setArray.invoke(obj, new Object[]{name});
/*test get method.*/
Method getArray = testObj.getDeclaredMethod("getStringArray");
getArray.setAccessible(true);
String[] result = (String[])getArray.invoke(obj);
assertEquals("hello",result[0]);
assertEquals("world",result[1]);
/*test private field visit.*/
Field array =testObj.getDeclaredField("array");
array.setAccessible(true);
array.set(obj, new String[]{"come","here","baby"});
result = (String[])array.get(obj);
assertEquals("come",result[0]);
assertEquals("here",result[1]);
assertEquals("baby",result[2]);
}
}
当然不用单元测试,也可以用通过打印语句来验证结果集的正确性。
@Override
public ArrayList filterOutlinks(ArrayList list) {
ArrayList filterList = new ArrayList();
Outlink filterLink = null;
for (Outlink outlink : list) {
if (isCrawlOutLineUrl(outlink.getToUrl())) {
try {
filterLink = new Outlink(outlink.getToUrl(), "ydss");
} catch (MalformedURLException e) {
dMsg.warn("MalformedURLException:", e);
continue;
}
if (!filterList.contains(filterLink)) {
filterList.add(filterLink);
}
}
}
return filterList;
}
// @Test
public void testIsCrawlOutLineUrl() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException,
IllegalAccessException, InstantiationException, NoSuchFieldException{
String outLinkUrl = "http://bbs.ydss.cn/thread-199593-1-1.html";
Class ypObj = Class.forName("org.apache.nutch.parse.html.YdssParser");
Object yp = ypObj.newInstance();
Method isCrawlOutLineUrl = ypObj.getDeclaredMethod("isCrawlOutLineUrl", new Class[]{String.class});
isCrawlOutLineUrl.setAccessible(true);
Field url = ypObj.getDeclaredField("url");
url.setAccessible(true);
url.set(yp, "http://bbs.ydss.cn/forum.php?mod=viewthread&tid=505896&extra=page%3D1%26filter%3Dauthor%26orderby%3Ddateline");
assertFalse((Boolean) isCrawlOutLineUrl.invoke(yp, outLinkUrl));
outLinkUrl = "http://bbs.ydss.cn/thread-505896-1-1.html";
assertFalse((Boolean) isCrawlOutLineUrl.invoke(yp, outLinkUrl));
}