< prev index next >

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

Print this page

135 
136     public void testToStringEmpty() throws Throwable {
137         CallSite cs = (CallSite)ObjectMethods.bootstrap(LOOKUP, "toString", Empty.TO_STRING_DESC, Empty.class, Empty.NAME_LIST, Empty.ACCESSORS);
138         MethodHandle handle = cs.dynamicInvoker();
139         assertEquals((String)handle.invokeExact(new Empty()),    "Empty[]");
140     }
141 
142     Class<NullPointerException> NPE = NullPointerException.class;
143     Class<IllegalArgumentException> IAE = IllegalArgumentException.class;
144 
145     public void exceptions()  {
146         assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "badName",  C.EQUALS_DESC,    C.class,         C.NAME_LIST, C.ACCESSORS));
147         assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "toString", C.TO_STRING_DESC, C.class,         "x;y;z",     C.ACCESSORS));
148         assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "toString", C.TO_STRING_DESC, C.class,         "x;y",       new MethodHandle[]{}));
149         assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "toString", C.TO_STRING_DESC, this.getClass(), "x;y",       C.ACCESSORS));
150 
151         assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "toString", C.EQUALS_DESC,    C.class, "x;y", C.ACCESSORS));
152         assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "hashCode", C.TO_STRING_DESC, C.class, "x;y", C.ACCESSORS));
153         assertThrows(IAE, () -> ObjectMethods.bootstrap(LOOKUP, "equals",   C.HASHCODE_DESC,  C.class, "x;y", C.ACCESSORS));
154 







155         record NamePlusType(String mn, MethodType mt) {}
156         List<NamePlusType> namePlusTypeList = List.of(
157                 new NamePlusType("toString", C.TO_STRING_DESC),
158                 new NamePlusType("equals", C.EQUALS_DESC),
159                 new NamePlusType("hashCode", C.HASHCODE_DESC)
160         );
161 
162         for (NamePlusType npt : namePlusTypeList) {
163             assertThrows(NPE, () -> ObjectMethods.bootstrap(LOOKUP, npt.mn(), npt.mt(), C.class, "x;y", null));
164             assertThrows(NPE, () -> ObjectMethods.bootstrap(LOOKUP, npt.mn(), npt.mt(), C.class, "x;y", new MethodHandle[]{null}));
165             assertThrows(NPE, () -> ObjectMethods.bootstrap(LOOKUP, npt.mn(), npt.mt(), C.class, null,  C.ACCESSORS));
166             assertThrows(NPE, () -> ObjectMethods.bootstrap(LOOKUP, npt.mn(), npt.mt(), null,    "x;y", C.ACCESSORS));
167             assertThrows(NPE, () -> ObjectMethods.bootstrap(LOOKUP, npt.mn(), null,     C.class, "x;y", C.ACCESSORS));
168             assertThrows(NPE, () -> ObjectMethods.bootstrap(LOOKUP, null,     npt.mt(), C.class, "x;y", C.ACCESSORS));
169             assertThrows(NPE, () -> ObjectMethods.bootstrap(null, npt.mn(),     npt.mt(), C.class, "x;y", C.ACCESSORS));
170         }
171     }
172 



173     // Based on the ObjectMethods internal implementation
174     private static int hashCombiner(int x, int y) {
175         return x*31 + y;
176     }
177 }

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