brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [07/10] brooklyn-server git commit: misc utils minor improvements
Date Thu, 07 Jul 2016 09:18:41 GMT
misc utils minor improvements

mainly around findMethod


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/b1999e3e
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/b1999e3e
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/b1999e3e

Branch: refs/heads/master
Commit: b1999e3e26519965c99a4e3bc8c1b23b011e8195
Parents: ec4da19
Author: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Authored: Wed Jun 29 03:45:30 2016 +0100
Committer: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Committed: Wed Jun 29 10:50:59 2016 +0100

----------------------------------------------------------------------
 .../java/org/apache/brooklyn/test/Asserts.java  |  9 +++++
 .../brooklyn/util/collections/MutableSet.java   |  4 +-
 .../brooklyn/util/javalang/Reflections.java     | 41 ++++++++++++++++++++
 3 files changed, 52 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b1999e3e/utils/common/src/main/java/org/apache/brooklyn/test/Asserts.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/org/apache/brooklyn/test/Asserts.java b/utils/common/src/main/java/org/apache/brooklyn/test/Asserts.java
index a544f01..43dcaa1 100644
--- a/utils/common/src/main/java/org/apache/brooklyn/test/Asserts.java
+++ b/utils/common/src/main/java/org/apache/brooklyn/test/Asserts.java
@@ -712,6 +712,9 @@ public class Asserts {
     public static void assertFalse(boolean condition, String message) {
         if (condition) fail(message);
     }
+    public static void assertFalse(boolean condition) {
+        if (condition) fail();
+    }
 
     /**
      * Fails a test with the given message.
@@ -720,6 +723,7 @@ public class Asserts {
     public static AssertionError fail(String message) {
         throw new AssertionError(message);
     }
+    public static AssertionError fail() { throw new AssertionError(); }
 
     public static void assertEqualsIgnoringOrder(Iterable<?> actual, Iterable<?>
expected) {
         assertEqualsIgnoringOrder(actual, expected, false, null);
@@ -1347,4 +1351,9 @@ public class Asserts {
         eventuallyOnNotify(object, Suppliers.ofInstance(object), predicate, null);
     }
 
+    public static void assertSize(Iterable<?> list, int expectedSize) {
+        if (list==null) fail("List is null");
+        if (Iterables.size(list)!=expectedSize) fail("List has wrong size "+Iterables.size(list)+"
(expected "+expectedSize+"): "+list);
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b1999e3e/utils/common/src/main/java/org/apache/brooklyn/util/collections/MutableSet.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/collections/MutableSet.java
b/utils/common/src/main/java/org/apache/brooklyn/util/collections/MutableSet.java
index 70d8a9d..bcadf33 100644
--- a/utils/common/src/main/java/org/apache/brooklyn/util/collections/MutableSet.java
+++ b/utils/common/src/main/java/org/apache/brooklyn/util/collections/MutableSet.java
@@ -55,7 +55,7 @@ public class MutableSet<V> extends LinkedHashSet<V> {
         return result;
     }
     
-    public static <V> MutableSet<V> of(V v1, V v2, V v3, V ...vMore) {
+    public static <V> MutableSet<V> of(V v1, V v2, V v3, @SuppressWarnings("unchecked")
V ...vMore) {
         MutableSet<V> result = new MutableSet<V>();
         result.add(v1);
         result.add(v2);
@@ -128,7 +128,7 @@ public class MutableSet<V> extends LinkedHashSet<V> {
             return this;
         }
 
-        public Builder<V> add(V v1, V v2, V ...values) {
+        public Builder<V> add(V v1, V v2, @SuppressWarnings("unchecked") V ...values)
{
             result.add(v1);
             result.add(v2);
             for (V value: values) result.add(value);

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b1999e3e/utils/common/src/main/java/org/apache/brooklyn/util/javalang/Reflections.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/javalang/Reflections.java
b/utils/common/src/main/java/org/apache/brooklyn/util/javalang/Reflections.java
index 7b2cba4..37f3fe6 100644
--- a/utils/common/src/main/java/org/apache/brooklyn/util/javalang/Reflections.java
+++ b/utils/common/src/main/java/org/apache/brooklyn/util/javalang/Reflections.java
@@ -583,6 +583,41 @@ public class Reflections {
         return result;
     }
 
+    /** Returns any method exactly matching the given signature, including privates and on
parent classes. */
+    public static Maybe<Method> findMethodMaybe(Class<?> clazz, String name,
Class<?>... parameterTypes) {
+        if (clazz == null || name == null) return Maybe.absentNoTrace("class or name is null");
+        Iterable<Method> result = findMethods(false, clazz, name, parameterTypes);
+        if (!result.iterator().hasNext()) return Maybe.absentNoTrace("no methods matching
"+clazz.getName()+"."+name+"("+Arrays.asList(parameterTypes)+")");
+        return Maybe.of(result.iterator().next());
+    }
+    /** Returns all methods compatible with the given argument types, including privates
and on parent classes and where the method takes a supertype. */
+    public static Iterable<Method> findMethodsCompatible(Class<?> clazz, String
name, Class<?>... parameterTypes) {
+        return findMethods(true, clazz, name, parameterTypes);
+    }
+    private static Iterable<Method> findMethods(boolean allowCovariantParameterClasses,
Class<?> clazz, String name, Class<?>... parameterTypes) {
+        if (clazz == null || name == null) {
+            return Collections.emptySet();
+        }
+        List<Method> result = MutableList.of();
+        Class<?> clazzToInspect = clazz;
+        
+        while (clazzToInspect != null) {
+            methods: for (Method m: clazzToInspect.getDeclaredMethods()) {
+                if (!name.equals(m.getName())) continue methods;
+                if (m.getParameterTypes().length!=parameterTypes.length) continue methods;
+                parameters: for (int i=0; i<parameterTypes.length; i++) {
+                    if (m.getParameterTypes()[i].equals(parameterTypes[i])) continue parameters;
+                    if (allowCovariantParameterClasses && m.getParameterTypes()[i].isAssignableFrom(parameterTypes[i]))
continue;
+                    continue methods;
+                }
+                result.add(m);
+            }
+            clazzToInspect = clazzToInspect.getSuperclass();
+        }
+        return result;
+    }
+    
+    /** @deprecated since 0.10.0 use {@link #findMethodMaybe(Class, String, Class...)} or
{@link #findMethodsCompatible(Class, String, Class...)} */ @Deprecated
     public static Method findMethod(Class<?> clazz, String name, Class<?>...
parameterTypes) throws NoSuchMethodException {
         if (clazz == null || name == null) {
             throw new NullPointerException("Must not be null: clazz="+clazz+"; name="+name);
@@ -951,4 +986,10 @@ public class Reflections {
         return name;
     }
 
+    public static boolean hasSpecialSerializationMethods(Class<? extends Object> type)
{
+        if (type==null) return false;
+        if (findMethodMaybe(type, "writeObject", java.io.ObjectOutputStream.class).isPresent())
return true;
+        return hasSpecialSerializationMethods(type.getSuperclass());
+    }
+
 }


Mime
View raw message