src/share/vm/prims/wbtestmethods/parserTests.cpp

Thu, 15 Mar 2012 13:37:13 +0100

author
nloodin
date
Thu, 15 Mar 2012 13:37:13 +0100
changeset 3681
51612f0c0a79
child 4037
da91efe96a93
permissions
-rw-r--r--

7148488: Whitebox tests for the Diagnostic Framework Parser
Reviewed-by: brutisso, sla, mgerdin

nloodin@3681 1 /*
nloodin@3681 2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
nloodin@3681 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
nloodin@3681 4 *
nloodin@3681 5 * This code is free software; you can redistribute it and/or modify it
nloodin@3681 6 * under the terms of the GNU General Public License version 2 only, as
nloodin@3681 7 * published by the Free Software Foundation.
nloodin@3681 8 *
nloodin@3681 9 * This code is distributed in the hope that it will be useful, but WITHOUT
nloodin@3681 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
nloodin@3681 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
nloodin@3681 12 * version 2 for more details (a copy is included in the LICENSE file that
nloodin@3681 13 * accompanied this code).
nloodin@3681 14 *
nloodin@3681 15 * You should have received a copy of the GNU General Public License version
nloodin@3681 16 * 2 along with this work; if not, write to the Free Software Foundation,
nloodin@3681 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
nloodin@3681 18 *
nloodin@3681 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
nloodin@3681 20 * or visit www.oracle.com if you need additional information or have any
nloodin@3681 21 * questions.
nloodin@3681 22 *
nloodin@3681 23 */
nloodin@3681 24
nloodin@3681 25 #include "precompiled.hpp"
nloodin@3681 26
nloodin@3681 27 #include "classfile/symbolTable.hpp"
nloodin@3681 28
nloodin@3681 29 #include "prims/jni.h"
nloodin@3681 30 #include "prims/whitebox.hpp"
nloodin@3681 31 #include "prims/wbtestmethods/parserTests.hpp"
nloodin@3681 32 #include "runtime/interfaceSupport.hpp"
nloodin@3681 33
nloodin@3681 34 #include "memory/oopFactory.hpp"
nloodin@3681 35
nloodin@3681 36 #include "services/diagnosticArgument.hpp"
nloodin@3681 37 #include "services/diagnosticFramework.hpp"
nloodin@3681 38
nloodin@3681 39 //There's no way of beforeahnd knowing an upper size
nloodin@3681 40 //Of the length of a string representation of
nloodin@3681 41 //the value of an argument.
nloodin@3681 42 #define VALUE_MAXLEN 256
nloodin@3681 43
nloodin@3681 44 // DiagnosticFramework test utility methods
nloodin@3681 45
nloodin@3681 46 /*
nloodin@3681 47 * The DiagnosticArgumentType class contains an enum that says which type
nloodin@3681 48 * this argument represents. (JLONG, BOOLEAN etc).
nloodin@3681 49 * This method Returns a char* representation of that enum value.
nloodin@3681 50 */
nloodin@3681 51 static const char* lookup_diagnosticArgumentEnum(const char* field_name, oop object) {
nloodin@3681 52 Thread* THREAD = Thread::current();
nloodin@3681 53 const char* enum_sig = "Lsun/hotspot/parser/DiagnosticCommand$DiagnosticArgumentType;";
nloodin@3681 54 TempNewSymbol enumSigSymbol = SymbolTable::lookup(enum_sig, (int) strlen(enum_sig), THREAD);
nloodin@3681 55 int offset = WhiteBox::offset_for_field(field_name, object, enumSigSymbol);
nloodin@3681 56 oop enumOop = object->obj_field(offset);
nloodin@3681 57
nloodin@3681 58 const char* ret = WhiteBox::lookup_jstring("name", enumOop);
nloodin@3681 59 return ret;
nloodin@3681 60 }
nloodin@3681 61
nloodin@3681 62 /*
nloodin@3681 63 * Takes an oop to a DiagnosticArgumentType-instance and
nloodin@3681 64 * reads the fields from it. Fills an native DCmdParser with
nloodin@3681 65 * this info.
nloodin@3681 66 */
nloodin@3681 67 static void fill_in_parser(DCmdParser* parser, oop argument)
nloodin@3681 68 {
nloodin@3681 69 const char* name = WhiteBox::lookup_jstring("name", argument);
nloodin@3681 70 const char* desc = WhiteBox::lookup_jstring("desc", argument);
nloodin@3681 71 const char* default_value = WhiteBox::lookup_jstring("defaultValue", argument);
nloodin@3681 72 bool mandatory = WhiteBox::lookup_bool("mandatory", argument);
nloodin@3681 73 const char* type = lookup_diagnosticArgumentEnum("type", argument);
nloodin@3681 74
nloodin@3681 75 if (strcmp(type, "STRING") == 0) {
nloodin@3681 76 DCmdArgument<char*>* argument = new DCmdArgument<char*>(
nloodin@3681 77 name, desc,
nloodin@3681 78 "STRING", mandatory, default_value);
nloodin@3681 79 parser->add_dcmd_option(argument);
nloodin@3681 80 } else if (strcmp(type, "NANOTIME") == 0) {
nloodin@3681 81 DCmdArgument<NanoTimeArgument>* argument = new DCmdArgument<NanoTimeArgument>(
nloodin@3681 82 name, desc,
nloodin@3681 83 "NANOTIME", mandatory, default_value);
nloodin@3681 84 parser->add_dcmd_option(argument);
nloodin@3681 85 } else if (strcmp(type, "JLONG") == 0) {
nloodin@3681 86 DCmdArgument<jlong>* argument = new DCmdArgument<jlong>(
nloodin@3681 87 name, desc,
nloodin@3681 88 "JLONG", mandatory, default_value);
nloodin@3681 89 parser->add_dcmd_option(argument);
nloodin@3681 90 } else if (strcmp(type, "BOOLEAN") == 0) {
nloodin@3681 91 DCmdArgument<bool>* argument = new DCmdArgument<bool>(
nloodin@3681 92 name, desc,
nloodin@3681 93 "BOOLEAN", mandatory, default_value);
nloodin@3681 94 parser->add_dcmd_option(argument);
nloodin@3681 95 } else if (strcmp(type, "MEMORYSIZE") == 0) {
nloodin@3681 96 DCmdArgument<MemorySizeArgument>* argument = new DCmdArgument<MemorySizeArgument>(
nloodin@3681 97 name, desc,
nloodin@3681 98 "MEMORY SIZE", mandatory, default_value);
nloodin@3681 99 parser->add_dcmd_option(argument);
nloodin@3681 100 } else if (strcmp(type, "STRINGARRAY") == 0) {
nloodin@3681 101 DCmdArgument<StringArrayArgument*>* argument = new DCmdArgument<StringArrayArgument*>(
nloodin@3681 102 name, desc,
nloodin@3681 103 "STRING SET", mandatory);
nloodin@3681 104 parser->add_dcmd_option(argument);
nloodin@3681 105 }
nloodin@3681 106 }
nloodin@3681 107
nloodin@3681 108 /*
nloodin@3681 109 * Will Fill in a java object array with alternating names of parsed command line options and
nloodin@3681 110 * the value that has been parsed for it:
nloodin@3681 111 * { name, value, name, value ... }
nloodin@3681 112 * This can then be checked from java.
nloodin@3681 113 */
nloodin@3681 114 WB_ENTRY(jobjectArray, WB_ParseCommandLine(JNIEnv* env, jobject o, jstring j_cmdline, jobjectArray arguments))
nloodin@3681 115 ResourceMark rm;
nloodin@3681 116 DCmdParser parser;
nloodin@3681 117
nloodin@3681 118 const char* c_cmdline = java_lang_String::as_utf8_string(JNIHandles::resolve(j_cmdline));
nloodin@3681 119 objArrayOop argumentArray = objArrayOop(JNIHandles::resolve_non_null(arguments));
nloodin@3681 120
nloodin@3681 121 int length = argumentArray->length();
nloodin@3681 122
nloodin@3681 123 for (int i = 0; i < length; i++) {
nloodin@3681 124 oop argument_oop = argumentArray->obj_at(i);
nloodin@3681 125 fill_in_parser(&parser, argument_oop);
nloodin@3681 126 }
nloodin@3681 127
nloodin@3681 128 CmdLine cmdline(c_cmdline, strlen(c_cmdline), true);
nloodin@3681 129 parser.parse(&cmdline,',',CHECK_NULL);
nloodin@3681 130
nloodin@3681 131 klassOop k = SystemDictionary::Object_klass();
nloodin@3681 132 objArrayOop returnvalue_array = oopFactory::new_objArray(k, parser.num_arguments() * 2, CHECK_NULL);
nloodin@3681 133
nloodin@3681 134 GrowableArray<const char *>*parsedArgNames = parser.argument_name_array();
nloodin@3681 135
nloodin@3681 136 for (int i = 0; i < parser.num_arguments(); i++) {
nloodin@3681 137 oop parsedName = java_lang_String::create_oop_from_str(parsedArgNames->at(i), CHECK_NULL);
nloodin@3681 138 returnvalue_array->obj_at_put(i*2, parsedName);
nloodin@3681 139 GenDCmdArgument* arg = parser.lookup_dcmd_option(parsedArgNames->at(i), strlen(parsedArgNames->at(i)));
nloodin@3681 140 char buf[VALUE_MAXLEN];
nloodin@3681 141 arg->value_as_str(buf, sizeof(buf));
nloodin@3681 142 oop parsedValue = java_lang_String::create_oop_from_str(buf, CHECK_NULL);
nloodin@3681 143 returnvalue_array->obj_at_put(i*2+1, parsedValue);
nloodin@3681 144 }
nloodin@3681 145
nloodin@3681 146 return (jobjectArray) JNIHandles::make_local(returnvalue_array);
nloodin@3681 147
nloodin@3681 148 WB_END

mercurial