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,33 +27,33 @@ 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
public void process(MethodAppearanceDecisionInput methodAppearanceDecisionInput, MethodAppearanceDecision methodAppearanceDecision) {
Method method = methodAppearanceDecisionInput.getMethod();
// How to define a setter? This is a weak approximation: a method whose name
// starts with "set" or returns void.
if ( method.getName().startsWith("set") ) {
methodAppearanceDecision.setExposeMethodAs(null);
@SuppressWarnings("rawtypes") } else if ( method.getReturnType().getName().equals("void") ) {
@Override methodAppearanceDecision.setExposeMethodAs(null);
protected void finetuneMethodAppearance(Class cls, Method method, MethodAppearanceDecision decision) {
// How to define a setter? This is a weak approximation: a method whose name } else {
// starts with "set" or returns void.
if ( method.getName().startsWith("set") ) {
decision.setExposeMethodAs(null);
} else if ( method.getReturnType().getName().equals("void") ) { Class<?> declaringClass = method.getDeclaringClass();
decision.setExposeMethodAs(null); if (declaringClass.equals(java.lang.Object.class)) {
methodAppearanceDecision.setExposeMethodAs(null);
} else { } else {
Package pkg = declaringClass.getPackage();
Class<?> declaringClass = method.getDeclaringClass(); if (pkg.getName().equals("java.util")) {
if (declaringClass.equals(java.lang.Object.class)) { methodAppearanceDecision.setExposeMethodAs(null);
decision.setExposeMethodAs(null); }
}
} else {
Package pkg = declaringClass.getPackage();
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

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);
}
} }
} }