1 /*
2  * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package com.sun.tools.javadoc;
27 
28 import static com.sun.tools.javac.code.Flags.*;
29 
30 /**
31  *   A class whose instances are filters over Modifier bits.
32  *   Filtering is done by returning boolean values.
33  *   Classes, methods and fields can be filtered, or filtering
34  *   can be done directly on modifier bits.
35  *
36  *  <p><b>This is NOT part of any supported API.
37  *  If you write code that depends on this, you do so at your own risk.
38  *  This code and its internal interfaces are subject to change or
39  *  deletion without notice.</b>
40  *
41  *   @see com.sun.tools.javac.code.Flags
42  *   @author Robert Field
43  */
44 
45 public class ModifierFilter {
46 
47     /**
48     * Package private access.
49     * A "pseudo-" modifier bit that can be used in the
50     * constructors of this class to specify package private
51     * access. This is needed since there is no Modifier.PACKAGE.
52     */
53     public static final long PACKAGE = 0x8000000000000000L;
54 
55     /**
56     * All access modifiers.
57     * A short-hand set of modifier bits that can be used in the
58     * constructors of this class to specify all access modifiers,
59     * Same as PRIVATE | PROTECTED | PUBLIC | PACKAGE.
60     */
61     public static final long ALL_ACCESS =
62                 PRIVATE | PROTECTED | PUBLIC | PACKAGE;
63 
64     private long oneOf;
65     private long must;
66     private long cannot;
67 
68     private static final int ACCESS_BITS = PRIVATE | PROTECTED | PUBLIC;
69 
70     /**
71      * Constructor - Specify a filter.
72      *
73      * @param   oneOf   If zero, everything passes the filter.
74      *                  If non-zero, at least one of the specified
75      *                  bits must be on in the modifier bits to
76      *                  pass the filter.
77      */
ModifierFilter(long oneOf)78     public ModifierFilter(long oneOf) {
79         this(oneOf, 0, 0);
80     }
81 
82     /**
83      * Constructor - Specify a filter.
84      * For example, the filter below  will only pass synchronized
85      * methods that are private or package private access and are
86      * not native or static.
87      * <pre>
88      * ModifierFilter(  Modifier.PRIVATE | ModifierFilter.PACKAGE,
89      *                  Modifier.SYNCHRONIZED,
90      *                  Modifier.NATIVE | Modifier.STATIC)
91      * </pre><p>
92      * Each of the three arguments must either be
93      * zero or the or'ed combination of the bits specified in the
94      * class Modifier or this class. During filtering, these values
95      * are compared against the modifier bits as follows:
96      *
97      * @param   oneOf   If zero, ignore this argument.
98      *                  If non-zero, at least one of the bits must be on.
99      * @param   must    All bits specified must be on.
100      * @param   cannot  None of the bits specified can be on.
101      */
ModifierFilter(long oneOf, long must, long cannot)102     public ModifierFilter(long oneOf, long must, long cannot) {
103         this.oneOf = oneOf;
104         this.must = must;
105         this.cannot = cannot;
106     }
107 
108     /**
109      * Filter on modifier bits.
110      *
111      * @param   modifierBits    Bits as specified in the Modifier class
112      *
113      * @return                  Whether the modifierBits pass this filter.
114      */
checkModifier(int modifierBits)115     public boolean checkModifier(int modifierBits) {
116         // Add in the "pseudo-" modifier bit PACKAGE, if needed
117         long fmod = ((modifierBits & ACCESS_BITS) == 0) ?
118                         modifierBits | PACKAGE :
119                         modifierBits;
120         return ((oneOf == 0) || ((oneOf & fmod) != 0)) &&
121                 ((must & fmod) == must) &&
122                 ((cannot & fmod) == 0);
123     }
124 
125 } // end ModifierFilter
126