mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 15:24:43 +02:00
8277451: java.lang.reflect.Field::set on static field with invalid argument type should throw IAE
Reviewed-by: alanb
This commit is contained in:
parent
e8acac2aba
commit
032067264f
13 changed files with 664 additions and 54 deletions
|
@ -120,7 +120,7 @@ abstract class FieldAccessorImpl extends MagicAccessorImpl
|
|||
}
|
||||
}
|
||||
|
||||
private String getQualifiedFieldName() {
|
||||
protected String getQualifiedFieldName() {
|
||||
return field.getDeclaringClass().getName() + "." +field.getName();
|
||||
}
|
||||
|
||||
|
@ -223,16 +223,6 @@ abstract class FieldAccessorImpl extends MagicAccessorImpl
|
|||
return err;
|
||||
}
|
||||
|
||||
protected String getMessage(boolean getter, String attemptedType) {
|
||||
String err = "Can not " + (getter ? "get" : "set");
|
||||
if (Modifier.isStatic(field.getModifiers()))
|
||||
err += " static";
|
||||
if (Modifier.isFinal(field.getModifiers()))
|
||||
err += " final";
|
||||
err += " " + field.getType().getName() + " field " + getQualifiedFieldName() + " on " + attemptedType;
|
||||
return err;
|
||||
}
|
||||
|
||||
protected void throwSetIllegalArgumentException(String attemptedType,
|
||||
String attemptedValue) {
|
||||
throw new IllegalArgumentException(getSetMessage(attemptedType,attemptedValue));
|
||||
|
|
|
@ -56,7 +56,6 @@ class MethodHandleBooleanFieldAccessorImpl extends MethodHandleFieldAccessorImpl
|
|||
}
|
||||
|
||||
public boolean getBoolean(Object obj) throws IllegalArgumentException {
|
||||
ensureObj(obj);
|
||||
try {
|
||||
if (isStatic()) {
|
||||
return (boolean) getter.invokeExact();
|
||||
|
@ -66,7 +65,7 @@ class MethodHandleBooleanFieldAccessorImpl extends MethodHandleFieldAccessorImpl
|
|||
} catch (IllegalArgumentException|NullPointerException e) {
|
||||
throw e;
|
||||
} catch (ClassCastException e) {
|
||||
throw newGetIllegalArgumentException(obj.getClass());
|
||||
throw newGetIllegalArgumentException(obj);
|
||||
} catch (Throwable e) {
|
||||
throw new InternalError(e);
|
||||
}
|
||||
|
@ -122,8 +121,8 @@ class MethodHandleBooleanFieldAccessorImpl extends MethodHandleFieldAccessorImpl
|
|||
public void setBoolean(Object obj, boolean z)
|
||||
throws IllegalArgumentException, IllegalAccessException
|
||||
{
|
||||
ensureObj(obj);
|
||||
if (isReadOnly()) {
|
||||
ensureObj(obj); // throw NPE if obj is null on instance field
|
||||
throwFinalFieldIllegalAccessException(z);
|
||||
}
|
||||
try {
|
||||
|
@ -135,7 +134,8 @@ class MethodHandleBooleanFieldAccessorImpl extends MethodHandleFieldAccessorImpl
|
|||
} catch (IllegalArgumentException|NullPointerException e) {
|
||||
throw e;
|
||||
} catch (ClassCastException e) {
|
||||
throw newSetIllegalArgumentException(obj.getClass());
|
||||
// receiver is of invalid type
|
||||
throw newSetIllegalArgumentException(obj);
|
||||
} catch (Throwable e) {
|
||||
throw new InternalError(e);
|
||||
}
|
||||
|
|
|
@ -69,7 +69,7 @@ class MethodHandleByteFieldAccessorImpl extends MethodHandleFieldAccessorImpl {
|
|||
} catch (IllegalArgumentException|NullPointerException e) {
|
||||
throw e;
|
||||
} catch (ClassCastException e) {
|
||||
throw newGetIllegalArgumentException(obj.getClass());
|
||||
throw newGetIllegalArgumentException(obj);
|
||||
} catch (Throwable e) {
|
||||
throw new InternalError(e);
|
||||
}
|
||||
|
@ -127,8 +127,8 @@ class MethodHandleByteFieldAccessorImpl extends MethodHandleFieldAccessorImpl {
|
|||
public void setByte(Object obj, byte b)
|
||||
throws IllegalArgumentException, IllegalAccessException
|
||||
{
|
||||
ensureObj(obj);
|
||||
if (isReadOnly()) {
|
||||
ensureObj(obj); // throw NPE if obj is null on instance field
|
||||
throwFinalFieldIllegalAccessException(b);
|
||||
}
|
||||
try {
|
||||
|
@ -140,7 +140,8 @@ class MethodHandleByteFieldAccessorImpl extends MethodHandleFieldAccessorImpl {
|
|||
} catch (IllegalArgumentException|NullPointerException e) {
|
||||
throw e;
|
||||
} catch (ClassCastException e) {
|
||||
throw newSetIllegalArgumentException(obj.getClass());
|
||||
// receiver is of invalid type
|
||||
throw newSetIllegalArgumentException(obj);
|
||||
} catch (Throwable e) {
|
||||
throw new InternalError(e);
|
||||
}
|
||||
|
|
|
@ -73,7 +73,7 @@ class MethodHandleCharacterFieldAccessorImpl extends MethodHandleFieldAccessorIm
|
|||
} catch (IllegalArgumentException|NullPointerException e) {
|
||||
throw e;
|
||||
} catch (ClassCastException e) {
|
||||
throw newGetIllegalArgumentException(obj.getClass());
|
||||
throw newGetIllegalArgumentException(obj);
|
||||
} catch (Throwable e) {
|
||||
throw new InternalError(e);
|
||||
}
|
||||
|
@ -102,8 +102,8 @@ class MethodHandleCharacterFieldAccessorImpl extends MethodHandleFieldAccessorIm
|
|||
public void set(Object obj, Object value)
|
||||
throws IllegalArgumentException, IllegalAccessException
|
||||
{
|
||||
ensureObj(obj);
|
||||
if (isReadOnly()) {
|
||||
ensureObj(obj); // throw NPE if obj is null on instance field
|
||||
throwFinalFieldIllegalAccessException(value);
|
||||
}
|
||||
|
||||
|
@ -146,7 +146,8 @@ class MethodHandleCharacterFieldAccessorImpl extends MethodHandleFieldAccessorIm
|
|||
} catch (IllegalArgumentException|NullPointerException e) {
|
||||
throw e;
|
||||
} catch (ClassCastException e) {
|
||||
throw newSetIllegalArgumentException(obj.getClass());
|
||||
// receiver is of invalid type
|
||||
throw newSetIllegalArgumentException(obj);
|
||||
} catch (Throwable e) {
|
||||
throw new InternalError(e);
|
||||
}
|
||||
|
|
|
@ -93,7 +93,7 @@ class MethodHandleDoubleFieldAccessorImpl extends MethodHandleFieldAccessorImpl
|
|||
} catch (IllegalArgumentException|NullPointerException e) {
|
||||
throw e;
|
||||
} catch (ClassCastException e) {
|
||||
throw newGetIllegalArgumentException(obj.getClass());
|
||||
throw newGetIllegalArgumentException(obj);
|
||||
} catch (Throwable e) {
|
||||
throw new InternalError(e);
|
||||
}
|
||||
|
@ -102,8 +102,8 @@ class MethodHandleDoubleFieldAccessorImpl extends MethodHandleFieldAccessorImpl
|
|||
public void set(Object obj, Object value)
|
||||
throws IllegalArgumentException, IllegalAccessException
|
||||
{
|
||||
ensureObj(obj);
|
||||
if (isReadOnly()) {
|
||||
ensureObj(obj); // throw NPE if obj is null on instance field
|
||||
throwFinalFieldIllegalAccessException(value);
|
||||
}
|
||||
|
||||
|
@ -195,7 +195,8 @@ class MethodHandleDoubleFieldAccessorImpl extends MethodHandleFieldAccessorImpl
|
|||
} catch (IllegalArgumentException|NullPointerException e) {
|
||||
throw e;
|
||||
} catch (ClassCastException e) {
|
||||
throw newSetIllegalArgumentException(obj.getClass());
|
||||
// receiver is of invalid type
|
||||
throw newSetIllegalArgumentException(obj);
|
||||
} catch (Throwable e) {
|
||||
throw new InternalError(e);
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@ package jdk.internal.reflect;
|
|||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
abstract class MethodHandleFieldAccessorImpl extends FieldAccessorImpl {
|
||||
private static final int IS_READ_ONLY_BIT = 0x0001;
|
||||
|
@ -64,21 +65,32 @@ abstract class MethodHandleFieldAccessorImpl extends FieldAccessorImpl {
|
|||
}
|
||||
}
|
||||
|
||||
private String getMessage(boolean getter, Class<?> type) {
|
||||
String err = "Can not " + (getter ? "get" : "set");
|
||||
if (Modifier.isStatic(field.getModifiers()))
|
||||
err += " static";
|
||||
if (Modifier.isFinal(field.getModifiers()))
|
||||
err += " final";
|
||||
err += " " + field.getType().getName() + " field " + getQualifiedFieldName();
|
||||
if (type != null) {
|
||||
err += " on " + type.getName();
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* IllegalArgumentException because Field::get on the specified object, which
|
||||
* is not an instance of the class or interface declaring the underlying method
|
||||
*/
|
||||
protected IllegalArgumentException newGetIllegalArgumentException(Class<?> type) {
|
||||
return new IllegalArgumentException(getMessage(true, type.getName()));
|
||||
protected IllegalArgumentException newGetIllegalArgumentException(Object o) {
|
||||
return new IllegalArgumentException(getMessage(true, o != null ? o.getClass() : null));
|
||||
}
|
||||
|
||||
/**
|
||||
* IllegalArgumentException because Field::set on the specified object, which
|
||||
* is not an instance of the class or interface declaring the underlying method
|
||||
*/
|
||||
protected IllegalArgumentException newSetIllegalArgumentException(Class<?> type) {
|
||||
return new IllegalArgumentException(getMessage(false, type.getName()));
|
||||
protected IllegalArgumentException newSetIllegalArgumentException(Object o) {
|
||||
return new IllegalArgumentException(getMessage(false, o != null ? o.getClass() : null));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -89,7 +89,7 @@ class MethodHandleFloatFieldAccessorImpl extends MethodHandleFieldAccessorImpl {
|
|||
} catch (IllegalArgumentException|NullPointerException e) {
|
||||
throw e;
|
||||
} catch (ClassCastException e) {
|
||||
throw newGetIllegalArgumentException(obj.getClass());
|
||||
throw newGetIllegalArgumentException(obj);
|
||||
} catch (Throwable e) {
|
||||
throw new InternalError(e);
|
||||
}
|
||||
|
@ -102,8 +102,8 @@ class MethodHandleFloatFieldAccessorImpl extends MethodHandleFieldAccessorImpl {
|
|||
public void set(Object obj, Object value)
|
||||
throws IllegalArgumentException, IllegalAccessException
|
||||
{
|
||||
ensureObj(obj);
|
||||
if (isReadOnly()) {
|
||||
ensureObj(obj); // throw NPE if obj is null on instance field
|
||||
throwFinalFieldIllegalAccessException(value);
|
||||
}
|
||||
|
||||
|
@ -186,7 +186,8 @@ class MethodHandleFloatFieldAccessorImpl extends MethodHandleFieldAccessorImpl {
|
|||
} catch (IllegalArgumentException|NullPointerException e) {
|
||||
throw e;
|
||||
} catch (ClassCastException e) {
|
||||
throw newSetIllegalArgumentException(obj.getClass());
|
||||
// receiver is of invalid type
|
||||
throw newSetIllegalArgumentException(obj);
|
||||
} catch (Throwable e) {
|
||||
throw new InternalError(e);
|
||||
}
|
||||
|
|
|
@ -81,7 +81,7 @@ class MethodHandleIntegerFieldAccessorImpl extends MethodHandleFieldAccessorImpl
|
|||
} catch (IllegalArgumentException|NullPointerException e) {
|
||||
throw e;
|
||||
} catch (ClassCastException e) {
|
||||
throw newGetIllegalArgumentException(obj.getClass());
|
||||
throw newGetIllegalArgumentException(obj);
|
||||
} catch (Throwable e) {
|
||||
throw new InternalError(e);
|
||||
}
|
||||
|
@ -102,8 +102,8 @@ class MethodHandleIntegerFieldAccessorImpl extends MethodHandleFieldAccessorImpl
|
|||
public void set(Object obj, Object value)
|
||||
throws IllegalArgumentException, IllegalAccessException
|
||||
{
|
||||
ensureObj(obj);
|
||||
if (isReadOnly()) {
|
||||
ensureObj(obj); // throw NPE if obj is null on instance field
|
||||
throwFinalFieldIllegalAccessException(value);
|
||||
}
|
||||
|
||||
|
@ -168,7 +168,8 @@ class MethodHandleIntegerFieldAccessorImpl extends MethodHandleFieldAccessorImpl
|
|||
} catch (IllegalArgumentException|NullPointerException e) {
|
||||
throw e;
|
||||
} catch (ClassCastException e) {
|
||||
throw newSetIllegalArgumentException(obj.getClass());
|
||||
// receiver is of invalid type
|
||||
throw newSetIllegalArgumentException(obj);
|
||||
} catch (Throwable e) {
|
||||
throw new InternalError(e);
|
||||
}
|
||||
|
|
|
@ -85,7 +85,7 @@ class MethodHandleLongFieldAccessorImpl extends MethodHandleFieldAccessorImpl {
|
|||
} catch (IllegalArgumentException|NullPointerException e) {
|
||||
throw e;
|
||||
} catch (ClassCastException e) {
|
||||
throw newGetIllegalArgumentException(obj.getClass());
|
||||
throw newGetIllegalArgumentException(obj);
|
||||
} catch (Throwable e) {
|
||||
throw new InternalError(e);
|
||||
}
|
||||
|
@ -102,8 +102,8 @@ class MethodHandleLongFieldAccessorImpl extends MethodHandleFieldAccessorImpl {
|
|||
public void set(Object obj, Object value)
|
||||
throws IllegalArgumentException, IllegalAccessException
|
||||
{
|
||||
ensureObj(obj);
|
||||
if (isReadOnly()) {
|
||||
ensureObj(obj); // throw NPE if obj is null on instance field
|
||||
throwFinalFieldIllegalAccessException(value);
|
||||
}
|
||||
|
||||
|
@ -177,7 +177,8 @@ class MethodHandleLongFieldAccessorImpl extends MethodHandleFieldAccessorImpl {
|
|||
} catch (IllegalArgumentException|NullPointerException e) {
|
||||
throw e;
|
||||
} catch (ClassCastException e) {
|
||||
throw newSetIllegalArgumentException(obj.getClass());
|
||||
// receiver is of invalid type
|
||||
throw newSetIllegalArgumentException(obj);
|
||||
} catch (Throwable e) {
|
||||
throw new InternalError(e);
|
||||
}
|
||||
|
|
|
@ -58,7 +58,7 @@ class MethodHandleObjectFieldAccessorImpl extends MethodHandleFieldAccessorImpl
|
|||
} catch (IllegalArgumentException|NullPointerException e) {
|
||||
throw e;
|
||||
} catch (ClassCastException e) {
|
||||
throw newGetIllegalArgumentException(obj.getClass());
|
||||
throw newGetIllegalArgumentException(obj);
|
||||
} catch (Throwable e) {
|
||||
throw new InternalError(e);
|
||||
}
|
||||
|
@ -98,8 +98,8 @@ class MethodHandleObjectFieldAccessorImpl extends MethodHandleFieldAccessorImpl
|
|||
|
||||
@Override
|
||||
public void set(Object obj, Object value) throws IllegalAccessException {
|
||||
ensureObj(obj);
|
||||
if (isReadOnly()) {
|
||||
ensureObj(obj); // throw NPE if obj is null on instance field
|
||||
throwFinalFieldIllegalAccessException(value);
|
||||
}
|
||||
try {
|
||||
|
@ -111,7 +111,8 @@ class MethodHandleObjectFieldAccessorImpl extends MethodHandleFieldAccessorImpl
|
|||
} catch (IllegalArgumentException|NullPointerException e) {
|
||||
throw e;
|
||||
} catch (ClassCastException e) {
|
||||
throw newSetIllegalArgumentException(obj.getClass());
|
||||
// already ensure the receiver type. So this CCE is due to the value.
|
||||
throwSetIllegalArgumentException(value);
|
||||
} catch (Throwable e) {
|
||||
throw new InternalError(e);
|
||||
}
|
||||
|
|
|
@ -77,7 +77,7 @@ class MethodHandleShortFieldAccessorImpl extends MethodHandleFieldAccessorImpl {
|
|||
} catch (IllegalArgumentException|NullPointerException e) {
|
||||
throw e;
|
||||
} catch (ClassCastException e) {
|
||||
throw newGetIllegalArgumentException(obj.getClass());
|
||||
throw newGetIllegalArgumentException(obj);
|
||||
} catch (Throwable e) {
|
||||
throw new InternalError(e);
|
||||
}
|
||||
|
@ -102,8 +102,8 @@ class MethodHandleShortFieldAccessorImpl extends MethodHandleFieldAccessorImpl {
|
|||
public void set(Object obj, Object value)
|
||||
throws IllegalArgumentException, IllegalAccessException
|
||||
{
|
||||
ensureObj(obj);
|
||||
if (isReadOnly()) {
|
||||
ensureObj(obj); // throw NPE if obj is null on instance field
|
||||
throwFinalFieldIllegalAccessException(value);
|
||||
}
|
||||
|
||||
|
@ -156,7 +156,8 @@ class MethodHandleShortFieldAccessorImpl extends MethodHandleFieldAccessorImpl {
|
|||
} catch (IllegalArgumentException|NullPointerException e) {
|
||||
throw e;
|
||||
} catch (ClassCastException e) {
|
||||
throw newSetIllegalArgumentException(obj.getClass());
|
||||
// receiver is of invalid type
|
||||
throw newSetIllegalArgumentException(obj);
|
||||
} catch (Throwable e) {
|
||||
throw new InternalError(e);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue