use of com.virjar.vscrawler.core.processor.configurableprocessor.annotiondriven.annotation.AutoProcessor in project vscrawler by virjar.
the class AnnotationProcessorBuilder method judgeMatchStrategy.
private AnnotationSeedProcessor.MatchStrategy judgeMatchStrategy(Class<? extends AbstractAutoProcessModel> aClass) {
final AutoProcessor autoProcessor = aClass.getAnnotation(AutoProcessor.class);
if (autoProcessor == null) {
return null;
}
String seedPattern = autoProcessor.seedPattern();
if (StringUtils.isNotBlank(seedPattern)) {
try {
final Pattern pattern = Pattern.compile(seedPattern);
return new AnnotationSeedProcessor.MatchStrategy() {
@Override
public boolean matchSeed(Seed seed) {
return pattern.matcher(seed.getData()).matches();
}
@Override
public int priority() {
return autoProcessor.priority();
}
};
} catch (PatternSyntaxException e) {
throw new IllegalStateException("error when register processor for class" + aClass.getName() + " regex error for seedPattern", e);
}
}
Method[] methods = aClass.getMethods();
for (final Method method : methods) {
if (method.getAnnotation(MatchSeed.class) == null) {
continue;
}
Preconditions.checkArgument(Boolean.class.isAssignableFrom(method.getReturnType()));
Preconditions.checkArgument(Modifier.isStatic(method.getModifiers()));
return new AnnotationSeedProcessor.MatchStrategy() {
@Override
public boolean matchSeed(Seed seed) {
try {
return (Boolean) method.invoke(null, seed);
} catch (Exception e) {
throw new IllegalStateException("can not jude seed match method", e);
}
}
@Override
public int priority() {
return autoProcessor.priority();
}
};
}
return null;
}
Aggregations