Commit df682857 authored by yihua.huang's avatar yihua.huang

fix test and some refactor

parent 0e6eb46e
...@@ -79,16 +79,15 @@ class PageModelExtractor { ...@@ -79,16 +79,15 @@ class PageModelExtractor {
private void checkFormat(Field field, FieldExtractor fieldExtractor) { private void checkFormat(Field field, FieldExtractor fieldExtractor) {
//check custom formatter //check custom formatter
Formatter formatter = field.getAnnotation(Formatter.class); Formatter formatter = field.getAnnotation(Formatter.class);
if (formatter != null && !formatter.formatter().equals(ObjectFormatter.class)) { if (formatter == null) {
if (formatter != null) { return;
if (!formatter.formatter().equals(ObjectFormatter.class)) { }
if (!formatter.formatter().equals(Formatter.DEFAULT_FORMATTER)) {
ObjectFormatter objectFormatter = initFormatter(formatter.formatter()); ObjectFormatter objectFormatter = initFormatter(formatter.formatter());
objectFormatter.initParam(formatter.value()); objectFormatter.initParam(formatter.value());
fieldExtractor.setObjectFormatter(objectFormatter); fieldExtractor.setObjectFormatter(objectFormatter);
return; return;
} }
}
}
if (!fieldExtractor.isMulti() && !String.class.isAssignableFrom(field.getType())) { if (!fieldExtractor.isMulti() && !String.class.isAssignableFrom(field.getType())) {
Class<?> fieldClazz = BasicTypeFormatter.detectBasicClass(field.getType()); Class<?> fieldClazz = BasicTypeFormatter.detectBasicClass(field.getType());
ObjectFormatter objectFormatter = getObjectFormatter(field, fieldClazz, formatter); ObjectFormatter objectFormatter = getObjectFormatter(field, fieldClazz, formatter);
...@@ -126,11 +125,10 @@ class PageModelExtractor { ...@@ -126,11 +125,10 @@ class PageModelExtractor {
try { try {
return formatterClazz.newInstance(); return formatterClazz.newInstance();
} catch (InstantiationException e) { } catch (InstantiationException e) {
logger.error("init ObjectFormatter fail", e); throw new RuntimeException(e);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
logger.error("init ObjectFormatter fail", e); throw new RuntimeException(e);
} }
return null;
} }
private FieldExtractor getAnnotationExtractByUrl(Class clazz, Field field) { private FieldExtractor getAnnotationExtractByUrl(Class clazz, Field field) {
...@@ -201,7 +199,7 @@ class PageModelExtractor { ...@@ -201,7 +199,7 @@ class PageModelExtractor {
} }
fieldExtractor = new FieldExtractor(field, selector, source, fieldExtractor = new FieldExtractor(field, selector, source,
extractBy.notNull(), extractBy.multi() || List.class.isAssignableFrom(field.getType())); extractBy.notNull(), List.class.isAssignableFrom(field.getType()));
Method setterMethod = getSetterMethod(clazz, field); Method setterMethod = getSetterMethod(clazz, field);
if (setterMethod != null) { if (setterMethod != null) {
fieldExtractor.setSetterMethod(setterMethod); fieldExtractor.setSetterMethod(setterMethod);
......
...@@ -16,6 +16,8 @@ import java.lang.annotation.Target; ...@@ -16,6 +16,8 @@ import java.lang.annotation.Target;
@Target({ElementType.FIELD}) @Target({ElementType.FIELD})
public @interface Formatter { public @interface Formatter {
Class<ObjectFormatter> DEFAULT_FORMATTER = ObjectFormatter.class;
/** /**
* Set formatter params. * Set formatter params.
* *
...@@ -36,6 +38,6 @@ public @interface Formatter { ...@@ -36,6 +38,6 @@ public @interface Formatter {
* If there are more than one formatter for a class, just specify the implement. * If there are more than one formatter for a class, just specify the implement.
* @return implement * @return implement
*/ */
Class<? extends ObjectFormatter> formatter() default ObjectFormatter.class; Class<? extends ObjectFormatter> formatter() default DEFAULT_FORMATTER;
} }
...@@ -20,7 +20,7 @@ public class PageMapperTest { ...@@ -20,7 +20,7 @@ public class PageMapperTest {
public static class GithubRepo { public static class GithubRepo {
@ExtractBy(type = ExtractBy.Type.JsonPath, value = "$.name") @ExtractBy(type = ExtractBy.Type.JsonPath, value = "$.name",source = ExtractBy.Source.RawText)
private String name; private String name;
public String getName() { public String getName() {
......
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