incubator-cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ahu...@apache.org
Subject [28/50] [abbrv] git commit: utils: Reflection utilities to get cmd, annotation and fields
Date Wed, 09 Jan 2013 13:26:48 GMT
utils: Reflection utilities to get cmd, annotation and fields

Signed-off-by: Rohit Yadav <bhaisaab@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/db297a33
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/db297a33
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/db297a33

Branch: refs/heads/javelin
Commit: db297a338f8a96350c99a4316953cf3631728cd3
Parents: d22229e
Author: Rohit Yadav <bhaisaab@apache.org>
Authored: Tue Jan 8 18:36:31 2013 -0800
Committer: Rohit Yadav <bhaisaab@apache.org>
Committed: Tue Jan 8 18:54:29 2013 -0800

----------------------------------------------------------------------
 utils/pom.xml                              |    5 ++
 utils/src/com/cloud/utils/ReflectUtil.java |   63 +++++++++++++++++++++++
 2 files changed, 68 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/db297a33/utils/pom.xml
----------------------------------------------------------------------
diff --git a/utils/pom.xml b/utils/pom.xml
index c916888..87c078a 100644
--- a/utils/pom.xml
+++ b/utils/pom.xml
@@ -146,6 +146,11 @@
         <version>${cs.commons-io.version}</version>
         <scope>provided</scope>
     </dependency>
+    <dependency>
+      <groupId>org.reflections</groupId>
+      <artifactId>reflections</artifactId>
+      <version>${cs.reflections.version}</version>
+    </dependency>
   </dependencies>
   <build>
     <defaultGoal>install</defaultGoal>

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/db297a33/utils/src/com/cloud/utils/ReflectUtil.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/ReflectUtil.java b/utils/src/com/cloud/utils/ReflectUtil.java
index f1d1409..0944705 100755
--- a/utils/src/com/cloud/utils/ReflectUtil.java
+++ b/utils/src/com/cloud/utils/ReflectUtil.java
@@ -16,10 +16,14 @@
 // under the License.
 package com.cloud.utils;
 
+import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
+import java.util.HashSet;
+import java.util.Set;
 
 import com.cloud.utils.exception.CloudRuntimeException;
+import org.reflections.Reflections;
 
 public class ReflectUtil {
     public static Pair<Class<?>, Field> getAnyField(Class<?> clazz, String
fieldName) {
@@ -50,4 +54,63 @@ public class ReflectUtil {
         return null;
     }
 
+    // Gets all classes with some annotation from a package
+    public static Set<Class<?>> getClassesWithAnnotation(Class<? extends Annotation>
annotation,
+                                                         String[] packageNames) {
+        Reflections reflections;
+        Set<Class<?>> classes = new HashSet<Class<?>>();
+        for(String packageName: packageNames) {
+            reflections = new Reflections(packageName);
+            classes.addAll(reflections.getTypesAnnotatedWith(annotation));
+        }
+        return classes;
+    }
+
+    // Checks against posted search classes if cmd is async
+    public static boolean isCmdClassAsync(Class<?> cmdClass,
+                                          Class<?>[] searchClasses) {
+        boolean isAsync = false;
+        Class<?> superClass = cmdClass;
+
+        while (superClass != null && superClass != Object.class) {
+            String superName = superClass.getName();
+            for (Class<?> baseClass: searchClasses) {
+                if (superName.equals(baseClass.getName())) {
+                    isAsync = true;
+                    break;
+                }
+            }
+            if (isAsync)
+                break;
+            superClass = superClass.getSuperclass();
+        }
+        return isAsync;
+    }
+
+    // Returns all fields across the base class for a cmd
+    public static Field[] getAllFieldsForClass(Class<?> cmdClass,
+                                               Class<?>[] searchClasses) {
+        Field[] fields = cmdClass.getDeclaredFields();
+        Class<?> superClass = cmdClass.getSuperclass();
+
+        while (superClass != null && superClass != Object.class) {
+            String superName = superClass.getName();
+            for (Class<?> baseClass: searchClasses) {
+                if(!baseClass.isAssignableFrom(superClass))
+                    continue;
+                if (!superName.equals(baseClass.getName())) {
+                    Field[] superClassFields = superClass.getDeclaredFields();
+                    if (superClassFields != null) {
+                        Field[] tmpFields = new Field[fields.length + superClassFields.length];
+                        System.arraycopy(fields, 0, tmpFields, 0, fields.length);
+                        System.arraycopy(superClassFields, 0, tmpFields, fields.length, superClassFields.length);
+                        fields = tmpFields;
+                    }
+                }
+            }
+            superClass = superClass.getSuperclass();
+        }
+        return fields;
+    }
+
 }


Mime
View raw message