Scala,使用 Java 接口实现 OSGI 服务时出现 VerifyError
Scala, VerifyError when implementing OSGI service with Java interface
Java摘要classes:
public abstract class ActionProcessorExtensionPoint<A extends Action<?>> extends ExtensionPoint
public abstract class ActionProcessorExtensionBase<A extends Action<?>> extends ActionProcessorExtensionPoint<A> {
protected final Log log = LogFactory.getLog(getClass());
private final Set<Class<? extends A>> supportedClasses;
public ActionProcessorExtensionBase(Class<? extends A> actionClass) {
supportedClasses = computeClasses(actionClass);
}
}
Scala class:
@Component(immediate = true)
@Provides(specifications = Array(classOf[ActionProcessorExtensionPoint[_ <:Action[T] forSome {type T}]]))
@Instantiate
class ActionPolicyDefinitionCreateProcessorExtension(
val actionClass: Class[ActionPolicyDefinitionCreate]
) extends ActionProcessorExtensionBase[ActionPolicyDefinitionCreate](actionClass) {
def this() {
this(classOf[ActionPolicyDefinitionCreate])
}
}
错误:
[ActionPolicyDefinitionCreateProcessorExtension-0] createInstance -> The POJO constructor invocation failed : Bad type on operand stack
Exception Details:
Location:
ActionPolicyDefinitionCreateProcessorExtension.<init>(Lorg/apache/felix/ipojo/InstanceManager;Ljava/lang/Class;)V @2: invokevirtual
Reason:
Type uninitializedThis (current frame, stack[0]) is not assignable to 'ActionPolicyDefinitionCreateProcessorExtension'
Current Frame:
bci: @2
flags: { flagThisUninit }
locals: { uninitializedThis, 'org/apache/felix/ipojo/InstanceManager', 'java/lang/Class' }
stack: { uninitializedThis, 'java/lang/Class' }
Bytecode:
0x0000000: 2a2c b600 f02a 2cb7 00f2 2a2b b700 f6b1
0x0000010:
java.lang.VerifyError: Bad type on operand stack Exception Details: Location: ActionPolicyDefinitionCreateProcessorExtension.<init>(Lorg/apache/felix/ipojo/InstanceManager;Ljava/lang/Class;)V @2: invokevirtual
问题是 ActionPolicyDefinitionCreateProcessorExtension 不应该有带有参数的构造函数,因为它是一个 IPOJO 组件。解决方案是将其重写为:
@Component(immediate = true)
@Provides(specifications = Array(classOf[ActionProcessorExtensionPoint[_ <:Action[T] forSome {type T}]]))
@Instantiate
class ActionPolicyDefinitionCreateProcessorExtension
extends ActionProcessorExtensionBase[ActionPolicyDefinitionCreate](classOf[ActionPolicyDefinitionCreate]) { ... }
Java摘要classes:
public abstract class ActionProcessorExtensionPoint<A extends Action<?>> extends ExtensionPoint
public abstract class ActionProcessorExtensionBase<A extends Action<?>> extends ActionProcessorExtensionPoint<A> {
protected final Log log = LogFactory.getLog(getClass());
private final Set<Class<? extends A>> supportedClasses;
public ActionProcessorExtensionBase(Class<? extends A> actionClass) {
supportedClasses = computeClasses(actionClass);
}
}
Scala class:
@Component(immediate = true)
@Provides(specifications = Array(classOf[ActionProcessorExtensionPoint[_ <:Action[T] forSome {type T}]]))
@Instantiate
class ActionPolicyDefinitionCreateProcessorExtension(
val actionClass: Class[ActionPolicyDefinitionCreate]
) extends ActionProcessorExtensionBase[ActionPolicyDefinitionCreate](actionClass) {
def this() {
this(classOf[ActionPolicyDefinitionCreate])
}
}
错误:
[ActionPolicyDefinitionCreateProcessorExtension-0] createInstance -> The POJO constructor invocation failed : Bad type on operand stack
Exception Details:
Location:
ActionPolicyDefinitionCreateProcessorExtension.<init>(Lorg/apache/felix/ipojo/InstanceManager;Ljava/lang/Class;)V @2: invokevirtual
Reason:
Type uninitializedThis (current frame, stack[0]) is not assignable to 'ActionPolicyDefinitionCreateProcessorExtension'
Current Frame:
bci: @2
flags: { flagThisUninit }
locals: { uninitializedThis, 'org/apache/felix/ipojo/InstanceManager', 'java/lang/Class' }
stack: { uninitializedThis, 'java/lang/Class' }
Bytecode:
0x0000000: 2a2c b600 f02a 2cb7 00f2 2a2b b700 f6b1
0x0000010:
java.lang.VerifyError: Bad type on operand stack Exception Details: Location: ActionPolicyDefinitionCreateProcessorExtension.<init>(Lorg/apache/felix/ipojo/InstanceManager;Ljava/lang/Class;)V @2: invokevirtual
问题是 ActionPolicyDefinitionCreateProcessorExtension 不应该有带有参数的构造函数,因为它是一个 IPOJO 组件。解决方案是将其重写为:
@Component(immediate = true)
@Provides(specifications = Array(classOf[ActionProcessorExtensionPoint[_ <:Action[T] forSome {type T}]]))
@Instantiate
class ActionPolicyDefinitionCreateProcessorExtension
extends ActionProcessorExtensionBase[ActionPolicyDefinitionCreate](classOf[ActionPolicyDefinitionCreate]) { ... }