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;
|
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
|
* Returns a DataGetter using information in the
|
||||||
* displayModel for the individual with the URI given by dataGetterURI
|
* displayModel for the individual with the URI given by dataGetterURI
|
||||||
|
@ -106,40 +86,34 @@ public class DataGetterUtils {
|
||||||
public static DataGetter dataGetterForURI(Model displayModel, String dataGetterURI)
|
public static DataGetter dataGetterForURI(Model displayModel, String dataGetterURI)
|
||||||
throws InstantiationException, IllegalAccessException, ClassNotFoundException, IllegalArgumentException, InvocationTargetException, SecurityException, NoSuchMethodException
|
throws InstantiationException, IllegalAccessException, ClassNotFoundException, IllegalArgumentException, InvocationTargetException, SecurityException, NoSuchMethodException
|
||||||
{
|
{
|
||||||
|
|
||||||
//get java class for dataGetterURI
|
//get java class for dataGetterURI
|
||||||
String dgClassName = getJClassForDataGetterURI(displayModel, dataGetterURI);
|
String dgClassName = getJClassForDataGetterURI(displayModel, dataGetterURI);
|
||||||
|
|
||||||
//figure out if it implements interface DataGetter
|
//figure out if it implements interface DataGetter
|
||||||
Class dgClass = Class.forName(dgClassName);
|
Class<?> clz = Class.forName(dgClassName);
|
||||||
if( ! isInstanceOfInterface( dgClass, DataGetter.class) ){
|
if( ! DataGetter.class.isAssignableFrom(clz) ){
|
||||||
|
log.debug("Class doesn't implement DataGetter: '" + dgClassName + "'");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Class<DataGetter> dgClass = (Class<DataGetter>) clz;
|
||||||
|
|
||||||
//try to run constructor with (Model, String) parameters
|
Constructor<DataGetter> ct = null;
|
||||||
Class partypes[] = { Model.class , String.class };
|
|
||||||
Constructor ct = dgClass.getConstructor( partypes );
|
|
||||||
|
|
||||||
Object obj = null;
|
ct = dgClass.getConstructor(Model.class, String.class);
|
||||||
if( ct != null ){
|
if (ct != null) {
|
||||||
Object[] initargs = new Object[2];
|
log.debug("Using this constructor: " + ct);
|
||||||
initargs[0]= displayModel;
|
return ct.newInstance(displayModel, dataGetterURI);
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if( !(obj instanceof DataGetter) ){
|
ct = dgClass.getConstructor();
|
||||||
log.debug("For <" + dataGetterURI + "> the class " +
|
if (ct != null) {
|
||||||
"for its rdf:type " + dgClassName + " does not implement the interface DataGetter.");
|
log.debug("Using this constructor: " + ct);
|
||||||
return null;
|
return ct.newInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
log.debug("dataGetterForURI: " + obj);
|
log.debug("Didn't find a suitable constructor for '" + dgClassName + "'");
|
||||||
return (DataGetter)obj;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getJClassForDataGetterURI(Model displayModel, String dataGetterURI) throws IllegalAccessException {
|
public static String getJClassForDataGetterURI(Model displayModel, String dataGetterURI) throws IllegalAccessException {
|
||||||
|
|
|
@ -126,12 +126,9 @@ public class PageDataGetterUtils {
|
||||||
String className = getClassNameFromUri(dgClassName);
|
String className = getClassNameFromUri(dgClassName);
|
||||||
Class clz = Class.forName(className);
|
Class clz = Class.forName(className);
|
||||||
|
|
||||||
if( DataGetterUtils.isInstanceOfInterface(clz, PageDataGetter.class)){
|
if( PageDataGetter.class.isAssignableFrom(clz)){
|
||||||
Object obj = clz.newInstance();
|
PageDataGetter pg = (PageDataGetter) clz.newInstance();
|
||||||
if(obj != null && obj instanceof PageDataGetter) {
|
dataGetterObjects.add(pg);
|
||||||
PageDataGetter pg = (PageDataGetter) obj;
|
|
||||||
dataGetterObjects.add(pg);
|
|
||||||
}
|
|
||||||
}// else skip if class does not implement PageDataGetter
|
}// else skip if class does not implement PageDataGetter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue