JavaCard学习笔记: CAP Component 之 Class Component

2024-04-22 11:52

本文主要是介绍JavaCard学习笔记: CAP Component 之 Class Component,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 整体结构
  • tag和size字段
  • signature_pool_length和signature_pool
  • type_descriptor结构
    • 导入类型编码
    • 导入项签名示例
      • 导入类
      • 导入数组
      • 导入远程方法
  • interfaces[]
    • interface_info结构
    • flags
    • inteface_count
    • superinterfaces
    • interface_name
  • class_info_compact classes[]
    • 结构
    • flags
    • inteface_count
    • super_class_ref
    • declared_instance_size
    • first_reference_token
    • reference_count
    • public_method_table_base
    • public_method_table_count
    • package_method_table_base
    • package_method_table_count
    • public_virtual_method_table[]
    • package_virtual_method_table[package_method_table_count]
    • interfaces[interface_count]
      • implemented_interface_info结构
    • remote_interface_info remote_interfaces
      • remote_method_info remote_methods[remote_methods_count]
        • remote_method_info
      • u1 hash_modifier[hash_modifier_length]
      • u1 class_name[class_name_length]
      • class_ref remote_interfaces[remote_interfaces_count]
    • u1 public_virtual_method_token_mapping[public_method_count]
    • CAP22_inheritable_public_method_token_count

整体结构

Class Component的紧凑格式和扩展格式结构如下,本篇笔记只记录了紧凑格式的内容。

class_component_compact { u1 tag u2 size u2 signature_pool_length (since CAP format 2.2)type_descriptor signature_pool[](since CAP format 2.2)interface_info interfaces[] class_info_compact classes[]
}class_component_extended { (since CAP format 2.3)u1 tag u2 size u2 signature_pool_length type_descriptor signature_pool[] interface_info interfaces[] class_info_extended classes[]
}

tag和size字段

所有组件开头都是这两个字段,tag表示组件类型,size表示组件大小。

signature_pool_length和signature_pool

signature_pool是一个长度为signature_pool_length个字节(注意不是数组元素个数)的type_descriptor类型数组,用以表示远程方法的签名。
我个人的理解是:在类定义的.java文件中,前面可能会出现类似importXXX的语句,signature_pool数组中的每一项就表示一个导入项的签名,签名的具体内容由【导入类型】+【位置索引】组成。如果导入的是一个方法,签名的具体内容由【导入类型】+【位置token】+【返回类型】组成。

type_descriptor结构

type_descriptor的结构定义如下

type_descriptor { (since CAP format 2.2)u1 nibble_count; u1 type[(nibble_count+1) / 2]; 
}

导入类型编码

存在11种导入类型,编码如下。
在这里插入图片描述

导入项签名示例

导入类

类和方法都属于reference类型。如果package0要导入package1中的c1类,需要6个nibble,该type_descriptor的type占3个字节,具体值如下。
在这里插入图片描述

导入数组

在这里插入图片描述

导入远程方法

在这里插入图片描述

interfaces[]

表示所有接口的信息,数组中每项用interface_info结构表示单个接口的信息。

interface_info结构

interface_info { u1 bitfield { bit[4] flags bit[4] interface_count } class_ref superinterfaces[interface_count] interface_name_info interface_name
} 

flags

这个字段接口和类通用,定义如下:
在这里插入图片描述
flags字段的最高位(0x8)置1表示是接口info,置0表示类info。0X4位置1表示共享接口或共享类,0则反之。0x2位置1表示远程接口或远程类,0则反之。

inteface_count

表示该接口的父接口数。

superinterfaces

一个class_ref类型的数组,存放该接口所有父接口的位置token。

interface_name

非远程接口该项为空。
远程接口,该项的结构如下。interface_name_length表示接口名字的长度,随后就是接口名字的内容, 以UTF-8 格式存放。

interface_name_info {u1 interface_name_lengthu1 interface_name[interface_name_length]
} 

class_info_compact classes[]

表示所有类的信息,紧凑版中每项用class_info_compact结构表示单个类的信息。

结构

