changeset 1270:ba8884f1ddea

7901491: @Param-s with the same name in class hierarchy are set incorrectly Contributed-by: Sergey Kuksenko <sergey.kuksenko@oracle.com>
author shade
date Fri, 28 Aug 2015 18:28:29 +0300
parents 6495767361cf
children a427fb8dccc1
files jmh-core-ct/src/test/java/org/openjdk/jmh/ct/params/ParamOverridingTest.java jmh-core-it/src/test/java/org/openjdk/jmh/it/params/OverridingParamsTest.java jmh-core/src/main/java/org/openjdk/jmh/generators/core/MethodGroup.java jmh-core/src/main/java/org/openjdk/jmh/generators/core/StateObject.java jmh-core/src/main/java/org/openjdk/jmh/generators/core/StateObjectHandler.java
diffstat 5 files changed, 159 insertions(+), 36 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/params/ParamOverridingTest.java	Fri Aug 28 18:28:29 2015 +0300
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2014, 2015, 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.params;
+
+import org.junit.Test;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.ct.CompileTest;
+
+@State(Scope.Benchmark)
+public class ParamOverridingTest {
+
+    @Param("value")
+    private String param;
+
+    static class OverridingTest extends ParamOverridingTest {
+        @Param("newvalue")
+        private String param;
+    }
+
+    @Test
+    public void compileTest() {
+        CompileTest.assertOK(this.getClass());
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-it/src/test/java/org/openjdk/jmh/it/params/OverridingParamsTest.java	Fri Aug 28 18:28:29 2015 +0300
@@ -0,0 +1,81 @@
+/*
+ * 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.it.params;
+
+import junit.framework.Assert;
+import org.junit.Test;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.it.Fixtures;
+import org.openjdk.jmh.runner.Runner;
+import org.openjdk.jmh.runner.RunnerException;
+import org.openjdk.jmh.runner.options.Options;
+import org.openjdk.jmh.runner.options.OptionsBuilder;
+import org.openjdk.jmh.runner.options.TimeValue;
+
+public class OverridingParamsTest {
+
+    @State(Scope.Benchmark)
+    public static class Benchmark1 {
+        @Param("1")
+        private int x;
+
+        @Benchmark
+        public void test() {
+
+        }
+    }
+
+    @State(Scope.Benchmark)
+    public static class Benchmark2 extends Benchmark1 {
+        @Param("2")
+        private int x;
+
+        @Benchmark
+        public void overtest() {
+            // check that all x parameters set to the same value
+            Assert.assertEquals("this.x != super.x", this.x, super.x);
+        }
+    }
+
+    @Test
+    public void constrainedX() throws RunnerException {
+        Options opts = new OptionsBuilder()
+                .include(Fixtures.getTestMask(this.getClass()))
+                .warmupIterations(1)
+                .warmupTime(TimeValue.milliseconds(100))
+                .measurementIterations(1)
+                .measurementTime(TimeValue.milliseconds(100))
+                .forks(1)
+                .shouldFailOnError(true)
+                .param("x", "2", "3")
+                .build();
+
+        Assert.assertEquals(6, new Runner(opts).run().size());
+    }
+
+}
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/core/MethodGroup.java	Fri Aug 21 18:16:47 2015 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/core/MethodGroup.java	Fri Aug 28 18:28:29 2015 +0300
@@ -103,7 +103,9 @@
     }
 
     public void addParamValues(String name, String[] value) {
-        params.put(name, value);
+        if (!params.containsKey(name)) {
+            params.put(name, value);
+        }
     }
 
     public void addStrictFP(boolean sfp) {
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/core/StateObject.java	Fri Aug 21 18:16:47 2015 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/core/StateObject.java	Fri Aug 28 18:28:29 2015 +0300
@@ -25,12 +25,12 @@
 package org.openjdk.jmh.generators.core;
 
 import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.util.Multimap;
+import org.openjdk.jmh.util.TreeMultimap;
 
 import java.util.Collection;
 import java.util.Comparator;
-import java.util.Map;
 import java.util.SortedSet;
-import java.util.TreeMap;
 import java.util.TreeSet;
 
 class StateObject {
@@ -48,7 +48,7 @@
     public final Scope scope;
     public final String localIdentifier;
     public final String fieldIdentifier;
-    public final Map<String, FieldInfo> params;
+    public final Multimap<String, FieldInfo> params;
     public final SortedSet<HelperMethodInvocation> helpers;
 
     public StateObject(Identifiers identifiers, ClassInfo info, Scope scope) {
@@ -61,7 +61,7 @@
         this.localIdentifier = "l_" + id;
         this.fieldIdentifier = "f_" + id;
 
-        this.params = new TreeMap<String, FieldInfo>();
+        this.params = new TreeMultimap<String, FieldInfo>();
         this.helpers = new TreeSet<HelperMethodInvocation>();
     }
 
@@ -97,19 +97,20 @@
     }
 
     public Collection<String> getParamsLabels() {
-        return params.keySet();
+        return params.keys();
     }
 
     public void addParam(FieldInfo fieldInfo) {
         params.put(fieldInfo.getName(), fieldInfo);
     }
 
-    public FieldInfo getParam(String name) {
+    public Collection<FieldInfo> getParam(String name) {
         return params.get(name);
     }
 
-    public String getParamAccessor(String name) {
-        String type = params.get(name).getType().getQualifiedName();
+    public String getParamAccessor(FieldInfo paramField) {
+        String name = paramField.getName();
+        String type = paramField.getType().getQualifiedName();
 
         if (type.equalsIgnoreCase("java.lang.String")) {
             return "control.getParam(\"" + name + "\")";
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/core/StateObjectHandler.java	Fri Aug 21 18:16:47 2015 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/core/StateObjectHandler.java	Fri Aug 28 18:28:29 2015 +0300
@@ -24,13 +24,7 @@
  */
 package org.openjdk.jmh.generators.core;
 
-import org.openjdk.jmh.annotations.AuxCounters;
-import org.openjdk.jmh.annotations.Level;
-import org.openjdk.jmh.annotations.Param;
-import org.openjdk.jmh.annotations.Scope;
-import org.openjdk.jmh.annotations.Setup;
-import org.openjdk.jmh.annotations.State;
-import org.openjdk.jmh.annotations.TearDown;
+import org.openjdk.jmh.annotations.*;
 import org.openjdk.jmh.infra.BenchmarkParams;
 import org.openjdk.jmh.infra.Control;
 import org.openjdk.jmh.infra.IterationParams;
@@ -41,17 +35,7 @@
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.lang.annotation.Annotation;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.LinkedHashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.SortedSet;
-import java.util.TreeSet;
+import java.util.*;
 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
 
 class StateObjectHandler {
@@ -584,9 +568,11 @@
                 result.add("        Field f;");
             }
             for (String paramName : so.getParamsLabels()) {
-                result.add("        f = " + so.getParam(paramName).getDeclaringClass().getQualifiedName() + ".class.getDeclaredField(\"" + paramName + "\");");
-                result.add("        f.setAccessible(true);");
-                result.add("        f.set(val, " + so.getParamAccessor(paramName) + ");");
+                for (FieldInfo paramField : so.getParam(paramName)) {
+                    result.add("        f = " + paramField.getDeclaringClass().getQualifiedName() + ".class.getDeclaredField(\"" + paramName + "\");");
+                    result.add("        f.setAccessible(true);");
+                    result.add("        f.set(val, " + so.getParamAccessor(paramField) + ");");
+                }
             }
             for (HelperMethodInvocation hmi : so.getHelpers()) {
                 if (hmi.helperLevel != Level.Trial) continue;
@@ -635,9 +621,11 @@
                 result.add("            Field f;");
             }
             for (String paramName : so.getParamsLabels()) {
-                result.add("        f = " + so.getParam(paramName).getDeclaringClass().getQualifiedName() + ".class.getDeclaredField(\"" + paramName + "\");");
-                result.add("        f.setAccessible(true);");
-                result.add("        f.set(val, " + so.getParamAccessor(paramName) + ");");
+                for (FieldInfo paramField : so.getParam(paramName)) {
+                    result.add("            f = " + paramField.getDeclaringClass().getQualifiedName() + ".class.getDeclaredField(\"" + paramName + "\");");
+                    result.add("            f.setAccessible(true);");
+                    result.add("            f.set(val, " + so.getParamAccessor(paramField) + ");");
+                }
             }
             for (HelperMethodInvocation hmi : so.getHelpers()) {
                 if (hmi.helperLevel != Level.Trial) continue;
@@ -672,9 +660,11 @@
                 result.add("        Field f;");
             }
             for (String paramName : so.getParamsLabels()) {
-                result.add("        f = " + so.getParam(paramName).getDeclaringClass().getQualifiedName() + ".class.getDeclaredField(\"" + paramName + "\");");
-                result.add("        f.setAccessible(true);");
-                result.add("        f.set(val, " + so.getParamAccessor(paramName) + ");");
+                for(FieldInfo paramField : so.getParam(paramName)) {
+                    result.add("        f = " + paramField.getDeclaringClass().getQualifiedName() + ".class.getDeclaredField(\"" + paramName + "\");");
+                    result.add("        f.setAccessible(true);");
+                    result.add("        f.set(val, " + so.getParamAccessor(paramField) + ");");
+                }
             }
             for (HelperMethodInvocation hmi : so.getHelpers()) {
                 if (hmi.helperLevel != Level.Trial) continue;