changeset 1322:13ae22114e22

7901706: @State classes are not validated when accessed as dependency
author shade
date Thu, 23 Jun 2016 20:18:50 +0300
parents 448bd18c85f6
children 086e57a61659
files jmh-core-ct/src/test/java/org/openjdk/jmh/ct/states/dag/AbstractDependencyTest.java jmh-core/src/main/java/org/openjdk/jmh/generators/core/BenchmarkGenerator.java jmh-core/src/main/java/org/openjdk/jmh/generators/core/StateObjectHandler.java jmh-generator-annprocess/src/main/java/org/openjdk/jmh/generators/annotations/APClassInfo.java
diffstat 4 files changed, 165 insertions(+), 78 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/states/dag/AbstractDependencyTest.java	Thu Jun 23 20:18:50 2016 +0300
@@ -0,0 +1,65 @@
+/*
+ * 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.states.dag;
+
+import org.junit.Test;
+import org.openjdk.jmh.annotations.*;
+import org.openjdk.jmh.ct.CompileTest;
+
+public class AbstractDependencyTest {
+
+    @State(Scope.Benchmark)
+    public abstract static class A {
+        @Setup(Level.Trial)
+        public void setup() {}
+
+        protected abstract void helper();
+    }
+
+    public static class CA extends A {
+        @Override
+        protected void helper() {
+            // do nothing
+        }
+    }
+
+    @State(Scope.Thread)
+    public static class B {
+        @Setup(Level.Trial)
+        public void setup(A b2) {}
+
+    }
+
+    @Benchmark
+    public void test(CA ca, B b) {
+
+    }
+
+    @Test
+    public void compileTest() {
+        CompileTest.assertFail(this.getClass(), "abstract");
+    }
+
+}
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/core/BenchmarkGenerator.java	Mon May 30 20:13:31 2016 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/core/BenchmarkGenerator.java	Thu Jun 23 20:18:50 2016 +0300
@@ -42,7 +42,6 @@
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.Reader;
-import java.lang.annotation.IncompleteAnnotationException;
 import java.lang.reflect.Field;
 import java.util.*;
 import java.util.concurrent.TimeUnit;
@@ -262,77 +261,20 @@
             throw new GenerationException("Benchmark classes should not be final.", clazz);
         }
 
-        Collection<ClassInfo> states = new ArrayList<ClassInfo>();
-
         // validate all arguments are @State-s
         for (MethodInfo e : methods) {
-            for (ParameterInfo var : e.getParameters()) {
-                if (BenchmarkGeneratorUtils.getAnnSuper(var.getType(), State.class) == null) {
-                    throw new GenerationException(
-                            "Method parameters should be @" + State.class.getSimpleName() + " classes.",
-                            e);
-                }
-                states.add(var.getType());
-            }
+            StateObjectHandler.validateStateArgs(e);
         }
 
+        boolean explicitState = BenchmarkGeneratorUtils.getAnnSuper(clazz, State.class) != null;
+
         // validate if enclosing class is implicit @State
-        if (BenchmarkGeneratorUtils.getAnnSuper(clazz, State.class) != null) {
-            states.add(clazz);
-        }
-
-        // validate @State classes
-        for (ClassInfo state : states) {
-            // Because of https://bugs.openjdk.java.net/browse/JDK-8031122,
-            // we need to preemptively check the annotation value, and
-            // the API can only allow that by catching the exception, argh.
-            try {
-                BenchmarkGeneratorUtils.getAnnSuper(state, State.class).value();
-            } catch (IncompleteAnnotationException iae) {
-                throw new GenerationException("The " + State.class.getSimpleName() +
-                        " annotation should have the explicit " + Scope.class.getSimpleName() + " argument",
-                        state);
-            }
-
-            if (!state.isPublic()) {
-                throw new GenerationException("The " + State.class.getSimpleName() +
-                        " annotation only supports public classes.", state);
-            }
-
-            if (state.isFinal()) {
-                throw new GenerationException("The " + State.class.getSimpleName() +
-                        " annotation does not support final classes.", state);
-            }
-
-            if (state.isInner()) {
-                throw new GenerationException("The " + State.class.getSimpleName() +
-                        " annotation does not support inner classes, make sure your class is static.", state);
-            }
-
-            if (state.isAbstract()) {
-                throw new GenerationException("The " + State.class.getSimpleName() +
-                        " annotation does not support abstract classes.", state);
-            }
-
-            boolean hasDefaultConstructor = false;
-            for (MethodInfo constructor : state.getConstructors()) {
-                hasDefaultConstructor |= (constructor.getParameters().isEmpty() && constructor.isPublic());
-            }
-
-            // These classes use the special init sequence:
-            hasDefaultConstructor |= state.getQualifiedName().equals(BenchmarkParams.class.getCanonicalName());
-            hasDefaultConstructor |= state.getQualifiedName().equals(IterationParams.class.getCanonicalName());
-            hasDefaultConstructor |= state.getQualifiedName().equals(ThreadParams.class.getCanonicalName());
-
-            if (!hasDefaultConstructor) {
-                throw new GenerationException("The " + State.class.getSimpleName() +
-                        " annotation can only be applied to the classes having the default public constructor.",
-                        state);
-            }
+        if (explicitState) {
+            StateObjectHandler.validateState(clazz);
         }
 
         // validate against rogue fields
-        if (BenchmarkGeneratorUtils.getAnnSuper(clazz, State.class) == null || clazz.isAbstract()) {
+        if (!explicitState || clazz.isAbstract()) {
             for (FieldInfo fi : BenchmarkGeneratorUtils.getAllFields(clazz)) {
                 // allow static fields
                 if (fi.isStatic()) continue;
@@ -345,29 +287,16 @@
 
         // validate rogue annotations on classes
         BenchmarkGeneratorUtils.checkAnnotations(clazz);
-        for (ClassInfo state : states) {
-            BenchmarkGeneratorUtils.checkAnnotations(state);
-        }
 
         // validate rogue annotations on fields
         for (FieldInfo fi : BenchmarkGeneratorUtils.getAllFields(clazz)) {
             BenchmarkGeneratorUtils.checkAnnotations(fi);
         }
-        for (ClassInfo state : states) {
-            for (FieldInfo fi : BenchmarkGeneratorUtils.getAllFields(state)) {
-                BenchmarkGeneratorUtils.checkAnnotations(fi);
-            }
-        }
 
         // validate rogue annotations on methods
         for (MethodInfo mi : methods) {
             BenchmarkGeneratorUtils.checkAnnotations(mi);
         }
-        for (ClassInfo state : states) {
-            for (MethodInfo mi : BenchmarkGeneratorUtils.getMethods(state)) {
-                BenchmarkGeneratorUtils.checkAnnotations(mi);
-            }
-        }
 
         // check modifiers
         for (MethodInfo m : methods) {
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/core/StateObjectHandler.java	Mon May 30 20:13:31 2016 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/core/StateObjectHandler.java	Thu Jun 23 20:18:50 2016 +0300
@@ -35,6 +35,7 @@
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.lang.annotation.Annotation;
+import java.lang.annotation.IncompleteAnnotationException;
 import java.util.*;
 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
 
@@ -63,6 +64,92 @@
         this.identifiers = new Identifiers();
     }
 
+    public static void validateState(ClassInfo state) {
+        // Because of https://bugs.openjdk.java.net/browse/JDK-8031122,
+        // we need to preemptively check the annotation value, and
+        // the API can only allow that by catching the exception, argh.
+        try {
+            State ann = BenchmarkGeneratorUtils.getAnnSuper(state, State.class);
+            if (ann != null) {
+                ann.value();
+            }
+        } catch (IncompleteAnnotationException iae) {
+            throw new GenerationException("The @" + State.class.getSimpleName() +
+                    " annotation should have the explicit " + Scope.class.getSimpleName() + " argument",
+                    state);
+        }
+
+        if (!state.isPublic()) {
+            throw new GenerationException("The instantiated @" + State.class.getSimpleName() +
+                    " annotation only supports public classes.", state);
+        }
+
+        if (state.isFinal()) {
+            throw new GenerationException("The instantiated @" + State.class.getSimpleName() +
+                    " annotation does not support final classes. This class is not " , state);
+        }
+
+        if (state.isInner()) {
+            throw new GenerationException("The instantiated @" + State.class.getSimpleName() +
+                    " annotation does not support inner classes, make sure your class is static.", state);
+        }
+
+        if (state.isAbstract()) {
+            throw new GenerationException("The instantiated @" + State.class.getSimpleName() +
+                    " class cannot be abstract.", state);
+        }
+
+        boolean hasDefaultConstructor = false;
+        for (MethodInfo constructor : state.getConstructors()) {
+            hasDefaultConstructor |= (constructor.getParameters().isEmpty() && constructor.isPublic());
+        }
+
+        // These classes use the special init sequence:
+        hasDefaultConstructor |= state.getQualifiedName().equals(BenchmarkParams.class.getCanonicalName());
+        hasDefaultConstructor |= state.getQualifiedName().equals(IterationParams.class.getCanonicalName());
+        hasDefaultConstructor |= state.getQualifiedName().equals(ThreadParams.class.getCanonicalName());
+
+        if (!hasDefaultConstructor) {
+            throw new GenerationException("The @" + State.class.getSimpleName() +
+                    " annotation can only be applied to the classes having the default public constructor.",
+                    state);
+        }
+
+        // validate rogue annotations on classes
+        BenchmarkGeneratorUtils.checkAnnotations(state);
+        for (FieldInfo fi : BenchmarkGeneratorUtils.getAllFields(state)) {
+            BenchmarkGeneratorUtils.checkAnnotations(fi);
+        }
+
+        // validate rogue annotations on methods
+        for (MethodInfo mi : BenchmarkGeneratorUtils.getMethods(state)) {
+            BenchmarkGeneratorUtils.checkAnnotations(mi);
+        }
+
+        // check @Setup/@TearDown have only @State arguments
+        for (MethodInfo mi : BenchmarkGeneratorUtils.getAllMethods(state)) {
+            if (mi.getAnnotation(Setup.class) != null || mi.getAnnotation(TearDown.class) != null) {
+                for (ParameterInfo var : mi.getParameters()) {
+                    if (BenchmarkGeneratorUtils.getAnnSuper(var.getType(), State.class) == null) {
+                        throw new GenerationException(
+                                "Method parameters should be @" + State.class.getSimpleName() + " classes.",
+                                mi);
+                    }
+                }
+            }
+        }
+    }
+
+    public static void validateStateArgs(MethodInfo e) {
+        for (ParameterInfo var : e.getParameters()) {
+            if (BenchmarkGeneratorUtils.getAnnSuper(var.getType(), State.class) == null) {
+                throw new GenerationException(
+                        "Method parameters should be @" + State.class.getSimpleName() + " classes.",
+                        e);
+            }
+        }
+    }
+
     public State getState(ClassInfo ci, ParameterInfo pi) {
         State ann = BenchmarkGeneratorUtils.getAnnSuper(ci, State.class);
         if (ann == null) {
@@ -75,6 +162,9 @@
         for (MethodInfo method : mg.methods()) {
             Set<StateObject> seen = new HashSet<StateObject>();
 
+            // Check that all arguments are states.
+            validateStateArgs(method);
+
             for (ParameterInfo ppi : method.getParameters()) {
                 ClassInfo pci = ppi.getType();
 
@@ -130,6 +220,9 @@
     }
 
     private void bindState(MethodInfo execMethod, StateObject so, ClassInfo ci) {
+        // Check it is a valid state
+        validateState(ci);
+
         // auxiliary result, produce the accessors
         if (ci.getAnnotation(AuxCounters.class) != null) {
             if (so.scope != Scope.Thread) {
--- a/jmh-generator-annprocess/src/main/java/org/openjdk/jmh/generators/annotations/APClassInfo.java	Mon May 30 20:13:31 2016 +0300
+++ b/jmh-generator-annprocess/src/main/java/org/openjdk/jmh/generators/annotations/APClassInfo.java	Thu Jun 23 20:18:50 2016 +0300
@@ -62,7 +62,7 @@
     }
 
     public APClassInfo(ProcessingEnvironment processEnv, TypeMirror mirror) {
-        super(processEnv, null);
+        super(processEnv, processEnv.getTypeUtils().asElement(mirror));
         this.mirror = mirror;
         this.isSpecial = mirror.getKind().isPrimitive() || (mirror.getKind() == TypeKind.ARRAY);
         if (isSpecial) {