KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > classfile > constant > StringConstant


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This library 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 library 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 Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package proguard.classfile.constant;
22
23 import proguard.classfile.*;
24 import proguard.classfile.constant.visitor.ConstantVisitor;
25 import proguard.classfile.visitor.*;
26
27 import java.io.*;
28
29 /**
30  * This Constant represents a string constant in the constant pool.
31  *
32  * @author Eric Lafortune
33  */

34 public class StringConstant extends Constant
35 {
36     public int u2stringIndex;
37
38     /**
39      * An extra field pointing to the referenced Clazz object, if this
40      * string is being used in Class.forName(), .class, or
41      * Class.getDeclaredField/Method constructs.
42      * This field is typically filled out by the <code>{@link
43      * proguard.classfile.util.DynamicClassReferenceInitializer
44      * DynamicClassReferenceInitializer}</code> or by the <code>{@link
45      * proguard.classfile.util.DynamicMemberReferenceInitializer
46      * DynamicMemberReferenceInitializer}</code>.
47      */

48     public Clazz referencedClass;
49
50     /**
51      * An extra field pointing to the referenced Member object, if this
52      * string is being used in Class.getDeclaredField/Method constructs.
53      * This field is typically filled out by the <code>{@link
54      * proguard.classfile.util.DynamicMemberReferenceInitializer
55      * DynamicMemberReferenceInitializer}</code>.
56      */

57     public Member referencedMember;
58
59
60     /**
61      * Creates an uninitialized StringConstant.
62      */

63     public StringConstant()
64     {
65     }
66
67
68     /**
69      * Creates a new StringConstant with the given string index.
70      * @param u2stringIndex the index of the string in the constant pool.
71      * @param referencedClass the referenced class, if any.
72      * @param referenceMember the referenced class member, if any.
73      */

74     public StringConstant(int u2stringIndex,
75                           Clazz referencedClass,
76                           Member referenceMember)
77     {
78         this.u2stringIndex = u2stringIndex;
79         this.referencedClass = referencedClass;
80         this.referencedMember = referenceMember;
81     }
82
83
84     /**
85      * Returns the string value.
86      */

87     public String JavaDoc getString(Clazz clazz)
88     {
89         return clazz.getString(u2stringIndex);
90     }
91
92
93     // Implementations for Constant.
94

95     public int getTag()
96     {
97         return ClassConstants.CONSTANT_String;
98     }
99
100     public void accept(Clazz clazz, ConstantVisitor constantVisitor)
101     {
102         constantVisitor.visitStringConstant(clazz, this);
103     }
104
105
106     /**
107      * Lets the referenced class accept the given visitor.
108      */

109     public void referencedClassAccept(ClassVisitor classVisitor)
110     {
111         if (referencedClass != null &&
112             referencedMember == null)
113         {
114             referencedClass.accept(classVisitor);
115         }
116     }
117
118
119     /**
120      * Lets the referenced member accept the given visitor.
121      */

122     public void referencedMemberAccept(MemberVisitor memberVisitor)
123     {
124         if (referencedMember != null)
125         {
126             referencedMember.accept(referencedClass, memberVisitor);
127         }
128     }
129 }
130
Popular Tags