summaryrefslogtreecommitdiff
path: root/src/proguard/ClassSpecification.java
blob: c2b6bfdc423fd8255e8ac1986083e1d8d279cdef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
 * ProGuard -- shrinking, optimization, obfuscation, and preverification
 *             of Java bytecode.
 *
 * Copyright (c) 2002-2014 Eric Lafortune (eric@graphics.cornell.edu)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package proguard;

import java.util.*;

/**
 * This class stores a specification of classes and possibly class members.
 * The specification is template-based: the class names and class member names
 * and descriptors can contain wildcards. Classes can be specified explicitly,
 * or as extensions or implementations in the class hierarchy.
 *
 * @author Eric Lafortune
 */
public class ClassSpecification implements Cloneable
{
    public final String comments;
    public       int    requiredSetAccessFlags;
    public       int    requiredUnsetAccessFlags;
    public final String annotationType;
    public       String className;
    public final String extendsAnnotationType;
    public final String extendsClassName;

    public       List   fieldSpecifications;
    public       List   methodSpecifications;


    /**
     * Creates a new ClassSpecification for all possible classes, without
     * comments or class members.
     */
    public ClassSpecification()
    {
        this(null,
             0,
             0,
             null,
             null,
             null,
             null);
    }


    /**
     * Creates a new ClassSpecification that is a copy of the given specification.
     */
    public ClassSpecification(ClassSpecification classSpecification)
    {
        this(classSpecification.comments,
             classSpecification.requiredSetAccessFlags,
             classSpecification.requiredUnsetAccessFlags,
             classSpecification.annotationType,
             classSpecification.className,
             classSpecification.extendsAnnotationType,
             classSpecification.extendsClassName,
             classSpecification.fieldSpecifications,
             classSpecification.methodSpecifications);
    }


    /**
     * Creates a new ClassSpecification for the specified class(es), without
     * class members.
     *
     * @param comments                 provides optional comments on this
     *                                 specification.
     * @param requiredSetAccessFlags   the class access flags that must be set
     *                                 in order for the class to apply.
     * @param requiredUnsetAccessFlags the class access flags that must be
     *                                 unset in order for the class to apply.
     * @param annotationType           the name of the class that must be an
     *                                 annotation of the class in order for it
     *                                 to apply. The name may be null to
     *                                 specify that no annotation is required.
     * @param className                the class name. The name may be null to
     *                                 specify any class, or it may contain
     *                                 "**", "*", or "?" wildcards.
     * @param extendsAnnotationType    the name of the class of that must be
     *                                 an annotation of the class that the
     *                                 class must extend or implement in order
     *                                 to apply. The name may be null to
     *                                 specify that no annotation is required.
     * @param extendsClassName         the name of the class that the class
     *                                 must extend or implement in order to
     *                                 apply. The name may be null to specify
     *                                 any class.
     */
    public ClassSpecification(String comments,
                              int    requiredSetAccessFlags,
                              int    requiredUnsetAccessFlags,
                              String annotationType,
                              String className,
                              String extendsAnnotationType,
                              String extendsClassName)
    {
        this(comments,
             requiredSetAccessFlags,
             requiredUnsetAccessFlags,
             annotationType,
             className,
             extendsAnnotationType,
             extendsClassName,
             null,
             null);
    }


