1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  *             of Java bytecode.
4  *
5  * Copyright (c) 2002-2014 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  */
21 package proguard.classfile.attribute;
22 
23 import proguard.classfile.*;
24 import proguard.classfile.attribute.visitor.AttributeVisitor;
25 import proguard.classfile.visitor.*;
26 
27 /**
28  * This Attribute represents an enclosing method attribute.
29  *
30  * @author Eric Lafortune
31  */
32 public class EnclosingMethodAttribute extends Attribute
33 {
34     public int u2classIndex;
35     public int u2nameAndTypeIndex;
36 
37     /**
38      * An extra field pointing to the referenced Clazz object.
39      * This field is typically filled out by the <code>{@link
40      * proguard.classfile.util.ClassReferenceInitializer
41      * ClassReferenceInitializer}</code>.
42      */
43     public Clazz referencedClass;
44 
45     /**
46      * An extra field optionally pointing to the referenced Method object.
47      * This field is typically filled out by the <code>{@link
48      * proguard.classfile.util.ClassReferenceInitializer
49      * ClassReferenceInitializer}</code>.
50      */
51     public Method referencedMethod;
52 
53 
54     /**
55      * Creates an uninitialized EnclosingMethodAttribute.
56      */
EnclosingMethodAttribute()57     public EnclosingMethodAttribute()
58     {
59     }
60 
61 
62     /**
63      * Creates an initialized EnclosingMethodAttribute.
64      */
EnclosingMethodAttribute(int u2attributeNameIndex, int u2classIndex, int u2nameAndTypeIndex)65     public EnclosingMethodAttribute(int u2attributeNameIndex,
66                                     int u2classIndex,
67                                     int u2nameAndTypeIndex)
68     {
69         super(u2attributeNameIndex);
70 
71         this.u2classIndex       = u2classIndex;
72         this.u2nameAndTypeIndex = u2nameAndTypeIndex;
73     }
74 
75 
76     /**
77      * Returns the class name.
78      */
getClassName(Clazz clazz)79     public String getClassName(Clazz clazz)
80     {
81         return clazz.getClassName(u2classIndex);
82     }
83 
84     /**
85      * Returns the method/field name.
86      */
getName(Clazz clazz)87     public String getName(Clazz clazz)
88     {
89         return clazz.getName(u2nameAndTypeIndex);
90     }
91 
92     /**
93      * Returns the type.
94      */
getType(Clazz clazz)95     public String getType(Clazz clazz)
96     {
97         return clazz.getType(u2nameAndTypeIndex);
98     }
99 
100 
101     /**
102      * Lets the referenced class accept the given visitor.
103      */
referencedClassAccept(ClassVisitor classVisitor)104     public void referencedClassAccept(ClassVisitor classVisitor)
105     {
106         if (referencedClass != null)
107         {
108             referencedClass.accept(classVisitor);
109         }
110     }
111 
112 
113     /**
114      * Lets the referenced class member accept the given visitor.
115      */
referencedMethodAccept(MemberVisitor memberVisitor)116     public void referencedMethodAccept(MemberVisitor memberVisitor)
117     {
118         if (referencedMethod != null)
119         {
120             referencedMethod.accept(referencedClass,
121                                     memberVisitor);
122         }
123     }
124 
125 
126     // Implementations for Attribute.
127 
accept(Clazz clazz, AttributeVisitor attributeVisitor)128     public void accept(Clazz clazz, AttributeVisitor attributeVisitor)
129     {
130         attributeVisitor.visitEnclosingMethodAttribute(clazz, this);
131     }
132 }
133