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.info;
22
23import proguard.classfile.*;
24import proguard.classfile.attribute.CodeAttribute;
25import proguard.classfile.constant.*;
26import proguard.classfile.constant.visitor.ConstantVisitor;
27import proguard.classfile.instruction.*;
28import proguard.classfile.instruction.visitor.InstructionVisitor;
29import proguard.classfile.util.SimplifiedVisitor;
30import proguard.classfile.visitor.MemberVisitor;
31
32/**
33 * This InstructionVisitor counts the number of times methods are invoked from
34 * the instructions that are visited.
35 *
36 * @author Eric Lafortune
37 */
38public class MethodInvocationMarker
39extends      SimplifiedVisitor
40implements   InstructionVisitor,
41             ConstantVisitor,
42             MemberVisitor
43{
44    // Implementations for InstructionVisitor.
45
46    public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction) {}
47
48
49    public void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction)
50    {
51        clazz.constantPoolEntryAccept(constantInstruction.constantIndex, this);
52    }
53
54
55    // Implementations for ConstantVisitor.
56
57    public void visitAnyConstant(Clazz clazz, Constant constant) {}
58
59
60    public void visitStringConstant(Clazz clazz, StringConstant stringConstant)
61    {
62        // Mark the referenced method, if any.
63        stringConstant.referencedMemberAccept(this);
64    }
65
66
67    public void visitAnyMethodrefConstant(Clazz clazz, RefConstant refConstant)
68    {
69        // Mark the referenced method.
70        refConstant.referencedMemberAccept(this);
71    }
72
73
74    // Implementations for MemberVisitor.
75
76    public void visitAnyMember(Clazz Clazz, Member member) {}
77
78
79    public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
80    {
81        incrementInvocationCount(programMethod);
82    }
83
84
85    // Small utility methods.
86
87    private static void incrementInvocationCount(Method method)
88    {
89        MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method);
90        if (info != null)
91        {
92            info.incrementInvocationCount();
93        }
94    }
95
96
97    /**
98     * Returns the number of times the given method was invoked by the
99     * instructions that were visited.
100     */
101    public static int getInvocationCount(Method method)
102    {
103        MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method);
104        return info != null ? info.getInvocationCount() :
105                              Integer.MAX_VALUE;
106    }
107}
108