NIHVIVO-3746 Simplify the use of reflection in constructing DataGetter instances.
This commit is contained in:
parent
b688d6f1c6
commit
18e171e1b9
2 changed files with 19 additions and 48 deletions
|
@ -74,26 +74,6 @@ public class DataGetterUtils {
|
|||
return dgList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if classInQuestion implements interFace.
|
||||
*/
|
||||
public static boolean isInstanceOfInterface( Class classInQuestion, Class interFace){
|
||||
if( classInQuestion == null || interFace == null )
|
||||
throw new IllegalAccessError("classInQuestion or interFace must not be null");
|
||||
|
||||
//figure out if it implements interface
|
||||
Class[] interfaces = classInQuestion.getInterfaces();
|
||||
if( interfaces == null )
|
||||
return false;
|
||||
boolean foundIface = false;
|
||||
for( Class cz : interfaces ){
|
||||
if( interFace.equals( cz ) ){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a DataGetter using information in the
|
||||
* displayModel for the individual with the URI given by dataGetterURI
|
||||
|
@ -106,42 +86,36 @@ public class DataGetterUtils {
|
|||
public static DataGetter dataGetterForURI(Model displayModel, String dataGetterURI)
|
||||
throws InstantiationException, IllegalAccessException, ClassNotFoundException, IllegalArgumentException, InvocationTargetException, SecurityException, NoSuchMethodException
|
||||
{
|
||||
|
||||
//get java class for dataGetterURI
|
||||
String dgClassName = getJClassForDataGetterURI(displayModel, dataGetterURI);
|
||||
|
||||
//figure out if it implements interface DataGetter
|
||||
Class dgClass = Class.forName(dgClassName);
|
||||
if( ! isInstanceOfInterface( dgClass, DataGetter.class) ){
|
||||
Class<?> clz = Class.forName(dgClassName);
|
||||
if( ! DataGetter.class.isAssignableFrom(clz) ){
|
||||
log.debug("Class doesn't implement DataGetter: '" + dgClassName + "'");
|
||||
return null;
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
Class<DataGetter> dgClass = (Class<DataGetter>) clz;
|
||||
|
||||
//try to run constructor with (Model, String) parameters
|
||||
Class partypes[] = { Model.class , String.class };
|
||||
Constructor ct = dgClass.getConstructor( partypes );
|
||||
Constructor<DataGetter> ct = null;
|
||||
|
||||
Object obj = null;
|
||||
if( ct != null ){
|
||||
Object[] initargs = new Object[2];
|
||||
initargs[0]= displayModel;
|
||||
initargs[1] = dataGetterURI;
|
||||
obj = ct.newInstance(initargs);
|
||||
} else {
|
||||
log.debug("no constructor with signature " +
|
||||
"(Model displayModel,String URI) found, trying empty constructor");
|
||||
obj = dgClass.newInstance();
|
||||
ct = dgClass.getConstructor(Model.class, String.class);
|
||||
if (ct != null) {
|
||||
log.debug("Using this constructor: " + ct);
|
||||
return ct.newInstance(displayModel, dataGetterURI);
|
||||
}
|
||||
|
||||
if( !(obj instanceof DataGetter) ){
|
||||
log.debug("For <" + dataGetterURI + "> the class " +
|
||||
"for its rdf:type " + dgClassName + " does not implement the interface DataGetter.");
|
||||
ct = dgClass.getConstructor();
|
||||
if (ct != null) {
|
||||
log.debug("Using this constructor: " + ct);
|
||||
return ct.newInstance();
|
||||
}
|
||||
|
||||
log.debug("Didn't find a suitable constructor for '" + dgClassName + "'");
|
||||
return null;
|
||||
}
|
||||
|
||||
log.debug("dataGetterForURI: " + obj);
|
||||
return (DataGetter)obj;
|
||||
}
|
||||
|
||||
public static String getJClassForDataGetterURI(Model displayModel, String dataGetterURI) throws IllegalAccessException {
|
||||
String query = prefixes +
|
||||
"SELECT ?type WHERE { ?dgURI rdf:type ?type } ";
|
||||
|
|
|
@ -126,12 +126,9 @@ public class PageDataGetterUtils {
|
|||
String className = getClassNameFromUri(dgClassName);
|
||||
Class clz = Class.forName(className);
|
||||
|
||||
if( DataGetterUtils.isInstanceOfInterface(clz, PageDataGetter.class)){
|
||||
Object obj = clz.newInstance();
|
||||
if(obj != null && obj instanceof PageDataGetter) {
|
||||
PageDataGetter pg = (PageDataGetter) obj;
|
||||
if( PageDataGetter.class.isAssignableFrom(clz)){
|
||||
PageDataGetter pg = (PageDataGetter) clz.newInstance();
|
||||
dataGetterObjects.add(pg);
|
||||
}
|
||||
}// else skip if class does not implement PageDataGetter
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue