You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by ko...@apache.org on 2013/10/09 20:41:58 UTC
[1/2] git commit: updated refs/heads/master to d1dd85a
Updated Branches:
refs/heads/master 0d7aa931b -> d1dd85a4c
ReflectUtil: test for getAllFieldsForClass
- code comment turned to javadoc
- added braces to if and for statements to make it look more like the rest
- tests added
Signed-off-by: Laszlo Hornyak <la...@gmail.com>
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/d1dd85a4
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/d1dd85a4
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/d1dd85a4
Branch: refs/heads/master
Commit: d1dd85a4c42d1dfc282a0b71407ac668701ccd12
Parents: 66fe3ab
Author: Laszlo Hornyak <la...@gmail.com>
Authored: Tue Oct 8 21:02:58 2013 +0200
Committer: Laszlo Hornyak <la...@gmail.com>
Committed: Wed Oct 9 20:39:41 2013 +0200
----------------------------------------------------------------------
utils/src/com/cloud/utils/ReflectUtil.java | 16 +++--
utils/test/com/cloud/utils/ReflectUtilTest.java | 63 ++++++++++++++++++++
2 files changed, 75 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d1dd85a4/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 930e0b6..fecd896 100755
--- a/utils/src/com/cloud/utils/ReflectUtil.java
+++ b/utils/src/com/cloud/utils/ReflectUtil.java
@@ -106,7 +106,12 @@ public class ReflectUtil {
return fields;
}
- // Returns all unique fields except excludeClasses for a cmd class
+ /**
+ * Returns all unique fields except excludeClasses for a cmd class
+ * @param cmdClass the class in which fields should be collected
+ * @param excludeClasses the classes whose fields must be ignored
+ * @return list of fields
+ */
public static Set<Field> getAllFieldsForClass(Class<?> cmdClass,
Class<?>[] excludeClasses) {
Set<Field> fields = new HashSet<Field>();
@@ -116,14 +121,17 @@ public class ReflectUtil {
while (superClass != null && superClass != Object.class) {
String superName = superClass.getName();
boolean isNameEqualToSuperName = false;
- for (Class<?> baseClass: excludeClasses)
- if (superName.equals(baseClass.getName()))
+ for (Class<?> baseClass: excludeClasses) {
+ if (superName.equals(baseClass.getName())) {
isNameEqualToSuperName = true;
+ }
+ }
if (!isNameEqualToSuperName) {
Field[] superClassFields = superClass.getDeclaredFields();
- if (superClassFields != null)
+ if (superClassFields != null) {
Collections.addAll(fields, superClassFields);
+ }
}
superClass = superClass.getSuperclass();
}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d1dd85a4/utils/test/com/cloud/utils/ReflectUtilTest.java
----------------------------------------------------------------------
diff --git a/utils/test/com/cloud/utils/ReflectUtilTest.java b/utils/test/com/cloud/utils/ReflectUtilTest.java
index 4c8eb50..fc1cda6 100644
--- a/utils/test/com/cloud/utils/ReflectUtilTest.java
+++ b/utils/test/com/cloud/utils/ReflectUtilTest.java
@@ -16,7 +16,11 @@
// under the License.package com.cloud.utils;
package com.cloud.utils;
+import java.lang.reflect.Field;
import java.util.List;
+import java.util.Set;
+
+import org.junit.Assert;
import org.junit.Test;
import static com.cloud.utils.ReflectUtil.flattenProperties;
@@ -88,4 +92,63 @@ public final class ReflectUtilTest {
}
}
+
+ static class Empty {
+ }
+
+ static class Foo {
+ String fooField;
+ int fooIntField;
+ }
+
+ static class Bar extends Foo {
+ String barField;
+ int barIntField;
+ }
+
+ static class Baz extends Foo {
+ String bazField;
+ int bazIntField;
+ }
+
+ @Test
+ public void getAllFieldsForClassWithFoo() throws NoSuchFieldException, SecurityException {
+ Set<Field> fooFields = ReflectUtil.getAllFieldsForClass(Foo.class, new Class<?> [] {});
+ Assert.assertNotNull(fooFields);
+ Assert.assertEquals(2, fooFields.size());
+ Assert.assertTrue(fooFields.contains(Foo.class.getDeclaredField("fooField")));
+ Assert.assertTrue(fooFields.contains(Foo.class.getDeclaredField("fooIntField")));
+ }
+
+ @Test
+ public void getAllFieldsForClassWithBar() throws NoSuchFieldException, SecurityException {
+ Set<Field> barFields = ReflectUtil.getAllFieldsForClass(Bar.class, new Class<?> [] {});
+ Assert.assertNotNull(barFields);
+ Assert.assertEquals(4, barFields.size());
+ Assert.assertTrue(barFields.contains(Foo.class.getDeclaredField("fooField")));
+ Assert.assertTrue(barFields.contains(Foo.class.getDeclaredField("fooIntField")));
+ Assert.assertTrue(barFields.contains(Bar.class.getDeclaredField("barField")));
+ Assert.assertTrue(barFields.contains(Bar.class.getDeclaredField("barIntField")));
+ }
+
+ @Test
+ public void getAllFieldsForClassWithBarWithoutFoo() throws NoSuchFieldException, SecurityException {
+ Set<Field> barFields = ReflectUtil.getAllFieldsForClass(Bar.class, new Class<?> [] {Foo.class});
+ Assert.assertNotNull(barFields);
+ Assert.assertEquals(2, barFields.size());
+ Assert.assertTrue(barFields.contains(Bar.class.getDeclaredField("barField")));
+ Assert.assertTrue(barFields.contains(Bar.class.getDeclaredField("barIntField")));
+ }
+
+ @Test
+ public void getAllFieldsForClassWithBazWithoutBar() throws NoSuchFieldException, SecurityException {
+ Set<Field> bazFields = ReflectUtil.getAllFieldsForClass(Baz.class, new Class<?> [] {Bar.class});
+ Assert.assertNotNull(bazFields);
+ Assert.assertEquals(4, bazFields.size());
+ Assert.assertTrue(bazFields.contains(Foo.class.getDeclaredField("fooField")));
+ Assert.assertTrue(bazFields.contains(Foo.class.getDeclaredField("fooIntField")));
+ Assert.assertTrue(bazFields.contains(Baz.class.getDeclaredField("bazField")));
+ Assert.assertTrue(bazFields.contains(Baz.class.getDeclaredField("bazIntField")));
+ }
+
}
[2/2] git commit: updated refs/heads/master to d1dd85a
Posted by ko...@apache.org.
ReflectUtil: findMethod removed
findMethod was not used, it is removed
Signed-off-by: Laszlo Hornyak <la...@gmail.com>
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/66fe3abe
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/66fe3abe
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/66fe3abe
Branch: refs/heads/master
Commit: 66fe3abe3b73766df03771cb68640424ee2099c8
Parents: 0d7aa93
Author: Laszlo Hornyak <la...@gmail.com>
Authored: Tue Oct 8 20:06:44 2013 +0200
Committer: Laszlo Hornyak <la...@gmail.com>
Committed: Wed Oct 9 20:39:41 2013 +0200
----------------------------------------------------------------------
utils/src/com/cloud/utils/ReflectUtil.java | 14 --------------
1 file changed, 14 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/66fe3abe/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 08a780a..930e0b6 100755
--- a/utils/src/com/cloud/utils/ReflectUtil.java
+++ b/utils/src/com/cloud/utils/ReflectUtil.java
@@ -26,7 +26,6 @@ import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
@@ -58,19 +57,6 @@ public class ReflectUtil {
return null;
}
}
-
- public static Method findMethod(Class<?> clazz, String methodName) {
- do {
- Method[] methods = clazz.getDeclaredMethods();
- for (Method method : methods) {
- if (methodName.equals(method.getName())) {
- return method;
- }
- }
- clazz = clazz.getSuperclass();
- } while (clazz != null);
- return null;
- }
// Gets all classes with some annotation from a package
public static Set<Class<?>> getClassesWithAnnotation(Class<? extends Annotation> annotation,