KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > optimize > peephole > MemberPrivatizer


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 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  */

21 package proguard.optimize.peephole;
22
23 import proguard.classfile.*;
24 import proguard.classfile.editor.MethodInvocationFixer;
25 import proguard.classfile.util.*;
26 import proguard.classfile.visitor.MemberVisitor;
27 import proguard.optimize.info.NonPrivateMemberMarker;
28
29 /**
30  * This MemberVisitor makes all class members that it visits private, unless they
31  * have been marked by a NonPrivateMemberMarker. The invocations of the
32  * privatized method still have to be fixed.
33  *
34  * @see NonPrivateMemberMarker
35  * @see MethodInvocationFixer
36  * @author Eric Lafortune
37  */

38 public class MemberPrivatizer
39 extends SimplifiedVisitor
40 implements MemberVisitor
41 {
42     private MemberVisitor extraFieldVisitor;
43     private MemberVisitor extraMethodVisitor;
44
45
46     /**
47      * Creates a new MemberPrivatizer.
48      */

49     public MemberPrivatizer()
50     {
51         this(null, null);
52     }
53
54
55     /**
56      * Creates a new MemberPrivatizer.
57      * @param extraFieldVisitor an optional extra visitor for all privatized
58      * fields.
59      * @param extraMethodVisitor an optional extra visitor for all privatized
60      * methods.
61      */

62     public MemberPrivatizer(MemberVisitor extraFieldVisitor,
63                             MemberVisitor extraMethodVisitor)
64     {
65         this.extraFieldVisitor = extraFieldVisitor;
66         this.extraMethodVisitor = extraMethodVisitor;
67     }
68
69
70     // Implementations for MemberVisitor.
71

72     public void visitProgramField(ProgramClass programClass, ProgramField programField)
73     {
74         // Is the field unmarked?
75
if (NonPrivateMemberMarker.canBeMadePrivate(programField))
76         {
77             // Make the field private.
78
programField.u2accessFlags =
79                 AccessUtil.replaceAccessFlags(programField.u2accessFlags,
80                                               ClassConstants.INTERNAL_ACC_PRIVATE);
81
82             // Visit the field, if required.
83
if (extraFieldVisitor != null)
84             {
85                 extraFieldVisitor.visitProgramField(programClass, programField);
86             }
87         }
88     }
89
90
91     public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
92     {
93         // Is the method unmarked?
94
if (NonPrivateMemberMarker.canBeMadePrivate(programMethod))
95         {
96             // Make the method private.
97
programMethod.u2accessFlags =
98                 AccessUtil.replaceAccessFlags(programMethod.u2accessFlags,
99                                               ClassConstants.INTERNAL_ACC_PRIVATE);
100
101             // Visit the method, if required.
102
if (extraMethodVisitor != null)
103             {
104                 extraMethodVisitor.visitProgramMethod(programClass, programMethod);
105             }
106         }
107     }
108 }
109
Popular Tags