1 /*
2  * Copyright (c) 2018, 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 /*
25  * @test
26  * @bug 8201793
27  * @summary Test Reference::clone to throw CloneNotSupportedException
28  */
29 package test.java.lang.ref;
30 
31 import java.lang.ref.*;
32 
33 import org.testng.annotations.Test;
34 
35 public class ReferenceCloneTest {
36     private static final ReferenceQueue<Object> QUEUE = new ReferenceQueue<>();
37 
38     @Test
testReferenceClone(String... args)39     public void testReferenceClone(String... args) {
40         ReferenceCloneTest refClone = new ReferenceCloneTest();
41         refClone.test();
42     }
43 
test()44     public void test() {
45         // test Reference::clone that throws CNSE
46         Object o = new Object();
47         assertCloneNotSupported(new SoftRef(o));
48         assertCloneNotSupported(new WeakRef(o));
49         assertCloneNotSupported(new PhantomRef(o));
50 
51         // Reference subclass may override the clone method
52         CloneableReference ref = new CloneableReference(o);
53         try {
54             ref.clone();
55         } catch (CloneNotSupportedException e) {}
56     }
57 
assertCloneNotSupported(CloneableRef ref)58     private void assertCloneNotSupported(CloneableRef ref) {
59         try {
60             ref.clone();
61             throw new RuntimeException("Reference::clone should throw CloneNotSupportedException");
62         } catch (CloneNotSupportedException e) {}
63     }
64 
65     // override clone to be public that throws CNSE
66     interface CloneableRef extends Cloneable {
clone()67         public Object clone() throws CloneNotSupportedException;
68     }
69 
70     class SoftRef extends SoftReference<Object> implements CloneableRef {
SoftRef(Object referent)71         public SoftRef(Object referent) {
72             super(referent, QUEUE);
73         }
clone()74         public Object clone() throws CloneNotSupportedException {
75             return super.clone();
76         }
77     }
78 
79     class WeakRef extends WeakReference<Object> implements CloneableRef {
WeakRef(Object referent)80         public WeakRef(Object referent) {
81             super(referent, QUEUE);
82         }
clone()83         public Object clone() throws CloneNotSupportedException {
84             return super.clone();
85         }
86     }
87 
88     class PhantomRef extends PhantomReference<Object> implements CloneableRef {
PhantomRef(Object referent)89         public PhantomRef(Object referent) {
90             super(referent, QUEUE);
91         }
92 
clone()93         public Object clone() throws CloneNotSupportedException {
94             return super.clone();
95         }
96     }
97 
98     // override clone to return a new instance
99     class CloneableReference extends WeakReference<Object> implements Cloneable {
CloneableReference(Object referent)100         public CloneableReference(Object referent) {
101             super(referent, QUEUE);
102         }
103 
clone()104         public Object clone() throws CloneNotSupportedException {
105             return new CloneableReference(this.get());
106         }
107     }
108 
109 }
110