MemberClassAccessFilter.java revision b72c5c2e5482cf10117b2b25f642f7616b2326c3
1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2009 Eric Lafortune (eric@graphics.cornell.edu)
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21package proguard.classfile.visitor;
22
23import proguard.classfile.*;
24import proguard.classfile.util.*;
25
26/**
27 * This <code>MemberVisitor</code> delegates its visits to another given
28 * <code>MemberVisitor</code>, but only when the visited member is accessible
29 * from the given referencing class.
30 *
31 * @author Eric Lafortune
32 */
33public class MemberClassAccessFilter
34implements   MemberVisitor
35{
36    private final Clazz         referencingClass;
37    private final MemberVisitor memberVisitor;
38
39
40    /**
41     * Creates a new MemberAccessFilter.
42     * @param referencingClass the class that is accessing the member.
43     * @param memberVisitor    the <code>MemberVisitor</code> to which visits
44     *                         will be delegated.
45     */
46    public MemberClassAccessFilter(Clazz         referencingClass,
47                                   MemberVisitor memberVisitor)
48    {
49        this.referencingClass = referencingClass;
50        this.memberVisitor    = memberVisitor;
51    }
52
53
54    // Implementations for MemberVisitor.
55
56    public void visitProgramField(ProgramClass programClass, ProgramField programField)
57    {
58        if (accepted(programClass, programField.getAccessFlags()))
59        {
60            memberVisitor.visitProgramField(programClass, programField);
61        }
62    }
63
64
65    public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
66    {
67        if (accepted(programClass, programMethod.getAccessFlags()))
68        {
69            memberVisitor.visitProgramMethod(programClass, programMethod);
70        }
71    }
72
73
74    public void visitLibraryField(LibraryClass libraryClass, LibraryField libraryField)
75    {
76        if (accepted(libraryClass, libraryField.getAccessFlags()))
77        {
78            memberVisitor.visitLibraryField(libraryClass, libraryField);
79        }
80    }
81
82
83    public void visitLibraryMethod(LibraryClass libraryClass, LibraryMethod libraryMethod)
84    {
85        if (accepted(libraryClass, libraryMethod.getAccessFlags()))
86        {
87            memberVisitor.visitLibraryMethod(libraryClass, libraryMethod);
88        }
89    }
90
91
92    // Small utility methods.
93
94    private boolean accepted(Clazz clazz, int memberAccessFlags)
95    {
96        int accessLevel = AccessUtil.accessLevel(memberAccessFlags);
97
98        return
99            (accessLevel >= AccessUtil.PUBLIC                                                              ) ||
100            (accessLevel >= AccessUtil.PRIVATE         && referencingClass.equals(clazz)                   ) ||
101            (accessLevel >= AccessUtil.PACKAGE_VISIBLE && (ClassUtil.internalPackageName(referencingClass.getName()).equals(
102                                                           ClassUtil.internalPackageName(clazz.getName())))) ||
103            (accessLevel >= AccessUtil.PROTECTED       && (referencingClass.extends_(clazz)                  ||
104                                                           referencingClass.extendsOrImplements(clazz))            );
105    }
106}
107