class_info_compact { u1 bitfield { bit[4] flags bit[4] interface_count } class_ref super_class_ref u1 declared_instance_size u1 first_reference_token u1 reference_count u1 public_method_table_base u1 public_method_table_count u1 package_method_table_base u1 package_method_table_count u2 public_virtual_method_table[public_method_table_count] u2 package_virtual_method_table[package_method_table_count] implemented_interface_info interfaces[interface_count] remote_interface_info remote_interfaces (since CAP format 2.2)u1 public_virtual_method_token_mapping[public_method_count](since CAP format 2.3)u1 CAP22_inheritable_public_method_token_count(since CAP format 2.3)
} 

flags

这个字段接口和类通用,见上方接口flags描述。

inteface_count

表示该类实现的接口数。仅包括该类直接实现的接口数,不包括其父类实现的接口。如下例子中,c0类实现了接口i0,其inteface_count为1;c1类实现了接口i1、i2、i3,其inteface_count为3。注意,c1类没有直接实现i0,接口i0是由其父类c0实现的。

interface i0 {} 
interface i1 {} 
interface i2 extends i1 {} 
interface i3 {} 
class c0 implements i0 {} 
class c1 extends c0 implements i2, i3 {} 

super_class_ref

该类的父类。Java仅支持单继承,所以只有一个引用,不是数组。

declared_instance_size

类中声明的所有成员变量需要的内存大小,以16位为一个单位。其中,除了int类型的成员变量会占用2个16位,其它类型都只用1个16位。

first_reference_token

表示该类第一个引用变量的类型。JavaCard的引用类型有4种,如下图。但我不理解这个字段是用来做什么的= =
在这里插入图片描述

reference_count

表示该类声明的引用类型的成员变量数。

public_method_table_base

如果该类有公有虚方法,该项的值为第一个公共虚方法的token值,即接下来的共有虚方法表public_virtual_method_table[public_method_table_count]第一项的token值。
如果该类无公有虚方法,该项的值为【父类public_method_table_base值】加上【父类public_method_table_count值】。如果该类无父类,则该项值为0.

public_method_table_count

public_virtual_method_table的项数。这个字段还有一些细节,待完善,感兴趣的可以自行查阅java智能卡虚拟机规范。

package_method_table_base

如果该类有包可见的虚方法(个人理解就是没有static关键字修饰的方法),该项的值为第一个包可见虚方法的token值,即接下来的共有虚方法表package_virtual_method_table[package_method_table_count] 第一项的token值。
如果该类无包可见虚方法,该项的值为【父类package_method_table_base值】加上【父类package_method_table_count值】。如果该类无父类,则该项值为0.

package_method_table_count

package_virtual_method_table的项数。

public_virtual_method_table[]

表示该类声明或定义的所有public或protected类型的虚方法,也可能会包含父类声明或定义的方法。数组中的每一项的值代表一个方法在Method组件中相对method_info字段起始地址的偏移量。如果该方法在另一个包里,则该项的偏移量为0xff。

package_virtual_method_table[package_method_table_count]

表示所有包内可见的虚方法。个人理解就是没有static关键字修饰的虚方法。其它细节的应以应该可以参考public_virtual_method_table[].

interfaces[interface_count]

表示该类直接实现的所有接口的信息。

implemented_interface_info结构

implemented_interface_info { class_ref interface u1 count u1 index[count] 
} 

interface为接口的引用,count代表接口的实现数量,index中每一项表示一种【实现】的【虚方法】的【token值】。

remote_interface_info remote_interfaces

如果前面的flags中,ACC_REMOTE为0,则该字段为空。如果非0,表示这是一个remote接口,remote_interface_info的定义如下

remote_interface_info { (since CAP format 2.2)u1 remote_methods_countremote_method_info remote_methods[remote_methods_count]u1 hash_modifier_lengthu1 hash_modifier[hash_modifier_length]u1 class_name_lengthu1 class_name[class_name_length]u1 remote_interfaces_countclass_ref remote_interfaces[remote_interfaces_count] 
}

remote_method_info remote_methods[remote_methods_count]

remote_method_info
remote_method_info { (since CAP format 2.2)u2 remote_method_hashu2 signature_offsetu1 virtual_method_token
} 

数组的每一项包含3个信息:

  1. remote方法的哈希值,该值在同一类内是唯一的。
  2. 方法参数的类型定义,即在签名池中的偏移。
  3. 该类的远程方法的虚拟方法token。

u1 hash_modifier[hash_modifier_length]

防冲突字符串。相关定义和作用可自行百度。

u1 class_name[class_name_length]

顾名思义,这个类的名字。

class_ref remote_interfaces[remote_interfaces_count]

