< prev index next >

## src/java.base/share/classes/java/security/PrivilegedActionException.java

Print this page

      1 /*
      2  * Copyright (c) 1998, 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.  Oracle designates this
      8  * particular file as subject to the "Classpath" exception as provided
      9  * by Oracle in the LICENSE file that accompanied this code.
     10  *
     11  * This code is distributed in the hope that it will be useful, but WITHOUT
     12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     14  * version 2 for more details (a copy is included in the LICENSE file that
     15  * accompanied this code).
     16  *
     17  * You should have received a copy of the GNU General Public License version
     18  * 2 along with this work; if not, write to the Free Software Foundation,
     19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     20  *
     21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     22  * or visit www.oracle.com if you need additional information or have any
     81      *         resulted in this {@code PrivilegedActionException}.
     82      * @see PrivilegedExceptionAction
     83      * @see AccessController#doPrivileged(PrivilegedExceptionAction)
     84      * @see AccessController#doPrivileged(PrivilegedExceptionAction,
     85      *                                            AccessControlContext)
     86      */
     87     public Exception getException() {
     88         return (Exception)super.getCause();
     89     }
     90
     91     public String toString() {
     92         String s = getClass().getName();
     93         Throwable cause = super.getCause();
     94         return (cause != null) ? (s + ": " + cause.toString()) : s;
     95     }
     96
     97
     98     /**
     99      * Serializable fields for UndeclaredThrowableException.
    100      *
    101      * @serialField undeclaredThrowable Throwable
    102      */
    103     @java.io.Serial
    104     private static final ObjectStreamField[] serialPersistentFields = {
    105         new ObjectStreamField("exception", Exception.class)
    106     };
    107
    108     /*
    109      * Reconstitutes the PrivilegedActionException instance from a stream
    110      * and initialize the cause properly when deserializing from an older
    111      * version.
    112      *
    113      * The getException and getCause method returns the private "exception"
    114      * field in the older implementation and PrivilegedActionException::cause
    115      * was set to null.

    116      */
    117     @java.io.Serial
    118     private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
    119         ObjectInputStream.GetField fields = s.readFields();
    120         Exception exception = (Exception) fields.get("exception", null);
    121         if (exception != null) {
    122             SharedSecrets.getJavaLangAccess().setCause(this, exception);
    123         }
    124     }
    125
    126     /*
    127      * To maintain compatibility with older implementation, write a serial
    128      * "exception" field with the cause as the value.

    129      */
    130     @java.io.Serial
    131     private void writeObject(ObjectOutputStream out) throws IOException {
    132         ObjectOutputStream.PutField fields = out.putFields();
    133         fields.put("exception", super.getCause());
    134         out.writeFields();
    135     }
    136 }
      1 /*
      2  * Copyright (c) 1998, 2021, 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.  Oracle designates this
      8  * particular file as subject to the "Classpath" exception as provided
      9  * by Oracle in the LICENSE file that accompanied this code.
     10  *
     11  * This code is distributed in the hope that it will be useful, but WITHOUT
     12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     14  * version 2 for more details (a copy is included in the LICENSE file that
     15  * accompanied this code).
     16  *
     17  * You should have received a copy of the GNU General Public License version
     18  * 2 along with this work; if not, write to the Free Software Foundation,
     19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     20  *
     21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     22  * or visit www.oracle.com if you need additional information or have any
     81      *         resulted in this {@code PrivilegedActionException}.
     82      * @see PrivilegedExceptionAction
     83      * @see AccessController#doPrivileged(PrivilegedExceptionAction)
     84      * @see AccessController#doPrivileged(PrivilegedExceptionAction,
     85      *                                            AccessControlContext)
     86      */
     87     public Exception getException() {
     88         return (Exception)super.getCause();
     89     }
     90
     91     public String toString() {
     92         String s = getClass().getName();
     93         Throwable cause = super.getCause();
     94         return (cause != null) ? (s + ": " + cause.toString()) : s;
     95     }
     96
     97
     98     /**
     99      * Serializable fields for UndeclaredThrowableException.
    100      *
    101      * @serialField exception Exception the undeclaredThrowable
    102      */
    103     @java.io.Serial
    104     private static final ObjectStreamField[] serialPersistentFields = {
    105         new ObjectStreamField("exception", Exception.class)
    106     };
    107
    108     /**
    109      * Reconstitutes the PrivilegedActionException instance from a stream
    110      * and initialize the cause properly when deserializing from an older
    111      * version.
    112      *
    113      * <p>The getException and getCause method returns the private "exception"
    114      * field in the older implementation and PrivilegedActionException::cause
    115      * was set to null.
    116      *
    117      * @param  s the {@code ObjectInputStream} from which data is read
    118      * @throws IOException if an I/O error occurs
    119      * @throws ClassNotFoundException if a serialized class cannot be loaded
    120      */
    121     @java.io.Serial
    122     private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
    123         ObjectInputStream.GetField fields = s.readFields();
    124         Exception exception = (Exception) fields.get("exception", null);
    125         if (exception != null) {
    126             SharedSecrets.getJavaLangAccess().setCause(this, exception);
    127         }
    128     }
    129
    130     /**
    131      * To maintain compatibility with older implementation, write a serial
    132      * "exception" field with the cause as the value.
    133      *
    134      * @param  out the {@code ObjectOutputStream} to which data is written
    135      * @throws IOException if an I/O error occurs
    136      */
    137     @java.io.Serial
    138     private void writeObject(ObjectOutputStream out) throws IOException {
    139         ObjectOutputStream.PutField fields = out.putFields();
    140         fields.put("exception", super.getCause());
    141         out.writeFields();
    142     }
    143 }

< prev index next >