简体   繁体   English

在Google App Engine上使用ByteBuddy创建类时出现反射错误

[英]Reflection error when creating a class using ByteBuddy on Google App Engine

I am trying to define a sublclass dynamically from a concrete class using ByteBuddy as follows 我正在尝试使用ByteBuddy从具体类动态定义子类,如下所示

Class<? extends MyConcreteClass> dynamicType = new ByteBuddy()
    .subclass(MyConcreteClass.class, ConstructorStrategy.Default.DEFAULT_CONSTRUCTOR)
    .name(dynamicClassName)
    .annotateType(MyConcreteClass.class.getDeclaredAnnotations())
    .make()
    .load(MyConcreteClass.class.getClassLoader())
    .getLoaded();

This code works from within a JUnit test, but when I run it under Google App Engine, I get the following exception. 该代码可在JUnit测试中使用,但是当我在Google App Engine下运行它时,出现以下异常。

java.lang.IllegalStateException: Cannot access java.lang.reflect.Parameter#getModifiers
at net.bytebuddy.description.method.ParameterList$ForLoadedExecutable$Dispatcher$ForJava8CapableVm.getParameterCount(ParameterList.java:261)
at net.bytebuddy.description.method.ParameterList$ForLoadedExecutable.size(ParameterList.java:157)
at net.bytebuddy.description.method.ParameterList$TypeSubstituting.size(ParameterList.java:582)
at net.bytebuddy.matcher.CollectionSizeMatcher.matches(CollectionSizeMatcher.java:34)
at net.bytebuddy.matcher.CollectionSizeMatcher.matches(CollectionSizeMatcher.java:13)
at net.bytebuddy.matcher.MethodParametersMatcher.matches(MethodParametersMatcher.java:32)
at net.bytebuddy.matcher.MethodParametersMatcher.matches(MethodParametersMatcher.java:13)
at net.bytebuddy.matcher.ElementMatcher$Junction$Conjunction.matches(ElementMatcher.java:101)
at net.bytebuddy.matcher.ElementMatcher$Junction$Conjunction.matches(ElementMatcher.java:101)
at net.bytebuddy.matcher.FilterableList$AbstractBase.filter(FilterableList.java:96)
at net.bytebuddy.dynamic.scaffold.subclass.ConstructorStrategy$Default$2.doExtractConstructors(ConstructorStrategy.java:82)
at net.bytebuddy.dynamic.scaffold.subclass.ConstructorStrategy$Default.extractConstructors(ConstructorStrategy.java:176)
at net.bytebuddy.dynamic.scaffold.subclass.SubclassDynamicTypeBuilder.applyConstructorStrategy(SubclassDynamicTypeBuilder.java:185)
at net.bytebuddy.dynamic.scaffold.subclass.SubclassDynamicTypeBuilder.make(SubclassDynamicTypeBuilder.java:162)
at net.bytebuddy.dynamic.scaffold.subclass.SubclassDynamicTypeBuilder.make(SubclassDynamicTypeBuilder.java:155)
at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase.make(DynamicType.java:2559)
...
Caused by: java.lang.IllegalAccessException: Reflection is not allowed on public int java.lang.reflect.Executable.getParameterCount()
at com.google.appengine.tools.development.agent.runtime.Runtime.verifyWhiteListed(Runtime.java:90)
at com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:125)
at net.bytebuddy.description.method.ParameterList$ForLoadedExecutable$Dispatcher$ForJava8CapableVm.getParameterCount(ParameterList.java:259)

The exception is thrown while executing the method make() . 执行方法make()引发异常。

Is there a way to make this work under the GAE? 有没有办法根据GAE进行这项工作?

That's a very random restriction and most definitely a bug on the GAE. 这是一个非常随机的限制,而且绝对是GAE上的一个错误。 Please report it. 请报告。

As a background: Byte Buddy detects a JVM's version and offers features accordingly. 作为背景:Byte Buddy检测JVM的版本并相应地提供功能。 In this case, it detects that the parameter API that was added with Java 8 exists and uses it. 在这种情况下,它检测到存在并随Java 8添加了参数API。 However, GAE seems to disallow its use via reflection. 但是,GAE似乎不允许通过反射使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM