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.optimize.peephole;
22
23import proguard.classfile.*;
24import proguard.classfile.editor.MethodInvocationFixer;
25import proguard.classfile.util.*;
26import proguard.classfile.visitor.MemberVisitor;
27import proguard.optimize.info.NonPrivateMemberMarker;
28
29/**
30 * This MemberVisitor makes all class members that it visits private, unless
31 * they have been marked by a NonPrivateMemberMarker. The invocations of
32 * privatized methods still have to be fixed.
33 *
34 * @see NonPrivateMemberMarker
35 * @see MethodInvocationFixer
36 * @author Eric Lafortune
37 */
38public class MemberPrivatizer
39extends      SimplifiedVisitor
40implements   MemberVisitor
41{
42    private final MemberVisitor extraMemberVisitor;
43
44
45    /**
46     * Creates a new MemberPrivatizer.
47     */
48    public MemberPrivatizer()
49    {
50        this(null);
51    }
52
53
54    /**
55     * Creates a new MemberPrivatizer.
56     * @param extraMemberVisitor an optional extra visitor for all privatized
57     *                           class members.
58     */
59    public MemberPrivatizer(MemberVisitor extraMemberVisitor)
60    {
61        this.extraMemberVisitor = extraMemberVisitor;
62    }
63
64
65    // Implementations for MemberVisitor.
66
67    public void visitProgramField(ProgramClass programClass, ProgramField programField)
68    {
69        // Is the field unmarked?
70        if (NonPrivateMemberMarker.canBeMadePrivate(programField))
71        {
72            // Make the field private.
73            programField.u2accessFlags =
74                AccessUtil.replaceAccessFlags(programField.u2accessFlags,
75                                              ClassConstants.INTERNAL_ACC_PRIVATE);
76
77            // Visit the field, if required.
78            if (extraMemberVisitor != null)
79            {
80                extraMemberVisitor.visitProgramField(programClass, programField);
81            }
82        }
83    }
84
85
86    public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
87    {
88        // Is the method unmarked?
89        if (NonPrivateMemberMarker.canBeMadePrivate(programMethod))
90        {
91            // Make the method private.
92            programMethod.u2accessFlags =
93                AccessUtil.replaceAccessFlags(programMethod.u2accessFlags,
94                                              ClassConstants.INTERNAL_ACC_PRIVATE);
95
96            // Visit the method, if required.
97            if (extraMemberVisitor != null)
98            {
99                extraMemberVisitor.visitProgramMethod(programClass, programMethod);
100            }
101        }
102    }
103}
104