1 /*
2  * Copyright (c) 2013, 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 8013416
27  * @summary Tests public synthetic methods
28  * @run main/othervm -Djava.security.manager=allow Test8013416
29  * @author Sergey Malenkov
30  */
31 
32 import java.beans.DefaultPersistenceDelegate;
33 import java.beans.Encoder;
34 import java.beans.Expression;
35 import java.beans.Statement;
36 import java.beans.XMLEncoder;
37 import java.util.HashMap;
38 import java.util.Map.Entry;
39 import java.util.Set;
40 
41 public class Test8013416 extends AbstractTest {
main(String[] args)42     public static void main(String[] args) {
43         new Test8013416().test(true);
44     }
45 
getObject()46     protected Object getObject() {
47         Public<String, String> map = new Public<String, String>();
48         map.put(" pz1 ", " pz2 ");
49         map.put(" pz3 ", " pz4 ");
50         return map;
51     }
52 
53     @Override
initialize(XMLEncoder encoder)54     protected void initialize(XMLEncoder encoder) {
55         super.initialize(encoder);
56         encoder.setPersistenceDelegate(Public.class, new PublicPersistenceDelegate());
57     }
58 
59     private static final class PublicPersistenceDelegate extends DefaultPersistenceDelegate {
60         @Override
instantiate(Object oldInstance, Encoder out)61         protected Expression instantiate(Object oldInstance, Encoder out) {
62             return new Expression(oldInstance, oldInstance.getClass(), "new", null);
63         }
64 
65         @Override
initialize(Class<?> type, Object oldInstance, Object newInstance, Encoder out)66         protected void initialize(Class<?> type, Object oldInstance, Object newInstance, Encoder out) {
67             super.initialize(type, oldInstance, newInstance, out);
68 
69             Public<String, String> map = (Public) oldInstance;
70             for (Entry<String, String> entry : map.getAll()) {
71                 String[] args = {entry.getKey(), entry.getValue()};
72                 out.writeStatement(new Statement(oldInstance, "put", args));
73             }
74         }
75     }
76 
77     public static final class Public<K, V> extends Private<K, V> {
78     }
79 
80     private static class Private<K, V> {
81         private HashMap<K, V> map = new HashMap<K, V>();
82 
put(K key, V value)83         public void put(K key, V value) {
84             this.map.put(key, value);
85         }
86 
getAll()87         public Set<Entry<K, V>> getAll() {
88             return this.map.entrySet();
89         }
90     }
91 }
92