|
| 1 | +package com.hubspot.jinjava.el; |
| 2 | + |
| 3 | +import com.hubspot.jinjava.el.ext.AllowlistReturnTypeValidator; |
| 4 | +import java.beans.FeatureDescriptor; |
| 5 | +import java.util.Iterator; |
| 6 | +import javax.el.ELContext; |
| 7 | +import javax.el.ELResolver; |
| 8 | + |
| 9 | +class ReturnTypeValidatingJinjavaInterpreterResolver extends ELResolver { |
| 10 | + |
| 11 | + private final AllowlistReturnTypeValidator returnTypeValidator; |
| 12 | + private final JinjavaInterpreterResolver delegate; |
| 13 | + |
| 14 | + ReturnTypeValidatingJinjavaInterpreterResolver( |
| 15 | + AllowlistReturnTypeValidator returnTypeValidator, |
| 16 | + JinjavaInterpreterResolver delegate |
| 17 | + ) { |
| 18 | + this.returnTypeValidator = returnTypeValidator; |
| 19 | + this.delegate = delegate; |
| 20 | + } |
| 21 | + |
| 22 | + @Override |
| 23 | + public Class<?> getCommonPropertyType(ELContext context, Object base) { |
| 24 | + return delegate.getCommonPropertyType(context, base); |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public Iterator<FeatureDescriptor> getFeatureDescriptors( |
| 29 | + ELContext context, |
| 30 | + Object base |
| 31 | + ) { |
| 32 | + return delegate.getFeatureDescriptors(context, base); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public Class<?> getType(ELContext context, Object base, Object property) { |
| 37 | + return delegate.getType(context, base, property); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public Object getValue(ELContext context, Object base, Object property) { |
| 42 | + return returnTypeValidator.validateReturnType( |
| 43 | + wrap(delegate.getValue(context, base, property)) |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public boolean isReadOnly(ELContext context, Object base, Object property) { |
| 49 | + return delegate.isReadOnly(context, base, property); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void setValue(ELContext context, Object base, Object property, Object value) { |
| 54 | + delegate.setValue(context, base, property, value); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public Object invoke( |
| 59 | + ELContext context, |
| 60 | + Object base, |
| 61 | + Object method, |
| 62 | + Class<?>[] paramTypes, |
| 63 | + Object[] params |
| 64 | + ) { |
| 65 | + return returnTypeValidator.validateReturnType( |
| 66 | + wrap(delegate.invoke(context, base, method, paramTypes, params)) |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + Object wrap(Object object) { |
| 71 | + return delegate.wrap(object); |
| 72 | + } |
| 73 | +} |
0 commit comments