Fix deprecated use of Freemarker

This commit is contained in:
Graham Triggs 2016-12-17 18:23:02 +00:00
parent a13e353228
commit 56dc6cf750
2 changed files with 40 additions and 39 deletions

View file

@ -4,6 +4,7 @@ package edu.cornell.mannlib.vitro.webapp.web.beanswrappers;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import freemarker.ext.beans.MethodAppearanceFineTuner;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -26,35 +27,35 @@ public class ReadOnlyBeansWrapper extends BeansWrapper {
public ReadOnlyBeansWrapper() { public ReadOnlyBeansWrapper() {
// Start by exposing all safe methods. // Start by exposing all safe methods.
setExposureLevel(EXPOSE_SAFE); setExposureLevel(EXPOSE_SAFE);
} setMethodAppearanceFineTuner(new MethodAppearanceFineTuner() {
@Override
@SuppressWarnings("rawtypes") public void process(MethodAppearanceDecisionInput methodAppearanceDecisionInput, MethodAppearanceDecision methodAppearanceDecision) {
@Override Method method = methodAppearanceDecisionInput.getMethod();
protected void finetuneMethodAppearance(Class cls, Method method, MethodAppearanceDecision decision) { // How to define a setter? This is a weak approximation: a method whose name
// starts with "set" or returns void.
// How to define a setter? This is a weak approximation: a method whose name if ( method.getName().startsWith("set") ) {
// starts with "set" or returns void. methodAppearanceDecision.setExposeMethodAs(null);
if ( method.getName().startsWith("set") ) {
decision.setExposeMethodAs(null); } else if ( method.getReturnType().getName().equals("void") ) {
methodAppearanceDecision.setExposeMethodAs(null);
} else if ( method.getReturnType().getName().equals("void") ) {
decision.setExposeMethodAs(null); } else {
} else { Class<?> declaringClass = method.getDeclaringClass();
if (declaringClass.equals(java.lang.Object.class)) {
Class<?> declaringClass = method.getDeclaringClass(); methodAppearanceDecision.setExposeMethodAs(null);
if (declaringClass.equals(java.lang.Object.class)) {
decision.setExposeMethodAs(null); } else {
Package pkg = declaringClass.getPackage();
} else { if (pkg.getName().equals("java.util")) {
Package pkg = declaringClass.getPackage(); methodAppearanceDecision.setExposeMethodAs(null);
if (pkg.getName().equals("java.util")) { }
decision.setExposeMethodAs(null); }
} }
} }
} });
} }
// For exposing a method as a property (when it's not named getX or isX). Note that this is not // For exposing a method as a property (when it's not named getX or isX). Note that this is not
// just a syntactic change in the template from X() to X, but also makes the value get precomputed. // just a syntactic change in the template from X() to X, but also makes the value get precomputed.
// private void exposeAsProperty(Method method, MethodAppearanceDecision decision) { // private void exposeAsProperty(Method method, MethodAppearanceDecision decision) {

View file

@ -5,6 +5,7 @@ package edu.cornell.mannlib.vitro.webapp.web.templatemodels;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import freemarker.ext.beans.MethodAppearanceFineTuner;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -46,20 +47,19 @@ public class Tags extends BaseTemplateModel {
public TagsWrapper() { public TagsWrapper() {
// Start by exposing all safe methods. // Start by exposing all safe methods.
setExposureLevel(EXPOSE_SAFE); setExposureLevel(EXPOSE_SAFE);
} setMethodAppearanceFineTuner(new MethodAppearanceFineTuner() {
@Override
@SuppressWarnings("rawtypes") public void process(MethodAppearanceDecisionInput methodAppearanceDecisionInput, MethodAppearanceDecision methodAppearanceDecision) {
@Override try {
protected void finetuneMethodAppearance(Class cls, Method method, MethodAppearanceDecision decision) { String methodName = methodAppearanceDecisionInput.getMethod().getName();
if ( ! ( methodName.equals("add") || methodName.equals("list")) ) {
try { methodAppearanceDecision.setExposeMethodAs(null);
String methodName = method.getName(); }
if ( ! ( methodName.equals("add") || methodName.equals("list")) ) { } catch (Exception e) {
decision.setExposeMethodAs(null); log.error(e, e);
}
} }
} catch (Exception e) { });
log.error(e, e);
}
} }
} }