test/jdk/java/io/Serializable/checkModifiers/CheckModifiers.java
changeset 58565 baa5969ecf34
parent 47216 71c04702a3d5
equal deleted inserted replaced
58564:218a1a642c6f 58565:baa5969ecf34
     1 /*
     1 /*
     2  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    34  *
    34  *
    35  */
    35  */
    36 
    36 
    37 import java.io.*;
    37 import java.io.*;
    38 class TestClass1 implements Serializable {
    38 class TestClass1 implements Serializable {
       
    39     private static final long serialVersionUID = 1L;
       
    40 
    39     // Missing the "final" modifier
    41     // Missing the "final" modifier
       
    42     @SuppressWarnings("serial") /* Incorrect declarations are being tested */
    40     private static ObjectStreamField[] serialPersistentFields = {
    43     private static ObjectStreamField[] serialPersistentFields = {
    41         new ObjectStreamField("field1", Integer.class),
    44         new ObjectStreamField("field1", Integer.class),
    42         new ObjectStreamField("field2", Double.TYPE),
    45         new ObjectStreamField("field2", Double.TYPE),
    43     };
    46     };
    44 
    47 
    56 
    59 
    57     private void readObject(ObjectInputStream ois)
    60     private void readObject(ObjectInputStream ois)
    58         throws IOException, ClassNotFoundException {
    61         throws IOException, ClassNotFoundException {
    59         ObjectInputStream.GetField pfields = ois.readFields();
    62         ObjectInputStream.GetField pfields = ois.readFields();
    60 
    63 
    61         field1 = (Integer) pfields.get("field1", new Integer(100));
    64         field1 = (Integer) pfields.get("field1", Integer.valueOf(100));
    62         field2 = pfields.get("field2", 99.99);
    65         field2 = pfields.get("field2", 99.99);
    63 
    66 
    64         /* These fields must be present in the stream */
    67         /* These fields must be present in the stream */
    65         try {
    68         try {
    66             field3 = pfields.get("field3", 99);
    69             field3 = pfields.get("field3", 99);
    77     }
    80     }
    78 };
    81 };
    79 
    82 
    80 
    83 
    81 class TestClass2 implements Serializable {
    84 class TestClass2 implements Serializable {
       
    85     private static final long serialVersionUID = 1L;
       
    86 
    82     // public instead of private
    87     // public instead of private
       
    88     @SuppressWarnings("serial") /* Incorrect declarations are being tested */
    83     public static final ObjectStreamField[] serialPersistentFields = {
    89     public static final ObjectStreamField[] serialPersistentFields = {
    84         new ObjectStreamField("field1", Integer.class),
    90         new ObjectStreamField("field1", Integer.class),
    85         new ObjectStreamField("field2", Double.TYPE),
    91         new ObjectStreamField("field2", Double.TYPE),
    86     };
    92     };
    87 
    93 
    99 
   105 
   100     private void readObject(ObjectInputStream ois)
   106     private void readObject(ObjectInputStream ois)
   101         throws IOException, ClassNotFoundException {
   107         throws IOException, ClassNotFoundException {
   102         ObjectInputStream.GetField pfields = ois.readFields();
   108         ObjectInputStream.GetField pfields = ois.readFields();
   103 
   109 
   104         field1 = (Integer) pfields.get("field1", new Integer(100));
   110         field1 = (Integer) pfields.get("field1", Integer.valueOf(100));
   105         field2 = pfields.get("field2", 99.99);
   111         field2 = pfields.get("field2", 99.99);
   106 
   112 
   107         /* These fields must be present in the stream */
   113         /* These fields must be present in the stream */
   108         try {
   114         try {
   109             field3 = pfields.get("field3", 99);
   115             field3 = pfields.get("field3", 99);
   119         }
   125         }
   120     }
   126     }
   121 };
   127 };
   122 
   128 
   123 class TestClass3 implements Serializable{
   129 class TestClass3 implements Serializable{
       
   130     private static final long serialVersionUID = 1L;
       
   131 
   124     // Not of type ObjectStreamField
   132     // Not of type ObjectStreamField
       
   133     @SuppressWarnings("serial") /* Incorrect declarations are being tested */
   125     private final String[] serialPersistentFields =  {"Foo","Foobar"};;
   134     private final String[] serialPersistentFields =  {"Foo","Foobar"};;
   126     Integer field1;
   135     Integer field1;
   127     double field2;
   136     double field2;
   128     int field3;
   137     int field3;
   129     String field4;
   138     String field4;
   137 
   146 
   138     private void readObject(ObjectInputStream ois)
   147     private void readObject(ObjectInputStream ois)
   139         throws IOException, ClassNotFoundException {
   148         throws IOException, ClassNotFoundException {
   140         ObjectInputStream.GetField pfields = ois.readFields();
   149         ObjectInputStream.GetField pfields = ois.readFields();
   141 
   150 
   142         field1 = (Integer) pfields.get("field1", new Integer(100));
   151         field1 = (Integer) pfields.get("field1", Integer.valueOf(100));
   143         field2 = pfields.get("field2", 99.99);
   152         field2 = pfields.get("field2", 99.99);
   144         field3 = pfields.get("field3", 99);
   153         field3 = pfields.get("field3", 99);
   145         field4 = (String) pfields.get("field4", "Default string");
   154         field4 = (String) pfields.get("field4", "Default string");
   146 
   155 
   147         try {
   156         try {
   154         }
   163         }
   155     }
   164     }
   156 };
   165 };
   157 
   166 
   158 class TestClass4 implements Serializable {
   167 class TestClass4 implements Serializable {
       
   168     private static final long serialVersionUID = 1L;
       
   169 
   159     // Correct format
   170     // Correct format
   160     private static final ObjectStreamField[] serialPersistentFields = {
   171     private static final ObjectStreamField[] serialPersistentFields = {
   161         new ObjectStreamField("field1", Integer.class),
   172         new ObjectStreamField("field1", Integer.class),
   162         new ObjectStreamField("field2", Double.TYPE),
   173         new ObjectStreamField("field2", Double.TYPE),
   163     };
   174     };
   176 
   187 
   177     private void readObject(ObjectInputStream ois)
   188     private void readObject(ObjectInputStream ois)
   178         throws IOException, ClassNotFoundException {
   189         throws IOException, ClassNotFoundException {
   179         ObjectInputStream.GetField pfields = ois.readFields();
   190         ObjectInputStream.GetField pfields = ois.readFields();
   180 
   191 
   181         field1 = (Integer) pfields.get("field1", new Integer(100));
   192         field1 = (Integer) pfields.get("field1", Integer.valueOf(100));
   182         field2 = pfields.get("field2", 99.99);
   193         field2 = pfields.get("field2", 99.99);
   183 
   194 
   184         try {
   195         try {
   185             field3 = pfields.get("field3", 99);
   196             field3 = pfields.get("field3", 99);
   186             throw new Error("data field: field3 in the persistent stream");
   197             throw new Error("data field: field3 in the persistent stream");
   197 };
   208 };
   198 
   209 
   199 public class CheckModifiers {
   210 public class CheckModifiers {
   200     public static void main(String[] args)
   211     public static void main(String[] args)
   201         throws ClassNotFoundException, IOException{
   212         throws ClassNotFoundException, IOException{
   202         TestClass1 tc1 = new TestClass1(new Integer(100), 25.56, 2000,
   213         TestClass1 tc1 = new TestClass1(100, 25.56, 2000,
   203             new String("Test modifiers of serialPersistentFields"));
   214             new String("Test modifiers of serialPersistentFields"));
   204 
   215 
   205         TestClass2 tc2 = new TestClass2(new Integer(100), 25.56, 2000,
   216         TestClass2 tc2 = new TestClass2(100, 25.56, 2000,
   206             new String("Test modifiers of serialPersistentFields"));
   217             new String("Test modifiers of serialPersistentFields"));
   207 
   218 
   208         TestClass3 tc3 = new TestClass3(new Integer(100), 25.56, 2000,
   219         TestClass3 tc3 = new TestClass3(100, 25.56, 2000,
   209             new String("Test Type of serialPersistentFields"));
   220             new String("Test Type of serialPersistentFields"));
   210 
   221 
   211         TestClass4 tc4 = new TestClass4(new Integer(100), 25.56, 2000,
   222         TestClass4 tc4 = new TestClass4(100, 25.56, 2000,
   212             new String("Test modifiers of serialPersistentFields"));
   223             new String("Test modifiers of serialPersistentFields"));
   213 
   224 
   214 
   225 
   215         FileOutputStream fos = new FileOutputStream("fields.ser");
   226         FileOutputStream fos = new FileOutputStream("fields.ser");
   216         try {
   227         try {