forked from microbean/microbean-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractReflectiveProxier.java
More file actions
172 lines (158 loc) · 6.76 KB
/
AbstractReflectiveProxier.java
File metadata and controls
172 lines (158 loc) · 6.76 KB
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
/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
*
* Copyright © 2025–2026 microBean™.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.microbean.proxy;
import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.List;
import java.util.function.Supplier;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Parameterizable;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import org.microbean.construct.Domain;
/**
* An {@link AbstractProxier} that helps subclasses create {@link Proxy proxies} using the {@link
* java.lang.reflect.Proxy java.lang.reflect.Proxy} machinery present in the Java Development Kit.
*
* <p>This class also contains various {@code protected} utility methods that help with converting reflective {@link
* Type}s and {@link Executable}s to their {@link TypeMirror} and {@link ExecutableElement} counterparts.</p>
*
* @param <PS> the {@link ProxySpecification} type
*
* @author <a href="https://about.me/lairdnelson" target="_top">Laird Nelson</a>
*
* @see #proxy(ProxySpecification, Supplier)
*/
public abstract non-sealed class AbstractReflectiveProxier<PS extends ProxySpecification> extends AbstractProxier<PS> {
private static final TypeMirror[] EMPTY_TYPE_MIRROR_ARRAY = new TypeMirror[0];
/**
* Creates a new {@link AbstractReflectiveProxier} implementation.
*
* @param domain a {@link Domain}; must not be {@code null}
*
* @exception NullPointerException if {@code domain} is {@code null}
*/
protected AbstractReflectiveProxier(final Domain domain) {
super(domain);
}
/**
* A convenience method that returns {@code true} if the supplied {@link Method} is the {@link Object#equals(Object)
* java.lang.Object#equals(java.lang.Object)} method.
*
* @param m a {@link Method}; must not be {@code null}
*
* @return {@code true} if the supplied {@link Method} is the {@link Object#equals(Object)
* java.lang.Object#equals(java.lang.Object)} method
*
* @exception NullPointerException if {@code m} is {@code null}
*/
// (Convenience.)
protected final boolean equalsMethod(final Method m) {
return
m.getDeclaringClass() == Object.class &&
m.getReturnType() == boolean.class &&
m.getParameterCount() == 1 &&
m.getParameterTypes()[0] == Object.class &&
m.getName().equals("equals");
}
/**
* A convenience method that returns {@code true} if the supplied {@link Method} is the {@link Object#hashCode()
* java.lang.Object#hashCode()} method.
*
* @param m a {@link Method}; must not be {@code null}
*
* @return {@code true} if the supplied {@link Method} is the {@link Object#hashCode() java.lang.Object#hashCode()}
* method
*
* @exception NullPointerException if {@code m} is {@code null}
*/
// (Convenience.)
protected final boolean hashCodeMethod(final Method m) {
return
m.getDeclaringClass() == Object.class &&
m.getReturnType() == int.class &&
m.getParameterCount() == 0 &&
m.getName().equals("hashCode");
}
/**
* Returns a {@link Proxy} appropriate for the supplied specification and {@link Supplier} of instances.
*
* @param ps an appropriate proxy specification; must not be {@code null}
*
* @param instanceSupplier a {@link Supplier} of contextual instances; must not be {@code null}; may or may not create
* a new contextual instance each time it is invoked; may or may not be invoked multiple times depending on the
* subclass implementation
*
* @return a non-{@code null} {@link Proxy}
*
* @exception NullPointerException if any argument is {@code null}
*
* @exception IllegalArgumentException if the {@linkplain ProxySpecification#superclass() superclass} is not {@link
* Object java.lang.Object} (only interfaces may be proxied reflectively)
*
* @see #proxy(ProxySpecification, Class[], Supplier)
*
* @see #classLoader()
*/
@Override // AbstractProxier<PS>
public final <R> Proxy<R> proxy(final PS ps, final Supplier<? extends R> instanceSupplier) {
final Domain domain = this.domain();
if (!domain.javaLangObject(ps.superclass())) {
throw new IllegalArgumentException("ps: " + ps);
}
final List<? extends TypeMirror> interfaceTypeMirrors = ps.interfaces();
final int size = interfaceTypeMirrors.size();
final Class<?>[] interfaces = new Class<?>[size];
final ClassLoader classLoader = this.classLoader();
try {
for (int i = 0; i < size; i++) {
final TypeElement e = (TypeElement)((DeclaredType)interfaceTypeMirrors.get(i)).asElement();
final String binaryName = domain.toString(domain.binaryName(e));
interfaces[i] = Class.forName(binaryName, false, classLoader);
}
} catch (final ClassNotFoundException cnfe) {
throw new IllegalArgumentException("ps: " + ps, cnfe);
}
return this.proxy(ps, interfaces, instanceSupplier);
}
/**
* Returns a {@link Proxy} appropriate for the supplied specification and {@link Supplier} of contextual instances.
*
* @param <R> the contextual instance type
*
* @param ps an appropriate proxy specification; must not be {@code null}
*
* @param interfaces the interfaces to implement; every element is guaranteed to {@linkplain Class#isInterface() be an interface}
*
* @param instanceSupplier a {@link Supplier} of contextual instances; must not be {@code null}; may or may not create
* a new contextual instance each time it is invoked; may or may not be invoked multiple times depending on the
* subclass implementation
*
* @return a non-{@code null} {@link Proxy}
*
* @exception NullPointerException if any argument is {@code null}
*/
protected abstract <R> Proxy<R> proxy(final PS ps,
final Class<?>[] interfaces,
final Supplier<? extends R> instanceSupplier);
}