JaroslavTulach closed pull request #1013: [NETBEANS-1478] Use a public API to
convert ThreadInfo to CompositeDa…
URL: https://github.com/apache/incubator-netbeans/pull/1013
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/platform/sampler/src/org/netbeans/modules/sampler/SamplesOutputStream.java
b/platform/sampler/src/org/netbeans/modules/sampler/SamplesOutputStream.java
index d1c8623cc6..6deb91ffce 100644
--- a/platform/sampler/src/org/netbeans/modules/sampler/SamplesOutputStream.java
+++ b/platform/sampler/src/org/netbeans/modules/sampler/SamplesOutputStream.java
@@ -24,8 +24,6 @@
import java.io.OutputStream;
import java.lang.management.ThreadInfo;
import java.lang.ref.WeakReference;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@@ -35,7 +33,10 @@
import java.util.Set;
import java.util.WeakHashMap;
import java.util.zip.GZIPOutputStream;
-import javax.management.openmbean.CompositeData;
+
+import javax.management.StandardMBean;
+
+import org.openide.util.Exceptions;
/**
*
@@ -43,33 +44,12 @@
*/
class SamplesOutputStream {
- private static final String[][] methods = new String[][]{
- {"sun.management.ThreadInfoCompositeData", "toCompositeData"}, //
NOI18N Sun JVM
- {"com.ibm.lang.management.ManagementUtils",
"toThreadInfoCompositeData"} // NOI18N IBM J9
- };
static final String ID = "NPSS"; // NetBeans Profiler samples stream
public static final String FILE_EXT = ".npss"; // NOI18N
static final int RESET_THRESHOLD = 5000;
static final int STEPS = 1000;
static byte version = 2;
- private static Method toCompositeDataMethod;
- static {
- for (String[] method : methods) {
- String className = method[0];
- String methodName = method[1];
- try {
- Class clazz = Class.forName(className);
- toCompositeDataMethod = clazz.getMethod(methodName,
ThreadInfo.class);
- if (toCompositeDataMethod != null) {
- break;
- }
- } catch (ClassNotFoundException ex) {
- } catch (NoSuchMethodException ex) {
- } catch (SecurityException ex) {
- }
- }
- }
OutputStream outStream;
Map<Long, ThreadInfo> lastThreadInfos;
Map<StackTraceElement, WeakReference<StackTraceElement>> steCache;
@@ -79,7 +59,7 @@
int offset;
public static boolean isSupported() {
- return toCompositeDataMethod != null;
+ return true;
}
SamplesOutputStream(OutputStream os, Sampler progress, int max) throws
IOException {
@@ -173,18 +153,6 @@ private void updateLastSample(Sample removedSample, Sample
lastSample) {
}
}
- private static CompositeData toCompositeData(ThreadInfo tinfo) {
- try {
- return (CompositeData) toCompositeDataMethod.invoke(null, tinfo);
- } catch (IllegalAccessException ex) {
- throw new RuntimeException(ex);
- } catch (IllegalArgumentException ex) {
- throw new RuntimeException(ex);
- } catch (InvocationTargetException ex) {
- throw new RuntimeException(ex);
- }
- }
-
void close() throws IOException {
steCache = null;
GZIPOutputStream stream = new GZIPOutputStream(outStream, 64 * 1024);
@@ -281,9 +249,31 @@ private void writeToStream(ObjectOutputStream out) throws
IOException {
out.writeLong(tid.longValue());
}
out.writeInt(newThreads.size());
- for (ThreadInfo tic : newThreads) {
- out.writeObject(toCompositeData(tic));
+ for (Object t : (Object[]) toCompositeData(newThreads)) {
+ out.writeObject(t);
+ }
+ }
+
+ private Object[] toCompositeData(final List<ThreadInfo> threadInfos) {
+ CompositeDataGetter getter = new CompositeDataGetter() {
+ @Override
+ public ThreadInfo[] getThreads() {
+ return threadInfos.toArray(new ThreadInfo[0]);
+ }
+ };
+ try {
+ StandardMBean getterBean = new StandardMBean(getter,
+
CompositeDataGetter.class,
+ true);
+ return (Object[]) getterBean.getAttribute("Threads");
+ } catch (Exception ex) {
+ Exceptions.printStackTrace(ex);
+ return new Object[0];
}
}
+
+ public interface CompositeDataGetter {
+ public ThreadInfo[] getThreads();
+ }
}
}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists