NoSideEffectMethodMarker.java revision 8a6199f0c36a778f22394364347a301b0b28e94b
1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2013 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.info;
22
23import proguard.classfile.*;
24import proguard.classfile.util.*;
25import 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 */
35public class NoSideEffectMethodMarker
36extends      SimplifiedVisitor
37implements   MemberVisitor
38{
39    // A visitor info flag to indicate the visitor accepter is being kept,
40    // but that it doesn't have any side effects.
41    public static final Object KEPT_BUT_NO_SIDE_EFFECTS = new Object();
42
43
44    // Implementations for MemberVisitor.
45
46    public void visitAnyMember(Clazz Clazz, Member member)
47    {
48        // Ignore any attempts to mark fields.
49    }
50
51
52    public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
53    {
54        markNoSideEffects(programMethod);
55    }
56
57
58    public void visitLibraryMethod(LibraryClass libraryClass, LibraryMethod libraryMethod)
59    {
60        markNoSideEffects(libraryMethod);
61    }
62
63
64    // Small utility methods.
65
66    private static void markNoSideEffects(Method method)
67    {
68        MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method);
69        if (info != null)
70        {
71            info.setNoSideEffects();
72        }
73        else
74        {
75            MethodLinker.lastMember(method).setVisitorInfo(KEPT_BUT_NO_SIDE_EFFECTS);
76        }
77    }
78
79
80    public static boolean hasNoSideEffects(Method method)
81    {
82        if (MethodLinker.lastVisitorAccepter(method).getVisitorInfo() == KEPT_BUT_NO_SIDE_EFFECTS)
83        {
84            return true;
85        }
86
87        MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method);
88        return info != null &&
89               info.hasNoSideEffects();
90    }
91}
92