Commit 69245e8c authored by yihua.huang's avatar yihua.huang

fix Class.assinable bug

parent 65518f76
...@@ -39,7 +39,7 @@ class PageModelExtractor { ...@@ -39,7 +39,7 @@ class PageModelExtractor {
this.clazz = clazz; this.clazz = clazz;
initTargetUrlPatterns(); initTargetUrlPatterns();
fieldExtractors = new ArrayList<FieldExtractor>(); fieldExtractors = new ArrayList<FieldExtractor>();
if (clazz.isAssignableFrom(AfterExtractor.class)) { if (AfterExtractor.class.isAssignableFrom(clazz)) {
try { try {
afterExtractor = (AfterExtractor) clazz.newInstance(); afterExtractor = (AfterExtractor) clazz.newInstance();
} catch (Exception e) { } catch (Exception e) {
...@@ -50,9 +50,9 @@ class PageModelExtractor { ...@@ -50,9 +50,9 @@ class PageModelExtractor {
field.setAccessible(true); field.setAccessible(true);
ExtractBy extractBy = field.getAnnotation(ExtractBy.class); ExtractBy extractBy = field.getAnnotation(ExtractBy.class);
if (extractBy != null) { if (extractBy != null) {
if (!extractBy.multi() && !field.getType().isAssignableFrom(String.class)) { if (!extractBy.multi() && !String.class.isAssignableFrom(field.getType())) {
throw new IllegalStateException("Field " + field.getName() + " must be string"); throw new IllegalStateException("Field " + field.getName() + " must be string");
} else if (extractBy.multi() && !field.getType().isAssignableFrom(List.class)) { } else if (extractBy.multi() && !List.class.isAssignableFrom(field.getType())) {
throw new IllegalStateException("Field " + field.getName() + " must be list"); throw new IllegalStateException("Field " + field.getName() + " must be list");
} }
String value = extractBy.value(); String value = extractBy.value();
...@@ -82,9 +82,9 @@ class PageModelExtractor { ...@@ -82,9 +82,9 @@ class PageModelExtractor {
} }
ExtractByUrl extractByUrl = field.getAnnotation(ExtractByUrl.class); ExtractByUrl extractByUrl = field.getAnnotation(ExtractByUrl.class);
if (extractByUrl != null) { if (extractByUrl != null) {
if (!extractByUrl.multi() && !field.getType().isAssignableFrom(String.class)) { if (!extractByUrl.multi() && !String.class.isAssignableFrom(field.getType())) {
throw new IllegalStateException("Field " + field.getName() + " must be string"); throw new IllegalStateException("Field " + field.getName() + " must be string");
} else if (extractByUrl.multi() && !field.getType().isAssignableFrom(List.class)) { } else if (extractByUrl.multi() && !List.class.isAssignableFrom(field.getType())) {
throw new IllegalStateException("Field " + field.getName() + " must be list"); throw new IllegalStateException("Field " + field.getName() + " must be list");
} }
String regexPattern = extractByUrl.value(); String regexPattern = extractByUrl.value();
......
package us.codecraft.webmagic.oo; package us.codecraft.webmagic.oo;
import us.codecraft.webmagic.Page;
import java.util.List; import java.util.List;
/** /**
...@@ -8,7 +10,7 @@ import java.util.List; ...@@ -8,7 +10,7 @@ import java.util.List;
* Time: 下午10:18 <br> * Time: 下午10:18 <br>
*/ */
@TargetUrl("http://my.oschina.net/flashsword/blog/*") @TargetUrl("http://my.oschina.net/flashsword/blog/*")
public class OschinaBlog { public class OschinaBlog implements AfterExtractor<OschinaBlog>{
@ExtractBy("//title") @ExtractBy("//title")
private String title; private String title;
...@@ -19,4 +21,7 @@ public class OschinaBlog { ...@@ -19,4 +21,7 @@ public class OschinaBlog {
@ExtractBy(value = "//div[@class='BlogTags']/a/text()", multi = true) @ExtractBy(value = "//div[@class='BlogTags']/a/text()", multi = true)
private List<String> tags; private List<String> tags;
@Override
public void afterProcess(Page page, OschinaBlog oschinaBlog) {
}
} }
package us.codecraft.webmagic.oo; package us.codecraft.webmagic.oo;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import us.codecraft.webmagic.Site; import us.codecraft.webmagic.Site;
import java.util.ArrayList;
import java.util.List;
/** /**
* @author yihua.huang@dianping.com <br> * @author yihua.huang@dianping.com <br>
* @date: 13-8-1 <br> * @date: 13-8-1 <br>
...@@ -10,12 +14,15 @@ import us.codecraft.webmagic.Site; ...@@ -10,12 +14,15 @@ import us.codecraft.webmagic.Site;
*/ */
public class TestFetcher { public class TestFetcher {
// @Ignore("takes long") @Ignore("takes long")
@Test @Test
public void test() { public void test() {
System.out.println(List.class.isAssignableFrom(ArrayList.class));
OOSpider.create(Site.me().addStartUrl("http://my.oschina.net/flashsword/blog/145796"), OschinaBlog.class) OOSpider.create(Site.me().addStartUrl("http://my.oschina.net/flashsword/blog/145796"), OschinaBlog.class)
.run(); .run();
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment