forked from microbean/microbean-clientproxy-bytebuddy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBBClientProxier.java
More file actions
160 lines (135 loc) · 5.93 KB
/
BBClientProxier.java
File metadata and controls
160 lines (135 loc) · 5.93 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
/* -*- 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.clientproxy.bytebuddy;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandles.Lookup;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.pool.TypePool;
import org.microbean.bean.Id;
import org.microbean.construct.Domain;
import org.microbean.proxy.AbstractToolkitProxier;
import org.microbean.proxy.Proxy;
import org.microbean.proxy.ProxySpecification;
import org.microbean.reference.ClientProxier;
import org.microbean.reference.ReferenceException;
import static java.lang.invoke.MethodType.methodType;
import static java.util.Objects.requireNonNull;
/**
* An {@link AbstractToolkitProxier} and {@link ClientProxier} that uses <a href="https://bytebuddy.net/#/">Byte
* Buddy</a> to {@linkplain #generate(ProxySpecification) generate} {@linkplain org.microbean.proxy.Proxy client
* proxies}.
*
* @author <a href="https://about.me/lairdnelson" target="_top">Laird Nelson</a>
*
* @see BBClientProxyClassGenerator
*/
public final class BBClientProxier extends AbstractToolkitProxier<ProxySpecification, DynamicType.Unloaded<?>> implements ClientProxier {
private static final Map<ProxySpecification, Object> clientProxyInstances = new ConcurrentHashMap<>();
private final TypeDefinitions tds;
private final BBClientProxyClassGenerator g;
/**
* Creates a new {@link BBClientProxier}.
*
* @param domain a non-{@code null} {@link Domain}
*
* @exception NullPointerException if any argument is {@code null}
*
* @see TypeElementTypePool#TypeElementTypePool(Domain)
*
* @see #BBClientProxier(Domain, TypePool)
*/
public BBClientProxier(final Domain domain) {
this(domain, new TypeElementTypePool(domain));
}
/**
* Creates a new {@link BBClientProxier}.
*
* @param domain a non-{@code null} {@link Domain}
*
* @param typePool a non-{@code null} {@link TypePool}
*
* @exception NullPointerException if any argument is {@code null}
*
* @see TypeDefinitions#TypeDefinitions(TypePool)
*
* @see BBClientProxyClassGenerator#BBClientProxyClassGenerator(TypePool)
*
* @see #BBClientProxier(Domain, TypeDefinitions, BBClientProxyClassGenerator)
*/
public BBClientProxier(final Domain domain,
final TypePool typePool) {
this(domain, new TypeDefinitions(typePool), new BBClientProxyClassGenerator(typePool));
}
/**
* Creates a new {@link BBClientProxier}.
*
* @param domain a non-{@code null} {@link Domain}; must not be {@code null}
*
* @param tds a non-{@code null} {@link TypeDefinitions}
*
* @param g a non-{@code nulll} {@link BBClientProxyClassGenerator}
*
* @exception NullPointerException if any argument is {@code null}
*/
public BBClientProxier(final Domain domain,
final TypeDefinitions tds,
final BBClientProxyClassGenerator g) {
super(domain, MethodHandles.lookup());
this.tds = requireNonNull(tds, "tds");
this.g = requireNonNull(g, "g");
}
@Override // ClientProxier
public <R> R clientProxy(final Id id, final Supplier<? extends R> instanceSupplier) {
return this.proxy(new ProxySpecification(this.domain(), id), instanceSupplier).$cast();
}
@Override // AbstractClientProxier<DynamicType.Unloaded<?>>
protected final DynamicType.Unloaded<?> generate(final ProxySpecification ps) {
return
this.g.generate(ps.name(),
this.tds.typeDescription(ps.superclass()),
ps.interfaces().stream().map(this.tds::typeDescriptionGeneric).toList());
}
@Override // AbstractToolkitProxier<ProxySpecification, DynamicType.Unloaded<?>>
@SuppressWarnings("unchecked")
public final <R> Proxy<R> proxy(final ProxySpecification ps, final Supplier<? extends R> instanceSupplier) {
return (Proxy<R>)clientProxyInstances.computeIfAbsent(ps, ps0 -> {
try {
final Class<?> proxyClass = this.proxyClass(ps);
this.getClass().getModule().addReads(proxyClass.getModule());
return
this.lookup(proxyClass).findConstructor(proxyClass, methodType(void.class, Supplier.class))
.asType(methodType(Object.class, Supplier.class))
.invokeExact(instanceSupplier);
} catch (final RuntimeException | Error e) {
throw e;
} catch (final Throwable e) {
throw new ReferenceException(e.getMessage(), e);
}
});
}
@Override // AbstractClientProxier<DynamicType.Unloaded<?>>
protected final Class<?> proxyClass(final DynamicType.Unloaded<?> dtu, final ClassLoader cl)
throws ClassNotFoundException {
// getTypeName() invoked on a TypeDescription will be its binary name (required by Class#forName(String)):
// https://javadoc.io/static/net.bytebuddy/byte-buddy/1.17.3/net/bytebuddy/description/type/TypeDefinition.html#getTypeName--
final String binaryName = dtu.getTypeDescription().getSuperClass().asErasure().getTypeName();
final Class<?> superclass = Class.forName(binaryName, false, cl);
return dtu.load(superclass.getClassLoader(), ClassLoadingStrategy.UsingLookup.of(lookup(superclass))).getLoaded();
}
}