1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 25 /* 26 * @test 27 * @summary test value bootstrap methods 28 * @run main/othervm -Dvalue.bsm.salt=1 ValueBootstrapMethodsTest 29 */ 30 31 import java.util.List; 32 import java.util.Objects; 33 34 public class ValueBootstrapMethodsTest { 35 36 public static final primitive class Value { 37 private final int i; 38 private final double d; 39 private final String s; 40 private final List<String> l; 41 Value(int i, double d, String s, String... items) { 42 this.i = i; 43 this.d = d; 44 this.s = s; 45 this.l = List.of(items); 46 } 47 48 private List<Object> values() { 49 return List.of(Value.class.asValueType(), i, d, s, l); 50 } 51 52 public int localHashCode() { 53 return values().hashCode(); 54 } 55 56 public String localToString() { 57 System.out.println(l); 58 return String.format("%s@%s", Value.class.asValueType().getName(), Integer.toHexString(localHashCode())); 59 } 60 61 @Override 62 public boolean equals(Object obj) { 63 if (obj instanceof Value) { 64 Value v = (Value)obj; 65 return this.i == v.i && this.d == v.d && 66 Objects.equals(this.s, v.s) && 67 Objects.equals(this.l, this.l); 68 } 69 return false; 70 } 71 } 72 73 private static void assertEquals(Object o1, Object expected) { 74 if (!Objects.equals(o1, expected)) { 75 throw new RuntimeException(o1 + " expected: " + expected); 76 } 77 } 78 79 public static void main(String... args) throws Throwable { 80 81 Value value = new Value(10, 5.03, "foo", "bar", "goo"); 82 83 assertEquals(value.localHashCode(), value.hashCode()); 84 assertEquals(value.localToString(), value.toString()); 85 86 // verify ifacmp and the overridden equals method 87 88 // same instance 89 if (value != value || !value.equals(value)) { 90 throw new RuntimeException("expected == and equals"); 91 } 92 93 // value and v2 are of different values 94 Value v2 = new Value(20, 5.03, "foo", "bar", "goo"); 95 if (value == v2 || value.equals(v2)) { 96 throw new RuntimeException("expected != and unequals"); 97 } 98 99 // v2 and v3 are of different values but Value::equals 100 // returns true because v2::l and v3::l field contain the same elements 101 Value v3 = new Value(20, 5.03, "foo", "bar", "goo"); 102 if (v2 == v3 || !v2.equals(v3)) { 103 throw new RuntimeException("expected != and equals"); 104 } 105 } 106 }