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

Updates to OGNL 3.1.x OgnlRuntime #63

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
74 changes: 49 additions & 25 deletions src/java/ognl/OgnlRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -881,8 +881,11 @@ public static Object invokeMethod(Object target, Method method, Object[] argsArr
}

((AccessibleObject) method).setAccessible(true);
result = method.invoke(target, argsArray);
((AccessibleObject) method).setAccessible(false);
try {
result = method.invoke(target, argsArray);
} finally {
((AccessibleObject) method).setAccessible(false);
}
}
} else
{
Expand Down Expand Up @@ -1977,7 +1980,7 @@ public static Object getFieldValue(OgnlContext context, Object target, String pr
throws NoSuchFieldException
{
Object result = null;
Field f = getField((target == null) ? null : target.getClass(), propertyName);
final Field f = getField((target == null) ? null : target.getClass(), propertyName);

if (checkAccessAndExistence) {
if ((f == null) || !context.getMemberAccess().isAccessible(context, target, f, propertyName)) {
Expand All @@ -1989,14 +1992,17 @@ public static Object getFieldValue(OgnlContext context, Object target, String pr
throw new NoSuchFieldException(propertyName);
} else {
try {
Object state = null;

if (!Modifier.isStatic(f.getModifiers())) {
state = context.getMemberAccess().setup(context, target, f, propertyName);
result = f.get(target);
context.getMemberAccess().restore(context, target, f, propertyName, state);
} else
final Object state = context.getMemberAccess().setup(context, target, f, propertyName);
try {
result = f.get(target);
} finally {
context.getMemberAccess().restore(context, target, f, propertyName, state);
}
} else {
throw new NoSuchFieldException(propertyName);
}

} catch (IllegalAccessException ex) {
throw new NoSuchFieldException(propertyName);
Expand All @@ -2012,19 +2018,21 @@ public static boolean setFieldValue(OgnlContext context, Object target, String p
boolean result = false;

try {
Field f = getField((target == null) ? null : target.getClass(), propertyName);
Object state;

if ((f != null) && !Modifier.isStatic(f.getModifiers())) {
state = context.getMemberAccess().setup(context, target, f, propertyName);
try {
if (isTypeCompatible(value, f.getType())
|| ((value = getConvertedType(context, target, f, propertyName, value, f.getType())) != null)) {
f.set(target, value);
result = true;
final Field f = getField((target == null) ? null : target.getClass(), propertyName);

if (f != null) {
final int fModifiers = f.getModifiers();
if (!Modifier.isStatic(fModifiers) && !Modifier.isFinal(fModifiers)) {
final Object state = context.getMemberAccess().setup(context, target, f, propertyName);
try {
if (isTypeCompatible(value, f.getType())
|| ((value = getConvertedType(context, target, f, propertyName, value, f.getType())) != null)) {
f.set(target, value);
result = true;
}
} finally {
context.getMemberAccess().restore(context, target, f, propertyName, state);
}
} finally {
context.getMemberAccess().restore(context, target, f, propertyName, state);
}
}
} catch (IllegalAccessException ex) {
Expand All @@ -2050,15 +2058,30 @@ public static boolean hasField(OgnlContext context, Object target, Class inClass
return (f != null) && isFieldAccessible(context, target, f, propertyName);
}

/**
* Method name is getStaticField(), but actually behaves more like "getStaticFieldValue()".
* <p/>
* Typical usage: Returns the value (not the actual {@link Field}) for the given (static) fieldName.
* May return the {@link Enum} constant value for the given fieldName when className is an {@link Enum}.
* May return a {@link Class} instance when the given fieldName is "class".
* <p/>
* @param context The current ognl context
* @param className The name of the class which contains the field
* @param fieldName The name of the field whose value should be returned
*
* @return The value of the (static) fieldName
* @throws OgnlException
*/
public static Object getStaticField(OgnlContext context, String className, String fieldName)
throws OgnlException
{
Exception reason = null;
try {
Class c = classForName(context, className);
final Class c = classForName(context, className);

if (c == null)
if (c == null) {
throw new OgnlException("Unable to find class " + className + " when resolving field name of " + fieldName);
}

/*
* Check for virtual static field "class"; this cannot interfere with normal static
Expand All @@ -2076,16 +2099,17 @@ public static Object getStaticField(OgnlContext context, String className, Strin
}
}

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

Object result = null;
if (context.getMemberAccess().isAccessible(context, null, f, null)) {
Object state = context.getMemberAccess().setup(context, null, f, null);
final Object state = context.getMemberAccess().setup(context, null, f, null);
try {
result = f.get(null);
} finally {
Expand Down
Loading