-
Notifications
You must be signed in to change notification settings - Fork 53
/
TypesCtr.java
executable file
·176 lines (162 loc) · 6.08 KB
/
TypesCtr.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* Copyright (c) 2010, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.hk2.classmodel.reflect.impl;
import org.glassfish.hk2.classmodel.reflect.Type;
import org.glassfish.hk2.classmodel.reflect.Types;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.ReentrantLock;
/**
* contains all the parsed types references.
* @author Jerome Dochez
*/
public class TypesCtr implements Types {
@Override
public Type getBy(String name) {
for (Map<String, TypeProxy<Type>> map : storage.values()) {
TypeProxy proxy = map.get(name);
if (proxy!=null) {
return proxy.get();
}
}
return null;
}
@Override
public <T extends Type> T getBy(Class<T> type, String name) {
Type t = getBy(name);
try {
return type.cast(t);
} catch (ClassCastException e) {
return null;
}
}
public TypeProxy<Type> getHolder(String name) {
if (name.equals("java.lang.Object")) return null;
// we look first in our storage pools.
for (Map<String, TypeProxy<Type>> map : storage.values()) {
TypeProxy<Type> proxy = map.get(name);
if (proxy!=null) {
return proxy;
}
}
// ok let's look in our unknown storage pool.
if (unknownTypesStorage.containsKey(name)) {
return unknownTypesStorage.get(name);
}
// ok we don't have and since we don't know its type
// let's put it in the unknown storage pool.
TypeProxy<Type> typeProxy = new TypeProxy<Type>(null, name);
TypeProxy<Type> old = unknownTypesStorage.putIfAbsent(name, typeProxy);
if (old==null) {
nonVisited.push(typeProxy);
return typeProxy;
}
return old;
}
public <T extends Type> TypeProxy<Type> getHolder(String name, Class<T> type) {
if (name.equals("java.lang.Object")) {
return null;
}
ConcurrentMap<String, TypeProxy<Type>> typeStorage = storage.get(type);
if (typeStorage == null) {
typeStorage = new ConcurrentHashMap<String, TypeProxy<Type>>();
ConcurrentMap<String, TypeProxy<Type>> old = storage.putIfAbsent(type, typeStorage);
if (old != null) {
// some other thread got to set that type storage before us, let's use it
typeStorage = old;
}
}
TypeProxy<Type> typeProxy = typeStorage.get(name);
if (typeProxy == null) {
// in our unknown type pool ?
TypeProxy<Type> tmp = unknownTypesStorage.get(name);
// in our unknown type pool ?
if (tmp!=null) {
unknownTypesStorageLock.lock();
try {
typeProxy = unknownTypesStorage.remove(name);
if (typeProxy == null) {
typeProxy = tmp;
}
} finally {
unknownTypesStorageLock.unlock();
}
if (typeProxy!=null) {
TypeProxy<Type> old = typeStorage.putIfAbsent(name, typeProxy);
if (old!=null) {
typeProxy = old;
}
}
} else {
typeProxy = new TypeProxy<Type>(null, name);
TypeProxy<Type> old = typeStorage.putIfAbsent(name, typeProxy);
if (old==null) {
nonVisited.push(typeProxy);
} else {
typeProxy=old;
}
}
}
return typeProxy;
}
public interface ProxyTask {
public void on(TypeProxy<?> proxy);
}
/**
* Runs a task on each non visited types parsing discovered.
*
* @param proxyTask the task to run on each non visited type.
*/
public void onNotVisitedEntries(ProxyTask proxyTask) {
while(!nonVisited.isEmpty()) {
TypeProxy proxy = nonVisited.pop();
if (!proxy.isVisited()) {
proxyTask.on(proxy);
}
}
}
public void clearNonVisitedEntries() {
nonVisited.clear();
unknownTypesStorage.clear();
}
@Override
public Collection<Type> getAllTypes() {
List<Type> allTypes = new ArrayList<Type>();
for (Map<String, TypeProxy<Type>> map : storage.values()) {
for (TypeProxy typeProxy : map.values()) {
if (typeProxy.get()!=null) {
allTypes.add(typeProxy.get());
}
}
}
return allTypes;
}
/**
* Storage indexed by TYPE : interface | class | annotation and then by name.
*/
private final ConcurrentMap<Class, ConcurrentMap<String, TypeProxy<Type>>> storage=
new ConcurrentHashMap<Class, ConcurrentMap<String, TypeProxy<Type>>>();
private final ReentrantLock unknownTypesStorageLock = new ReentrantLock();
/**
* Map of encountered types which we don't know if it is an interface, class or annotation
*/
private final ConcurrentMap<String, TypeProxy<Type>> unknownTypesStorage = new ConcurrentHashMap<String, TypeProxy<Type>>();
/**
* Stack on type proxy as they have been instantiated in FILO order.
*/
private final Stack<TypeProxy> nonVisited = new Stack<TypeProxy>();
}