changeset 1341:14809d1f5c61

7901779: JMH APT processor claims all annotations Summary: Should only capture JMH annotations.
author shade
date Mon, 05 Sep 2016 10:37:29 +0300
parents 2e5a7761ce12
children da0cc0a41bed
files jmh-core-it/src/main/java/org/openjdk/jmh/it/annsteal/CustomAnnotation.java jmh-core-it/src/main/java/org/openjdk/jmh/it/annsteal/CustomBenchmarkProcessor.java jmh-core-it/src/test/java/org/openjdk/jmh/it/annsteal/CustomTest.java jmh-core-it/src/test/resources/META-INF/services/javax.annotation.processing.Processor jmh-generator-annprocess/src/main/java/org/openjdk/jmh/generators/BenchmarkProcessor.java
diffstat 5 files changed, 188 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-it/src/main/java/org/openjdk/jmh/it/annsteal/CustomAnnotation.java	Mon Sep 05 10:37:29 2016 +0300
@@ -0,0 +1,35 @@
+/*
+ * 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.it.annsteal;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface CustomAnnotation {
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-it/src/main/java/org/openjdk/jmh/it/annsteal/CustomBenchmarkProcessor.java	Mon Sep 05 10:37:29 2016 +0300
@@ -0,0 +1,71 @@
+/*
+ * 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.it.annsteal;
+
+import javax.annotation.processing.AbstractProcessor;
+import javax.annotation.processing.RoundEnvironment;
+import javax.annotation.processing.SupportedAnnotationTypes;
+import javax.lang.model.SourceVersion;
+import javax.lang.model.element.Element;
+import javax.lang.model.element.TypeElement;
+import javax.tools.FileObject;
+import javax.tools.StandardLocation;
+import java.io.IOException;
+import java.io.Writer;
+import java.util.Set;
+
+@SupportedAnnotationTypes("*")
+public class CustomBenchmarkProcessor extends AbstractProcessor {
+
+    public static final String CBP_LIST = "tmp/CBP.list";
+
+    @Override
+    public SourceVersion getSupportedSourceVersion() {
+        // We may claim to support the latest version, since we are not using
+        // any version-specific extensions.
+        return SourceVersion.latest();
+    }
+
+    @Override
+    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
+        for (TypeElement te : annotations) {
+            if (te.getQualifiedName().toString().equals(CustomAnnotation.class.getCanonicalName())) {
+                try {
+                    FileObject file = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", CBP_LIST);
+                    Writer writer = file.openWriter();
+                    Set<? extends Element> els = roundEnv.getElementsAnnotatedWith(te);
+                    for (Element el : els) {
+                        writer.append(el.getSimpleName());
+                    }
+                    writer.close();
+                } catch (IOException e) {
+                    throw new IllegalStateException(e);
+                }
+            }
+        }
+        return false;
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-it/src/test/java/org/openjdk/jmh/it/annsteal/CustomTest.java	Mon Sep 05 10:37:29 2016 +0300
@@ -0,0 +1,53 @@
+/*
+ * 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.it.annsteal;
+
+import org.junit.Test;
+import org.junit.Assert;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.util.FileUtils;
+import org.openjdk.jmh.util.Utils;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Collection;
+
+@CustomAnnotation // here!
+public class CustomTest {
+
+    @Benchmark
+    public void bench() {
+        // intentionally left blank
+    }
+
+    @Test
+    public void test() throws IOException {
+        File file = FileUtils.extractFromResource('/' + CustomBenchmarkProcessor.CBP_LIST);
+        Collection<String> strings = FileUtils.readAllLines(file);
+        Assert.assertTrue(strings.contains(CustomTest.class.getSimpleName()));
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-it/src/test/resources/META-INF/services/javax.annotation.processing.Processor	Mon Sep 05 10:37:29 2016 +0300
@@ -0,0 +1,28 @@
+#    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.
+#
+
+# The order of these two processors is important: we want the JMH's processor
+# to fire first, and then see if it claims all JMH annotations, and nothing else.
+org.openjdk.jmh.generators.BenchmarkProcessor
+org.openjdk.jmh.it.annsteal.CustomBenchmarkProcessor
\ No newline at end of file
--- a/jmh-generator-annprocess/src/main/java/org/openjdk/jmh/generators/BenchmarkProcessor.java	Tue Aug 16 16:18:15 2016 +0300
+++ b/jmh-generator-annprocess/src/main/java/org/openjdk/jmh/generators/BenchmarkProcessor.java	Mon Sep 05 10:37:29 2016 +0300
@@ -38,7 +38,7 @@
 import javax.lang.model.element.TypeElement;
 import java.util.Set;
 
-@SupportedAnnotationTypes("*")
+@SupportedAnnotationTypes("org.openjdk.jmh.annotations.*")
 public class BenchmarkProcessor extends AbstractProcessor {
 
     private final BenchmarkGenerator generator = new BenchmarkGenerator();