changeset 441:e285f2d94773

Yak shaving: Remove AnnUtils and GeneratorSource.getAnnotationRecursive() in favor of common methods.
author shade
date Fri, 28 Feb 2014 15:44:40 +0400
parents 3ccedf7d3298
children 0b204ee93eaf
files jmh-core/src/main/java/org/openjdk/jmh/generators/annotations/APClassInfo.java jmh-core/src/main/java/org/openjdk/jmh/generators/annotations/APMethodInfo.java jmh-core/src/main/java/org/openjdk/jmh/generators/annotations/AnnUtils.java jmh-core/src/main/java/org/openjdk/jmh/generators/bytecode/ASMClassInfo.java jmh-core/src/main/java/org/openjdk/jmh/generators/bytecode/ASMMethodInfo.java jmh-core/src/main/java/org/openjdk/jmh/generators/core/BenchmarkGenerator.java jmh-core/src/main/java/org/openjdk/jmh/generators/core/BenchmarkGeneratorUtils.java jmh-core/src/main/java/org/openjdk/jmh/generators/core/ParamValidationPlugin.java jmh-core/src/main/java/org/openjdk/jmh/generators/reflective/ReflectiveClassInfo.java jmh-core/src/main/java/org/openjdk/jmh/generators/source/ClassInfo.java jmh-core/src/main/java/org/openjdk/jmh/generators/source/FieldInfo.java jmh-core/src/main/java/org/openjdk/jmh/generators/source/MethodGroup.java jmh-core/src/main/java/org/openjdk/jmh/generators/source/MethodInfo.java
diffstat 13 files changed, 68 insertions(+), 120 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/annotations/APClassInfo.java	Fri Feb 28 15:00:35 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/annotations/APClassInfo.java	Fri Feb 28 15:44:40 2014 +0400
@@ -29,8 +29,11 @@
 import org.openjdk.jmh.generators.source.MethodInfo;
 
 import javax.annotation.processing.ProcessingEnvironment;
+import javax.lang.model.element.Element;
+import javax.lang.model.element.ElementKind;
 import javax.lang.model.element.ExecutableElement;
 import javax.lang.model.element.Modifier;
+import javax.lang.model.element.PackageElement;
 import javax.lang.model.element.TypeElement;
 import javax.lang.model.element.VariableElement;
 import javax.lang.model.type.TypeKind;
@@ -56,11 +59,6 @@
     }
 
     @Override
-    public <T extends Annotation> T getAnnotationRecursive(Class<T> annClass) {
-        return AnnUtils.getAnnotationRecursive(el, annClass);
-    }
-
-    @Override
     public Collection<MethodInfo> getConstructors() {
         Collection<MethodInfo> mis = new ArrayList<MethodInfo>();
         for (ExecutableElement e : ElementFilter.constructorsIn(el.getEnclosedElements())) {
@@ -71,7 +69,13 @@
 
     @Override
     public String getNestedName() {
-        return AnnUtils.getNestedName(el);
+        String name = "";
+        Element walk = el;
+        while (walk.getKind() != ElementKind.PACKAGE) {
+            name = walk.getSimpleName().toString() + (name.isEmpty() ? "" : "_" + name);
+            walk = walk.getEnclosingElement();
+        }
+        return name.substring(0, name.length());
     }
 
     @Override
@@ -100,7 +104,11 @@
 
     @Override
     public String getPackageName() {
-        return AnnUtils.getPackageName(el);
+        Element walk = el;
+        while (walk.getKind() != ElementKind.PACKAGE) {
+            walk = walk.getEnclosingElement();
+        }
+        return ((PackageElement)walk).getQualifiedName().toString();
     }
 
     @Override
@@ -108,10 +116,20 @@
         TypeMirror superclass = el.getSuperclass();
         if (superclass.getKind() == TypeKind.NONE) {
             return null;
+        } else {
+            TypeElement element = (TypeElement) processEnv.getTypeUtils().asElement(superclass);
+            return new APClassInfo(processEnv, element);
         }
+    }
 
-        TypeElement element = (TypeElement) processEnv.getTypeUtils().asElement(superclass);
-        return new APClassInfo(processEnv, element);
+    @Override
+    public ClassInfo getEnclosingClass() {
+        Element enclosingElement = el.getEnclosingElement();
+        if (enclosingElement.getKind() == ElementKind.CLASS) {
+            return new APClassInfo(processEnv, (TypeElement) enclosingElement);
+        } else {
+            return null;
+        }
     }
 
     @Override
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/annotations/APMethodInfo.java	Fri Feb 28 15:00:35 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/annotations/APMethodInfo.java	Fri Feb 28 15:44:40 2014 +0400
@@ -77,11 +77,6 @@
     }
 
     @Override
