changeset 1106:39574a0ada2a

7901278: JMH and Swing causes CompletionFailure
author shade
date Wed, 28 Jan 2015 16:37:07 +0300
parents a355260808dc
children ceb8cfe4701d
files jmh-core-ct/src/test/java/org/openjdk/jmh/ct/other/SwingTest.java jmh-generator-annprocess/src/main/java/org/openjdk/jmh/generators/annotations/APGeneratorSource.java
diffstat 2 files changed, 70 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-ct/src/test/java/org/openjdk/jmh/ct/other/SwingTest.java	Wed Jan 28 16:37:07 2015 +0300
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package org.openjdk.jmh.ct.other;
+
+import org.junit.Test;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.ct.CompileTest;
+
+import javax.swing.*;
+
+public class SwingTest {
+
+    public static class S extends JFrame {
+
+    }
+
+    @Benchmark
+    public void test() {
+
+    }
+
+    @Test
+    public void compileTest() {
+        CompileTest.assertOK(this.getClass());
+    }
+
+}
--- a/jmh-generator-annprocess/src/main/java/org/openjdk/jmh/generators/annotations/APGeneratorSource.java	Wed Jan 28 16:30:49 2015 +0300
+++ b/jmh-generator-annprocess/src/main/java/org/openjdk/jmh/generators/annotations/APGeneratorSource.java	Wed Jan 28 16:37:07 2015 +0300
@@ -33,6 +33,7 @@
 import javax.lang.model.element.ElementKind;
 import javax.lang.model.element.TypeElement;
 import javax.lang.model.util.ElementFilter;
+import javax.tools.Diagnostic;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Comparator;
@@ -79,12 +80,27 @@
             lastSize = discoveredClasses.size();
             List<TypeElement> newClasses = new ArrayList<TypeElement>();
             for (Element e : discoveredClasses) {
-                TypeElement walk = (TypeElement) e;
-                do {
-                    for (TypeElement nested : ElementFilter.typesIn(walk.getEnclosedElements())) {
-                        newClasses.add(nested);
+                try {
+                    TypeElement walk = (TypeElement) e;
+                    do {
+                        for (TypeElement nested : ElementFilter.typesIn(walk.getEnclosedElements())) {
+                            newClasses.add(nested);
+                        }
                     }
-                } while ((walk = (TypeElement) processingEnv.getTypeUtils().asElement(walk.getSuperclass())) != null);
+                    while ((walk = (TypeElement) processingEnv.getTypeUtils().asElement(walk.getSuperclass())) != null);
+                } catch (Exception t) {
+                    // Working around the javac bug:
+                    //   https://bugs.openjdk.java.net/browse/JDK-8071778
+                    //
+                    // JMH ignores these exceptions since they probably consider the classes that do not
+                    // have any JMH-related annotations. We can do nothing better than to notify the user,
+                    // and bail from traversing a current class.
+                    if (t.getClass().getName().endsWith("CompletionFailure")) {
+                        processingEnv.getMessager().printMessage(Diagnostic.Kind.MANDATORY_WARNING, "While traversing " + e + ", caught " + t);
+                    } else {
+                        throw new RuntimeException(t);
+                    }
+                }
             }
             discoveredClasses.addAll(newClasses);
         }