[杂货铺系列]-【INVOKESPECIAL/STATIC on interfaces require ASM 5】

7 篇文章 0 订阅
2 篇文章 0 订阅

猿Why碰到一个异常【INVOKESPECIAL/STATIC on interfaces require ASM 5】。
跟踪到异常抛出的类:org.springframework.asm.MethodVisitor

  /**
   * Visits a method instruction. A method instruction is an instruction that invokes a method.
   *
   * @param opcode the opcode of the type instruction to be visited. This opcode is either
   *     INVOKEVIRTUAL, INVOKESPECIAL, INVOKESTATIC or INVOKEINTERFACE.
   * @param owner the internal name of the method's owner class (see {@link
   *     Type#getInternalName()}).
   * @param name the method's name.
   * @param descriptor the method's descriptor (see {@link Type}).
   * @param isInterface if the method's owner class is an interface.
   */
  public void visitMethodInsn(
      final int opcode,
      final String owner,
      final String name,
      final String descriptor,
      final boolean isInterface) {
    if (api < Opcodes.ASM5 && (opcode & Opcodes.SOURCE_DEPRECATED) == 0) {
      if (isInterface != (opcode == Opcodes.INVOKEINTERFACE)) {
        throw new UnsupportedOperationException("INVOKESPECIAL/STATIC on interfaces requires ASM5");
      }
      visitMethodInsn(opcode, owner, name, descriptor);
      return;
    }
    if (mv != null) {
      mv.visitMethodInsn(opcode & ~Opcodes.SOURCE_MASK, owner, name, descriptor, isInterface);
    }
  }

大概意思:spring中对bean做了一些asm增强,同时会限制一些调用。例如:异常抛出点的意思是:当org.springframework.asm.MethodVisitor#api的值小于org.springframework.asm.Opcodes#ASM5的时候,interface中的static方法不允许被调用!也就是说:如果使用的是org.springframework.asm.Opcodes#ASM5之前的版本对代码做了字节码增强,那么在增强后的类中不允许出现interface中的static方法被调用的情况。

举例说明

public interface Asm4Interface{
	static void asm4Method();
}
@service
public class Asm4Service{

	public void serviceMethod(){
		Asm4Interface.asm4Method();
	}
}

如果Asm4Service所在的应用中ASM的级别小于org.springframework.asm.Opcodes#ASM5,就会触发异常抛出了!
org.springframework.asm.SpringAsmInfo#ASM_VERSION则是ASM设置的值。
org.springframework.asm.SpringAsmInfo在两个版本的值分别如下:
spring-core-3.2.9.RELEASE.jar

public final class SpringAsmInfo {
	/**
	 * The ASM version used internally throughout the framework.
	 *
	 * @see Opcodes#ASM4
	 */
	public static final int ASM_VERSION = Opcodes.ASM4;

}

spring-core-5.3.12.jar

public final class SpringAsmInfo {

	/**
	 * The ASM compatibility version for Spring's ASM visitor implementations:
	 * currently {@link Opcodes#ASM10_EXPERIMENTAL}, as of Spring Framework 5.3.
	 */
	public static final int ASM_VERSION = Opcodes.ASM10_EXPERIMENTAL;

}

另外,通过mvn命令来查看项目实际使用哪个版本的jar
mvn dependency:tree -Dincludes=:spring-context

建议

低版本的spring中,尽可能调用class的static方法,不要使用interface的static方法

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值