1/*
2 * Copyright (c) 2014, 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
23 * questions.
24 */
25package com.sun.beans.introspect;
26
27import com.sun.beans.util.Cache;
28
29import java.lang.reflect.Method;
30import java.util.List;
31import java.util.Map;
32
33import static sun.reflect.misc.ReflectUtil.checkPackageAccess;
34
35public final class ClassInfo {
36    private static final ClassInfo DEFAULT = new ClassInfo(null);
37    private static final Cache<Class<?>,ClassInfo> CACHE
38            = new Cache<Class<?>,ClassInfo>(Cache.Kind.SOFT, Cache.Kind.SOFT) {
39        @Override
40        public ClassInfo create(Class<?> type) {
41            return new ClassInfo(type);
42        }
43    };
44
45    public static ClassInfo get(Class<?> type) {
46        if (type == null) {
47            return DEFAULT;
48        }
49        try {
50            checkPackageAccess(type);
51            return CACHE.get(type);
52        } catch (SecurityException exception) {
53            return DEFAULT;
54        }
55    }
56
57    private final Object mutex = new Object();
58    private final Class<?> type;
59    private List<Method> methods;
60    private Map<String,PropertyInfo> properties;
61    private Map<String,EventSetInfo> eventSets;
62
63    private ClassInfo(Class<?> type) {
64        this.type = type;
65    }
66
67    public List<Method> getMethods() {
68        if (this.methods == null) {
69            synchronized (this.mutex) {
70                if (this.methods == null) {
71                    this.methods = MethodInfo.get(this.type);
72                }
73            }
74        }
75        return this.methods;
76    }
77
78    public Map<String,PropertyInfo> getProperties() {
79        if (this.properties == null) {
80            synchronized (this.mutex) {
81                if (this.properties == null) {
82                    this.properties = PropertyInfo.get(this.type);
83                }
84            }
85        }
86        return this.properties;
87    }
88
89    public Map<String,EventSetInfo> getEventSets() {
90        if (this.eventSets == null) {
91            synchronized (this.mutex) {
92                if (this.eventSets == null) {
93                    this.eventSets = EventSetInfo.get(this.type);
94                }
95            }
96        }
97        return this.eventSets;
98    }
99}
100