我在自己的项目中使用Truffle& Graal项目。我遇到一个问题,当我在Java中使用getDeclaredField()
时,即使字段存在,它也会返回noSuchFieldException。
代码生成异常是
Field field = NodeUtil.class.getDeclaredField("unsafeFieldOffsetProvider");
field.setAccessible(true);
字段unsafeFieldOffsetProvider
在NodeUtil.java中声明
private static final FieldOffsetProvider unsafeFieldOffsetProvider = new FieldOffsetProvider() {
@Override
public long objectFieldOffset(Field field) {
return unsafe.objectFieldOffset(field);
}
@Override
public int getTypeSize(Class<?> clazz) {
if (!clazz.isPrimitive()) {
return Unsafe.ARRAY_OBJECT_INDEX_SCALE;
} else if (clazz == int.class) {
return Unsafe.ARRAY_INT_INDEX_SCALE;
} else {
throw new UnsupportedOperationException("unsupported field type: " + clazz);
}
}
};
以下是错误消息:
Exception in thread "main" java.lang.ExceptionInInitializerError
at edu.uci.python.runtime.object.FixedObjectLayout.<init>(FixedObjectLayout.java:100)
at edu.uci.python.runtime.object.FixedObjectLayout.addAttribute(FixedObjectLayout.java:149)
at edu.uci.python.runtime.object.PythonObject.setAttribute(PythonObject.java:153)
at edu.uci.python.runtime.standardtype.PythonModule.addDefaultConstants(PythonModule.java:45)
at edu.uci.python.runtime.standardtype.PythonModule.<init>(PythonModule.java:41)
at edu.uci.python.builtins.PythonDefaultBuiltinsLookup.createModule(PythonDefaultBuiltinsLookup.java:88)
at edu.uci.python.builtins.PythonDefaultBuiltinsLookup.populateBuiltins(PythonDefaultBuiltinsLookup.java:54)
at edu.uci.python.runtime.PythonContext.<init>(PythonContext.java:79)
at python.truffle.toa.runtime.PythonContextOA.<init>(PythonContextOA.java:13)
at python.truffle.toa.main.PythonMainOA.testFile(PythonMainOA.java:78)
at python.truffle.toa.main.PythonMainOA.main(PythonMainOA.java:39)
Caused by: java.lang.RuntimeException: java.lang.NoSuchFieldException: unsafeFieldOffsetProvider
at edu.uci.python.runtime.object.ObjectLayoutUtil.unsafeFieldOffsetProvider(ObjectLayoutUtil.java:43)
at edu.uci.python.runtime.object.ObjectLayoutUtil.<clinit>(ObjectLayoutUtil.java:47)
... 11 more
Caused by: java.lang.NoSuchFieldException: unsafeFieldOffsetProvider
at java.lang.Class.getDeclaredField(Unknown Source)
at edu.uci.python.runtime.object.ObjectLayoutUtil.unsafeFieldOffsetProvider(ObjectLayoutUtil.java:39)
... 12 more
答案 0 :(得分:0)
您正在使用的松露版本中不存在该字段。
v0.6 - NodeUtil.java
v0.7 - NodeUtil.java
答案 1 :(得分:0)
最终我们发现(我和@lhan在同一个项目中)它是由Eclipse项目配置的依赖关系的错误顺序引起的...我们不得不改变依赖关系的顺序选项卡&#34;订单和导出&#34;在&#34; Java Build Path&#34;设置项目。具体来说,我们必须将com.oracle.truffle.api
和com.oracle.truffle.api.dsl
放在JRE系统库之上。