1 /*
2  * Hibernate Validator, declare and validate application constraints
3  *
4  * License: Apache License, Version 2.0
5  * See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6  */

7 package org.hibernate.validator.internal.util.classhierarchy;
8
9 /**
10  * Provides filters to be used when invoking
11  * {@link ClassHierarchyHelper#getHierarchy(Class, Filter...)}.
12  *
13  * @author Gunnar Morling
14  */

15 public class Filters {
16
17     private static final Filter PROXY_FILTER = new WeldProxyFilter();
18     private static final Filter INTERFACES_FILTER = new InterfacesFilter();
19
20     private Filters() {
21         // Not allowed
22     }
23
24     /**
25      * Returns a filter which excludes interfaces.
26      *
27      * @return a filter which excludes interfaces
28      */

29     public static Filter excludeInterfaces() {
30         return INTERFACES_FILTER;
31     }
32
33     /**
34      * Returns a filter which excludes proxy objects.
35      *
36      * @return a filter which excludes proxy objects
37      */

38     public static Filter excludeProxies() {
39         return PROXY_FILTER;
40     }
41
42     private static class InterfacesFilter implements Filter {
43
44         @Override
45         public boolean accepts(Class<?> clazz) {
46             return !clazz.isInterface();
47         }
48     }
49
50     private static class WeldProxyFilter implements Filter {
51
52         private static final String WELD_PROXY_INTERFACE_NAME = "org.jboss.weld.bean.proxy.ProxyObject";
53
54         @Override
55         public boolean accepts(Class<?> clazz) {
56             return !isWeldProxy( clazz );
57         }
58
59         /**
60          * Whether the given class is a proxy created by Weld or not. This is
61          * the case if the given class implements the interface
62          * {@code org.jboss.weld.bean.proxy.ProxyObject}.
63          *
64          * @param clazz the class of interest
65          *
66          * @return {@code trueif the given class is a Weld proxy,
67          *         {@code false} otherwise
68          */

69         private boolean isWeldProxy(Class<?> clazz) {
70             for ( Class<?> implementedInterface : clazz.getInterfaces() ) {
71                 if ( implementedInterface.getName().equals( WELD_PROXY_INTERFACE_NAME ) ) {
72                     return true;
73                 }
74             }
75
76             return false;
77         }
78     }
79 }
80