1 /* 2 * Copyright (c) 2011, 2018, 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 25 package org.graalvm.compiler.hotspot; 26 27 import static jdk.vm.ci.common.InitTimer.timer; 28 import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.runtime; 29 import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC; 30 import static org.graalvm.compiler.core.common.GraalOptions.HotSpotPrintInlining; 31 32 import java.io.PrintStream; 33 import java.util.ArrayList; 34 import java.util.EnumMap; 35 import java.util.List; 36 import java.util.Map; 37 import java.util.concurrent.atomic.AtomicReference; 38 39 import jdk.internal.vm.compiler.collections.EconomicMap; 40 import jdk.internal.vm.compiler.collections.EconomicSet; 41 import jdk.internal.vm.compiler.collections.Equivalence; 42 import jdk.internal.vm.compiler.collections.UnmodifiableMapCursor; 43 import org.graalvm.compiler.api.replacements.SnippetReflectionProvider; 44 import org.graalvm.compiler.api.runtime.GraalRuntime; 45 import org.graalvm.compiler.core.CompilationWrapper.ExceptionAction; 46 import org.graalvm.compiler.core.common.CompilationIdentifier; 47 import org.graalvm.compiler.core.common.GraalOptions; 48 import org.graalvm.compiler.core.common.spi.ForeignCallsProvider; 49 import org.graalvm.compiler.core.target.Backend; 50 import org.graalvm.compiler.debug.DebugContext; 51 import org.graalvm.compiler.debug.DebugContext.Description; 52 import org.graalvm.compiler.debug.DebugHandlersFactory; 53 import org.graalvm.compiler.debug.DebugOptions; 54 import org.graalvm.compiler.debug.DiagnosticsOutputDirectory; 55 import org.graalvm.compiler.debug.GlobalMetrics; 56 import org.graalvm.compiler.debug.GraalError; 57 import org.graalvm.compiler.debug.TTY; 58 import org.graalvm.compiler.hotspot.CompilationStatistics.Options; 59 import org.graalvm.compiler.hotspot.CompilerConfigurationFactory.BackendMap; 60 import org.graalvm.compiler.hotspot.debug.BenchmarkCounters; 61 import org.graalvm.compiler.hotspot.meta.HotSpotProviders; 62 import org.graalvm.compiler.nodes.spi.StampProvider; 63 import org.graalvm.compiler.options.EnumOptionKey; 64 import org.graalvm.compiler.options.OptionDescriptor; 65 import org.graalvm.compiler.options.OptionDescriptors; 66 import org.graalvm.compiler.options.OptionKey; 67 import org.graalvm.compiler.options.OptionValues; 68 import org.graalvm.compiler.options.OptionsParser; 69 import org.graalvm.compiler.phases.tiers.CompilerConfiguration; 70 import org.graalvm.compiler.replacements.SnippetCounter; 71 import org.graalvm.compiler.replacements.SnippetCounter.Group; 72 import org.graalvm.compiler.runtime.RuntimeProvider; 73 import org.graalvm.compiler.serviceprovider.GraalServices; 74 75 import jdk.vm.ci.code.Architecture; 76 import jdk.vm.ci.code.stack.StackIntrospection; 77 import jdk.vm.ci.common.InitTimer; 78 import jdk.vm.ci.hotspot.HotSpotCompilationRequest; 79 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime; 80 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; 81 import jdk.vm.ci.hotspot.HotSpotResolvedJavaType; 82 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType; 83 import jdk.vm.ci.hotspot.HotSpotVMConfigStore; 84 import jdk.vm.ci.meta.JavaKind; 85 import jdk.vm.ci.meta.MetaAccessProvider; 86 import jdk.vm.ci.meta.ResolvedJavaMethod; 87 import jdk.vm.ci.meta.ResolvedJavaType; 88 import jdk.vm.ci.runtime.JVMCI; 89 import jdk.vm.ci.runtime.JVMCIBackend; 90 91 //JaCoCo Exclude 92 93 /** 94 * Singleton class holding the instance of the {@link GraalRuntime}. 95 */ 96 public final class HotSpotGraalRuntime implements HotSpotGraalRuntimeProvider { 97 98 private static boolean checkArrayIndexScaleInvariants(MetaAccessProvider metaAccess) { 99 assert metaAccess.getArrayIndexScale(JavaKind.Byte) == 1; 100 assert metaAccess.getArrayIndexScale(JavaKind.Boolean) == 1; 101 assert metaAccess.getArrayIndexScale(JavaKind.Char) == 2; 102 assert metaAccess.getArrayIndexScale(JavaKind.Short) == 2; 103 assert metaAccess.getArrayIndexScale(JavaKind.Int) == 4; 104 assert metaAccess.getArrayIndexScale(JavaKind.Long) == 8; 105 assert metaAccess.getArrayIndexScale(JavaKind.Float) == 4; 106 assert metaAccess.getArrayIndexScale(JavaKind.Double) == 8; 107 return true; 108 } 109 110 private final String runtimeName; 111 private final String compilerConfigurationName; 112 private final HotSpotBackend hostBackend; 113 private final GlobalMetrics metricValues = new GlobalMetrics(); 114 private final List<SnippetCounter.Group> snippetCounterGroups; 115 private final HotSpotGC garbageCollector; 116 117 private final EconomicMap<Class<? extends Architecture>, HotSpotBackend> backends = EconomicMap.create(Equivalence.IDENTITY); 118 119 private final GraalHotSpotVMConfig config; 120 121 /** 122 * The options can be {@linkplain #setOptionValues(String[], String[]) updated} by external 123 * interfaces such as JMX. This comes with the risk that inconsistencies can arise as an 124 * {@link OptionValues} object can be cached by various parts of Graal instead of always 125 * obtaining them from this object. However, concurrent updates are never lost. 126 */ 127 private AtomicReference<OptionValues> optionsRef = new AtomicReference<>(); 128 129 private final HotSpotGraalCompiler compiler; 130 131 private final DiagnosticsOutputDirectory outputDirectory; 132 private final Map<ExceptionAction, Integer> compilationProblemsPerAction; 133 134 /** 135 * @param nameQualifier a qualifier to be added to this runtime's {@linkplain #getName() name} 136 * @param compilerConfigurationFactory factory for the compiler configuration 137 * {@link CompilerConfigurationFactory#selectFactory(String, OptionValues)} 138 */ 139 @SuppressWarnings("try") 140 HotSpotGraalRuntime(String nameQualifier, HotSpotJVMCIRuntime jvmciRuntime, CompilerConfigurationFactory compilerConfigurationFactory, OptionValues initialOptions) { 141 this.runtimeName = getClass().getSimpleName() + ":" + nameQualifier; 142 HotSpotVMConfigStore store = jvmciRuntime.getConfigStore(); 143 config = GeneratePIC.getValue(initialOptions) ? new AOTGraalHotSpotVMConfig(store) : new GraalHotSpotVMConfig(store); 144 145 // Only set HotSpotPrintInlining if it still has its default value (false). 146 if (GraalOptions.HotSpotPrintInlining.getValue(initialOptions) == false && config.printInlining) { 147 optionsRef.set(new OptionValues(initialOptions, HotSpotPrintInlining, true)); 148 } else { 149 optionsRef.set(initialOptions); 150 } 151 OptionValues options = optionsRef.get(); 152 153 garbageCollector = getSelectedGC(); 154 155 outputDirectory = new DiagnosticsOutputDirectory(options); 156 compilationProblemsPerAction = new EnumMap<>(ExceptionAction.class); 157 snippetCounterGroups = GraalOptions.SnippetCounters.getValue(options) ? new ArrayList<>() : null; 158 CompilerConfiguration compilerConfiguration = compilerConfigurationFactory.createCompilerConfiguration(); 159 compilerConfigurationName = compilerConfigurationFactory.getName(); 160 161 compiler = new HotSpotGraalCompiler(jvmciRuntime, this, options); 162 management = GraalServices.loadSingle(HotSpotGraalManagementRegistration.class, false); 163 if (management != null) { 164 management.initialize(this); 165 } 166 167 BackendMap backendMap = compilerConfigurationFactory.createBackendMap(); 168 169 JVMCIBackend hostJvmciBackend = jvmciRuntime.getHostJVMCIBackend(); 170 Architecture hostArchitecture = hostJvmciBackend.getTarget().arch; 171 try (InitTimer t = timer("create backend:", hostArchitecture)) { 172 HotSpotBackendFactory factory = backendMap.getBackendFactory(hostArchitecture); 173 if (factory == null) { 174 throw new GraalError("No backend available for host architecture \"%s\"", hostArchitecture); 175 } 176 hostBackend = registerBackend(factory.createBackend(this, compilerConfiguration, jvmciRuntime, null)); 177 } 178 179 for (JVMCIBackend jvmciBackend : jvmciRuntime.getJVMCIBackends().values()) { 180 if (jvmciBackend == hostJvmciBackend) { 181 continue; 182 } 183 184 Architecture gpuArchitecture = jvmciBackend.getTarget().arch; 185 HotSpotBackendFactory factory = backendMap.getBackendFactory(gpuArchitecture); 186 if (factory == null) { 187 throw new GraalError("No backend available for specified GPU architecture \"%s\"", gpuArchitecture); 188 } 189 try (InitTimer t = timer("create backend:", gpuArchitecture)) { 190 registerBackend(factory.createBackend(this, compilerConfiguration, null, hostBackend)); 191 } 192 } 193 194 // Complete initialization of backends 195 try (InitTimer st = timer(hostBackend.getTarget().arch.getName(), ".completeInitialization")) { 196 hostBackend.completeInitialization(jvmciRuntime, options); 197 } 198 for (HotSpotBackend backend : backends.getValues()) { 199 if (backend != hostBackend) { 200 try (InitTimer st = timer(backend.getTarget().arch.getName(), ".completeInitialization")) { 201 backend.completeInitialization(jvmciRuntime, options); 202 } 203 } 204 } 205 206 BenchmarkCounters.initialize(jvmciRuntime, options); 207 208 assert checkArrayIndexScaleInvariants(hostJvmciBackend.getMetaAccess()); 209 210 runtimeStartTime = System.nanoTime(); 211 bootstrapJVMCI = config.getFlag("BootstrapJVMCI", Boolean.class); 212 } 213 214 /** 215 * Constants denoting the GC algorithms available in HotSpot. 216 */ 217 public enum HotSpotGC { 218 // Supported GCs 219 Serial(true, "UseSerialGC"), 220 Parallel(true, "UseParallelGC", "UseParallelOldGC", "UseParNewGC"), 221 CMS(true, "UseConcMarkSweepGC"), 222 G1(true, "UseG1GC"), 223 224 // Unsupported GCs 225 Epsilon(false, "UseEpsilonGC"), 226 Z(false, "UseZGC"); 227 228 HotSpotGC(boolean supported, String... flags) { 229 this.supported = supported; 230 this.flags = flags; 231 } 232 233 final boolean supported; 234 private final String[] flags; 235 236 public boolean isSelected(GraalHotSpotVMConfig config) { 237 for (String flag : flags) { 238 final boolean notPresent = false; 239 if (config.getFlag(flag, Boolean.class, notPresent)) { 240 return true; 241 } 242 } 243 return false; 244 } 245 246 } 247 248 private HotSpotGC getSelectedGC() throws GraalError { 249 for (HotSpotGC gc : HotSpotGC.values()) { 250 if (gc.isSelected(config)) { 251 if (!gc.supported) { 252 throw new GraalError(gc.name() + " garbage collector is not supported by Graal"); 253 } 254 return gc; 255 } 256 } 257 // As of JDK 9, exactly one GC flag is guaranteed to be selected. 258 // On JDK 8, the default GC is Serial when no GC flag is true. 259 return HotSpotGC.Serial; 260 } 261 262 private HotSpotBackend registerBackend(HotSpotBackend backend) { 263 Class<? extends Architecture> arch = backend.getTarget().arch.getClass(); 264 HotSpotBackend oldValue = backends.put(arch, backend); 265 assert oldValue == null : "cannot overwrite existing backend for architecture " + arch.getSimpleName(); 266 return backend; 267 } 268 269 @Override 270 public HotSpotProviders getHostProviders() { 271 return getHostBackend().getProviders(); 272 } 273 274 @Override 275 public GraalHotSpotVMConfig getVMConfig() { 276 return config; 277 } 278 279 @Override 280 public DebugContext openDebugContext(OptionValues compilationOptions, CompilationIdentifier compilationId, Object compilable, Iterable<DebugHandlersFactory> factories, PrintStream logStream) { 281 if (management != null && management.poll(false) != null) { 282 if (compilable instanceof HotSpotResolvedJavaMethod) { 283 HotSpotResolvedObjectType type = ((HotSpotResolvedJavaMethod) compilable).getDeclaringClass(); 284 if (type instanceof HotSpotResolvedJavaType) { 285 Class<?> clazz = runtime().getMirror(type); 286 try { 287 ClassLoader cl = clazz.getClassLoader(); 288 if (cl != null) { 289 loaders.add(cl); 290 } 291 } catch (SecurityException e) { 292 // This loader can obviously not be used for resolving class names 293 } 294 } 295 } 296 } 297 Description description = new Description(compilable, compilationId.toString(CompilationIdentifier.Verbosity.ID)); 298 return DebugContext.create(compilationOptions, description, metricValues, logStream, factories); 299 } 300 301 @Override 302 public OptionValues getOptions() { 303 return optionsRef.get(); 304 } 305 306 @Override 307 public Group createSnippetCounterGroup(String groupName) { 308 if (snippetCounterGroups != null) { 309 Group group = new Group(groupName); 310 snippetCounterGroups.add(group); 311 return group; 312 } 313 return null; 314 } 315 316 @Override 317 public String getName() { 318 return runtimeName; 319 } 320 321 @SuppressWarnings("unchecked") 322 @Override 323 public <T> T getCapability(Class<T> clazz) { 324 if (clazz == RuntimeProvider.class) { 325 return (T) this; 326 } else if (clazz == OptionValues.class) { 327 return (T) optionsRef.get(); 328 } else if (clazz == StackIntrospection.class) { 329 return (T) this; 330 } else if (clazz == SnippetReflectionProvider.class) { 331 return (T) getHostProviders().getSnippetReflection(); 332 } else if (clazz == GraalHotSpotVMConfig.class) { 333 return (T) getVMConfig(); 334 } else if (clazz == StampProvider.class) { 335 return (T) getHostProviders().getStampProvider(); 336 } else if (ForeignCallsProvider.class.isAssignableFrom(clazz)) { 337 return (T) getHostProviders().getForeignCalls(); 338 } 339 return null; 340 } 341 342 public HotSpotGC getGarbageCollector() { 343 return garbageCollector; 344 } 345 346 @Override 347 public HotSpotBackend getHostBackend() { 348 return hostBackend; 349 } 350 351 @Override 352 public <T extends Architecture> Backend getBackend(Class<T> arch) { 353 assert arch != Architecture.class; 354 return backends.get(arch); 355 } 356 357 @Override 358 public String getCompilerConfigurationName() { 359 return compilerConfigurationName; 360 } 361 362 private long runtimeStartTime; 363 private boolean shutdown; 364 365 /** 366 * Take action related to entering a new execution phase. 367 * 368 * @param phase the execution phase being entered 369 */ 370 void phaseTransition(String phase) { 371 if (Options.UseCompilationStatistics.getValue(optionsRef.get())) { 372 CompilationStatistics.clear(phase); 373 } 374 } 375 376 void shutdown() { 377 shutdown = true; 378 metricValues.print(optionsRef.get()); 379 380 phaseTransition("final"); 381 382 if (snippetCounterGroups != null) { 383 for (Group group : snippetCounterGroups) { 384 TTY.out().out().println(group); 385 } 386 } 387 BenchmarkCounters.shutdown(runtime(), optionsRef.get(), runtimeStartTime); 388 389 outputDirectory.close(); 390 } 391 392 void clearMetrics() { 393 metricValues.clear(); 394 } 395 396 private final boolean bootstrapJVMCI; 397 private boolean bootstrapFinished; 398 399 public void notifyBootstrapFinished() { 400 bootstrapFinished = true; 401 } 402 403 @Override 404 public boolean isBootstrapping() { 405 return bootstrapJVMCI && !bootstrapFinished; 406 } 407 408 @Override 409 public boolean isShutdown() { 410 return shutdown; 411 } 412 413 @Override 414 public DiagnosticsOutputDirectory getOutputDirectory() { 415 return outputDirectory; 416 } 417 418 @Override 419 public Map<ExceptionAction, Integer> getCompilationProblemsPerAction() { 420 return compilationProblemsPerAction; 421 } 422 423 // ------- Management interface --------- 424 425 private final HotSpotGraalManagementRegistration management; 426 427 /** 428 * @returns the management object for this runtime or {@code null} 429 */ 430 public HotSpotGraalManagementRegistration getManagement() { 431 return management; 432 } 433 434 /** 435 * Set of weak references to {@link ClassLoader}s available for resolving class names present in 436 * management {@linkplain #invokeManagementAction(String, Object[]) action} arguments. 437 */ 438 private final WeakClassLoaderSet loaders = new WeakClassLoaderSet(ClassLoader.getSystemClassLoader()); 439 440 /** 441 * Sets or updates this object's {@linkplain #getOptions() options} from {@code names} and 442 * {@code values}. 443 * 444 * @param values the values to set. The empty string represents {@code null} which resets an 445 * option to its default value. For string type options, a non-empty value must be 446 * enclosed in double quotes. 447 * @return an array of Strings where the element at index i is {@code names[i]} if setting the 448 * denoted option succeeded, {@code null} if the option is unknown otherwise an error 449 * message describing the failure to set the option 450 */ 451 public String[] setOptionValues(String[] names, String[] values) { 452 EconomicMap<String, OptionDescriptor> optionDescriptors = getOptionDescriptors(); 453 EconomicMap<OptionKey<?>, Object> newValues = EconomicMap.create(names.length); 454 EconomicSet<OptionKey<?>> resetValues = EconomicSet.create(names.length); 455 String[] result = new String[names.length]; 456 for (int i = 0; i < names.length; i++) { 457 String name = names[i]; 458 OptionDescriptor option = optionDescriptors.get(name); 459 if (option != null) { 460 String svalue = values[i]; 461 Class<?> optionValueType = option.getOptionValueType(); 462 OptionKey<?> optionKey = option.getOptionKey(); 463 if (svalue == null || svalue.isEmpty() && !(optionKey instanceof EnumOptionKey)) { 464 resetValues.add(optionKey); 465 result[i] = name; 466 } else { 467 String valueToParse; 468 if (optionValueType == String.class) { 469 if (svalue.length() < 2 || svalue.charAt(0) != '"' || svalue.charAt(svalue.length() - 1) != '"') { 470 result[i] = "Invalid value for String option '" + name + "': must be the empty string or be enclosed in double quotes: " + svalue; 471 continue; 472 } else { 473 valueToParse = svalue.substring(1, svalue.length() - 1); 474 } 475 } else { 476 valueToParse = svalue; 477 } 478 try { 479 OptionsParser.parseOption(name, valueToParse, newValues, OptionsParser.getOptionsLoader()); 480 result[i] = name; 481 } catch (IllegalArgumentException e) { 482 result[i] = e.getMessage(); 483 continue; 484 } 485 } 486 } else { 487 result[i] = null; 488 } 489 } 490 491 OptionValues currentOptions; 492 OptionValues newOptions; 493 do { 494 currentOptions = optionsRef.get(); 495 UnmodifiableMapCursor<OptionKey<?>, Object> cursor = currentOptions.getMap().getEntries(); 496 while (cursor.advance()) { 497 OptionKey<?> key = cursor.getKey(); 498 if (!resetValues.contains(key) && !newValues.containsKey(key)) { 499 newValues.put(key, OptionValues.decodeNull(cursor.getValue())); 500 } 501 } 502 newOptions = new OptionValues(newValues); 503 } while (!optionsRef.compareAndSet(currentOptions, newOptions)); 504 505 return result; 506 } 507 508 /** 509 * Gets the values for the options corresponding to {@code names} encoded as strings. The empty 510 * string represents {@code null}. For string type options, non-{@code null} values will be 511 * enclosed in double quotes. 512 * 513 * @param names a list of option names 514 * @return the values for each named option. If an element in {@code names} does not denote an 515 * existing option, the corresponding element in the returned array will be {@code null} 516 */ 517 public String[] getOptionValues(String... names) { 518 String[] values = new String[names.length]; 519 EconomicMap<String, OptionDescriptor> optionDescriptors = getOptionDescriptors(); 520 for (int i = 0; i < names.length; i++) { 521 OptionDescriptor option = optionDescriptors.get(names[i]); 522 if (option != null) { 523 OptionKey<?> optionKey = option.getOptionKey(); 524 Object value = optionKey.getValue(getOptions()); 525 String svalue; 526 if (option.getOptionValueType() == String.class && value != null) { 527 svalue = "\"" + value + "\""; 528 } else if (value == null) { 529 svalue = ""; 530 } else { 531 svalue = String.valueOf(value); 532 } 533 values[i] = svalue; 534 } else { 535 // null denotes the option does not exist 536 values[i] = null; 537 } 538 } 539 return values; 540 } 541 542 private static EconomicMap<String, OptionDescriptor> getOptionDescriptors() { 543 EconomicMap<String, OptionDescriptor> result = EconomicMap.create(); 544 for (OptionDescriptors set : OptionsParser.getOptionsLoader()) { 545 for (OptionDescriptor option : set) { 546 result.put(option.getName(), option); 547 } 548 } 549 return result; 550 } 551 552 private void dumpMethod(String className, String methodName, String filter, String host, int port) throws Exception { 553 EconomicSet<ClassNotFoundException> failures = EconomicSet.create(); 554 EconomicSet<Class<?>> found = loaders.resolve(className, failures); 555 if (found.isEmpty()) { 556 ClassNotFoundException cause = failures.isEmpty() ? new ClassNotFoundException(className) : failures.iterator().next(); 557 throw new Exception("Cannot find class " + className + " to schedule recompilation", cause); 558 } 559 for (Class<?> clazz : found) { 560 ResolvedJavaType type = JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess().lookupJavaType(clazz); 561 for (ResolvedJavaMethod method : type.getDeclaredMethods()) { 562 if (methodName.equals(method.getName()) && method instanceof HotSpotResolvedJavaMethod) { 563 HotSpotResolvedJavaMethod hotSpotMethod = (HotSpotResolvedJavaMethod) method; 564 dumpMethod(hotSpotMethod, filter, host, port); 565 } 566 } 567 } 568 } 569 570 private void dumpMethod(HotSpotResolvedJavaMethod hotSpotMethod, String filter, String host, int port) throws Exception { 571 EconomicMap<OptionKey<?>, Object> extra = EconomicMap.create(); 572 extra.put(DebugOptions.Dump, filter); 573 extra.put(DebugOptions.PrintGraphHost, host); 574 extra.put(DebugOptions.PrintBinaryGraphPort, port); 575 OptionValues compileOptions = new OptionValues(getOptions(), extra); 576 compiler.compileMethod(new HotSpotCompilationRequest(hotSpotMethod, -1, 0L), false, compileOptions); 577 } 578 579 public Object invokeManagementAction(String actionName, Object[] params) throws Exception { 580 if ("dumpMethod".equals(actionName)) { 581 if (params.length != 0 && params[0] instanceof HotSpotResolvedJavaMethod) { 582 HotSpotResolvedJavaMethod method = param(params, 0, "method", HotSpotResolvedJavaMethod.class, null); 583 String filter = param(params, 1, "filter", String.class, ":3"); 584 String host = param(params, 2, "host", String.class, "localhost"); 585 Number port = param(params, 3, "port", Number.class, 4445); 586 dumpMethod(method, filter, host, port.intValue()); 587 } else { 588 String className = param(params, 0, "className", String.class, null); 589 String methodName = param(params, 1, "methodName", String.class, null); 590 String filter = param(params, 2, "filter", String.class, ":3"); 591 String host = param(params, 3, "host", String.class, "localhost"); 592 Number port = param(params, 4, "port", Number.class, 4445); 593 dumpMethod(className, methodName, filter, host, port.intValue()); 594 } 595 } 596 return null; 597 } 598 599 private static <T> T param(Object[] arr, int index, String name, Class<T> type, T defaultValue) { 600 Object value = arr.length > index ? arr[index] : null; 601 if (value == null || (value instanceof String && ((String) value).isEmpty())) { 602 if (defaultValue == null) { 603 throw new IllegalArgumentException(name + " must be specified"); 604 } 605 value = defaultValue; 606 } 607 if (type.isInstance(value)) { 608 return type.cast(value); 609 } 610 throw new IllegalArgumentException("Expecting " + type.getName() + " for " + name + " but was " + value); 611 } 612 }