01: /*
02: * ProGuard -- shrinking, optimization, obfuscation, and preverification
03: * of Java bytecode.
04: *
05: * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
06: *
07: * This program is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU General Public License as published by the Free
09: * 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 extends SimplifiedVisitor implements
39: MemberVisitor {
40: private final MemberVisitor extraFieldVisitor;
41: private final MemberVisitor extraMethodVisitor;
42:
43: /**
44: * Creates a new MemberPrivatizer.
45: */
46: public MemberPrivatizer() {
47: this (null, null);
48: }
49:
50: /**
51: * Creates a new MemberPrivatizer.
52: * @param extraFieldVisitor an optional extra visitor for all privatized
53: * fields.
54: * @param extraMethodVisitor an optional extra visitor for all privatized
55: * methods.
56: */
57: public MemberPrivatizer(MemberVisitor extraFieldVisitor,
58: MemberVisitor extraMethodVisitor) {
59: this .extraFieldVisitor = extraFieldVisitor;
60: this .extraMethodVisitor = extraMethodVisitor;
61: }
62:
63: // Implementations for MemberVisitor.
64:
65: public void visitProgramField(ProgramClass programClass,
66: ProgramField programField) {
67: // Is the field unmarked?
68: if (NonPrivateMemberMarker.canBeMadePrivate(programField)) {
69: // Make the field private.
70: programField.u2accessFlags = AccessUtil.replaceAccessFlags(
71: programField.u2accessFlags,
72: ClassConstants.INTERNAL_ACC_PRIVATE);
73:
74: // Visit the field, if required.
75: if (extraFieldVisitor != null) {
76: extraFieldVisitor.visitProgramField(programClass,
77: programField);
78: }
79: }
80: }
81:
82: public void visitProgramMethod(ProgramClass programClass,
83: ProgramMethod programMethod) {
84: // Is the method unmarked?
85: if (NonPrivateMemberMarker.canBeMadePrivate(programMethod)) {
86: // Make the method private.
87: programMethod.u2accessFlags = AccessUtil
88: .replaceAccessFlags(programMethod.u2accessFlags,
89: ClassConstants.INTERNAL_ACC_PRIVATE);
90:
91: // Visit the method, if required.
92: if (extraMethodVisitor != null) {
93: extraMethodVisitor.visitProgramMethod(programClass,
94: programMethod);
95: }
96: }
97: }
98: }
|