changeset 188:90631ae36974

Force @Group(<tag>) tag to be valid Java identifier, to evade some nasty collisions.
author shade
date Thu, 26 Sep 2013 21:05:45 +0400
parents 5aa156267caa
children c6ec28d46af3
files jmh-core-it/src/test/java/org/openjdk/jmh/it/compile/GroupLabelTest.java jmh-core/src/main/java/org/openjdk/jmh/annotations/Group.java jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java
diffstat 3 files changed, 14 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core-it/src/test/java/org/openjdk/jmh/it/compile/GroupLabelTest.java	Thu Sep 26 20:17:25 2013 +0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,41 +0,0 @@
-/*
- * 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;
-
-import org.openjdk.jmh.annotations.BenchmarkMode;
-import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
-import org.openjdk.jmh.annotations.Group;
-import org.openjdk.jmh.annotations.Mode;
-
-@BenchmarkMode(Mode.All)
-public class GroupLabelTest {
-
-    @GenerateMicroBenchmark
-    @Group("my_%_forbidden-java-identifier")
-    public void test() {
-
-    }
-
-}
--- a/jmh-core/src/main/java/org/openjdk/jmh/annotations/Group.java	Thu Sep 26 20:17:25 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/annotations/Group.java	Thu Sep 26 21:05:45 2013 +0400
@@ -42,6 +42,7 @@
 
     /**
      * Group tag.
+     * Should be valid Java identifier.
      */
     String value() default "";
 }
--- a/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java	Thu Sep 26 20:17:25 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java	Thu Sep 26 21:05:45 2013 +0400
@@ -200,9 +200,16 @@
 
             Group groupAnn = method.getAnnotation(Group.class);
             String groupName = (groupAnn != null) ? groupAnn.value() : method.getSimpleName().toString();
+
+            if (!checkJavaIdentifier(groupName)) {
+                processingEnv.getMessager().printMessage(Kind.ERROR,
+                        "Group name should be the legal Java identifier. (" + clazz + ")", method);
+                return null;
+            }
+
             MethodGroup group = result.get(groupName);
             if (group == null) {
-                group = new MethodGroup(convertToJavaIdentfier(groupName));
+                group = new MethodGroup(groupName);
                 result.put(groupName, group);
             }
 
@@ -242,17 +249,14 @@
         return new BenchmarkInfo(clazz.getQualifiedName().toString(), generatedPackageName, generatedClassName, result);
     }
 
-    public static String convertToJavaIdentfier(String id) {
-        char[] result = new char[id.length()];
-        for (int i = 0; i < result.length; i++) {
+    public static boolean checkJavaIdentifier(String id) {
+        for (int i = 0; i < id.length(); i++) {
             char c = id.charAt(i);
-            if (Character.isJavaIdentifierPart(c)) {
-                result[i] = c;
-            } else {
-                result[i] = '_';
+            if (!Character.isJavaIdentifierPart(c)) {
+                return false;
             }
         }
-        return String.valueOf(result);
+        return true;
     }
 
     /**