changeset 300:83b404ab30db

Fix @AuxCounter for Scope.Benchmark/Scope.Group.
author shade
date Wed, 04 Dec 2013 22:16:09 +0400
parents 60753aab06cf
children 525dec23062a
files jmh-core-it/src/test/java/org/openjdk/jmh/it/compile/AuxCountersTest.java jmh-core/src/main/java/org/openjdk/jmh/annotations/AuxCounters.java jmh-core/src/main/java/org/openjdk/jmh/processor/internal/StateObjectHandler.java
diffstat 3 files changed, 77 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-it/src/test/java/org/openjdk/jmh/it/compile/AuxCountersTest.java	Wed Dec 04 22:16:09 2013 +0400
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2005, 2013, 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.it.compile;
+
+import org.openjdk.jmh.annotations.AuxCounters;
+import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
+import org.openjdk.jmh.annotations.Group;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.State;
+
+public class AuxCountersTest {
+
+    @AuxCounters
+    @State(Scope.Benchmark)
+    public static class BenchmarkState {
+        public int x;
+    }
+
+    @AuxCounters
+    @State(Scope.Group)
+    public static class GroupState {
+        public int x;
+    }
+
+    @AuxCounters
+    @State(Scope.Thread)
+    public static class ThreadState {
+        public int x;
+    }
+
+    @GenerateMicroBenchmark
+    public void testBenchmark(BenchmarkState s) {
+
+    }
+
+    @GenerateMicroBenchmark
+    @Group("test")
+    public void testGroup(GroupState s) {
+
+    }
+
+    @GenerateMicroBenchmark
+    public void testThread(ThreadState s) {
+
+    }
+
+}
--- a/jmh-core/src/main/java/org/openjdk/jmh/annotations/AuxCounters.java	Wed Dec 04 21:35:55 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/annotations/AuxCounters.java	Wed Dec 04 22:16:09 2013 +0400
@@ -30,8 +30,9 @@
  * <p>
  * This annotation can be used to mark {@link State} objects as the bearers of
  * auxiliary secondary results. Marking the class with this annotation will enable
- * JMH to look for {int, long} fields, as well as methods returning {int, long}
- * values, and treat their values as the operation counts in current iteration.
+ * JMH to look for public {int, long} fields, as well as public methods returning
+ * {int, long} values, and treat their values as the operation counts in current
+ * iteration.
  * <p>
  * NOTE: You have to explicitly reset the state if you don't want the counters
  * to be shared across the iterations.
--- a/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/StateObjectHandler.java	Wed Dec 04 21:35:55 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/StateObjectHandler.java	Wed Dec 04 22:16:09 2013 +0400
@@ -39,6 +39,7 @@
 import javax.lang.model.element.Element;
 import javax.lang.model.element.ElementKind;
 import javax.lang.model.element.ExecutableElement;
+import javax.lang.model.element.Modifier;
 import javax.lang.model.element.TypeElement;
 import javax.lang.model.util.ElementFilter;
 import java.util.ArrayList;
@@ -155,24 +156,20 @@
         // auxiliary result, produce the accessors
         if (element.getAnnotation(AuxCounters.class) != null) {
             for (Element sub : element.getEnclosedElements()) {
-                if (sub.getKind() == ElementKind.FIELD) {
+                if (sub.getKind() == ElementKind.FIELD && sub.getModifiers().contains(Modifier.PUBLIC)) {
                     String fieldType = sub.asType().toString();
                     if (fieldType.equals("int") || fieldType.equals("long")) {
                         String name = sub.getSimpleName().toString();
-                        if (!auxNames.add(name)) {
-                            throw new GenerationException("Conflicting @" + AuxCounters.class.getSimpleName() + " fields", sub);
-                        }
+                        auxNames.add(name);
                         auxAccessors.put(name, so.localIdentifier + "." + name);
                     }
                 }
 
-                if (sub.getKind() == ElementKind.METHOD) {
+                if (sub.getKind() == ElementKind.METHOD && sub.getModifiers().contains(Modifier.PUBLIC)) {
                     String returnType = ((ExecutableElement) sub).getReturnType().toString();
                     if (returnType.equals("int") || returnType.equals("long")) {
                         String name = sub.getSimpleName().toString();
-                        if (!auxNames.add(name)) {
-                            throw new GenerationException("Conflicting @" + AuxCounters.class.getSimpleName() + " fields", sub);
-                        }
+                        auxNames.add(name);
                         auxAccessors.put(name, so.localIdentifier + "." + name + "()");
                     }
                 }