Skip to content

Commit

Permalink
feat: update ognl version to 3.4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
RaymondLam1 committed Aug 28, 2023
1 parent e4fab59 commit eb2d618
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
package com.taobao.arthas.core.command.express;

import java.util.Map;

import com.taobao.arthas.core.GlobalOptions;

import ognl.ObjectPropertyAccessor;
import ognl.OgnlContext;
import ognl.OgnlException;

/**
*
* @author hengyunabc 2022-03-24
*
*/
public class ArthasObjectPropertyAccessor extends ObjectPropertyAccessor {

@Override
public Object setPossibleProperty(Map context, Object target, String name, Object value) throws OgnlException {
public Object setPossibleProperty(OgnlContext context, Object target, String name, Object value)
throws OgnlException {
if (GlobalOptions.strict) {
throw new IllegalAccessError(GlobalOptions.STRICT_MESSAGE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.concurrent.ConcurrentHashMap;

import ognl.ClassResolver;
import ognl.OgnlContext;

/**
*
Expand All @@ -21,8 +22,7 @@ public ClassLoaderClassResolver(ClassLoader classLoader) {
}

@Override
public Class<?> classForName(String className, @SuppressWarnings("rawtypes") Map context)
throws ClassNotFoundException {
public <T> Class<T> classForName(String className, OgnlContext context) throws ClassNotFoundException {
Class<?> result = null;

if ((result = classes.get(className)) == null) {
Expand All @@ -39,7 +39,6 @@ public Class<?> classForName(String className, @SuppressWarnings("rawtypes") Map
}
classes.put(className, result);
}
return result;
return (Class<T>) result;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.taobao.arthas.core.command.express;

import ognl.ClassResolver;
import ognl.OgnlContext;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -19,7 +20,8 @@ private CustomClassResolver() {

}

public Class<?> classForName(String className, Map context) throws ClassNotFoundException {
@Override
public <T> Class<T> classForName(String className, OgnlContext ognlContext) throws ClassNotFoundException {
Class<?> result = null;

if ((result = classes.get(className)) == null) {
Expand All @@ -38,6 +40,6 @@ public Class<?> classForName(String className, Map context) throws ClassNotFound
}
classes.put(className, result);
}
return result;
return (Class<T>) result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.taobao.arthas.core.command.express;

import ognl.MemberAccess;
import ognl.OgnlContext;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Member;
import java.lang.reflect.Modifier;
import java.util.Map;

/**
* @author jiaming.lf
* @date 2023/8/28 10:02
*/
public class DefaultMemberAccess implements MemberAccess {
public boolean allowPrivateAccess;
public boolean allowProtectedAccess;
public boolean allowPackageProtectedAccess;

public DefaultMemberAccess(boolean allowAllAccess) {
this(allowAllAccess, allowAllAccess, allowAllAccess);
}

public DefaultMemberAccess(boolean allowPrivateAccess, boolean allowProtectedAccess, boolean allowPackageProtectedAccess) {
this.allowPrivateAccess = false;
this.allowProtectedAccess = false;
this.allowPackageProtectedAccess = false;
this.allowPrivateAccess = allowPrivateAccess;
this.allowProtectedAccess = allowProtectedAccess;
this.allowPackageProtectedAccess = allowPackageProtectedAccess;
}

public boolean getAllowPrivateAccess() {
return this.allowPrivateAccess;
}

public void setAllowPrivateAccess(boolean value) {
this.allowPrivateAccess = value;
}

public boolean getAllowProtectedAccess() {
return this.allowProtectedAccess;
}

public void setAllowProtectedAccess(boolean value) {
this.allowProtectedAccess = value;
}

public boolean getAllowPackageProtectedAccess() {
return this.allowPackageProtectedAccess;
}

public void setAllowPackageProtectedAccess(boolean value) {
this.allowPackageProtectedAccess = value;
}
@Override
public Object setup(OgnlContext context, Object target, Member member, String propertyName) {
Object result = null;
if (this.isAccessible(context, target, member, propertyName)) {
AccessibleObject accessible = (AccessibleObject) member;
if (!accessible.isAccessible()) {
result = Boolean.FALSE;
accessible.setAccessible(true);
}
}

return result;
}

@Override
public void restore(OgnlContext context, Object target, Member member, String propertyName, Object state) {
if (state != null) {
((AccessibleObject) member).setAccessible((Boolean) state);
}
}

@Override
public boolean isAccessible(OgnlContext context, Object target, Member member, String propertyName) {
int modifiers = member.getModifiers();
boolean result = Modifier.isPublic(modifiers);
if (!result) {
if (Modifier.isPrivate(modifiers)) {
result = this.getAllowPrivateAccess();
} else if (Modifier.isProtected(modifiers)) {
result = this.getAllowProtectedAccess();
} else {
result = this.getAllowPackageProtectedAccess();
}
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.alibaba.arthas.deps.org.slf4j.LoggerFactory;

import ognl.ClassResolver;
import ognl.DefaultMemberAccess;
import ognl.MemberAccess;
import ognl.Ognl;
import ognl.OgnlContext;
Expand All @@ -28,10 +27,7 @@ public OgnlExpress() {

public OgnlExpress(ClassResolver classResolver) {
OgnlRuntime.setPropertyAccessor(Object.class, OBJECT_PROPERTY_ACCESSOR);
context = new OgnlContext();
context.setClassResolver(classResolver);
// allow private field access
context.setMemberAccess(MEMBER_ACCESS);
context = new OgnlContext(MEMBER_ACCESS, classResolver, null, null);
}

@Override
Expand Down Expand Up @@ -65,9 +61,6 @@ public Express bind(String name, Object value) {
@Override
public Express reset() {
context.clear();
context.setClassResolver(CustomClassResolver.customClassResolver);
// allow private field access
context.setMemberAccess(MEMBER_ACCESS);
return this;
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>3.1.19</version>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down

0 comments on commit eb2d618

Please sign in to comment.