当前位置: 首页 > article >正文

openjdk17 C++源码是怎么给java字段赋值的

##java源码

public class OtherClass {

    public static int CONSTANT_O=9876;
    public int o=1234;


    public void dddd()
    {
        String dddd = "dddd";
        //System.out.println(dddd);
        System.out.println(dddd+CONSTANT_O);
    }

}

public int o=1234;

在openjdk17中 C++源码怎么执行这段代码的,字节码sipush ,putfield      #2

##字节码

 public OtherClass();
    descriptor: ()V
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=2, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: aload_0
         5: sipush        1234
         8: putfield      #2                  // Field o:I
        11: return
      LineNumberTable:
        line 1: 0
        line 4: 4

##C++源码

// This function is the interface to the assembly code. It returns the resolved
// cpCache entry.  This doesn't safepoint, but the helper routines safepoint.
// This function will check for redefinition!
JRT_ENTRY(void, InterpreterRuntime::resolve_from_cache(JavaThread* current, Bytecodes::Code bytecode)) {
  switch (bytecode) {
  case Bytecodes::_getstatic:
  case Bytecodes::_putstatic:
  case Bytecodes::_getfield:
  case Bytecodes::_putfield:
    resolve_get_put(current, bytecode);
    break;
  case Bytecodes::_invokevirtual:
  case Bytecodes::_invokespecial:
  case Bytecodes::_invokestatic:
  case Bytecodes::_invokeinterface:
    resolve_invoke(current, bytecode);
    break;
  case Bytecodes::_invokehandle:
    resolve_invokehandle(current);
    break;
  case Bytecodes::_invokedynamic:
    resolve_invokedynamic(current);
    break;
  default:
    fatal("unexpected bytecode: %s", Bytecodes::name(bytecode));
    break;
  }
}
JRT_END


void InterpreterRuntime::resolve_get_put(JavaThread* current, Bytecodes::Code bytecode) {
  // resolve field
  fieldDescriptor info;
  LastFrameAccessor last_frame(current);
  constantPoolHandle pool(current, last_frame.method()->constants());
  methodHandle m(current, last_frame.method());
  bool is_put    = (bytecode == Bytecodes::_putfield  || bytecode == Bytecodes::_nofast_putfield ||
                    bytecode == Bytecodes::_putstatic);
  bool is_static = (bytecode == Bytecodes::_getstatic || bytecode == Bytecodes::_putstatic);

  {
    JvmtiHideSingleStepping jhss(current);
    JavaThread* THREAD = current; // For exception macros.
    LinkResolver::resolve_field_access(info, pool, last_frame.get_index_u2_cpcache(bytecode),
                                       m, bytecode, CHECK);
  } // end JvmtiHideSingleStepping
  // std::cout << "@@@@yym%%%%field" << info.name()->as_C_string() << ":offset:" << info.offset() << std::endl;
  // check if link resolution caused cpCache to be updated
  ConstantPoolCacheEntry* cp_cache_entry = last_frame.cache_entry();
  if (cp_cache_entry->is_resolved(bytecode)) {
    // std::cout << "@@@@yym%%%%field is_resolved" << info.name()->as_C_string() << ":result:" << "true" << std::endl;
    return;
  }

  // compute auxiliary field attributes
  TosState state  = as_TosState(info.field_type());

  // Resolution of put instructions on final fields is delayed. That is required so that
  // exceptions are thrown at the correct place (when the instruction is actually invoked).
  // If we do not resolve an instruction in the current pass, leaving the put_code
  // set to zero will cause the next put instruction to the same field to reresolve.

  // Resolution of put instructions to final instance fields with invalid updates (i.e.,
  // to final instance fields with updates originating from a method different than <init>)
  // is inhibited. A putfield instruction targeting an instance final field must throw
  // an IllegalAccessError if the instruction is not in an instance
  // initializer method <init>. If resolution were not inhibited, a putfield
  // in an initializer method could be resolved in the initializer. Subsequent
  // putfield instructions to the same field would then use cached information.
  // As a result, those instructions would not pass through the VM. That is,
  // checks in resolve_field_access() would not be executed for those instructions
  // and the required IllegalAccessError would not be thrown.
  //
  // Also, we need to delay resolving getstatic and putstatic instructions until the
  // class is initialized.  This is required so that access to the static
  // field will call the initialization function every time until the class
  // is completely initialized ala. in 2.17.5 in JVM Specification.
  InstanceKlass* klass = info.field_holder();
  bool uninitialized_static = is_static && !klass->is_initialized();
  bool has_initialized_final_update = info.field_holder()->major_version() >= 53 &&
                                      info.has_initialized_final_update();
  assert(!(has_initialized_final_update && !info.access_flags().is_final()), "Fields with initialized final updates must be final");

  Bytecodes::Code get_code = (Bytecodes::Code)0;
  Bytecodes::Code put_code = (Bytecodes::Code)0;
  if (!uninitialized_static) {
    get_code = ((is_static) ? Bytecodes::_getstatic : Bytecodes::_getfield);
    if ((is_put && !has_initialized_final_update) || !info.access_flags().is_final()) {
      put_code = ((is_static) ? Bytecodes::_putstatic : Bytecodes::_putfield);
    }
  }
  // std::cout << "@@@@yym%%%%field" << info.name()->as_C_string() << ":offset:" << info.offset() << std::endl;
  // std::string str1 = "o";
  // const char* cStr = info.name()->as_klass_external_name();  
  // std::string str2(cStr); // 使用构造函数进行转换
  // if (str1.compare(str2) == 0) {
  //     std::cout << "field name The strings are equal." << std::endl;
  //     std::cout << "@@@@yym%%%%field" << info.name()->as_C_string() << ":offset:" << info.offset() << std::endl;
  // }
  // std::string str3 = "CONSTANT_O";
  // const char* cStr1 = info.name()->as_klass_external_name();  
  // std::string str4(cStr1); // 使用构造函数进行转换
  // if (str3.compare(str4) == 0) {
  //     std::cout << "CONSTANT_O name The strings are equal." << std::endl;
  //     std::cout << "@@@@yym%%%%field" << info.name()->as_C_string() << ":offset:" << info.offset() << std::endl;
  // }
  cp_cache_entry->set_field(
    get_code,
    put_code,
    info.field_holder(),
    info.index(),
    info.offset(),
    state,
    info.access_flags().is_final(),
    info.access_flags().is_volatile()
  );
}


http://www.kler.cn/a/372203.html

相关文章:

  • 【马来西亚理工大学主办,ACM出版】2025年大数据、通信技术与计算机应用国际学术会议(BDCTA 2025)
  • 推动多语言语音科技迈向新高度:INTERSPEECH 2025 ML-SUPERB 2.0 挑战赛
  • 机器人技术:ModbusTCP转CCLINKIE网关应用
  • 如何使用进度条来显示QFle读取文件进度
  • selenium合集
  • Boost.Asio 同步读写及客户端 - 服务器实现详解
  • 每天10个vue面试题(四)
  • 钉钉与金蝶云星空数据集成:提高企业付款申请单处理效率
  • 轻松完成大量视频制作任务,视频剪辑高手软件的顺时针和逆时针90度功能大揭秘,一键实现大量视频的批量剪辑
  • Python+Selenium+Pytest+POM自动化测试框架封装(完整版)
  • 如何使用python来分析消费者行为?
  • 3D点云与2D图像的相互转换:2D图像对应像素的坐标 转为3D空间的对应坐标
  • 【大模型之Graph RAG系列之一】由谷歌搜索的演进看知识图谱如何改进RAG技术
  • MySQL数据类型——针对实习面试
  • Nginx 配置基于IP 地址的 Web 服务器
  • 「Mac畅玩鸿蒙与硬件13」鸿蒙UI组件篇3 - TextInput 组件获取用户输入
  • selenium学习日记
  • Elasticsearch 安装教程:驾驭数据海洋的星际导航仪
  • [快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。...
  • 建筑行业内部知识库的重要性与实施策略
  • 在MySQL中存储IP地址的最佳实践
  • 四、鸿蒙开发-常用布局(线性布局、层叠布局、弹性布局、网格布局、列表布局)
  • kubeadm部署安装
  • SOLID - 依赖倒置原则(Dependency Inversion Principle)
  • 流媒体协议.之(RTP,RTCP,RTSP,RTMP,HTTP)(二)
  • 如何使用python完成时间序列的数据分析?