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.info;
22:
23: import proguard.classfile.*;
24: import proguard.classfile.util.*;
25: import proguard.classfile.visitor.MemberVisitor;
26:
27: /**
28: * This MemberVisitor marks all methods that it visits as not having any side
29: * effects. It will make the SideEffectMethodMarker consider them as such
30: * without further analysis.
31: *
32: * @see SideEffectMethodMarker
33: * @author Eric Lafortune
34: */
35: public class NoSideEffectMethodMarker extends SimplifiedVisitor
36: implements MemberVisitor {
37: // A visitor info flag to indicate the visitor accepter is being kept,
38: // but that it doesn't have any side effects.
39: private static final Object KEPT_BUT_NO_SIDE_EFFECTS = new Object();
40:
41: // Implementations for MemberVisitor.
42:
43: public void visitAnyMember(Clazz Clazz, Member member) {
44: // Ignore any attempts to mark fields.
45: }
46:
47: public void visitProgramMethod(ProgramClass programClass,
48: ProgramMethod programMethod) {
49: markNoSideEffects(programMethod);
50: }
51:
52: public void visitLibraryMethod(LibraryClass libraryClass,
53: LibraryMethod libraryMethod) {
54: markNoSideEffects(libraryMethod);
55: }
56:
57: // Small utility methods.
58:
59: private static void markNoSideEffects(Method method) {
60: MethodOptimizationInfo info = MethodOptimizationInfo
61: .getMethodOptimizationInfo(method);
62: if (info != null) {
63: info.setNoSideEffects();
64: } else {
65: MethodLinker.lastMember(method).setVisitorInfo(
66: KEPT_BUT_NO_SIDE_EFFECTS);
67: }
68: }
69:
70: public static boolean hasNoSideEffects(Method method) {
71: if (MethodLinker.lastVisitorAccepter(method).getVisitorInfo() == KEPT_BUT_NO_SIDE_EFFECTS) {
72: return true;
73: }
74:
75: MethodOptimizationInfo info = MethodOptimizationInfo
76: .getMethodOptimizationInfo(method);
77: return info != null && info.hasNoSideEffects();
78: }
79: }
|