Home | History | Annotate | Download | only in io
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 package java.io;
     19 
     20 /**
     21  * Signals a problem during the serialization or or deserialization of an
     22  * object. Possible reasons include:
     23  * <ul>
     24  * <li>The SUIDs of the class loaded by the VM and the serialized class info do
     25  * not match.</li>
     26  * <li>A serializable or externalizable object cannot be instantiated (when
     27  * deserializing) because the no-arg constructor that needs to be run is not
     28  * visible or fails.</li>
     29  * </ul>
     30  *
     31  * @see ObjectInputStream #readObject()
     32  * @see ObjectInputValidation#validateObject()
     33  */
     34 public class InvalidClassException extends ObjectStreamException {
     35 
     36     private static final long serialVersionUID = -4333316296251054416L;
     37 
     38     /**
     39      * The fully qualified name of the class that caused the problem.
     40      */
     41     public String classname;
     42 
     43     /**
     44      * Constructs a new {@code InvalidClassException} with its stack trace and
     45      * detailed message filled in.
     46      *
     47      * @param detailMessage
     48      *            the detail message for this exception.
     49      */
     50     public InvalidClassException(String detailMessage) {
     51         super(detailMessage);
     52     }
     53 
     54     /**
     55      * Constructs a new {@code InvalidClassException} with its stack trace,
     56      * detail message and the fully qualified name of the class which caused the
     57      * exception filled in.
     58      *
     59      * @param className
     60      *            the name of the class that caused the exception.
     61      * @param detailMessage
     62      *            the detail message for this exception.
     63      */
     64     public InvalidClassException(String className, String detailMessage) {
     65         super(detailMessage);
     66         this.classname = className;
     67     }
     68 
     69     /**
     70      * Returns the detail message which was provided when the exception was
     71      * created. {@code null} is returned if no message was provided at creation
     72      * time. If a detail message as well as a class name are provided, then the
     73      * values are concatenated and returned.
     74      *
     75      * @return the detail message, possibly concatenated with the name of the
     76      *         class that caused the problem.
     77      */
     78     @Override
     79     public String getMessage() {
     80         String msg = super.getMessage();
     81         if (classname != null) {
     82             msg = classname + "; " + msg;
     83         }
     84         return msg;
     85     }
     86 }
     87