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.obfuscate;
22
23import proguard.classfile.*;
24import proguard.classfile.attribute.*;
25import proguard.classfile.attribute.visitor.AttributeVisitor;
26import proguard.classfile.util.SimplifiedVisitor;
27import proguard.classfile.visitor.*;
28
29/**
30 * This ClassVisitor removes attributes that are not marked as being used or
31 * required.
32 *
33 * @see AttributeUsageMarker
34 *
35 * @author Eric Lafortune
36 */
37public class AttributeShrinker
38extends      SimplifiedVisitor
39implements   ClassVisitor,
40             MemberVisitor,
41             AttributeVisitor
42{
43    // Implementations for ClassVisitor.
44
45    public void visitProgramClass(ProgramClass programClass)
46    {
47        // Compact the array for class attributes.
48        programClass.u2attributesCount =
49            shrinkArray(programClass.attributes,
50                        programClass.u2attributesCount);
51
52        // Compact the attributes in fields, methods, and class attributes,
53        programClass.fieldsAccept(this);
54        programClass.methodsAccept(this);
55        programClass.attributesAccept(this);
56    }
57
58
59    public void visitLibraryClass(LibraryClass libraryClass)
60    {
61        // Library classes are left unchanged.
62    }
63
64
65    // Implementations for MemberVisitor.
66
67    public void visitProgramMember(ProgramClass programClass, ProgramMember programMember)
68    {
69        // Compact the attributes array.
70        programMember.u2attributesCount =
71            shrinkArray(programMember.attributes,
72                        programMember.u2attributesCount);
73
74        // Compact any attributes of the remaining attributes.
75        programMember.attributesAccept(programClass, this);
76    }
77
78
79    // Implementations for AttributeVisitor.
80
81    public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}
82
83
84    public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
85    {
86        // Compact the attributes array.
87        codeAttribute.u2attributesCount =
88            shrinkArray(codeAttribute.attributes,
89                        codeAttribute.u2attributesCount);
90    }
91
92
93    // Small utility methods.
94
95    /**
96     * Removes all VisitorAccepter objects that are not marked as being used
97     * from the given array.
98     * @return the new number of VisitorAccepter objects.
99     */
100    private static int shrinkArray(VisitorAccepter[] array, int length)
101    {
102        int counter = 0;
103
104        // Shift the used objects together.
105        for (int index = 0; index < length; index++)
106        {
107            if (AttributeUsageMarker.isUsed(array[index]))
108            {
109                array[counter++] = array[index];
110            }
111        }
112
113        // Clear the remaining array elements.
114        for (int index = counter; index < length; index++)
115        {
116            array[index] = null;
117        }
118
119        return counter;
120    }
121}
122