38 if (setter != null) {
39 setter = setter.asType(MethodType.methodType(void.class, Object.class));
40 }
41 } else {
42 getter = getter.asType(MethodType.methodType(Object.class, Object.class));
43 if (setter != null) {
44 setter = setter.asType(MethodType.methodType(void.class, Object.class, Object.class));
45 }
46 }
47 return new MethodHandleObjectFieldAccessorImpl(field, getter, setter, isReadOnly, isStatic);
48 }
49
50 MethodHandleObjectFieldAccessorImpl(Field field, MethodHandle getter, MethodHandle setter, boolean isReadOnly, boolean isStatic) {
51 super(field, getter, setter, isReadOnly, isStatic);
52 }
53
54 @Override
55 public Object get(Object obj) throws IllegalArgumentException {
56 try {
57 return isStatic() ? getter.invokeExact() : getter.invokeExact(obj);
58 } catch (IllegalArgumentException|NullPointerException e) {
59 throw e;
60 } catch (ClassCastException e) {
61 throw newGetIllegalArgumentException(obj);
62 } catch (Throwable e) {
63 throw new InternalError(e);
64 }
65 }
66
67 public boolean getBoolean(Object obj) throws IllegalArgumentException {
68 throw newGetBooleanIllegalArgumentException();
69 }
70
71 public byte getByte(Object obj) throws IllegalArgumentException {
72 throw newGetByteIllegalArgumentException();
73 }
74
75 public char getChar(Object obj) throws IllegalArgumentException {
76 throw newGetCharIllegalArgumentException();
77 }
78
91 public float getFloat(Object obj) throws IllegalArgumentException {
92 throw newGetFloatIllegalArgumentException();
93 }
94
95 public double getDouble(Object obj) throws IllegalArgumentException {
96 throw newGetDoubleIllegalArgumentException();
97 }
98
99 @Override
100 public void set(Object obj, Object value) throws IllegalAccessException {
101 ensureObj(obj);
102 if (isReadOnly()) {
103 throwFinalFieldIllegalAccessException(value);
104 }
105 try {
106 if (isStatic()) {
107 setter.invokeExact(value);
108 } else {
109 setter.invokeExact(obj, value);
110 }
111 } catch (IllegalArgumentException|NullPointerException e) {
112 throw e;
113 } catch (ClassCastException e) {
114 // already ensure the receiver type. So this CCE is due to the value.
115 throwSetIllegalArgumentException(value);
116 } catch (Throwable e) {
117 throw new InternalError(e);
118 }
119 }
120
121 public void setBoolean(Object obj, boolean z)
122 throws IllegalArgumentException, IllegalAccessException
123 {
124 throwSetIllegalArgumentException(z);
125 }
126
127 public void setByte(Object obj, byte b)
128 throws IllegalArgumentException, IllegalAccessException
129 {
130 throwSetIllegalArgumentException(b);
131 }
|
38 if (setter != null) {
39 setter = setter.asType(MethodType.methodType(void.class, Object.class));
40 }
41 } else {
42 getter = getter.asType(MethodType.methodType(Object.class, Object.class));
43 if (setter != null) {
44 setter = setter.asType(MethodType.methodType(void.class, Object.class, Object.class));
45 }
46 }
47 return new MethodHandleObjectFieldAccessorImpl(field, getter, setter, isReadOnly, isStatic);
48 }
49
50 MethodHandleObjectFieldAccessorImpl(Field field, MethodHandle getter, MethodHandle setter, boolean isReadOnly, boolean isStatic) {
51 super(field, getter, setter, isReadOnly, isStatic);
52 }
53
54 @Override
55 public Object get(Object obj) throws IllegalArgumentException {
56 try {
57 return isStatic() ? getter.invokeExact() : getter.invokeExact(obj);
58 } catch (IllegalArgumentException|IllegalStateException|NullPointerException e) {
59 throw e;
60 } catch (ClassCastException e) {
61 throw newGetIllegalArgumentException(obj);
62 } catch (Throwable e) {
63 throw new InternalError(e);
64 }
65 }
66
67 public boolean getBoolean(Object obj) throws IllegalArgumentException {
68 throw newGetBooleanIllegalArgumentException();
69 }
70
71 public byte getByte(Object obj) throws IllegalArgumentException {
72 throw newGetByteIllegalArgumentException();
73 }
74
75 public char getChar(Object obj) throws IllegalArgumentException {
76 throw newGetCharIllegalArgumentException();
77 }
78
91 public float getFloat(Object obj) throws IllegalArgumentException {
92 throw newGetFloatIllegalArgumentException();
93 }
94
95 public double getDouble(Object obj) throws IllegalArgumentException {
96 throw newGetDoubleIllegalArgumentException();
97 }
98
99 @Override
100 public void set(Object obj, Object value) throws IllegalAccessException {
101 ensureObj(obj);
102 if (isReadOnly()) {
103 throwFinalFieldIllegalAccessException(value);
104 }
105 try {
106 if (isStatic()) {
107 setter.invokeExact(value);
108 } else {
109 setter.invokeExact(obj, value);
110 }
111 } catch (IllegalArgumentException|IllegalStateException|NullPointerException e) {
112 throw e;
113 } catch (ClassCastException e) {
114 // already ensure the receiver type. So this CCE is due to the value.
115 throwSetIllegalArgumentException(value);
116 } catch (Throwable e) {
117 throw new InternalError(e);
118 }
119 }
120
121 public void setBoolean(Object obj, boolean z)
122 throws IllegalArgumentException, IllegalAccessException
123 {
124 throwSetIllegalArgumentException(z);
125 }
126
127 public void setByte(Object obj, byte b)
128 throws IllegalArgumentException, IllegalAccessException
129 {
130 throwSetIllegalArgumentException(b);
131 }
|