-
Notifications
You must be signed in to change notification settings - Fork 53
/
MethodAltMethodImpl.java
executable file
·197 lines (161 loc) · 6.12 KB
/
MethodAltMethodImpl.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Copyright (c) 2015, 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.xml.internal.alt.clazz;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import org.glassfish.hk2.utilities.reflection.ClassReflectionHelper;
import org.glassfish.hk2.utilities.reflection.ReflectionHelper;
import org.glassfish.hk2.xml.internal.alt.AltAnnotation;
import org.glassfish.hk2.xml.internal.alt.AltClass;
import org.glassfish.hk2.xml.internal.alt.AltMethod;
import org.glassfish.hk2.xml.internal.alt.MethodInformationI;
/**
* @author jwells
*
*/
public class MethodAltMethodImpl implements AltMethod {
private final ReentrantLock lock = new ReentrantLock();
private final Method method;
private final ClassReflectionHelper helper;
private List<AltClass> parameterTypes;
private List<AltAnnotation> altAnnotations;
private MethodInformationI methodInformation;
public MethodAltMethodImpl(Method method, ClassReflectionHelper helper) {
this.method = method;
this.helper = helper;
}
public Method getOriginalMethod() {
return method;
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.internal.alt.AltMethod#getName()
*/
@Override
public String getName() {
return method.getName();
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.internal.alt.AltMethod#getReturnType()
*/
@Override
public AltClass getReturnType() {
Class<?> retVal = method.getReturnType();
if (retVal == null) retVal = void.class;
return new ClassAltClassImpl(retVal, helper);
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.internal.alt.AltMethod#getParameterTypes()
*/
@Override
public List<AltClass> getParameterTypes() {
lock.lock();
try {
if (parameterTypes != null) return parameterTypes;
Class<?> pTypes[] = method.getParameterTypes();
List<AltClass> retVal = new ArrayList<AltClass>(pTypes.length);
for (Class<?> pType : pTypes) {
retVal.add(new ClassAltClassImpl(pType, helper));
}
parameterTypes = Collections.unmodifiableList(retVal);
return parameterTypes;
} finally {
lock.unlock();
}
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.internal.alt.AltMethod#getFirstTypeArgument()
*/
@Override
public AltClass getFirstTypeArgument() {
Type type = method.getGenericReturnType();
if (type == null) return null;
Type first = ReflectionHelper.getFirstTypeArgument(type);
if (first == null) return null;
Class<?> retVal = ReflectionHelper.getRawClass(first);
if (retVal == null) return null;
return new ClassAltClassImpl(retVal, helper);
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.internal.alt.AltMethod#getFirstTypeArgumentOfParameter(int)
*/
@Override
public AltClass getFirstTypeArgumentOfParameter(int index) {
Type pTypes[] = method.getGenericParameterTypes();
Type pType = pTypes[index];
Type first = ReflectionHelper.getFirstTypeArgument(pType);
if (first == null) return null;
Class<?> retVal = ReflectionHelper.getRawClass(first);
if (retVal == null) return null;
return new ClassAltClassImpl(retVal, helper);
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.internal.alt.AltMethod#getAnnotation(java.lang.String)
*/
@Override
public AltAnnotation getAnnotation(String annotation) {
if (annotation == null) return null;
Annotation annotations[] = method.getAnnotations();
for (Annotation anno : annotations) {
if (annotation.equals(anno.annotationType().getName())) {
return new AnnotationAltAnnotationImpl(anno, helper);
}
}
return null;
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.internal.alt.AltMethod#getAnnotations()
*/
@Override
public List<AltAnnotation> getAnnotations() {
lock.lock();
try {
if (altAnnotations != null) return altAnnotations;
Annotation annotations[] = method.getAnnotations();
ArrayList<AltAnnotation> retVal = new ArrayList<AltAnnotation>(annotations.length);
for (Annotation annotation : annotations) {
retVal.add(new AnnotationAltAnnotationImpl(annotation, helper));
}
altAnnotations = Collections.unmodifiableList(retVal);
return altAnnotations;
} finally {
lock.unlock();
}
}
/* (non-Javadoc)
* @see org.glassfish.hk2.xml.internal.alt.AltMethod#isVarArgs()
*/
@Override
public boolean isVarArgs() {
return method.isVarArgs();
}
@Override
public void setMethodInformation(MethodInformationI methodInfo) {
methodInformation = methodInfo;
}
@Override
public MethodInformationI getMethodInformation() {
return methodInformation;
}
@Override
public String toString() {
return "MethodAltMethodImpl(" + method + "," + System.identityHashCode(this) + ")";
}
}