    /**
     * Creates a new ClassSpecification for the specified classes and class
     * members.
     *
     * @param comments                 provides optional comments on this
     *                                 specification.
     * @param requiredSetAccessFlags   the class access flags that must be set
     *                                 in order for the class to apply.
     * @param requiredUnsetAccessFlags the class access flags that must be
     *                                 unset in order for the class to apply.
     * @param annotationType           the name of the class that must be an
     *                                 annotation of the class in order for it
     *                                 to apply. The name may be null to
     *                                 specify that no annotation is required.
     * @param className                the class name. The name may be null to
     *                                 specify any class, or it may contain
     *                                 "**", "*", or "?" wildcards.
     * @param extendsAnnotationType    the name of the class of that must be
     *                                 an annotation of the class that the
     *                                 class must extend or implement in order
     *                                 to apply. The name may be null to
     *                                 specify that no annotation is required.
     * @param extendsClassName         the name of the class that the class
     *                                 must extend or implement in order to
     *                                 apply. The name may be null to specify
     *                                 any class.
     * @param fieldSpecifications      the field specifications.
     * @param methodSpecifications     the method specifications.
     */
    public ClassSpecification(String comments,
                              int    requiredSetAccessFlags,
                              int    requiredUnsetAccessFlags,
                              String annotationType,
                              String className,
                              String extendsAnnotationType,
                              String extendsClassName,
                              List   fieldSpecifications,
                              List   methodSpecifications)
    {
        this.comments                 = comments;
        this.requiredSetAccessFlags   = requiredSetAccessFlags;
        this.requiredUnsetAccessFlags = requiredUnsetAccessFlags;
        this.annotationType           = annotationType;
        this.className                = className;
        this.extendsAnnotationType    = extendsAnnotationType;
        this.extendsClassName         = extendsClassName;
        this.fieldSpecifications      = fieldSpecifications;
        this.methodSpecifications     = methodSpecifications;
    }


    /**
     * Specifies to keep the specified field(s) of this option's class(es).
     *
     * @param fieldSpecification the field specification.
     */
    public void addField(MemberSpecification fieldSpecification)
    {
        if (fieldSpecifications == null)
        {
            fieldSpecifications = new ArrayList();
        }

        fieldSpecifications.add(fieldSpecification);
    }


    /**
     * Specifies to keep the specified method(s) of this option's class(es).
     *
     * @param methodSpecification the method specification.
     */
    public void addMethod(MemberSpecification methodSpecification)
    {
        if (methodSpecifications == null)
        {
            methodSpecifications = new ArrayList();
        }

        methodSpecifications.add(methodSpecification);
    }



    // Implementations for Object.

    public boolean equals(Object object)
    {
        if (object == null ||
            this.getClass() != object.getClass())
        {
            return false;
        }

        ClassSpecification other = (ClassSpecification)object;
        return
//          (this.comments                 == null ? other.comments              == null : this.comments.equals(other.comments)                          ) &&
            (this.requiredSetAccessFlags   == other.requiredSetAccessFlags                                                                               ) &&
            (this.requiredUnsetAccessFlags == other.requiredUnsetAccessFlags                                                                             ) &&
            (this.annotationType           == null ? other.annotationType        == null : this.annotationType.equals(other.annotationType)              ) &&
            (this.className                == null ? other.className             == null : this.className.equals(other.className)                        ) &&
            (this.extendsAnnotationType    == null ? other.extendsAnnotationType == null : this.extendsAnnotationType.equals(other.extendsAnnotationType)) &&
            (this.extendsClassName         == null ? other.extendsClassName      == null : this.extendsClassName.equals(other.extendsClassName)          ) &&
            (this.fieldSpecifications      == null ? other.fieldSpecifications   == null : this.fieldSpecifications.equals(other.fieldSpecifications)    ) &&
            (this.methodSpecifications     == null ? other.methodSpecifications  == null : this.methodSpecifications.equals(other.methodSpecifications)  );
    }

    public int hashCode()
    {
        return
//          (comments              == null ? 0 : comments.hashCode()             ) ^
            (requiredSetAccessFlags                                              ) ^
            (requiredUnsetAccessFlags                                            ) ^
            (annotationType        == null ? 0 : annotationType.hashCode()       ) ^
            (className             == null ? 0 : className.hashCode()            ) ^
            (extendsAnnotationType == null ? 0 : extendsAnnotationType.hashCode()) ^
            (extendsClassName      == null ? 0 : extendsClassName.hashCode()     ) ^
            (fieldSpecifications   == null ? 0 : fieldSpecifications.hashCode()  ) ^
            (methodSpecifications  == null ? 0 : methodSpecifications.hashCode() );
    }

    public Object clone()
    {
        try
        {
            return super.clone();
        }
        catch (CloneNotSupportedException e)
        {
            return null;
        }
    }
}