All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.junit.internal.management.ReflectiveRuntimeMXBean Maven / Gradle / Ivy

Go to download

JUnit is a unit testing framework for Java, created by Erich Gamma and Kent Beck.

There is a newer version: 4.13.2
Show newest version
package org.junit.internal.management;

import org.junit.internal.Classes;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;

/**
 * Implementation of {@link RuntimeMXBean} using the JVM reflectively.
 */
final class ReflectiveRuntimeMXBean implements RuntimeMXBean {
  private final Object runtimeMxBean;

  private static final class Holder {
    private static final Method getInputArgumentsMethod;
    static {
      Method inputArguments = null;
      try {
        Class threadMXBeanClass = Classes.getClass("java.lang.management.RuntimeMXBean");
        inputArguments = threadMXBeanClass.getMethod("getInputArguments");
      } catch (ClassNotFoundException e) {
        // do nothing, input arguments will be null on failure
      } catch (NoSuchMethodException e) {
        // do nothing, input arguments will be null on failure
      } catch (SecurityException e) {
        // do nothing, input arguments will be null on failure
      }
      getInputArgumentsMethod = inputArguments;
    }
  }

  ReflectiveRuntimeMXBean(Object runtimeMxBean) {
    super();
    this.runtimeMxBean = runtimeMxBean;
  }

  /**
   * {@inheritDoc}
   */
  @SuppressWarnings("unchecked")
  public List getInputArguments() {
    if (Holder.getInputArgumentsMethod != null) {
      try {
        return (List) Holder.getInputArgumentsMethod.invoke(runtimeMxBean);
      } catch (ClassCastException e) { // no multi-catch with source level 6
        // fallthrough
      } catch (IllegalAccessException e) {
        // fallthrough
      } catch (IllegalArgumentException e) {
        // fallthrough
      } catch (InvocationTargetException e) {
        // fallthrough
      }
    }
    return Collections.emptyList();
  }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy