< prev index next >

test/hotspot/gtest/runtime/test_classPrinter.cpp

Print this page

  1 /*
  2  * Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 #include "classfile/classPrinter.hpp"
 25 #include "memory/resourceArea.hpp"

 26 #include "runtime/interfaceSupport.inline.hpp"
 27 #include "runtime/javaThread.hpp"
 28 #include "utilities/ostream.hpp"
 29 #include "unittest.hpp"
 30 
 31 using testing::ContainsRegex;
 32 using testing::HasSubstr;
 33 
 34 TEST_VM(ClassPrinter, print_classes) {
 35   JavaThread* THREAD = JavaThread::current();
 36   ThreadInVMfromNative invm(THREAD);
 37   ResourceMark rm;
 38 
 39   stringStream s1;
 40   ClassPrinter::print_classes("java/lang/Object", 0x03, &s1);
 41   const char* o1 = s1.freeze();
 42 
 43   ASSERT_THAT(o1, HasSubstr("class: java/lang/Object mirror:")) << "must find java/lang/Object";
 44   ASSERT_THAT(o1, HasSubstr("method wait : (J)V")) << "must find java/lang/Object::wait";
 45   ASSERT_THAT(o1, HasSubstr("method finalize : ()V\n   0 return")) << "must find java/lang/Object::finalize and disasm";
 46 
 47   // "." should also work as separator in class name
 48   stringStream s2;
 49   ClassPrinter::print_classes("java.lang.Object", 0x03, &s2);
 50   const char* o2 = s2.freeze();
 51   ASSERT_THAT(o2, HasSubstr("class: java/lang/Object mirror:")) << "must find java/lang/Object";
 52 
 53   // 0x20 is PRINT_CLASS_DETAILS
 54   stringStream s3;
 55   ClassPrinter::print_classes("java.lang.Integer", 0x20, &s3);
 56   const char* o3 = s3.freeze();
 57   ASSERT_THAT(o3, HasSubstr("class: java/lang/Integer mirror:")) << "must find java/lang/Integer";
 58   ASSERT_THAT(o3, HasSubstr("InstanceKlass: java.lang.Integer {0x")) << "must print InstanceKlass";
 59   ASSERT_THAT(o3, HasSubstr("Java mirror oop for java/lang/Integer:")) << "must print mirror oop";
 60 #if GTEST_USES_POSIX_RE
 61   // Complex regex not available on Windows
 62   ASSERT_THAT(o3, ContainsRegex("public static final 'MIN_VALUE' 'I'.* -2147483648 [(]0x80000000[)]")) << "must print static fields";




 63 #endif
 64 }
 65 
 66 TEST_VM(ClassPrinter, print_methods) {
 67   JavaThread* THREAD = JavaThread::current();
 68   ThreadInVMfromNative invm(THREAD);
 69   ResourceMark rm;
 70 
 71   stringStream s1;
 72   ClassPrinter::print_methods("*ang/Object*", "wait", 0x1, &s1);
 73   const char* o1 = s1.freeze();
 74   ASSERT_THAT(o1, HasSubstr("class: java/lang/Object mirror:")) << "must find java/lang/Object";
 75   ASSERT_THAT(o1, HasSubstr("method wait : (J)V")) << "must find java/lang/Object::wait(long)";
 76   ASSERT_THAT(o1, HasSubstr("method wait : ()V")) << "must find java/lang/Object::wait()";
 77   ASSERT_THAT(o1, Not(HasSubstr("method finalize : ()V"))) << "must not find java/lang/Object::finalize";
 78 
 79   stringStream s2;
 80   ClassPrinter::print_methods("j*ang/Object*", "wait:(*J*)V", 0x1, &s2);
 81   const char* o2 = s2.freeze();
 82   ASSERT_THAT(o2, HasSubstr("class: java/lang/Object mirror:")) << "must find java/lang/Object";

  1 /*
  2  * Copyright (c) 2022, 2026, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 #include "classfile/classPrinter.hpp"
 25 #include "memory/resourceArea.hpp"
 26 #include "runtime/arguments.hpp"
 27 #include "runtime/interfaceSupport.inline.hpp"
 28 #include "runtime/javaThread.hpp"
 29 #include "utilities/ostream.hpp"
 30 #include "unittest.hpp"
 31 
 32 using testing::ContainsRegex;
 33 using testing::HasSubstr;
 34 
 35 TEST_VM(ClassPrinter, print_classes) {
 36   JavaThread* THREAD = JavaThread::current();
 37   ThreadInVMfromNative invm(THREAD);
 38   ResourceMark rm;
 39 
 40   stringStream s1;
 41   ClassPrinter::print_classes("java/lang/Object", 0x03, &s1);
 42   const char* o1 = s1.freeze();
 43 
 44   ASSERT_THAT(o1, HasSubstr("class: java/lang/Object mirror:")) << "must find java/lang/Object";
 45   ASSERT_THAT(o1, HasSubstr("method wait : (J)V")) << "must find java/lang/Object::wait";
 46   ASSERT_THAT(o1, HasSubstr("method finalize : ()V\n   0 return")) << "must find java/lang/Object::finalize and disasm";
 47 
 48   // "." should also work as separator in class name
 49   stringStream s2;
 50   ClassPrinter::print_classes("java.lang.Object", 0x03, &s2);
 51   const char* o2 = s2.freeze();
 52   ASSERT_THAT(o2, HasSubstr("class: java/lang/Object mirror:")) << "must find java/lang/Object";
 53 
 54   // 0x20 is PRINT_CLASS_DETAILS
 55   stringStream s3;
 56   ClassPrinter::print_classes("java.lang.Integer", 0x20, &s3);
 57   const char* o3 = s3.freeze();
 58   ASSERT_THAT(o3, HasSubstr("class: java/lang/Integer mirror:")) << "must find java/lang/Integer";
 59   ASSERT_THAT(o3, HasSubstr("InstanceKlass: java.lang.Integer {0x")) << "must print InstanceKlass";
 60   ASSERT_THAT(o3, HasSubstr("Java mirror oop for java/lang/Integer:")) << "must print mirror oop";
 61 #if GTEST_USES_POSIX_RE
 62   // Complex regex not available on Windows
 63   const char* re = Arguments::is_valhalla_enabled()
 64                        ? "public static final value 'MIN_VALUE' 'I'.* -2147483648 [(]0x80000000[)]"
 65                        : "public static final 'MIN_VALUE' 'I'.* -2147483648 [(]0x80000000[)]";
 66 
 67   ASSERT_THAT(o3, ContainsRegex(re)) << "must print static fields";
 68 #endif
 69 }
 70 
 71 TEST_VM(ClassPrinter, print_methods) {
 72   JavaThread* THREAD = JavaThread::current();
 73   ThreadInVMfromNative invm(THREAD);
 74   ResourceMark rm;
 75 
 76   stringStream s1;
 77   ClassPrinter::print_methods("*ang/Object*", "wait", 0x1, &s1);
 78   const char* o1 = s1.freeze();
 79   ASSERT_THAT(o1, HasSubstr("class: java/lang/Object mirror:")) << "must find java/lang/Object";
 80   ASSERT_THAT(o1, HasSubstr("method wait : (J)V")) << "must find java/lang/Object::wait(long)";
 81   ASSERT_THAT(o1, HasSubstr("method wait : ()V")) << "must find java/lang/Object::wait()";
 82   ASSERT_THAT(o1, Not(HasSubstr("method finalize : ()V"))) << "must not find java/lang/Object::finalize";
 83 
 84   stringStream s2;
 85   ClassPrinter::print_methods("j*ang/Object*", "wait:(*J*)V", 0x1, &s2);
 86   const char* o2 = s2.freeze();
 87   ASSERT_THAT(o2, HasSubstr("class: java/lang/Object mirror:")) << "must find java/lang/Object";
< prev index next >