134
135 public void testToStringEmpty() throws Throwable {
136 CallSite cs = (CallSite)ObjectMethods.bootstrap(LOOKUP, "toString", Empty.TO_STRING_DESC, Empty.class, Empty.NAME_LIST, Empty.ACCESSORS);
137 MethodHandle handle = cs.dynamicInvoker();
138 assertEquals((String)handle.invokeExact(new Empty()), "Empty[]");
139 }
140
141 Class<NullPointerException> NPE = NullPointerException.class;
142 Class<IllegalArgumentException> IAE = IllegalArgumentException.class;
143
144 public void exceptions() {
145 assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "badName", C.EQUALS_DESC, C.class, C.NAME_LIST, C.ACCESSORS));
146 assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "toString", C.TO_STRING_DESC, C.class, "x;y;z", C.ACCESSORS));
147 assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "toString", C.TO_STRING_DESC, C.class, "x;y", new MethodHandle[]{}));
148 assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "toString", C.TO_STRING_DESC, this.getClass(), "x;y", C.ACCESSORS));
149
150 assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "toString", C.EQUALS_DESC, C.class, "x;y", C.ACCESSORS));
151 assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "hashCode", C.TO_STRING_DESC, C.class, "x;y", C.ACCESSORS));
152 assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "equals", C.HASHCODE_DESC, C.class, "x;y", C.ACCESSORS));
153
154 record NamePlusType(String mn, MethodType mt) {}
155 List<NamePlusType> namePlusTypeList = List.of(
156 new NamePlusType("toString", C.TO_STRING_DESC),
157 new NamePlusType("equals", C.EQUALS_DESC),
158 new NamePlusType("hashCode", C.HASHCODE_DESC)
159 );
160
161 for (NamePlusType npt : namePlusTypeList) {
162 assertThrows(NPE, () -> ObjectMethods.bootstrap(LOOKUP, npt.mn(), npt.mt(), C.class, "x;y", null));
163 assertThrows(NPE, () -> ObjectMethods.bootstrap(LOOKUP, npt.mn(), npt.mt(), C.class, "x;y", new MethodHandle[]{null}));
164 assertThrows(NPE, () -> ObjectMethods.bootstrap(LOOKUP, npt.mn(), npt.mt(), C.class, null, C.ACCESSORS));
165 assertThrows(NPE, () -> ObjectMethods.bootstrap(LOOKUP, npt.mn(), npt.mt(), null, "x;y", C.ACCESSORS));
166 assertThrows(NPE, () -> ObjectMethods.bootstrap(LOOKUP, npt.mn(), null, C.class, "x;y", C.ACCESSORS));
167 assertThrows(NPE, () -> ObjectMethods.bootstrap(LOOKUP, null, npt.mt(), C.class, "x;y", C.ACCESSORS));
168 assertThrows(NPE, () -> ObjectMethods.bootstrap(null, npt.mn(), npt.mt(), C.class, "x;y", C.ACCESSORS));
169 }
170 }
171
172 // Based on the ObjectMethods internal implementation
173 private static int hashCombiner(int x, int y) {
174 return x*31 + y;
175 }
176 }
|
134
135 public void testToStringEmpty() throws Throwable {
136 CallSite cs = (CallSite)ObjectMethods.bootstrap(LOOKUP, "toString", Empty.TO_STRING_DESC, Empty.class, Empty.NAME_LIST, Empty.ACCESSORS);
137 MethodHandle handle = cs.dynamicInvoker();
138 assertEquals((String)handle.invokeExact(new Empty()), "Empty[]");
139 }
140
141 Class<NullPointerException> NPE = NullPointerException.class;
142 Class<IllegalArgumentException> IAE = IllegalArgumentException.class;
143
144 public void exceptions() {
145 assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "badName", C.EQUALS_DESC, C.class, C.NAME_LIST, C.ACCESSORS));
146 assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "toString", C.TO_STRING_DESC, C.class, "x;y;z", C.ACCESSORS));
147 assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "toString", C.TO_STRING_DESC, C.class, "x;y", new MethodHandle[]{}));
148 assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "toString", C.TO_STRING_DESC, this.getClass(), "x;y", C.ACCESSORS));
149
150 assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "toString", C.EQUALS_DESC, C.class, "x;y", C.ACCESSORS));
151 assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "hashCode", C.TO_STRING_DESC, C.class, "x;y", C.ACCESSORS));
152 assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "equals", C.HASHCODE_DESC, C.class, "x;y", C.ACCESSORS));
153
154 assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "toString", methodType(String.class, this.getClass()), C.class, "x;y", C.ACCESSORS));
155 assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "toString", C.TO_STRING_DESC, C.class, "x;y",
156 new MethodHandle[]{
157 MethodHandles.lookup().findGetter(C.class, "x", int.class),
158 MethodHandles.lookup().findGetter(this.getClass(), "y", int.class),
159 }));
160
161 record NamePlusType(String mn, MethodType mt) {}
162 List<NamePlusType> namePlusTypeList = List.of(
163 new NamePlusType("toString", C.TO_STRING_DESC),
164 new NamePlusType("equals", C.EQUALS_DESC),
165 new NamePlusType("hashCode", C.HASHCODE_DESC)
166 );
167
168 for (NamePlusType npt : namePlusTypeList) {
169 assertThrows(NPE, () -> ObjectMethods.bootstrap(LOOKUP, npt.mn(), npt.mt(), C.class, "x;y", null));
170 assertThrows(NPE, () -> ObjectMethods.bootstrap(LOOKUP, npt.mn(), npt.mt(), C.class, "x;y", new MethodHandle[]{null}));
171 assertThrows(NPE, () -> ObjectMethods.bootstrap(LOOKUP, npt.mn(), npt.mt(), C.class, null, C.ACCESSORS));
172 assertThrows(NPE, () -> ObjectMethods.bootstrap(LOOKUP, npt.mn(), npt.mt(), null, "x;y", C.ACCESSORS));
173 assertThrows(NPE, () -> ObjectMethods.bootstrap(LOOKUP, npt.mn(), null, C.class, "x;y", C.ACCESSORS));
174 assertThrows(NPE, () -> ObjectMethods.bootstrap(LOOKUP, null, npt.mt(), C.class, "x;y", C.ACCESSORS));
175 assertThrows(NPE, () -> ObjectMethods.bootstrap(null, npt.mn(), npt.mt(), C.class, "x;y", C.ACCESSORS));
176 }
177 }
178
179 // same field name and type as C::y
180 private int y;
181
182 // Based on the ObjectMethods internal implementation
183 private static int hashCombiner(int x, int y) {
184 return x*31 + y;
185 }
186 }
|