fix test and some refactor
parent
0e6eb46eba
commit
df682857a7
|
@ -79,15 +79,14 @@ class PageModelExtractor {
|
|||
private void checkFormat(Field field, FieldExtractor fieldExtractor) {
|
||||
//check custom formatter
|
||||
Formatter formatter = field.getAnnotation(Formatter.class);
|
||||
if (formatter != null && !formatter.formatter().equals(ObjectFormatter.class)) {
|
||||
if (formatter != null) {
|
||||
if (!formatter.formatter().equals(ObjectFormatter.class)) {
|
||||
ObjectFormatter objectFormatter = initFormatter(formatter.formatter());
|
||||
objectFormatter.initParam(formatter.value());
|
||||
fieldExtractor.setObjectFormatter(objectFormatter);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (formatter == null) {
|
||||
return;
|
||||
}
|
||||
if (!formatter.formatter().equals(Formatter.DEFAULT_FORMATTER)) {
|
||||
ObjectFormatter objectFormatter = initFormatter(formatter.formatter());
|
||||
objectFormatter.initParam(formatter.value());
|
||||
fieldExtractor.setObjectFormatter(objectFormatter);
|
||||
return;
|
||||
}
|
||||
if (!fieldExtractor.isMulti() && !String.class.isAssignableFrom(field.getType())) {
|
||||
Class<?> fieldClazz = BasicTypeFormatter.detectBasicClass(field.getType());
|
||||
|
@ -126,11 +125,10 @@ class PageModelExtractor {
|
|||
try {
|
||||
return formatterClazz.newInstance();
|
||||
} catch (InstantiationException e) {
|
||||
logger.error("init ObjectFormatter fail", e);
|
||||
throw new RuntimeException(e);
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.error("init ObjectFormatter fail", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private FieldExtractor getAnnotationExtractByUrl(Class clazz, Field field) {
|
||||
|
@ -201,7 +199,7 @@ class PageModelExtractor {
|
|||
}
|
||||
|
||||
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);
|
||||
if (setterMethod != null) {
|
||||
fieldExtractor.setSetterMethod(setterMethod);
|
||||
|
|
|
@ -16,6 +16,8 @@ import java.lang.annotation.Target;
|
|||
@Target({ElementType.FIELD})
|
||||
public @interface Formatter {
|
||||
|
||||
Class<ObjectFormatter> DEFAULT_FORMATTER = ObjectFormatter.class;
|
||||
|
||||
/**
|
||||
* Set formatter params.
|
||||
*
|
||||
|
@ -36,6 +38,6 @@ public @interface Formatter {
|
|||
* If there are more than one formatter for a class, just specify the implement.
|
||||
* @return implement
|
||||
*/
|
||||
Class<? extends ObjectFormatter> formatter() default ObjectFormatter.class;
|
||||
Class<? extends ObjectFormatter> formatter() default DEFAULT_FORMATTER;
|
||||
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ public class PageMapperTest {
|
|||
|
||||
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;
|
||||
|
||||
public String getName() {
|
||||
|
|
Loading…
Reference in New Issue