
com.github.joekerouac.common.tools.proxy.bytebuddy.ByteBuddyProxyClient Maven / Gradle / Ivy
The newest version!
// Generated by delombok at Fri Mar 14 11:41:38 CST 2025
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
* file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
* to You 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 com.github.joekerouac.common.tools.proxy.bytebuddy;
import java.util.Arrays;
import com.github.joekerouac.common.tools.collection.CollectionUtil;
import com.github.joekerouac.common.tools.collection.Pair;
import com.github.joekerouac.common.tools.proxy.Interception;
import com.github.joekerouac.common.tools.proxy.ParentUtil;
import com.github.joekerouac.common.tools.proxy.ProxyClassLoader;
import com.github.joekerouac.common.tools.proxy.ProxyClient;
import com.github.joekerouac.common.tools.reflect.ClassUtils;
import com.github.joekerouac.common.tools.string.StringUtils;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.matcher.ElementMatcher;
/**
* 代理客户端bytebuddy实现
*
* @author JoeKerouac
* @since 1.0.0
*/
public class ByteBuddyProxyClient implements ProxyClient {
@java.lang.SuppressWarnings("all")
@lombok.Generated
private static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(ByteBuddyProxyClient.class);
private static final AnyMethodElementMatcher MATCHER = new AnyMethodElementMatcher();
@Override
public Object create(Class>[] parent, Object proxy, ClassLoader loader, String name, Interception interception, Class>[] paramTypes, Object[] params) {
if (!CollectionUtil.sizeEquals(params, paramTypes)) {
throw new IllegalArgumentException("构造器参数列表paramTypes长度和实际参数params长度不一致");
}
return ClassUtils.getInstance(createClass(parent, proxy, loader, name, interception), paramTypes, params);
}
@Override
public Class> createClass(Class>[] parent, Object proxy, ClassLoader loader, String name, Interception interception) {
ByteBuddy byteBuddy = new ByteBuddy();
Pair, Class>[]> pair = ParentUtil.setInterfaces(parent, proxy);
Class> superClass = pair.getKey();
Class>[] interfaces = pair.getValue();
DynamicType.Builder> builder;
if (superClass != null) {
builder = byteBuddy.subclass(superClass);
} else {
builder = byteBuddy.subclass(Object.class);
}
builder = builder.implement(Arrays.asList(interfaces));
if (!StringUtils.isBlank(name)) {
builder = builder.name(name);
}
ClassLoader usedLoader = loader == null ? ProxyClient.DEFAULT_LOADER : loader;
builder = builder.method(MATCHER).intercept(MethodDelegation.to(new GeneralInterceptor(interception, proxy, superClass, interfaces)));
ProxyClassLoader realLoader;
if (usedLoader instanceof ProxyClassLoader) {
realLoader = (ProxyClassLoader) usedLoader;
} else {
realLoader = new ProxyClassLoader(usedLoader);
}
return builder.make().load(realLoader, (classLoader, types) -> CollectionUtil.convert(types, classLoader::buildClass)).getLoaded();
}
@Override
public ClientType getClientType() {
return ClientType.BYTE_BUDDY;
}
/**
* 匹配任何方法
*/
private static class AnyMethodElementMatcher implements ElementMatcher {
@Override
public boolean matches(MethodDescription target) {
return true;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy