alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (jvmtiClassFileReconstituter.hpp)

This example Java source code file (jvmtiClassFileReconstituter.hpp) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

annotationarray, jvmticonstantpoolreconstituter, new_resource_array, null, share_vm_prims_jvmticlassfilereconstituter_hpp, stackobj, symbolhashmap, thread, thread\:\:current

The jvmtiClassFileReconstituter.hpp Java example source code

/*
 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

#ifndef SHARE_VM_PRIMS_JVMTICLASSFILERECONSTITUTER_HPP
#define SHARE_VM_PRIMS_JVMTICLASSFILERECONSTITUTER_HPP

#include "jvmtifiles/jvmtiEnv.hpp"


class JvmtiConstantPoolReconstituter : public StackObj {
 private:
  int                  _cpool_size;
  SymbolHashMap*       _symmap;
  SymbolHashMap*       _classmap;
  constantPoolHandle   _cpool;
  instanceKlassHandle  _ikh;
  jvmtiError           _err;

 protected:
  instanceKlassHandle  ikh()     { return _ikh; };
  constantPoolHandle   cpool()   { return _cpool; };

  u2 symbol_to_cpool_index(Symbol* sym) {
    return _symmap->symbol_to_value(sym);
  }

  u2 class_symbol_to_cpool_index(Symbol* sym) {
    return _classmap->symbol_to_value(sym);
  }

 public:
  // Calls to this constructor must be proceeded by a ResourceMark
  // and a HandleMark
  JvmtiConstantPoolReconstituter(instanceKlassHandle ikh){
    set_error(JVMTI_ERROR_NONE);
    _ikh = ikh;
    _cpool = constantPoolHandle(Thread::current(), ikh->constants());
    _symmap = new SymbolHashMap();
    _classmap = new SymbolHashMap();
    _cpool_size = _cpool->hash_entries_to(_symmap, _classmap);
    if (_cpool_size == 0) {
      set_error(JVMTI_ERROR_OUT_OF_MEMORY);
    } else if (_cpool_size < 0) {
      set_error(JVMTI_ERROR_INTERNAL);
    }
  }

  ~JvmtiConstantPoolReconstituter() {
    if (_symmap != NULL) {
      os::free(_symmap, mtClass);
      _symmap = NULL;
    }
    if (_classmap != NULL) {
      os::free(_classmap, mtClass);
      _classmap = NULL;
    }
  }


  void       set_error(jvmtiError err)    { _err = err; }
  jvmtiError get_error()                  { return _err; }

  int cpool_size()                        { return _cpool_size; }

  void copy_cpool_bytes(unsigned char *cpool_bytes) {
    if (cpool_bytes == NULL) {
      assert(cpool_bytes != NULL, "cpool_bytes pointer must not be NULL");
      return;
    }
    cpool()->copy_cpool_bytes(cpool_size(), _symmap, cpool_bytes);
  }
};


class JvmtiClassFileReconstituter : public JvmtiConstantPoolReconstituter {
 private:
  size_t               _buffer_size;
  u1*                  _buffer;
  u1*                  _buffer_ptr;
  Thread*              _thread;

  enum {
    // initial size should be power of two
    initial_buffer_size = 1024
  };

  inline Thread* thread() { return _thread; }

  void write_class_file_format();
  void write_field_infos();
  void write_method_infos();
  void write_method_info(methodHandle method);
  void write_code_attribute(methodHandle method);
  void write_exceptions_attribute(ConstMethod* const_method);
  void write_synthetic_attribute();
  void write_class_attributes();
  void write_source_file_attribute();
  void write_source_debug_extension_attribute();
  u2 line_number_table_entries(methodHandle method);
  void write_line_number_table_attribute(methodHandle method, u2 num_entries);
  void write_local_variable_table_attribute(methodHandle method, u2 num_entries);
  void write_local_variable_type_table_attribute(methodHandle method, u2 num_entries);
  void write_stackmap_table_attribute(methodHandle method, int stackmap_table_len);
  u2 inner_classes_attribute_length();
  void write_inner_classes_attribute(int length);
  void write_signature_attribute(u2 generic_signaure_index);
  void write_attribute_name_index(const char* name);
  void write_annotations_attribute(const char* attr_name, AnnotationArray* annos);
  void write_bootstrapmethod_attribute();

  address writeable_address(size_t size);
  void write_u1(u1 x);
  void write_u2(u2 x);
  void write_u4(u4 x);
  void write_u8(u8 x);

 public:
  // Calls to this constructor must be proceeded by a ResourceMark
  // and a HandleMark
  JvmtiClassFileReconstituter(instanceKlassHandle ikh) :
                                      JvmtiConstantPoolReconstituter(ikh) {
    _buffer_size = initial_buffer_size;
    _buffer = _buffer_ptr = NEW_RESOURCE_ARRAY(u1, _buffer_size);
    _thread = Thread::current();
    write_class_file_format();
  };

  size_t class_file_size()    { return _buffer_ptr - _buffer; }

  u1* class_file_bytes()      { return _buffer; }

  static void copy_bytecodes(methodHandle method, unsigned char* bytecodes);
};

#endif // SHARE_VM_PRIMS_JVMTICLASSFILERECONSTITUTER_HPP

Other Java examples (source code examples)

Here is a short list of links related to this Java jvmtiClassFileReconstituter.hpp source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.