Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MemberAccess support private static field. #58 #59

Merged
merged 1 commit into from
Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/java/ognl/ASTStaticField.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ public boolean isNodeConstant(OgnlContext context)
result = true;
} else
{
Field f = c.getField(fieldName);
Field f = OgnlRuntime.getField(c, fieldName);
if (f == null) {
throw new NoSuchFieldException(fieldName);
}

if (!Modifier.isStatic(f.getModifiers()))
throw new OgnlException("Field " + fieldName + " of class " + className + " is not static");
Expand Down Expand Up @@ -200,7 +203,7 @@ public String toSetSourceString(OgnlContext context, Object target)
{
throw OgnlOps.castToRuntime(t);
}

return className + "." + fieldName;
}
}
29 changes: 22 additions & 7 deletions src/java/ognl/OgnlRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public class OgnlRuntime {

static final Map<Method, Boolean> _methodAccessCache = new ConcurrentHashMap<Method, Boolean>();
static final Map<Method, Boolean> _methodPermCache = new ConcurrentHashMap<Method, Boolean>();

static final ClassPropertyMethodCache cacheSetMethod = new ClassPropertyMethodCache();
static final ClassPropertyMethodCache cacheGetMethod = new ClassPropertyMethodCache();

Expand Down Expand Up @@ -1463,7 +1463,7 @@ private static MatchingMethod findBestMethod(List methods, Class typeClass, Stri
return mm;
}

public static Object callAppropriateMethod(OgnlContext context, Object source, Object target, String methodName,
public static Object callAppropriateMethod(OgnlContext context, Object source, Object target, String methodName,
String propertyName, List methods, Object[] args)
throws MethodFailedException
{
Expand Down Expand Up @@ -2076,11 +2076,26 @@ public static Object getStaticField(OgnlContext context, String className, Strin
}
}

Field f = c.getField(fieldName);
Field f = getField(c, fieldName);
if (f == null) {
throw new NoSuchFieldException(fieldName);
}
if (!Modifier.isStatic(f.getModifiers()))
throw new OgnlException("Field " + fieldName + " of class " + className + " is not static");

return f.get(null);
Object result = null;
if (context.getMemberAccess().isAccessible(context, null, f, null)) {
Object state = context.getMemberAccess().setup(context, null, f, null);
try {
result = f.get(null);
} finally {
context.getMemberAccess().restore(context, null, f, null, state);
}
} else {
throw new IllegalAccessException("Access to " + fieldName + " of class " + className + " is forbidden");
}

return result;
} catch (ClassNotFoundException e) {
reason = e;
} catch (NoSuchFieldException e) {
Expand Down Expand Up @@ -2209,7 +2224,7 @@ static boolean isMethodCallable(Method m)

return true;
}

/**
* cache get methods
*/
Expand Down Expand Up @@ -2267,9 +2282,9 @@ public static boolean hasGetMethod(OgnlContext context, Object target, Class tar
{
return isMethodAccessible(context, target, getGetMethod(context, targetClass, propertyName), propertyName);
}

/**
* cache set methods method
* cache set methods method
*/
public static Method getSetMethod(OgnlContext context, Class targetClass, String propertyName)
throws IntrospectionException, OgnlException
Expand Down