|
| 1 | +package org.junit.internal.management; |
| 2 | + |
| 3 | +import org.junit.internal.Classes; |
| 4 | + |
| 5 | +import java.lang.reflect.InvocationTargetException; |
| 6 | +import java.lang.reflect.Method; |
| 7 | + |
| 8 | +/** |
| 9 | + * Implementation of {@link ThreadMXBean} using the JVM reflectively. |
| 10 | + */ |
| 11 | +final class ReflectiveThreadMXBean implements ThreadMXBean { |
| 12 | + private final Object threadMxBean; |
| 13 | + |
| 14 | + |
| 15 | + private static final class Holder { |
| 16 | + static final Method getThreadCpuTimeMethod; |
| 17 | + static final Method isThreadCpuTimeSupportedMethod; |
| 18 | + |
| 19 | + private static final String FAILURE_MESSAGE = "Unable to access ThreadMXBean"; |
| 20 | + |
| 21 | + static { |
| 22 | + Method threadCpuTime = null; |
| 23 | + Method threadCpuTimeSupported = null; |
| 24 | + try { |
| 25 | + Class<?> threadMXBeanClass = Classes.getClass("java.lang.management.ThreadMXBean"); |
| 26 | + threadCpuTime = threadMXBeanClass.getMethod("getThreadCpuTime", long.class); |
| 27 | + threadCpuTimeSupported = threadMXBeanClass.getMethod("isThreadCpuTimeSupported"); |
| 28 | + } catch (ClassNotFoundException e) { |
| 29 | + // do nothing, the methods will be null on failure |
| 30 | + } catch (NoSuchMethodException e) { |
| 31 | + // do nothing, the methods will be null on failure |
| 32 | + } catch (SecurityException e) { |
| 33 | + // do nothing, the methods will be null on failure |
| 34 | + } |
| 35 | + getThreadCpuTimeMethod = threadCpuTime; |
| 36 | + isThreadCpuTimeSupportedMethod = threadCpuTimeSupported; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + ReflectiveThreadMXBean(Object threadMxBean) { |
| 41 | + super(); |
| 42 | + this.threadMxBean = threadMxBean; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * {@inheritDoc} |
| 47 | + */ |
| 48 | + @Override |
| 49 | + public long getThreadCpuTime(long id) { |
| 50 | + if (Holder.getThreadCpuTimeMethod != null) { |
| 51 | + Exception error = null; |
| 52 | + try { |
| 53 | + return (Long) Holder.getThreadCpuTimeMethod.invoke(threadMxBean, id); |
| 54 | + } catch (ClassCastException e) { |
| 55 | + error = e; |
| 56 | + // fallthrough |
| 57 | + } catch (IllegalAccessException e) { |
| 58 | + error = e; |
| 59 | + // fallthrough |
| 60 | + } catch (IllegalArgumentException e) { |
| 61 | + error = e; |
| 62 | + // fallthrough |
| 63 | + } catch (InvocationTargetException e) { |
| 64 | + error = e; |
| 65 | + // fallthrough |
| 66 | + } |
| 67 | + throw new UnsupportedOperationException(Holder.FAILURE_MESSAGE, error); |
| 68 | + } |
| 69 | + throw new UnsupportedOperationException(Holder.FAILURE_MESSAGE); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * {@inheritDoc} |
| 74 | + */ |
| 75 | + @Override |
| 76 | + public boolean isThreadCpuTimeSupported() { |
| 77 | + if (Holder.isThreadCpuTimeSupportedMethod != null) { |
| 78 | + try { |
| 79 | + return (Boolean) Holder.isThreadCpuTimeSupportedMethod.invoke(threadMxBean); |
| 80 | + } catch (ClassCastException e) { |
| 81 | + // fallthrough |
| 82 | + } catch (IllegalAccessException e) { |
| 83 | + // fallthrough |
| 84 | + } catch (IllegalArgumentException e) { |
| 85 | + // fallthrough |
| 86 | + } catch (InvocationTargetException e) { |
| 87 | + // fallthrough |
| 88 | + } |
| 89 | + } |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | +} |
| 94 | + |
0 commit comments