Commit 516ff331 authored by yihua.huang's avatar yihua.huang

add failfast

parent 7ee567b8
...@@ -13,6 +13,8 @@ import java.lang.annotation.Target; ...@@ -13,6 +13,8 @@ import java.lang.annotation.Target;
@Target({ElementType.FIELD}) @Target({ElementType.FIELD})
public @interface ExtractBy { public @interface ExtractBy {
//TODO: add list support
String value(); String value();
public enum Type {XPath, Regex, Css}; public enum Type {XPath, Regex, Css};
......
...@@ -42,6 +42,9 @@ class PageModelExtractor { ...@@ -42,6 +42,9 @@ class PageModelExtractor {
fieldExtractors = new ArrayList<FieldExtractor>(); fieldExtractors = new ArrayList<FieldExtractor>();
for (Field field : clazz.getDeclaredFields()) { for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true); field.setAccessible(true);
if (!field.getType().isAssignableFrom(String.class)){
throw new IllegalStateException("Field "+field.getName()+" must be string");
}
ExtractBy extractBy = field.getAnnotation(ExtractBy.class); ExtractBy extractBy = field.getAnnotation(ExtractBy.class);
if (extractBy != null) { if (extractBy != null) {
String value = extractBy.value(); String value = extractBy.value();
......
...@@ -14,7 +14,7 @@ import us.codecraft.webmagic.annotation.TargetUrl; ...@@ -14,7 +14,7 @@ import us.codecraft.webmagic.annotation.TargetUrl;
public class IteyeBlog implements Blog{ public class IteyeBlog implements Blog{
@ExtractBy("//title") @ExtractBy("//title")
private String title; private int title;
@ExtractBy(value = "div#blog_content",type = ExtractBy.Type.Css) @ExtractBy(value = "div#blog_content",type = ExtractBy.Type.Css)
private String content; private String content;
...@@ -32,7 +32,7 @@ public class IteyeBlog implements Blog{ ...@@ -32,7 +32,7 @@ public class IteyeBlog implements Blog{
} }
public String getTitle() { public String getTitle() {
return title; return null;
} }
public String getContent() { public String getContent() {
......
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