001: /*
002: * ProGuard -- shrinking, optimization, obfuscation, and preverification
003: * of Java bytecode.
004: *
005: * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the Free
009: * Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful, but WITHOUT
013: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
015: * more details.
016: *
017: * You should have received a copy of the GNU General Public License along
018: * with this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: package proguard.classfile.visitor;
022:
023: import proguard.classfile.*;
024:
025: /**
026: * This <code>MemberVisitor</code> delegates its visits to another given
027: * <code>MemberVisitor</code>, but only when the visited member has the proper
028: * access flags.
029: * <p>
030: * If conflicting access flags (public/private/protected) are specified,
031: * having one of them set will be considered sufficient.
032: *
033: * @see ClassConstants
034: *
035: * @author Eric Lafortune
036: */
037: public class MemberAccessFilter implements MemberVisitor {
038: // A mask of conflicting access flags. These are interpreted in a special
039: // way if more of them are required at the same time. In that case, one
040: // of them being set is sufficient.
041: private static final int ACCESS_MASK = ClassConstants.INTERNAL_ACC_PUBLIC
042: | ClassConstants.INTERNAL_ACC_PRIVATE
043: | ClassConstants.INTERNAL_ACC_PROTECTED;
044:
045: private final int requiredSetAccessFlags;
046: private final int requiredUnsetAccessFlags;
047: private final int requiredOneSetAccessFlags;
048: private final MemberVisitor memberVisitor;
049:
050: /**
051: * Creates a new MemberAccessFilter.
052: * @param requiredSetAccessFlags the class access flags that should be
053: * set.
054: * @param requiredUnsetAccessFlags the class access flags that should be
055: * unset.
056: * @param memberVisitor the <code>MemberVisitor</code> to
057: * which visits will be delegated.
058: */
059: public MemberAccessFilter(int requiredSetAccessFlags,
060: int requiredUnsetAccessFlags, MemberVisitor memberVisitor) {
061: this .requiredSetAccessFlags = requiredSetAccessFlags
062: & ~ACCESS_MASK;
063: this .requiredUnsetAccessFlags = requiredUnsetAccessFlags;
064: this .requiredOneSetAccessFlags = requiredSetAccessFlags
065: & ACCESS_MASK;
066: this .memberVisitor = memberVisitor;
067: }
068:
069: // Implementations for MemberVisitor.
070:
071: public void visitProgramField(ProgramClass programClass,
072: ProgramField programField) {
073: if (accepted(programField.getAccessFlags())) {
074: memberVisitor.visitProgramField(programClass, programField);
075: }
076: }
077:
078: public void visitProgramMethod(ProgramClass programClass,
079: ProgramMethod programMethod) {
080: if (accepted(programMethod.getAccessFlags())) {
081: memberVisitor.visitProgramMethod(programClass,
082: programMethod);
083: }
084: }
085:
086: public void visitLibraryField(LibraryClass libraryClass,
087: LibraryField libraryField) {
088: if (accepted(libraryField.getAccessFlags())) {
089: memberVisitor.visitLibraryField(libraryClass, libraryField);
090: }
091: }
092:
093: public void visitLibraryMethod(LibraryClass libraryClass,
094: LibraryMethod libraryMethod) {
095: if (accepted(libraryMethod.getAccessFlags())) {
096: memberVisitor.visitLibraryMethod(libraryClass,
097: libraryMethod);
098: }
099: }
100:
101: // Small utility methods.
102:
103: private boolean accepted(int accessFlags) {
104: return (requiredSetAccessFlags & ~accessFlags) == 0
105: && (requiredUnsetAccessFlags & accessFlags) == 0
106: && (requiredOneSetAccessFlags == 0 || (requiredOneSetAccessFlags & accessFlags) != 0);
107: }
108: }
|