1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/prims/wbtestmethods/parserTests.cpp Thu Mar 15 13:37:13 2012 +0100 1.3 @@ -0,0 +1,148 @@ 1.4 +/* 1.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 + 1.30 +#include "classfile/symbolTable.hpp" 1.31 + 1.32 +#include "prims/jni.h" 1.33 +#include "prims/whitebox.hpp" 1.34 +#include "prims/wbtestmethods/parserTests.hpp" 1.35 +#include "runtime/interfaceSupport.hpp" 1.36 + 1.37 +#include "memory/oopFactory.hpp" 1.38 + 1.39 +#include "services/diagnosticArgument.hpp" 1.40 +#include "services/diagnosticFramework.hpp" 1.41 + 1.42 +//There's no way of beforeahnd knowing an upper size 1.43 +//Of the length of a string representation of 1.44 +//the value of an argument. 1.45 +#define VALUE_MAXLEN 256 1.46 + 1.47 +// DiagnosticFramework test utility methods 1.48 + 1.49 +/* 1.50 + * The DiagnosticArgumentType class contains an enum that says which type 1.51 + * this argument represents. (JLONG, BOOLEAN etc). 1.52 + * This method Returns a char* representation of that enum value. 1.53 + */ 1.54 +static const char* lookup_diagnosticArgumentEnum(const char* field_name, oop object) { 1.55 + Thread* THREAD = Thread::current(); 1.56 + const char* enum_sig = "Lsun/hotspot/parser/DiagnosticCommand$DiagnosticArgumentType;"; 1.57 + TempNewSymbol enumSigSymbol = SymbolTable::lookup(enum_sig, (int) strlen(enum_sig), THREAD); 1.58 + int offset = WhiteBox::offset_for_field(field_name, object, enumSigSymbol); 1.59 + oop enumOop = object->obj_field(offset); 1.60 + 1.61 + const char* ret = WhiteBox::lookup_jstring("name", enumOop); 1.62 + return ret; 1.63 +} 1.64 + 1.65 +/* 1.66 + * Takes an oop to a DiagnosticArgumentType-instance and 1.67 + * reads the fields from it. Fills an native DCmdParser with 1.68 + * this info. 1.69 + */ 1.70 +static void fill_in_parser(DCmdParser* parser, oop argument) 1.71 +{ 1.72 + const char* name = WhiteBox::lookup_jstring("name", argument); 1.73 + const char* desc = WhiteBox::lookup_jstring("desc", argument); 1.74 + const char* default_value = WhiteBox::lookup_jstring("defaultValue", argument); 1.75 + bool mandatory = WhiteBox::lookup_bool("mandatory", argument); 1.76 + const char* type = lookup_diagnosticArgumentEnum("type", argument); 1.77 + 1.78 + if (strcmp(type, "STRING") == 0) { 1.79 + DCmdArgument<char*>* argument = new DCmdArgument<char*>( 1.80 + name, desc, 1.81 + "STRING", mandatory, default_value); 1.82 + parser->add_dcmd_option(argument); 1.83 + } else if (strcmp(type, "NANOTIME") == 0) { 1.84 + DCmdArgument<NanoTimeArgument>* argument = new DCmdArgument<NanoTimeArgument>( 1.85 + name, desc, 1.86 + "NANOTIME", mandatory, default_value); 1.87 + parser->add_dcmd_option(argument); 1.88 + } else if (strcmp(type, "JLONG") == 0) { 1.89 + DCmdArgument<jlong>* argument = new DCmdArgument<jlong>( 1.90 + name, desc, 1.91 + "JLONG", mandatory, default_value); 1.92 + parser->add_dcmd_option(argument); 1.93 + } else if (strcmp(type, "BOOLEAN") == 0) { 1.94 + DCmdArgument<bool>* argument = new DCmdArgument<bool>( 1.95 + name, desc, 1.96 + "BOOLEAN", mandatory, default_value); 1.97 + parser->add_dcmd_option(argument); 1.98 + } else if (strcmp(type, "MEMORYSIZE") == 0) { 1.99 + DCmdArgument<MemorySizeArgument>* argument = new DCmdArgument<MemorySizeArgument>( 1.100 + name, desc, 1.101 + "MEMORY SIZE", mandatory, default_value); 1.102 + parser->add_dcmd_option(argument); 1.103 + } else if (strcmp(type, "STRINGARRAY") == 0) { 1.104 + DCmdArgument<StringArrayArgument*>* argument = new DCmdArgument<StringArrayArgument*>( 1.105 + name, desc, 1.106 + "STRING SET", mandatory); 1.107 + parser->add_dcmd_option(argument); 1.108 + } 1.109 +} 1.110 + 1.111 +/* 1.112 + * Will Fill in a java object array with alternating names of parsed command line options and 1.113 + * the value that has been parsed for it: 1.114 + * { name, value, name, value ... } 1.115 + * This can then be checked from java. 1.116 + */ 1.117 +WB_ENTRY(jobjectArray, WB_ParseCommandLine(JNIEnv* env, jobject o, jstring j_cmdline, jobjectArray arguments)) 1.118 + ResourceMark rm; 1.119 + DCmdParser parser; 1.120 + 1.121 + const char* c_cmdline = java_lang_String::as_utf8_string(JNIHandles::resolve(j_cmdline)); 1.122 + objArrayOop argumentArray = objArrayOop(JNIHandles::resolve_non_null(arguments)); 1.123 + 1.124 + int length = argumentArray->length(); 1.125 + 1.126 + for (int i = 0; i < length; i++) { 1.127 + oop argument_oop = argumentArray->obj_at(i); 1.128 + fill_in_parser(&parser, argument_oop); 1.129 + } 1.130 + 1.131 + CmdLine cmdline(c_cmdline, strlen(c_cmdline), true); 1.132 + parser.parse(&cmdline,',',CHECK_NULL); 1.133 + 1.134 + klassOop k = SystemDictionary::Object_klass(); 1.135 + objArrayOop returnvalue_array = oopFactory::new_objArray(k, parser.num_arguments() * 2, CHECK_NULL); 1.136 + 1.137 + GrowableArray<const char *>*parsedArgNames = parser.argument_name_array(); 1.138 + 1.139 + for (int i = 0; i < parser.num_arguments(); i++) { 1.140 + oop parsedName = java_lang_String::create_oop_from_str(parsedArgNames->at(i), CHECK_NULL); 1.141 + returnvalue_array->obj_at_put(i*2, parsedName); 1.142 + GenDCmdArgument* arg = parser.lookup_dcmd_option(parsedArgNames->at(i), strlen(parsedArgNames->at(i))); 1.143 + char buf[VALUE_MAXLEN]; 1.144 + arg->value_as_str(buf, sizeof(buf)); 1.145 + oop parsedValue = java_lang_String::create_oop_from_str(buf, CHECK_NULL); 1.146 + returnvalue_array->obj_at_put(i*2+1, parsedValue); 1.147 + } 1.148 + 1.149 + return (jobjectArray) JNIHandles::make_local(returnvalue_array); 1.150 + 1.151 +WB_END