我使用以下代码重新定义一个类:
DynamicType.Unloaded unloaded = new ByteBuddy().with(TypeValidation.DISABLED).rebase(typeDescription,
ClassFileLocator.Simple.of(className, classBytes,
ClassFileLocator.ForClassLoader.of((ClassLoader) classLoader))).method(
ElementMatchers.isPackagePrivate().and(ElementMatchers.not(ElementMatchers.isAbstract()))).intercept(
MethodDelegation.to(PackagePrivateInterceptor.class)).transform(
MethodTransformer.Simple.withModifiers(Visibility.PUBLIC)).make();
return new ClassPair(unloaded.load((ClassLoader) classLoader,
ClassLoadingStrategy.Default.INJECTION.withProtectionDomain(
classLoader.getClass().getProtectionDomain())).getLoaded(), unloaded.getBytes());
加载类时class com.google.gson.internal.ConstructorConstructor
它通过构造函数直到最终到达com.google.gson.internal.LinkedTreeMap
.
在初始化期间,我得到了一个 VerifyError:
1:24:20.227 [main] 错误 [io.hakansson.dynamicjar.core.main.Bootstrap] - java.lang.RuntimeException: java.lang.VerifyError: com.google.gson 类中常量池条目 195 处的非法类型。 internal.LinkedTreeMap$1 异常详情: 地点: com/google/gson/internal/LinkedTreeMap$1.thenComparing$accessor$vT023QbO(Ljava/util/function/Function;)Ljava/util/Comparator; @2:调用特殊 原因: 常量池索引 195 无效 字节码: 0x0000000: 2a2b b700 c3b0 在 io.hakansson.dynamicjar.core.api.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:52) 在 io.hakansson.dynamicjar.core.main.Bootstrap.main(Bootstrap.java:42) 原因:java.lang.VerifyError:com.google.gson.internal.LinkedTreeMap$1 类中常量池条目 195 处的非法类型 异常详情: 地点: com/google/gson/internal/LinkedTreeMap$1.thenComparing$accessor$vT023QbO(Ljava/util/function/Function;)Ljava/util/Comparator; @2:调用特殊 原因: 常量池索引 195 无效 字节码: 0x0000000: 2a2b b700 c3b0 在 com.google.gson.internal.LinkedTreeMap.classInitializer$oNOjADym(LinkedTreeMap.java:40) 在 com.google.gson.internal.LinkedTreeMap.(LinkedTreeMap.java) 在 com.google.gson.internal.ConstructorConstructor$13.construct$original$gt92dwVY(ConstructorConstructor.java:207) 在 com.google.gson.internal.ConstructorConstructor$13.construct$original$gt92dwVY$accessor$lof1omy8(ConstructorConstructor.java) 在 com.google.gson.internal.ConstructorConstructor$13$auxiliary$oB71rVyd.call(未知来源) 在 io.hakansson.dynamicjar.nestedjarclassloader.PackagePrivateInterceptor.intercept(PackagePrivateInterceptor.java:29) 在 com.google.gson.internal.ConstructorConstructor$13.construct(ConstructorConstructor.java) 在 com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:167) 在 com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:145) 在 com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read$original$ZTjOtCtb(ReflectiveTypeAdapterFactory.java:116) 在 com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read$original$ZTjOtCtb$accessor$WgRdwpwl(ReflectiveTypeAdapterFactory.java) 在 com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1$auxiliary$41HBhnNS.call(未知来源) 在 io.hakansson.dynamicjar.nestedjarclassloader.PackagePrivateInterceptor.intercept(PackagePrivateInterceptor.java:29) 在 com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java) 在 com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:216) 在 com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read$original$bOjIYDn5(TypeAdapterRuntimeTypeWrapper.java:40) 在 com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read$original$bOjIYDn5$accessor$hMWEZRZS(TypeAdapterRuntimeTypeWrapper.java) 在 com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper$auxiliary$U1tyihKy.call(未知来源) 在 io.hakansson.dynamicjar.nestedjarclassloader.PackagePrivateInterceptor.intercept(PackagePrivateInterceptor.java:29) 在 com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java) 在 com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82) 在 com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61) 在 com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read$original$ZTjOtCtb(ReflectiveTypeAdapterFactory.java:116) 在 com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read$original$ZTjOtCtb$accessor$WgRdwpwl(ReflectiveTypeAdapterFactory.java) 在 com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1$auxiliary$41HBhnNS.call(未知来源) 在 io.hakansson.dynamicjar.nestedjarclassloader.PackagePrivateInterceptor.intercept(PackagePrivateInterceptor.java:29) 在 com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java) 在 com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:216) 在 com.google.gson.Gson.fromJson(Gson.java:879) 在 com.google.gson.Gson.fromJson(Gson.java:817)
作为参考,拦截器基本上只是手动检查包私有访问(因为包私有方法被公开),然后只调用superCall.call()
. 我不相信问题出在那儿。
有任何想法吗?
编辑:这是PackagePrivateInterceptor
:公共类 PackagePrivateInterceptor {
@RuntimeType
@BindingPriority(1)
public static Object intercept(@SuperCall Callable<?> superCall, @Origin Class targetClass, @Origin String method) throws
Exception
{
Class callingClass = new InternalSecurityManager().getCallingClass();
String targetPackage = targetClass.getPackage().getName();
if (!callingClass.getPackage().getName().equals(targetPackage)) {
throw new IllegalAccessError(callingClass + " cannot access method " + method + " of Class " + targetClass);
}
//Default:
return superCall.call();
}
private static class InternalSecurityManager extends SecurityManager {
Class getCallingClass() {
Class[] classContext = getClassContext();
for (Class current : classContext) {
if (current.getName().startsWith("java.") ||
current.getName().equals(PackagePrivateInterceptor.class.getName()) ||
current.getName().equals(InternalSecurityManager.class.getName()))
{
continue;
}
return current;
}
throw new IllegalStateException("Failed to find calling Class");
}
}
EDIT2:以下代码不会触发问题:
//Only package-private methods should be proxied.
DynamicType.Unloaded unloaded = new ByteBuddy().with(TypeValidation.DISABLED).rebase(typeDescription,
ClassFileLocator.Simple.of(className, classBytes,
ClassFileLocator.ForClassLoader.of((ClassLoader) classLoader))).method(
ElementMatchers.isPackagePrivate().and(ElementMatchers.not(ElementMatchers.isAbstract()))).intercept(
MethodDelegation.to(PackagePrivateInterceptor.class)).transform(
MethodTransformer.Simple.withModifiers(Visibility.PUBLIC)).make();
DynamicType.Loaded loaded = unloaded.load((ClassLoader) classLoader,
ClassLoadingStrategy.Default.INJECTION.withProtectionDomain(
classLoader.getClass().getProtectionDomain()));
if (className.equals("com.google.gson.internal.LinkedTreeMap"))
System.out.println(DatatypeConverter.printHexBinary(loaded.getBytes()));
return new ClassPair(loaded.getLoaded(), unloaded.getBytes());
但以下是:
//All non-private methods should be proxied
//TODO: Actually, the class itself should be made visible and still only package-private methods should be proxied.
DynamicType.Unloaded unloaded = new ByteBuddy().with(TypeValidation.DISABLED).rebase(typeDescription,
ClassFileLocator.Simple.of(className, classBytes,
ClassFileLocator.ForClassLoader.of((ClassLoader) classLoader))).method(
ElementMatchers.not(ElementMatchers.isPrivate()).and(
ElementMatchers.not(ElementMatchers.isAbstract()))).intercept(
MethodDelegation.to(PackagePrivateInterceptor.class)).transform(
MethodTransformer.Simple.withModifiers(Visibility.PUBLIC)).make();
return new ClassPair(unloaded.load((ClassLoader) classLoader,
ClassLoadingStrategy.Default.INJECTION.withProtectionDomain(
classLoader.getClass().getProtectionDomain())).getLoaded(), unloaded.getBytes());