changeset 1145:71611fa284a5

7901320: JMH should check for final @Param fields
author shade
date Tue, 03 Mar 2015 21:31:20 +0300
parents 0de334030a48
children c00aaa531496
files jmh-core-ct/src/test/java/org/openjdk/jmh/ct/params/invalid/finals/BooleanTest.java jmh-core-ct/src/test/java/org/openjdk/jmh/ct/params/invalid/finals/ByteTest.java jmh-core-ct/src/test/java/org/openjdk/jmh/ct/params/invalid/finals/CharTest.java jmh-core-ct/src/test/java/org/openjdk/jmh/ct/params/invalid/finals/DoubleTest.java jmh-core-ct/src/test/java/org/openjdk/jmh/ct/params/invalid/finals/EnumTest.java jmh-core-ct/src/test/java/org/openjdk/jmh/ct/params/invalid/finals/FloatTest.java jmh-core-ct/src/test/java/org/openjdk/jmh/ct/params/invalid/finals/LongTest.java jmh-core-ct/src/test/java/org/openjdk/jmh/ct/params/invalid/finals/Short1Test.java jmh-core/src/main/java/org/openjdk/jmh/generators/core/FieldInfo.java jmh-core/src/main/java/org/openjdk/jmh/generators/core/StateObjectHandler.java jmh-generator-annprocess/src/main/java/org/openjdk/jmh/generators/annotations/APFieldInfo.java jmh-generator-asm/src/main/java/org/openjdk/jmh/generators/asm/ASMFieldInfo.java jmh-generator-reflection/src/main/java/org/openjdk/jmh/generators/reflection/RFFieldInfo.java
diffstat 13 files changed, 426 insertions(+), 0 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/invalid/finals/BooleanTest.java	Tue Mar 03 21:31:20 2015 +0300
@@ -0,0 +1,50 @@
+/*
+ * 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.params.invalid.finals;
+
+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.ct.CompileTest;
+
+@State(Scope.Benchmark)
+public class BooleanTest {
+
+    @Param("false")
+    public final boolean param = false;
+
+    @Benchmark
+    public void test() {
+
+    }
+
+    @Test
+    public void compileTest() {
+        CompileTest.assertFail(this.getClass(), "final fields");
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-ct/src/test/java/org/openjdk/jmh/ct/params/invalid/finals/ByteTest.java	Tue Mar 03 21:31:20 2015 +0300
@@ -0,0 +1,50 @@
+/*
+ * 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.params.invalid.finals;
+
+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.ct.CompileTest;
+
+@State(Scope.Benchmark)
+public class ByteTest {
+
+    @Param("0")
+    public final byte param = 0;
+
+    @Benchmark
+    public void test() {
+
+    }
+
+    @Test
+    public void compileTest() {
+        CompileTest.assertFail(this.getClass(), "final fields");
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-ct/src/test/java/org/openjdk/jmh/ct/params/invalid/finals/CharTest.java	Tue Mar 03 21:31:20 2015 +0300
@@ -0,0 +1,50 @@
+/*
+ * 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.params.invalid.finals;
+
+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.ct.CompileTest;
+
+@State(Scope.Benchmark)
+public class CharTest {
+
+    @Param("a")
+    public final char param = 'a';
+
+    @Benchmark
+    public void test() {
+
+    }
+
+    @Test
+    public void compileTest() {
+        CompileTest.assertFail(this.getClass(), "final fields");
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-ct/src/test/java/org/openjdk/jmh/ct/params/invalid/finals/DoubleTest.java	Tue Mar 03 21:31:20 2015 +0300
@@ -0,0 +1,50 @@
+/*
+ * 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.params.invalid.finals;
+
+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.ct.CompileTest;
+
+@State(Scope.Benchmark)
+public class DoubleTest {
+
+    @Param("0.0")
+    public final double param = 0.1;
+
+    @Benchmark
+    public void test() {
+
+    }
+
+    @Test
+    public void compileTest() {
+        CompileTest.assertFail(this.getClass(), "final fields");
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-ct/src/test/java/org/openjdk/jmh/ct/params/invalid/finals/EnumTest.java	Tue Mar 03 21:31:20 2015 +0300
@@ -0,0 +1,51 @@
+/*
+ * 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.params.invalid.finals;
+
+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.ct.CompileTest;
+import org.openjdk.jmh.ct.params.SampleEnum;
+
+@State(Scope.Benchmark)
+public class EnumTest {
+
+    @Param("VALUE_EXIST_1")
+    public final SampleEnum param = SampleEnum.VALUE_EXIST_1;
+
+    @Benchmark
+    public void test() {
+
+    }
+
+    @Test
+    public void compileTest() {
+        CompileTest.assertFail(this.getClass(), "final fields");
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-ct/src/test/java/org/openjdk/jmh/ct/params/invalid/finals/FloatTest.java	Tue Mar 03 21:31:20 2015 +0300
@@ -0,0 +1,50 @@
+/*
+ * 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.params.invalid.finals;
+
+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.ct.CompileTest;
+
+@State(Scope.Benchmark)
+public class FloatTest {
+
+    @Param("0.0")
+    public final float param = 0.1f;
+
+    @Benchmark
+    public void test() {
+
+    }
+
+    @Test
+    public void compileTest() {
+        CompileTest.assertFail(this.getClass(), "final fields");
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-ct/src/test/java/org/openjdk/jmh/ct/params/invalid/finals/LongTest.java	Tue Mar 03 21:31:20 2015 +0300
@@ -0,0 +1,50 @@
+/*
+ * 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.params.invalid.finals;
+
+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.ct.CompileTest;
+
+@State(Scope.Benchmark)
+public class LongTest {
+
+    @Param("0")
+    public final long param = 0;
+
+    @Benchmark
+    public void test() {
+
+    }
+
+    @Test
+    public void compileTest() {
+        CompileTest.assertFail(this.getClass(), "final fields");
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-ct/src/test/java/org/openjdk/jmh/ct/params/invalid/finals/Short1Test.java	Tue Mar 03 21:31:20 2015 +0300
@@ -0,0 +1,50 @@
+/*
+ * 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.params.invalid.finals;
+
+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.ct.CompileTest;
+
+@State(Scope.Benchmark)
+public class Short1Test {
+
+    @Param("0")
+    public final short param = 0;
+
+    @Benchmark
+    public void test() {
+
+    }
+
+    @Test
+    public void compileTest() {
+        CompileTest.assertFail(this.getClass(), "final fields");
+    }
+
+}
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/core/FieldInfo.java	Tue Mar 03 21:24:10 2015 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/core/FieldInfo.java	Tue Mar 03 21:31:20 2015 +0300
@@ -63,4 +63,8 @@
      */
     boolean isStatic();
 