-    public <T extends Annotation> T getAnnotationRecursive(Class<T> annClass) {
-        return AnnUtils.getAnnotationRecursive(el, annClass);
-    }
-
-    @Override
     public boolean isPublic() {
         return el.getModifiers().contains(Modifier.PUBLIC);
     }
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/annotations/AnnUtils.java	Fri Feb 28 15:00:35 2014 +0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,79 +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.generators.annotations;
-
-import javax.lang.model.element.Element;
-import javax.lang.model.element.ElementKind;
-import javax.lang.model.element.PackageElement;
-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 ((PackageElement)walk).getQualifiedName().toString();
-    }
-
-    /**
-     * Get the class name along with any nested class names
-     * @param clazz the subject
-     * @return the synthetic class name in form of "parent1_parent2_classname"
-     */
-    public static String getNestedName(TypeElement clazz) {
-        String name = "";
-        Element walk = clazz;
-        while (walk.getKind() != ElementKind.PACKAGE) {
-            name = walk.getSimpleName().toString() + (name.isEmpty() ? "" : "_" + name);
-            walk = walk.getEnclosingElement();
-        }
-        return name.substring(0, name.length());
-    }
-}
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/bytecode/ASMClassInfo.java	Fri Feb 28 15:00:35 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/bytecode/ASMClassInfo.java	Fri Feb 28 15:44:40 2014 +0400
@@ -155,8 +155,9 @@
     }
 
     @Override
-    public <T extends Annotation> T getAnnotationRecursive(Class<T> annClass) {
-        return getAnnotation(annClass); // TODO: FIXME
+    public ClassInfo getEnclosingClass() {
+        // TODO: FIXME
+        return null;
     }
 
     @Override
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/bytecode/ASMMethodInfo.java	Fri Feb 28 15:00:35 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/bytecode/ASMMethodInfo.java	Fri Feb 28 15:44:40 2014 +0400
@@ -71,16 +71,6 @@
     }
 
     @Override
-    public <T extends Annotation> T getAnnotationRecursive(Class<T> annClass) {
-        T ann = getAnnotation(annClass);
-        if (ann != null) {
-            return ann;
-        } else {
-            return classInfo.getAnnotation(annClass);
-        }
-    }
-
-    @Override
     public AnnotationVisitor visitAnnotation(final String desc, boolean visible) {
         AnnHandler annHandler = new AnnHandler(super.visitAnnotation(desc, visible));
         annotations.put(Type.getType(desc).getClassName(), annHandler);
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/core/BenchmarkGenerator.java	Fri Feb 28 15:00:35 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/core/BenchmarkGenerator.java	Fri Feb 28 15:44:40 2014 +0400
@@ -288,7 +288,7 @@
 
         // check annotations
         for (MethodInfo m : methods) {
-            OperationsPerInvocation opi = m.getAnnotationRecursive(OperationsPerInvocation.class);
+            OperationsPerInvocation opi = BenchmarkGeneratorUtils.getAnnotationRecursive(m, OperationsPerInvocation.class);
             if (opi != null && opi.value() < 1) {
                 throw new GenerationException("The " + OperationsPerInvocation.class.getSimpleName() +
                         " needs to be greater than 0.", m);
@@ -364,7 +364,7 @@
                 result.put(groupName, group);
             }
 
-            BenchmarkMode mbAn = method.getAnnotationRecursive(BenchmarkMode.class);
+            BenchmarkMode mbAn = BenchmarkGeneratorUtils.getAnnotationRecursive(method, BenchmarkMode.class);
             if (mbAn != null) {
                 group.addModes(mbAn.value());
             }
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/core/BenchmarkGeneratorUtils.java	Fri Feb 28 15:00:35 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/core/BenchmarkGeneratorUtils.java	Fri Feb 28 15:44:40 2014 +0400
@@ -86,4 +86,26 @@
         return ls;
     }
 
+    public static <T extends Annotation> T getAnnotationRecursive(ClassInfo ci, Class<T> annClass) {
+        T ann = ci.getAnnotation(annClass);
+        if (ann != null) {
+            return ann;
+        } else {
+            ClassInfo eci = ci.getEnclosingClass();
+            if (eci != null) {
+                return getAnnotationRecursive(eci, annClass);
+            }
+        }
+        return null;
+    }
+
+    public static <T extends Annotation> T getAnnotationRecursive(MethodInfo mi, Class<T> annClass) {
+        T ann = mi.getAnnotation(annClass);
+        if (ann != null) {
+            return ann;
+        } else {
+            return getAnnotationRecursive(mi.getOwner(), annClass);
+        }
+    }
+
 }
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/core/ParamValidationPlugin.java	Fri Feb 28 15:00:35 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/core/ParamValidationPlugin.java	Fri Feb 28 15:44:40 2014 +0400
@@ -42,7 +42,7 @@
                     );
                 }
 
