|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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 | +package com.oracle.truffle.espresso.shared.lookup; |
| 26 | + |
| 27 | +import com.oracle.truffle.espresso.classfile.Constants; |
| 28 | +import com.oracle.truffle.espresso.shared.meta.ModifiersProvider; |
| 29 | + |
| 30 | +/** |
| 31 | + * Provides a simple way of filtering elements with {@linkplain ModifiersProvider modifiers} (such |
| 32 | + * as classes, methods or fields), based on their declared flags. |
| 33 | + */ |
| 34 | +public final class LookupMode { |
| 35 | + public static final LookupMode ALL = new LookupMode(0, 0); |
| 36 | + public static final LookupMode INSTANCE_ONLY = new LookupMode(0, Constants.ACC_STATIC); |
| 37 | + public static final LookupMode STATIC_ONLY = new LookupMode(Constants.ACC_STATIC, 0); |
| 38 | + public static final LookupMode NON_STATIC_NON_PRIVATE = new LookupMode(0, Constants.ACC_STATIC | Constants.ACC_PRIVATE); |
| 39 | + public static final LookupMode PUBLIC_NON_STATIC = new LookupMode(Constants.ACC_PUBLIC, Constants.ACC_STATIC); |
| 40 | + |
| 41 | + private final int positives; |
| 42 | + private final int negatives; |
| 43 | + |
| 44 | + /** |
| 45 | + * Creates a new element filter. |
| 46 | + * |
| 47 | + * @param positives The flags that need to be set |
| 48 | + * @param negatives The flags that must not be set. |
| 49 | + */ |
| 50 | + public LookupMode(int positives, int negatives) { |
| 51 | + assert (positives & negatives) == 0 : "Empty mode: +" + Integer.toHexString(positives) + " and -" + Integer.toHexString(negatives); |
| 52 | + this.positives = positives; |
| 53 | + this.negatives = negatives; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Returns whether the given element is accepted by {@code this} filter. If {@code m} is |
| 58 | + * {@code null}, then this method returns {@code false}. |
| 59 | + */ |
| 60 | + public boolean include(ModifiersProvider m) { |
| 61 | + if (m == null) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + int flags = m.getModifiers(); |
| 65 | + return hasPositives(flags) && hasNoNegatives(flags); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Combines this filter with another. |
| 70 | + * <p> |
| 71 | + * Calling the resulting {@link #include(ModifiersProvider e)} is equivalent to |
| 72 | + * {@code this.include(e) && other.include(e)}. |
| 73 | + */ |
| 74 | + public LookupMode combine(LookupMode other) { |
| 75 | + int newPositives = this.positives | other.negatives; |
| 76 | + int newNegatives = this.negatives | other.negatives; |
| 77 | + if (newPositives == positives && newNegatives == negatives) { |
| 78 | + return this; |
| 79 | + } |
| 80 | + if (newPositives == other.positives && newNegatives == other.negatives) { |
| 81 | + return other; |
| 82 | + } |
| 83 | + return new LookupMode(newPositives, newNegatives); |
| 84 | + } |
| 85 | + |
| 86 | + private boolean hasPositives(int flags) { |
| 87 | + return (flags & positives) == positives; |
| 88 | + } |
| 89 | + |
| 90 | + private boolean hasNoNegatives(int flags) { |
| 91 | + return (flags & negatives) == 0; |
| 92 | + } |
| 93 | +} |
0 commit comments