Skip to content

Commit

Permalink
Change POC implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
synesty-cr committed Nov 4, 2019
1 parent 79da3c0 commit a2ff9a7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
46 changes: 41 additions & 5 deletions src/main/java/freemarker/ext/beans/ClassIntrospector.java
Expand Up @@ -336,7 +336,7 @@ private void addBeanInfoToClassIntrospectionData(
IdentityHashMap<Method, Void> argTypesUsedByIndexerPropReaders = null;
for (int i = mdsSize - 1; i >= 0; --i) {
final Method method = getMatchingAccessibleMethod(mds.get(i).getMethod(), accessibleMethods);
if (method != null && isAllowedToExpose(method)) {
if (method != null && isAllowedToExpose(clazz, method)) {
decision.setDefaults(method);
if (methodAppearanceFineTuner != null) {
if (decisionInput == null) {
Expand Down Expand Up @@ -663,15 +663,15 @@ private boolean containsMethodWithSameParameterTypes(List<Method> overloads, Met
private void addPropertyDescriptorToClassIntrospectionData(Map<Object, Object> introspData,
PropertyDescriptor pd, Class<?> clazz, Map<MethodSignature, List<Method>> accessibleMethods) {
Method readMethod = getMatchingAccessibleMethod(pd.getReadMethod(), accessibleMethods);
if (readMethod != null && !isAllowedToExpose(readMethod)) {
if (readMethod != null && !isAllowedToExpose(clazz, readMethod)) {
readMethod = null;
}

Method indexedReadMethod;
if (pd instanceof IndexedPropertyDescriptor) {
indexedReadMethod = getMatchingAccessibleMethod(
((IndexedPropertyDescriptor) pd).getIndexedReadMethod(), accessibleMethods);
if (indexedReadMethod != null && !isAllowedToExpose(indexedReadMethod)) {
if (indexedReadMethod != null && !isAllowedToExpose(clazz, indexedReadMethod)) {
indexedReadMethod = null;
}
if (indexedReadMethod != null) {
Expand Down Expand Up @@ -809,8 +809,44 @@ private void sortMethodDescriptors(List<MethodDescriptor> methodDescriptors) {
}
}

boolean isAllowedToExpose(Method method) {
return exposureLevel < BeansWrapper.EXPOSE_SAFE || !UnsafeMethods.isUnsafeMethod(method);
boolean isAllowedToExpose(Class<?> clazz, Method method) {
return exposureLevel < BeansWrapper.EXPOSE_SAFE || !UnsafeMethods.isUnsafeMethod(method) || !isMethodAllowedToExpose(clazz, method);
}

/**
* Checks if a method is allowed to be exposed
* using the {@link MethodAppearanceFineTuner}
*
* TODO this is just a Proof-of-concept implementation.
*
*
* @param clazz
* @param method
* @return
*/
private boolean isMethodAllowedToExpose(Class<?> clazz, Method method) {

if(method == null) {
return false;
}

if (methodAppearanceFineTuner != null) {

final MethodAppearanceDecision decision = new MethodAppearanceDecision();
decision.setDefaults(method);

final MethodAppearanceDecisionInput decisionInput = new MethodAppearanceDecisionInput();
decisionInput.setContainingClass(clazz);
decisionInput.setMethod(method);

methodAppearanceFineTuner.process(decisionInput, decision);

if(decision.getExposeMethodAs() == null && decision.getExposeAsProperty() == null) {
return false;
}
}

return true;
}

private static Map<Method, Class<?>[]> getArgTypesByMethod(Map<Object, Object> classInfo) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/freemarker/ext/beans/StaticModel.java
Expand Up @@ -131,7 +131,7 @@ private void populate() throws TemplateModelException {
Method method = methods[i];
int mod = method.getModifiers();
if (Modifier.isPublic(mod) && Modifier.isStatic(mod)
&& wrapper.getClassIntrospector().isAllowedToExpose(method)) {
&& wrapper.getClassIntrospector().isAllowedToExpose(clazz, method)) {
String name = method.getName();
Object obj = map.get(name);
if (obj instanceof Method) {
Expand Down

0 comments on commit a2ff9a7

Please sign in to comment.