You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by bf...@apache.org on 2013/10/14 19:17:46 UTC
[54/81] [abbrv] git commit: updated refs/heads/ui-restyle to e901b82
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/ui-restyle
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")));
+ }
+
}