changeset 431:ec6d40e5ec02

@Param can now be non-public.
author shade
date Fri, 28 Feb 2014 01:15:06 +0400
parents cbcbda02f66f
children 824c1925e81e
files jmh-core-it/src/test/java/org/openjdk/jmh/it/compile/params/ParamAccessDefaultTest.java jmh-core-it/src/test/java/org/openjdk/jmh/it/compile/params/ParamAccessPrivateTest.java jmh-core-it/src/test/java/org/openjdk/jmh/it/compile/params/ParamAccessProtectedTest.java jmh-core-it/src/test/java/org/openjdk/jmh/it/compile/params/ParamAccessPublicTest.java jmh-core/src/main/java/org/openjdk/jmh/annotations/Param.java jmh-core/src/main/java/org/openjdk/jmh/generators/core/BenchmarkGenerator.java jmh-core/src/main/java/org/openjdk/jmh/generators/core/ParamValidationPlugin.java jmh-core/src/main/java/org/openjdk/jmh/generators/core/StateObjectHandler.java
diffstat 8 files changed, 389 insertions(+), 12 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/params/ParamAccessDefaultTest.java	Fri Feb 28 01:15:06 2014 +0400
@@ -0,0 +1,91 @@
+/*
+ * 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.params;
+
+import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.State;
+
+@State(Scope.Benchmark)
+public class ParamAccessDefaultTest {
+
+    @Param("true")
+    boolean param_boolean;
+
+    @Param("0")
+    byte param_byte;
+
+    @Param("0")
+    short param_short;
+
+    @Param("0")
+    char param_char;
+
+    @Param("0")
+    int param_int;
+
+    @Param("0")
+    float param_float;
+
+    @Param("0")
+    long param_long;
+
+    @Param("0")
+    double param_double;
+
+    @Param("true")
+    Boolean param_Boolean;
+
+    @Param("0")
+    Byte param_Byte;
+
+    @Param("0")
+    Short param_Short;
+
+    @Param("0")
+    Character param_Char;
+
+    @Param("0")
+    Integer param_Int;
+
+    @Param("0")
+    Float param_Float;
+
+    @Param("0")
+    Long param_Long;
+
+    @Param("0")
+    Double param_Double;
+
+    @Param("null")
+    String param_String;
+
+    @GenerateMicroBenchmark
+    public void test() {
+
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-it/src/test/java/org/openjdk/jmh/it/compile/params/ParamAccessPrivateTest.java	Fri Feb 28 01:15:06 2014 +0400
@@ -0,0 +1,91 @@
+/*
+ * 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.params;
+
+import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.State;
+
+@State(Scope.Benchmark)
+public class ParamAccessPrivateTest {
+
+    @Param("true")
+    private boolean param_boolean;
+
+    @Param("0")
+    private byte param_byte;
+
+    @Param("0")
+    private short param_short;
+
+    @Param("0")
+    private char param_char;
+
+    @Param("0")
+    private int param_int;
+
+    @Param("0")
+    private float param_float;
+
+    @Param("0")
+    private long param_long;
+
+    @Param("0")
+    private double param_double;
+
+    @Param("true")
+    private Boolean param_Boolean;
+
+    @Param("0")
+    private Byte param_Byte;
+
+    @Param("0")
+    private Short param_Short;
+
+    @Param("0")
+    private Character param_Char;
+
+    @Param("0")
+    private Integer param_Int;
+
+    @Param("0")
+    private Float param_Float;
+
+    @Param("0")
+    private Long param_Long;
+
+    @Param("0")
+    private Double param_Double;
+
+    @Param("null")
+    private String param_String;
+
+    @GenerateMicroBenchmark
+    public void test() {
+
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-it/src/test/java/org/openjdk/jmh/it/compile/params/ParamAccessProtectedTest.java	Fri Feb 28 01:15:06 2014 +0400
@@ -0,0 +1,91 @@
+/*
+ * 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.params;
+
+import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.State;
+
+@State(Scope.Benchmark)
+public class ParamAccessProtectedTest {
+
+    @Param("true")
+    protected boolean param_boolean;
+
+    @Param("0")
+    protected byte param_byte;
+
+    @Param("0")
+    protected short param_short;
+
+    @Param("0")
+    protected char param_char;
+
+    @Param("0")
+    protected int param_int;
+
+    @Param("0")
+    protected float param_float;
+
+    @Param("0")
+    protected long param_long;
+
+    @Param("0")
+    protected double param_double;
+
+    @Param("true")
+    protected Boolean param_Boolean;
+
+    @Param("0")
+    protected Byte param_Byte;
+
+    @Param("0")
+    protected Short param_Short;
+
+    @Param("0")
+    protected Character param_Char;
+
+    @Param("0")
+    protected Integer param_Int;
+
+    @Param("0")
+    protected Float param_Float;
+
+    @Param("0")
+    protected Long param_Long;
+
+    @Param("0")
+    protected Double param_Double;
+
+    @Param("null")
+    protected String param_String;
+
+    @GenerateMicroBenchmark
+    public void test() {
+
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-it/src/test/java/org/openjdk/jmh/it/compile/params/ParamAccessPublicTest.java	Fri Feb 28 01:15:06 2014 +0400
@@ -0,0 +1,91 @@
+/*
+ * 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.params;
+
+import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.State;
+
+@State(Scope.Benchmark)
+public class ParamAccessPublicTest {
+
+    @Param("true")
+    public boolean param_boolean;
+
+    @Param("0")
+    public byte param_byte;
+
+    @Param("0")
+    public short param_short;
+
+    @Param("0")
+    public char param_char;
+
+    @Param("0")
+    public int param_int;
+
+    @Param("0")
+    public float param_float;
+
+    @Param("0")
+    public long param_long;
+
+    @Param("0")
+    public double param_double;
+
+    @Param("true")
+    public Boolean param_Boolean;
+
+    @Param("0")
+    public Byte param_Byte;
+
+    @Param("0")
+    public Short param_Short;
+
+    @Param("0")
+    public Character param_Char;
+
+    @Param("0")
+    public Integer param_Int;
+
+    @Param("0")
+    public Float param_Float;
+
+    @Param("0")
+    public Long param_Long;
+
+    @Param("0")
+    public Double param_Double;
+
+    @Param("null")
+    public String param_String;
+
+    @GenerateMicroBenchmark
+    public void test() {
+
+    }
+
+}
--- a/jmh-core/src/main/java/org/openjdk/jmh/annotations/Param.java	Fri Feb 28 00:55:47 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/annotations/Param.java	Fri Feb 28 01:15:06 2014 +0400
@@ -33,7 +33,7 @@
 /**
  * Marks the configurable parameter in the benchmark.
  * <p/>
- * {@link Param} fields should be public non-final instance fields,
+ * {@link Param} fields should be non-final instance fields,
  * and should only reside in in {@link State} classes. JMH will inject
  * the value into the annotated field before any {@link Setup} method
  * is called. It is *not* guaranteed the field value would be accessible
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/core/BenchmarkGenerator.java	Fri Feb 28 00:55:47 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/core/BenchmarkGenerator.java	Fri Feb 28 01:15:06 2014 +0400
@@ -67,6 +67,7 @@
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.lang.annotation.IncompleteAnnotationException;
+import java.lang.reflect.Field;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -521,7 +522,8 @@
                 Result.class, ThroughputResult.class, AverageTimeResult.class,
                 SampleTimeResult.class, SingleShotResult.class, SampleBuffer.class,
                 Mode.class, Fork.class, Measurement.class, Threads.class, Warmup.class,
-                BenchmarkMode.class, RawResults.class, ResultRole.class
+                BenchmarkMode.class, RawResults.class, ResultRole.class,
+                Field.class
         };
 
         for (Class<?> c : imports) {
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/core/ParamValidationPlugin.java	Fri Feb 28 00:55:47 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/core/ParamValidationPlugin.java	Fri Feb 28 01:15:06 2014 +0400
@@ -35,13 +35,6 @@
     public void process(GeneratorSource source) {
         try {
             for (FieldInfo element : BenchmarkGeneratorUtils.getFieldsAnnotatedWith(source, Param.class)) {
-                if (!element.isPublic()) {
-                    source.printError(
-                            "@" + Param.class.getSimpleName() + " annotation is not acceptable on non-public field.",
-                            element
-                    );
-                }
-
                 if (element.isStatic()) {
                     source.printError(
                             "@" + Param.class.getSimpleName() + " annotation is not acceptable on static fields.",
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/core/StateObjectHandler.java	Fri Feb 28 00:55:47 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/core/StateObjectHandler.java	Fri Feb 28 01:15:06 2014 +0400
@@ -389,8 +389,14 @@
             result.add("            " + so.fieldIdentifier + " = val;");
             result.add("        }");
             result.add("        if (!" + so.fieldIdentifier + ".ready" + Level.Trial + ") {");
+            if (!so.getParamsLabels().isEmpty()) {
+                result.add("            Field f;");
+                result.add("            Class c = " + so.userType + ".class;");
+            }
             for (String paramName : so.getParamsLabels()) {
-                result.add("            " + so.fieldIdentifier + "." + paramName + " = " + so.getParamAccessor(paramName) + ";");
+                result.add("            f = c.getDeclaredField(\"" + paramName + "\");");
+                result.add("            f.setAccessible(true);");
+                result.add("            f.set(" + so.fieldIdentifier + ", " + so.getParamAccessor(paramName) + ");");
             }
             for (HelperMethodInvocation hmi : helpersByState.get(so)) {
                 if (hmi.helperLevel != Level.Trial) continue;
@@ -412,8 +418,14 @@
             result.add("");
             result.add(so.type + " tryInit_" + so.fieldIdentifier + "(InfraControl control, " + so.type + " val) throws Throwable {");
             result.add("    if (" + so.fieldIdentifier + " == null) {");
+            if (!so.getParamsLabels().isEmpty()) {
+                result.add("            Field f;");
+                result.add("            Class c = " + so.userType + ".class;");
+            }
             for (String paramName : so.getParamsLabels()) {
-                result.add("                val." + paramName + " = " + so.getParamAccessor(paramName) + ";");
+                result.add("            f = c.getDeclaredField(\"" + paramName + "\");");
+                result.add("            f.setAccessible(true);");
+                result.add("            f.set(val, " + so.getParamAccessor(paramName) + ");");
             }
             for (HelperMethodInvocation hmi : helpersByState.get(so)) {
                 if (hmi.helperLevel != Level.Trial) continue;
@@ -441,8 +453,14 @@
             result.add("            local = val;");
             result.add("        }");
             result.add("        if (!local.ready" + Level.Trial + ") {");
+            if (!so.getParamsLabels().isEmpty()) {
+                result.add("            Field f;");
+                result.add("            Class c = " + so.userType + ".class;");
+            }
             for (String paramName : so.getParamsLabels()) {
-                result.add("            local." + paramName + " = " + so.getParamAccessor(paramName) + ";");
+                result.add("            f = c.getDeclaredField(\"" + paramName + "\");");
+                result.add("            f.setAccessible(true);");
+                result.add("            f.set(local, " + so.getParamAccessor(paramName) + ");");
             }
             for (HelperMethodInvocation hmi : helpersByState.get(so)) {
                 if (hmi.helperLevel != Level.Trial) continue;