-                if (element.getOwner().getAnnotationRecursive(State.class) == null) {
+                if (BenchmarkGeneratorUtils.getAnnotationRecursive(element.getOwner(), State.class) == null) {
                     source.printError(
                             "@" + Param.class.getSimpleName() + " annotation should be placed in @" + State.class.getSimpleName() +
                                     "-annotated class.",
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/reflective/ReflectiveClassInfo.java	Fri Feb 28 15:00:35 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/reflective/ReflectiveClassInfo.java	Fri Feb 28 15:44:40 2014 +0400
@@ -83,14 +83,14 @@
     }
 
     @Override
-    public <T extends Annotation> T getAnnotation(Class<T> annClass) {
-        return klass.getAnnotation(annClass);
+    public ClassInfo getEnclosingClass() {
+        // FIXME
+        return null;
     }
 
     @Override
-    public <T extends Annotation> T getAnnotationRecursive(Class<T> annClass) {
-        // TODO: FIXME
-        return getAnnotation(annClass);
+    public <T extends Annotation> T getAnnotation(Class<T> annClass) {
+        return klass.getAnnotation(annClass);
     }
 
     @Override
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/source/ClassInfo.java	Fri Feb 28 15:00:35 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/source/ClassInfo.java	Fri Feb 28 15:44:40 2014 +0400
@@ -40,6 +40,8 @@
 
     ClassInfo getSuperclass();
 
+    ClassInfo getEnclosingClass();
+
     Collection<FieldInfo> getFields();
 
     Collection<MethodInfo> getMethods();
@@ -48,8 +50,6 @@
 
     <T extends Annotation> T getAnnotation(Class<T> annClass);
 
-    <T extends Annotation> T getAnnotationRecursive(Class<T> annClass);
-
     boolean isAbstract();
 
     boolean isPublic();
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/source/FieldInfo.java	Fri Feb 28 15:00:35 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/source/FieldInfo.java	Fri Feb 28 15:44:40 2014 +0400
@@ -28,15 +28,16 @@
 
 public interface FieldInfo extends MetadataInfo {
 
+    ClassInfo getOwner();
+
     String getName();
 
     String getType();
 
+    <T extends Annotation> T getAnnotation(Class<T> annClass);
+
     boolean isPublic();
 
     boolean isStatic();
 
-    <T extends Annotation> T getAnnotation(Class<T> annClass);
-
-    ClassInfo getOwner();
 }
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/source/MethodGroup.java	Fri Feb 28 15:00:35 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/source/MethodGroup.java	Fri Feb 28 15:44:40 2014 +0400
@@ -32,6 +32,7 @@
 import org.openjdk.jmh.annotations.Param;
 import org.openjdk.jmh.annotations.Threads;
 import org.openjdk.jmh.annotations.Warmup;
+import org.openjdk.jmh.generators.core.BenchmarkGeneratorUtils;
 import org.openjdk.jmh.runner.parameters.TimeValue;
 import org.openjdk.jmh.util.internal.Optional;
 
@@ -241,7 +242,7 @@
     private <T extends Annotation> T getFinal(Class<T> klass) {
         T finalAnn = null;
         for (MethodInvocation mi : methods) {
-            T ann = mi.method.getAnnotationRecursive(klass);
+            T ann = BenchmarkGeneratorUtils.getAnnotationRecursive(mi.method, klass);
             if (ann != null && finalAnn != null) {
                 if (!finalAnn.equals(ann)) {
                     throw new GenerationException("Colliding annotations: " + ann + " vs. " + finalAnn, mi.method);
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/source/MethodInfo.java	Fri Feb 28 15:00:35 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/source/MethodInfo.java	Fri Feb 28 15:44:40 2014 +0400
@@ -38,7 +38,6 @@
     Collection<ParameterInfo> getParameters();
 
     <T extends Annotation> T getAnnotation(Class<T> annClass);
-    <T extends Annotation> T getAnnotationRecursive(Class<T> annClass);
 
     boolean isPublic();
     boolean isAbstract();