Path: blob/master/test/jdk/java/io/Serializable/badSerialVersionUID/BadSerialVersionUID.java
41152 views
/*1* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 443131825* @summary Verify that when serialVersionUID is declared with a type other26* than long, values that can be promoted to long will be used, and27* those that can't be will be ignored (but will not result in28* unchecked exceptions).29*/3031import java.io.*;3233class Z implements Serializable {34@SuppressWarnings("serial") /* Incorrect declarations are being tested */35private static final boolean serialVersionUID = false;36}3738class B implements Serializable {39@SuppressWarnings("serial") /* Incorrect declarations are being tested */40private static final byte serialVersionUID = 5;41}4243class C implements Serializable {44@SuppressWarnings("serial") /* Incorrect declarations are being tested */45private static final char serialVersionUID = 5;46}4748class S implements Serializable {49@SuppressWarnings("serial") /* Incorrect declarations are being tested */50private static final short serialVersionUID = 5;51}5253class I implements Serializable {54@SuppressWarnings("serial") /* Incorrect declarations are being tested */55private static final int serialVersionUID = 5;56}5758class F implements Serializable {59@SuppressWarnings("serial") /* Incorrect declarations are being tested */60private static final float serialVersionUID = 5.0F;61}6263class D implements Serializable {64@SuppressWarnings("serial") /* Incorrect declarations are being tested */65private static final double serialVersionUID = 5.0;66}6768class L implements Serializable {69@SuppressWarnings("serial") /* Incorrect declarations are being tested */70private static final Object serialVersionUID = "5";71}727374public class BadSerialVersionUID {75public static void main(String[] args) throws Exception {76Class<?>[] ignore = { Z.class, F.class, D.class, L.class };77Class<?>[] convert = { B.class, C.class, S.class, I.class };7879for (int i = 0; i < ignore.length; i++) {80ObjectStreamClass.lookup(ignore[i]).getSerialVersionUID();81}82for (int i = 0; i < convert.length; i++) {83ObjectStreamClass desc = ObjectStreamClass.lookup(convert[i]);84if (desc.getSerialVersionUID() != 5L) {85throw new Error();86}87}88}89}909192