changeset 317:f6ae869cd345

Yak shaving: move some utilities to AnnUtils.
author shade
date Fri, 06 Dec 2013 18:45:31 +0400
parents 9dfe9965fbf4
children 9497a55f865f
files jmh-core/src/main/java/org/openjdk/jmh/processor/internal/AnnUtils.java jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java
diffstat 2 files changed, 67 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/AnnUtils.java	Fri Dec 06 18:45:31 2013 +0400
@@ -0,0 +1,64 @@
+/*
+ * 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.processor.internal;
+
+import javax.lang.model.element.Element;
+import javax.lang.model.element.ElementKind;
+import javax.lang.model.element.TypeElement;
+import java.lang.annotation.Annotation;
+
+public class AnnUtils {
+
+    /**
+     * Recursively get the annotation, until found, or reached the top of hierarchy.
+     * @param root where to start
+     * @param annotation what to look for
+     * @param <A> type of what we look for
+     * @return annotation
+     */
+    public static <A extends Annotation> A getAnnotationRecursive(Element root, Class<A> annotation) {
+        Element walk = root;
+        A result = null;
+        while (walk != null && (result = walk.getAnnotation(annotation)) == null) {
+            walk = walk.getEnclosingElement();
+        }
+        return result;
+    }
+
+    /**
+     * Get the package name part of a class
+     *
+     * @param clazz the subject
+     * @return the package name or "" if no package
+     */
+    public static String getPackageName(TypeElement clazz) {
+        Element walk = clazz;
+        while (walk.getKind() != ElementKind.PACKAGE) {
+            walk = walk.getEnclosingElement();
+        }
+        return walk.toString();
+    }
+
+}
--- a/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java	Fri Dec 06 17:24:45 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/processor/internal/GenerateMicroBenchmarkProcessor.java	Fri Dec 06 18:45:31 2013 +0400
@@ -172,7 +172,7 @@
      * Do basic benchmark validation.
      */
     private void validateBenchmark(TypeElement clazz, Collection<? extends Element> methods) {
-        if (packageName(clazz).isEmpty()) {
+        if (AnnUtils.getPackageName(clazz).isEmpty()) {
             throw new GenerationException("Microbenchmark should have package other than default.", clazz);
         }
 
@@ -283,14 +283,9 @@
                 result.put(groupName, group);
             }
 
-            BenchmarkMode mbAn = method.getAnnotation(BenchmarkMode.class);
+            BenchmarkMode mbAn = AnnUtils.getAnnotationRecursive(method, BenchmarkMode.class);
             if (mbAn != null) {
                 group.addModes(mbAn.value());
-            } else {
-                mbAn = method.getEnclosingElement().getAnnotation(BenchmarkMode.class);
-                if (mbAn != null) {
-                    group.addModes(mbAn.value());
-                }
             }
 
             group.addStrictFP(classStrictFP);
@@ -305,7 +300,7 @@
             }
         }
 
-        String sourcePackage = packageName(clazz);
+        String sourcePackage = AnnUtils.getPackageName(clazz);
         String generatedPackageName = sourcePackage + ".generated";
         String generatedClassName = clazz.getSimpleName().toString();
 
@@ -510,25 +505,6 @@
         return true;
     }
 
-
-    /**
-     * Get the package name part of a class
-     *
-     * @param clazz
-     * @return the package name or "" if no package
-     */
-    private static String packageName(TypeElement clazz) {
-        String fullName = clazz.getQualifiedName().toString();
-        int index = fullName.lastIndexOf('.');
-
-        if (index > 0) {
-            return fullName.substring(0, index);
-        }
-
-        return "";
-    }
-
-
     private TimeUnit findTimeUnit(MethodGroup methodGroup) {
         OutputTimeUnit ann = methodGroup.methods().iterator().next().getEnclosingElement().getAnnotation(OutputTimeUnit.class);
         for (Element method : methodGroup.methods()) {