NIHVIVO-1564 Dump of template model objects provides information about methods and their return values
This commit is contained in:
parent
c17380deb8
commit
f246cfe0cc
6 changed files with 118 additions and 69 deletions
|
@ -18,7 +18,6 @@ import edu.cornell.mannlib.vitro.webapp.utils.StringUtils;
|
|||
import edu.cornell.mannlib.vitro.webapp.web.directives.BaseTemplateDirectiveModel;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel;
|
||||
import freemarker.core.Environment;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.SimpleScalar;
|
||||
import freemarker.template.TemplateDirectiveBody;
|
||||
import freemarker.template.TemplateException;
|
||||
|
@ -31,7 +30,6 @@ public class DescribeDirective extends BaseTemplateDirectiveModel {
|
|||
|
||||
private static final Log log = LogFactory.getLog(DescribeDirective.class);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void execute(Environment env, Map params, TemplateModel[] loopVars,
|
||||
TemplateDirectiveBody body) throws TemplateException, IOException {
|
||||
|
@ -74,16 +72,17 @@ public class DescribeDirective extends BaseTemplateDirectiveModel {
|
|||
varName + " is not a template model.");
|
||||
}
|
||||
|
||||
List<Method> methods = getPublicMethods(unwrappedModel.getClass());
|
||||
List<String> methodDescriptions = new ArrayList<String>(methods.size());
|
||||
DumpHelper helper = new DumpHelper(env);
|
||||
List<Method> methods = helper.getMethodsAvailableToTemplate(unwrappedModel.getClass());
|
||||
List<String> methodDisplayNames = new ArrayList<String>(methods.size());
|
||||
for (Method m : methods) {
|
||||
methodDescriptions.add(getMethodDescription(m));
|
||||
methodDisplayNames.add(helper.getMethodDisplayName(m));
|
||||
}
|
||||
Collections.sort(methodDescriptions);
|
||||
Collections.sort(methodDisplayNames);
|
||||
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("var", varName);
|
||||
map.put("methods", methodDescriptions);
|
||||
map.put("methods", methodDisplayNames);
|
||||
|
||||
try {
|
||||
map.put("stylesheets", dataModel.get("stylesheets"));
|
||||
|
@ -91,7 +90,6 @@ public class DescribeDirective extends BaseTemplateDirectiveModel {
|
|||
log.error("Error getting value of stylesheets variable from data model.");
|
||||
}
|
||||
|
||||
DumpHelper helper = new DumpHelper(env);
|
||||
helper.writeDump("describe.ftl", map, varName);
|
||||
}
|
||||
|
||||
|
@ -116,56 +114,5 @@ public class DescribeDirective extends BaseTemplateDirectiveModel {
|
|||
|
||||
return mergeToHelpTemplate(map, env);
|
||||
}
|
||||
|
||||
private List<Method> getPublicMethods(Class<?> cls) {
|
||||
List<Method> methods = new ArrayList<Method>();
|
||||
|
||||
// Go up the class hierarchy only as far as the immediate subclass of BaseTemplateModel
|
||||
if (! cls.getName().equals("edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel")) {
|
||||
methods = getDeclaredPublicMethods(cls);
|
||||
methods.addAll(getPublicMethods(cls.getSuperclass()));
|
||||
}
|
||||
|
||||
return methods;
|
||||
}
|
||||
|
||||
private List<Method> getDeclaredPublicMethods(Class<?> cls) {
|
||||
|
||||
List<Method> methods = new ArrayList<Method>();
|
||||
Method[] declaredMethods = cls.getDeclaredMethods();
|
||||
for (Method m : declaredMethods) {
|
||||
int mod = m.getModifiers();
|
||||
if (Modifier.isPublic(mod) && !Modifier.isStatic(mod)) {
|
||||
methods.add(m);
|
||||
}
|
||||
}
|
||||
return methods;
|
||||
}
|
||||
|
||||
|
||||
private String getMethodDescription(Method method) {
|
||||
|
||||
String methodName = method.getName();
|
||||
methodName = methodName.replaceAll("^(get|is)", "");
|
||||
methodName = methodName.substring(0, 1).toLowerCase() + methodName.substring(1);
|
||||
|
||||
Class<?>[] paramTypes = method.getParameterTypes();
|
||||
String paramList = "";
|
||||
if (paramTypes.length > 0) {
|
||||
List<String> paramTypeList = new ArrayList<String>(paramTypes.length);
|
||||
for (Class<?> cls : paramTypes) {
|
||||
String name = cls.getName();
|
||||
String[] nameParts = name.split("\\.");
|
||||
String typeName = nameParts[nameParts.length-1];
|
||||
typeName = typeName.replaceAll(";", "s");
|
||||
typeName = typeName.substring(0,1).toLowerCase() + typeName.substring(1);
|
||||
paramTypeList.add(typeName);
|
||||
}
|
||||
paramList = "(" + StringUtils.join(paramTypeList) + ")";
|
||||
}
|
||||
|
||||
return methodName + paramList;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,13 +4,19 @@ package edu.cornell.mannlib.vitro.webapp.web.directives.dump;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.TemplateProcessingHelper;
|
||||
import edu.cornell.mannlib.vitro.webapp.utils.StringUtils;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.directives.BaseTemplateDirectiveModel;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel;
|
||||
import freemarker.core.Environment;
|
||||
|
@ -73,7 +79,7 @@ public class DumpHelper {
|
|||
// view in the dump. Not sure if we should handle our application-specific, non-template
|
||||
// model objects in the same way. For now, these get assigned a shorthand type below.
|
||||
if (unwrappedModel instanceof BaseTemplateModel) {
|
||||
value = ((BaseTemplateModel)unwrappedModel).dump();
|
||||
value = getTemplateModelDump((BaseTemplateModel)unwrappedModel); //((BaseTemplateModel)unwrappedModel).dump();
|
||||
type = className;
|
||||
}
|
||||
// Can't use this, because tm of (at least some) POJOs are
|
||||
|
@ -123,4 +129,85 @@ public class DumpHelper {
|
|||
}
|
||||
}
|
||||
|
||||
protected List<Method> getMethodsAvailableToTemplate(Class<?> cls) {
|
||||
List<Method> methods = new ArrayList<Method>();
|
||||
|
||||
// Go up the class hierarchy only as far as the immediate subclass of BaseTemplateModel
|
||||
if (! cls.getName().equals("edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel")) {
|
||||
methods = getDeclaredPublicMethods(cls);
|
||||
methods.addAll(getMethodsAvailableToTemplate(cls.getSuperclass()));
|
||||
}
|
||||
|
||||
return methods;
|
||||
}
|
||||
|
||||
private List<Method> getDeclaredPublicMethods(Class<?> cls) {
|
||||
|
||||
List<Method> methods = new ArrayList<Method>();
|
||||
Method[] declaredMethods = cls.getDeclaredMethods();
|
||||
for (Method m : declaredMethods) {
|
||||
int mod = m.getModifiers();
|
||||
if (Modifier.isPublic(mod) && !Modifier.isStatic(mod)) {
|
||||
// if the method takes args, make sure the BeanWrapper used makes this method visible
|
||||
methods.add(m);
|
||||
}
|
||||
}
|
||||
return methods;
|
||||
}
|
||||
|
||||
protected String getMethodDisplayName(Method method) {
|
||||
String methodName = method.getName();
|
||||
methodName = methodName.replaceAll("^(get|is)", "");
|
||||
methodName = methodName.substring(0, 1).toLowerCase() + methodName.substring(1);
|
||||
|
||||
Class<?>[] paramTypes = method.getParameterTypes();
|
||||
String paramList = "";
|
||||
if (paramTypes.length > 0) {
|
||||
List<String> paramTypeList = new ArrayList<String>(paramTypes.length);
|
||||
for (Class<?> cls : paramTypes) {
|
||||
String name = cls.getName();
|
||||
String[] nameParts = name.split("\\.");
|
||||
String typeName = nameParts[nameParts.length-1];
|
||||
typeName = typeName.replaceAll(";", "s");
|
||||
typeName = typeName.substring(0,1).toLowerCase() + typeName.substring(1);
|
||||
paramTypeList.add(typeName);
|
||||
}
|
||||
paramList = "(" + StringUtils.join(paramTypeList) + ")";
|
||||
}
|
||||
|
||||
return methodName + paramList;
|
||||
}
|
||||
|
||||
private String getTemplateModelDump(BaseTemplateModel model) {
|
||||
|
||||
log.debug(model.getClass());
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
List<Method> publicMethods = getMethodsAvailableToTemplate(model.getClass());
|
||||
Map<String, String> properties = new HashMap<String, String>();
|
||||
List<String> methods = new ArrayList<String>();
|
||||
for (Method method : publicMethods) {
|
||||
String key = getMethodDisplayName(method);
|
||||
|
||||
if (key.endsWith(")")) {
|
||||
methods.add(key);
|
||||
} else {
|
||||
try {
|
||||
Object value = method.invoke(model);
|
||||
if (value == null) {
|
||||
value = "null"; // distinguish a null from an empty string
|
||||
}
|
||||
properties.put(key, value.toString());
|
||||
} catch (Exception e) {
|
||||
log.error(e, e);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
map.put("properties", properties);
|
||||
map.put("methods", methods);
|
||||
return BaseTemplateDirectiveModel.processTemplateToString("dump-tm.ftl", map, env);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -43,8 +43,8 @@ public abstract class BaseTemplateModel {
|
|||
servletContext = context;
|
||||
}
|
||||
|
||||
public String dump() {
|
||||
return toString(); // fallback when subclass doesn't define a class-specific dump()
|
||||
}
|
||||
// public String dump() {
|
||||
// return toString(); // fallback when subclass doesn't define a class-specific dump()
|
||||
// }
|
||||
|
||||
}
|
||||
|
|
|
@ -74,10 +74,6 @@ public abstract class Files extends BaseTemplateModel {
|
|||
}
|
||||
return tags;
|
||||
}
|
||||
|
||||
public String dump() {
|
||||
return list.toString();
|
||||
}
|
||||
|
||||
protected abstract String getTag(String url);
|
||||
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
<#-- $This file is distributed under the terms of the license in /doc/license.txt$ -->
|
||||
|
||||
<#-- Template for dumping a template model object -->
|
||||
|
||||
<#if properties?has_content>
|
||||
<ul>
|
||||
<#list properties?keys as property>
|
||||
<li>${property}: ${properties[property]?html}</li>
|
||||
</#list>
|
||||
</ul>
|
||||
</#if>
|
||||
|
||||
<#if methods?has_content>
|
||||
<ul>
|
||||
<#list methods as method>
|
||||
<li>${method}</li>
|
||||
</#list>
|
||||
</ul>
|
||||
</#if>
|
|
@ -5,8 +5,8 @@
|
|||
<div class="var">
|
||||
<p><strong>Variable name: <em>${var}</em></strong></p>
|
||||
<#if value??>
|
||||
<p><strong>Value:</strong> ${value}</p>
|
||||
<p><strong>Type:</strong> ${type}</p>
|
||||
<div><strong>Value:</strong> ${value}</div>
|
||||
<#else>
|
||||
<p>Variable is undefined in the data model</p>
|
||||
</#if>
|
||||
|
|
Loading…
Add table
Reference in a new issue