aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/junit/internal/management/ReflectiveThreadMXBean.java
blob: bc741bec8e61776b41b499ca5c9428244a09cc83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package org.junit.internal.management;

import org.junit.internal.Classes;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Implementation of {@link ThreadMXBean} using the JVM reflectively.
 */
final class ReflectiveThreadMXBean implements ThreadMXBean {
  private final Object threadMxBean;


  private static final class Holder {
    static final Method getThreadCpuTimeMethod;
    static final Method isThreadCpuTimeSupportedMethod;

    private static final String FAILURE_MESSAGE = "Unable to access ThreadMXBean";

    static {
      Method threadCpuTime = null;
      Method threadCpuTimeSupported = null;
      try {
        Class<?> threadMXBeanClass = Classes.getClass("java.lang.management.ThreadMXBean");
        threadCpuTime = threadMXBeanClass.getMethod("getThreadCpuTime", long.class);
        threadCpuTimeSupported = threadMXBeanClass.getMethod("isThreadCpuTimeSupported");
      } catch (ClassNotFoundException e) {
        // do nothing, the methods will be null on failure
      } catch (NoSuchMethodException e) {
        // do nothing, the methods will be null on failure
      } catch (SecurityException e) {
        // do nothing, the methods will be null on failure
      }
      getThreadCpuTimeMethod = threadCpuTime;
      isThreadCpuTimeSupportedMethod = threadCpuTimeSupported;
    }
  }

  ReflectiveThreadMXBean(Object threadMxBean) {
    super();
    this.threadMxBean = threadMxBean;
  }

  /**
   * {@inheritDoc}
   */
  public long getThreadCpuTime(long id) {
    if (Holder.getThreadCpuTimeMethod != null) {
      Exception error = null;
      try {
        return (Long) Holder.getThreadCpuTimeMethod.invoke(threadMxBean, id);
      } catch (ClassCastException e) {
        error = e;
        // fallthrough
      } catch (IllegalAccessException e) {
        error = e;
        // fallthrough
      } catch (IllegalArgumentException e) {
        error = e;
        // fallthrough
      } catch (InvocationTargetException e) {
        error = e;
        // fallthrough
      }
      throw new UnsupportedOperationException(Holder.FAILURE_MESSAGE, error);
    }
    throw new UnsupportedOperationException(Holder.FAILURE_MESSAGE);
  }

  /**
   * {@inheritDoc}
   */
  public boolean isThreadCpuTimeSupported() {
    if (Holder.isThreadCpuTimeSupportedMethod != null) {
      try {
        return (Boolean) Holder.isThreadCpuTimeSupportedMethod.invoke(threadMxBean);
      } catch (ClassCastException e) {
        // fallthrough
      } catch (IllegalAccessException e) {
        // fallthrough
      } catch (IllegalArgumentException e) {
        // fallthrough
      } catch (InvocationTargetException e) {
        // fallthrough
      }
    }
    return false;
  }

}