< prev index next >

test/jdk/java/lang/runtime/ObjectMethodsTest.java

Print this page

141     public void testToStringEmpty() throws Throwable {
142         CallSite cs = (CallSite)ObjectMethods.bootstrap(LOOKUP, "toString", Empty.TO_STRING_DESC, Empty.class, Empty.NAME_LIST, Empty.ACCESSORS);
143         MethodHandle handle = cs.dynamicInvoker();
144         assertEquals("Empty[]", (String)handle.invokeExact(new Empty()));
145     }
146 
147     Class<NullPointerException> NPE = NullPointerException.class;
148     Class<IllegalArgumentException> IAE = IllegalArgumentException.class;
149 
150     @Test
151     public void exceptions()  {
152         assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "badName",  C.EQUALS_DESC,    C.class,         C.NAME_LIST, C.ACCESSORS));
153         assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "toString", C.TO_STRING_DESC, C.class,         "x;y;z",     C.ACCESSORS));
154         assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "toString", C.TO_STRING_DESC, C.class,         "x;y",       new MethodHandle[]{}));
155         assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "toString", C.TO_STRING_DESC, this.getClass(), "x;y",       C.ACCESSORS));
156 
157         assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "toString", C.EQUALS_DESC,    C.class, "x;y", C.ACCESSORS));
158         assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "hashCode", C.TO_STRING_DESC, C.class, "x;y", C.ACCESSORS));
159         assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "equals",   C.HASHCODE_DESC,  C.class, "x;y", C.ACCESSORS));
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     // Based on the ObjectMethods internal implementation
180     private static int hashCombiner(int x, int y) {
181         return x*31 + y;
182     }
183 }

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