每一项都是一个引用,指向一个interface_info结构。

u1 public_virtual_method_token_mapping[public_method_count]

方法的token值映射,把【当前类中某个方法的token值】映射到【父类中该方法的token值】。这里可以看出来,虽然是同一个方法,但在父类和子类中的token值可能是不一样的。
特殊情况:类内定义的方法,映射入参为0xff。
举个例子,如果类C1是类C2的父类,C1中方法的映射入参为T1,C2中方法的映射入参为T2,那么有以下关系:

T1=C2.public_virtual_method_token_mapping[T2]

CAP22_inheritable_public_method_token_count

CAP22_inheritable_public_method_token_count项表示由2.2或更早版本的CAP文件中定义的子类可继承的公共或受保护的虚拟方法的数量。

这篇关于JavaCard学习笔记: CAP Component 之 Class Component的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/925708

相关文章

SpringBoot请求参数接收控制指南分享

《SpringBoot请求参数接收控制指南分享》:本文主要介绍SpringBoot请求参数接收控制指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring Boot 请求参数接收控制指南1. 概述2. 有注解时参数接收方式对比3. 无注解时接收参数默认位置

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾

SpringBoot项目中报错The field screenShot exceeds its maximum permitted size of 1048576 bytes.的问题及解决

《SpringBoot项目中报错ThefieldscreenShotexceedsitsmaximumpermittedsizeof1048576bytes.的问题及解决》这篇文章... 目录项目场景问题描述原因分析解决方案总结项目场景javascript提示:项目相关背景:项目场景:基于Spring

Spring Boot 整合 SSE的高级实践(Server-Sent Events)

《SpringBoot整合SSE的高级实践(Server-SentEvents)》SSE(Server-SentEvents)是一种基于HTTP协议的单向通信机制,允许服务器向浏览器持续发送实... 目录1、简述2、Spring Boot 中的SSE实现2.1 添加依赖2.2 实现后端接口2.3 配置超时时

Spring Boot读取配置文件的五种方式小结

《SpringBoot读取配置文件的五种方式小结》SpringBoot提供了灵活多样的方式来读取配置文件,这篇文章为大家介绍了5种常见的读取方式,文中的示例代码简洁易懂,大家可以根据自己的需要进... 目录1. 配置文件位置与加载顺序2. 读取配置文件的方式汇总方式一:使用 @Value 注解读取配置方式二

一文详解Java异常处理你都了解哪些知识

《一文详解Java异常处理你都了解哪些知识》:本文主要介绍Java异常处理的相关资料,包括异常的分类、捕获和处理异常的语法、常见的异常类型以及自定义异常的实现,文中通过代码介绍的非常详细,需要的朋... 目录前言一、什么是异常二、异常的分类2.1 受检异常2.2 非受检异常三、异常处理的语法3.1 try-

Java中的@SneakyThrows注解用法详解

《Java中的@SneakyThrows注解用法详解》:本文主要介绍Java中的@SneakyThrows注解用法的相关资料,Lombok的@SneakyThrows注解简化了Java方法中的异常... 目录前言一、@SneakyThrows 简介1.1 什么是 Lombok?二、@SneakyThrows

Java中字符串转时间与时间转字符串的操作详解

《Java中字符串转时间与时间转字符串的操作详解》Java的java.time包提供了强大的日期和时间处理功能,通过DateTimeFormatter可以轻松地在日期时间对象和字符串之间进行转换,下面... 目录一、字符串转时间(一)使用预定义格式(二)自定义格式二、时间转字符串(一)使用预定义格式(二)自

Spring 请求之传递 JSON 数据的操作方法

《Spring请求之传递JSON数据的操作方法》JSON就是一种数据格式,有自己的格式和语法,使用文本表示一个对象或数组的信息,因此JSON本质是字符串,主要负责在不同的语言中数据传递和交换,这... 目录jsON 概念JSON 语法JSON 的语法JSON 的两种结构JSON 字符串和 Java 对象互转

JAVA保证HashMap线程安全的几种方式

《JAVA保证HashMap线程安全的几种方式》HashMap是线程不安全的,这意味着如果多个线程并发地访问和修改同一个HashMap实例,可能会导致数据不一致和其他线程安全问题,本文主要介绍了JAV... 目录1. 使用 Collections.synchronizedMap2. 使用 Concurren