1 /* 2 * Copyright (c) 2001, 2019, 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 /* @test 25 * @bug 4431318 26 * @summary Verify that when serialVersionUID is declared with a type other 27 * than long, values that can be promoted to long will be used, and 28 * those that can't be will be ignored (but will not result in 29 * unchecked exceptions). 30 */ 31 32 import java.io.*; 33 34 class Z implements Serializable { 35 @SuppressWarnings("serial") /* Incorrect declarations are being tested */ 36 private static final boolean serialVersionUID = false; 37 } 38 39 class B implements Serializable { 40 @SuppressWarnings("serial") /* Incorrect declarations are being tested */ 41 private static final byte serialVersionUID = 5; 42 } 43 44 class C implements Serializable { 45 @SuppressWarnings("serial") /* Incorrect declarations are being tested */ 46 private static final char serialVersionUID = 5; 47 } 48 49 class S implements Serializable { 50 @SuppressWarnings("serial") /* Incorrect declarations are being tested */ 51 private static final short serialVersionUID = 5; 52 } 53 54 class I implements Serializable { 55 @SuppressWarnings("serial") /* Incorrect declarations are being tested */ 56 private static final int serialVersionUID = 5; 57 } 58 59 class F implements Serializable { 60 @SuppressWarnings("serial") /* Incorrect declarations are being tested */ 61 private static final float serialVersionUID = 5.0F; 62 } 63 64 class D implements Serializable { 65 @SuppressWarnings("serial") /* Incorrect declarations are being tested */ 66 private static final double serialVersionUID = 5.0; 67 } 68 69 class L implements Serializable { 70 @SuppressWarnings("serial") /* Incorrect declarations are being tested */ 71 private static final Object serialVersionUID = "5"; 72 } 73 74 75 public class BadSerialVersionUID { main(String[] args)76 public static void main(String[] args) throws Exception { 77 Class<?>[] ignore = { Z.class, F.class, D.class, L.class }; 78 Class<?>[] convert = { B.class, C.class, S.class, I.class }; 79 80 for (int i = 0; i < ignore.length; i++) { 81 ObjectStreamClass.lookup(ignore[i]).getSerialVersionUID(); 82 } 83 for (int i = 0; i < convert.length; i++) { 84 ObjectStreamClass desc = ObjectStreamClass.lookup(convert[i]); 85 if (desc.getSerialVersionUID() != 5L) { 86 throw new Error(); 87 } 88 } 89 } 90 } 91