Skip to content

Commit

Permalink
Merge pull request #63 from JCgH4164838Gh792C124B5/localOGNL_3_1_x_Br…
Browse files Browse the repository at this point in the history
…anch

Updates to OGNL 3.1.x OgnlRuntime
  • Loading branch information
lukaszlenart authored Nov 26, 2018
2 parents 5b5ff85 + bd86783 commit 01c4f1b
Show file tree
Hide file tree
Showing 4 changed files with 833 additions and 36 deletions.
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

0 comments on commit 01c4f1b

Please sign in to comment.