dnet-hadoop/dhp-workflows/dhp-bulktag/src/main/java/eu/dnetlib/dhp/selectioncriteria/VerbResolver.java

58 lines
2.6 KiB
Java
Raw Normal View History

2020-03-03 16:38:50 +01:00
package eu.dnetlib.dhp.selectioncriteria;
import io.github.classgraph.ClassGraph;
import io.github.classgraph.ClassInfo;
import io.github.classgraph.ClassInfoList;
import io.github.classgraph.ScanResult;
2020-03-03 16:38:50 +01:00
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.stream.Collectors;
public class VerbResolver implements Serializable {
private Map<String, Class<Selection>> map = null; // = new HashMap<>();
private final ClassGraph classgraph = new ClassGraph();
2020-03-03 16:38:50 +01:00
2020-04-21 18:00:53 +02:00
public VerbResolver() {
2020-03-03 16:38:50 +01:00
try (ScanResult scanResult = // Assign scanResult in try-with-resources
classgraph // Create a new ClassGraph instance
.verbose() // If you want to enable logging to stderr
.enableAllInfo() // Scan classes, methods, fields, annotations
.whitelistPackages(
"eu.dnetlib.dhp.selectioncriteria") // Scan com.xyz and subpackages
.scan()) { // Perform the scan and return a ScanResult
ClassInfoList routeClassInfoList =
scanResult.getClassesWithAnnotation(
"eu.dnetlib.dhp.selectioncriteria.VerbClass");
this.map =
routeClassInfoList.stream()
.collect(
Collectors.toMap(
value ->
(String)
((ClassInfo) value)
.getAnnotationInfo()
.get(0)
.getParameterValues()
.get(0)
.getValue(),
value ->
(Class<Selection>)
((ClassInfo) value).loadClass()));
} catch (Exception e) {
e.printStackTrace();
}
2020-04-21 18:00:53 +02:00
}
2020-03-03 16:38:50 +01:00
2020-04-21 18:00:53 +02:00
public Selection getSelectionCriteria(String name, String param)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException,
InstantiationException {
2020-03-03 16:38:50 +01:00
// return Class.forName(tmp_map.get(name)).
2020-03-03 16:38:50 +01:00
return map.get(name).getDeclaredConstructor((String.class)).newInstance(param);
}
2020-04-21 18:00:53 +02:00
}