You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bval.apache.org by mb...@apache.org on 2018/04/25 13:44:00 UTC

bval git commit: do not refer to non-publicly accessible types from blueprint method

Repository: bval
Updated Branches:
  refs/heads/bv2 6677d6bf6 -> cbdd0438c


do not refer to non-publicly accessible types from blueprint method


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

Branch: refs/heads/bv2
Commit: cbdd0438cf76c577a6af380b6f169dee7914bb5a
Parents: 6677d6b
Author: Matt Benson <mb...@apache.org>
Authored: Wed Apr 25 08:43:53 2018 -0500
Committer: Matt Benson <mb...@apache.org>
Committed: Wed Apr 25 08:43:53 2018 -0500

----------------------------------------------------------------------
 .../apache/bval/util/reflection/Reflection.java | 159 ++++++++++---------
 1 file changed, 85 insertions(+), 74 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/bval/blob/cbdd0438/bval-jsr/src/main/java/org/apache/bval/util/reflection/Reflection.java
----------------------------------------------------------------------
diff --git a/bval-jsr/src/main/java/org/apache/bval/util/reflection/Reflection.java b/bval-jsr/src/main/java/org/apache/bval/util/reflection/Reflection.java
index 97cafda..abb2f7c 100644
--- a/bval-jsr/src/main/java/org/apache/bval/util/reflection/Reflection.java
+++ b/bval-jsr/src/main/java/org/apache/bval/util/reflection/Reflection.java
@@ -41,6 +41,89 @@ import org.apache.commons.weaver.privilizer.Privilizing;
  * Security-agnostic "blueprint" class for reflection-related operations. Intended for use by Apache BVal code.
  */
 public class Reflection {
+    public static final class ClassHierarchy implements Iterable<Class<?>> {
+        private final Class<?> type;
+
+        public ClassHierarchy(Class<?> type) {
+            this.type = type;
+        }
+
+        @Override
+        public Iterator<Class<?>> iterator() {
+            return new Iterator<Class<?>>() {
+                Optional<Class<?>> next = Optional.of(type);
+
+                @Override
+                public boolean hasNext() {
+                    return next.isPresent();
+                }
+
+                @Override
+                public Class<?> next() {
+                    final Class<?> result = next.orElseThrow(NoSuchElementException::new);
+                    next = Optional.ofNullable(result.getSuperclass());
+                    return result;
+                }
+
+                @Override
+                public void remove() {
+                    throw new UnsupportedOperationException();
+                }
+            };
+        }
+    }
+
+    public static final class FullHierarchy implements Iterable<Class<?>> {
+        private final Iterable<Class<?>> classes;
+
+        public FullHierarchy(Iterable<Class<?>> classes) {
+            this.classes = classes;
+        }
+
+        @Override
+        public Iterator<Class<?>> iterator() {
+            final Set<Class<?>> seenInterfaces = new HashSet<Class<?>>();
+            final Iterator<Class<?>> wrapped = classes.iterator();
+
+            return new Iterator<Class<?>>() {
+                Iterator<Class<?>> interfaces = Collections.emptyIterator();
+
+                @Override
+                public boolean hasNext() {
+                    return interfaces.hasNext() || wrapped.hasNext();
+                }
+
+                @Override
+                public Class<?> next() {
+                    if (interfaces.hasNext()) {
+                        final Class<?> nextInterface = interfaces.next();
+                        seenInterfaces.add(nextInterface);
+                        return nextInterface;
+                    }
+                    final Class<?> nextSuperclass = wrapped.next();
+                    final Set<Class<?>> currentInterfaces = new LinkedHashSet<>();
+                    walkInterfaces(currentInterfaces, nextSuperclass);
+                    interfaces = currentInterfaces.iterator();
+                    return nextSuperclass;
+                }
+
+                private void walkInterfaces(final Set<Class<?>> addTo, final Class<?> c) {
+                    for (final Class<?> iface : c.getInterfaces()) {
+                        if (!seenInterfaces.contains(iface)) {
+                            addTo.add(iface);
+                        }
+                        walkInterfaces(addTo, iface);
+                    }
+                }
+
+                @Override
+                public void remove() {
+                    throw new UnsupportedOperationException();
+                }
+            };
+        }
+    }
+
     /**
      * Inclusivity literals for {@link #hierarchy(Class, Interfaces)}.
      * Taken from commons-lang3.
@@ -375,79 +458,7 @@ public class Reflection {
         if (type == null) {
             return Collections.emptySet();
         }
-        final Iterable<Class<?>> classes = new Iterable<Class<?>>() {
-
-            @Override
-            public Iterator<Class<?>> iterator() {
-                return new Iterator<Class<?>>() {
-                    Optional<Class<?>> next = Optional.of(type);
-
-                    @Override
-                    public boolean hasNext() {
-                        return next.isPresent();
-                    }
-
-                    @Override
-                    public Class<?> next() {
-                        final Class<?> result = next.orElseThrow(NoSuchElementException::new);
-                        next = Optional.ofNullable(result.getSuperclass());
-                        return result;
-                    }
-
-                    @Override
-                    public void remove() {
-                        throw new UnsupportedOperationException();
-                    }
-                };
-            }
-        };
-        if (interfacesBehavior != Interfaces.INCLUDE) {
-            return classes;
-        }
-        return new Iterable<Class<?>>() {
-
-            @Override
-            public Iterator<Class<?>> iterator() {
-                final Set<Class<?>> seenInterfaces = new HashSet<Class<?>>();
-                final Iterator<Class<?>> wrapped = classes.iterator();
-
-                return new Iterator<Class<?>>() {
-                    Iterator<Class<?>> interfaces = Collections.emptyIterator();
-
-                    @Override
-                    public boolean hasNext() {
-                        return interfaces.hasNext() || wrapped.hasNext();
-                    }
-
-                    @Override
-                    public Class<?> next() {
-                        if (interfaces.hasNext()) {
-                            final Class<?> nextInterface = interfaces.next();
-                            seenInterfaces.add(nextInterface);
-                            return nextInterface;
-                        }
-                        final Class<?> nextSuperclass = wrapped.next();
-                        final Set<Class<?>> currentInterfaces = new LinkedHashSet<>();
-                        walkInterfaces(currentInterfaces, nextSuperclass);
-                        interfaces = currentInterfaces.iterator();
-                        return nextSuperclass;
-                    }
-
-                    private void walkInterfaces(final Set<Class<?>> addTo, final Class<?> c) {
-                        for (final Class<?> iface : c.getInterfaces()) {
-                            if (!seenInterfaces.contains(iface)) {
-                                addTo.add(iface);
-                            }
-                            walkInterfaces(addTo, iface);
-                        }
-                    }
-
-                    @Override
-                    public void remove() {
-                        throw new UnsupportedOperationException();
-                    }
-                };
-            }
-        };
+        final Iterable<Class<?>> classes = new ClassHierarchy(type);
+        return interfacesBehavior == Interfaces.INCLUDE ? new FullHierarchy(classes) : classes;
     }
 }