+    /**
+     * @return true, if field is final
+     */
+    boolean isFinal();
 }
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/core/StateObjectHandler.java	Tue Mar 03 21:24:10 2015 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/core/StateObjectHandler.java	Tue Mar 03 21:31:20 2015 +0300
@@ -221,6 +221,12 @@
                     fi);
         }
 
+        if (fi.isFinal()) {
+            throw new GenerationException(
+                    "@" + Param.class.getSimpleName() + " annotation is not acceptable on final fields.",
+                    fi);
+        }
+
         if (BenchmarkGeneratorUtils.getAnnSyntax(fi.getDeclaringClass(), State.class) == null) {
             throw new GenerationException(
                     "@" + Param.class.getSimpleName() + " annotation should be placed in @" + State.class.getSimpleName() +
--- a/jmh-generator-annprocess/src/main/java/org/openjdk/jmh/generators/annotations/APFieldInfo.java	Tue Mar 03 21:24:10 2015 +0300
+++ b/jmh-generator-annprocess/src/main/java/org/openjdk/jmh/generators/annotations/APFieldInfo.java	Tue Mar 03 21:31:20 2015 +0300
@@ -66,6 +66,11 @@
     }
 
     @Override
+    public boolean isFinal() {
+        return ve.getModifiers().contains(Modifier.FINAL);
+    }
+
+    @Override
     public <T extends Annotation> T getAnnotation(Class<T> annClass) {
         return ve.getAnnotation(annClass);
     }
--- a/jmh-generator-asm/src/main/java/org/openjdk/jmh/generators/asm/ASMFieldInfo.java	Tue Mar 03 21:24:10 2015 +0300
+++ b/jmh-generator-asm/src/main/java/org/openjdk/jmh/generators/asm/ASMFieldInfo.java	Tue Mar 03 21:31:20 2015 +0300
@@ -74,6 +74,11 @@
     }
 
     @Override
+    public boolean isFinal() {
+        return (access & Opcodes.ACC_FINAL) > 0;
+    }
+
+    @Override
     public <T extends Annotation> T getAnnotation(Class<T> annClass) {
         AnnotationInvocationHandler handler = annotations.get(annClass.getCanonicalName());
         if (handler == null) {
--- a/jmh-generator-reflection/src/main/java/org/openjdk/jmh/generators/reflection/RFFieldInfo.java	Tue Mar 03 21:24:10 2015 +0300
+++ b/jmh-generator-reflection/src/main/java/org/openjdk/jmh/generators/reflection/RFFieldInfo.java	Tue Mar 03 21:31:20 2015 +0300
@@ -69,4 +69,9 @@
     public boolean isStatic() {
         return Modifier.isStatic(f.getModifiers());
     }
+
+    @Override
+    public boolean isFinal() {
+        return Modifier.isFinal(f.getModifiers());
+    }
 }