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.annotation; 22 23 import proguard.classfile.*; 24 import proguard.classfile.attribute.annotation.visitor.ElementValueVisitor; 25 import proguard.classfile.visitor.MemberVisitor; 26 27 /** 28 * This abstract class represents an element value that is attached to an 29 * annotation or an annotation default. Specific types of element values are 30 * subclassed from it. 31 * 32 * @author Eric Lafortune 33 */ 34 public abstract class ElementValue implements VisitorAccepter 35 { 36 /** 37 * An extra field for the optional element name. It is used in element value 38 * pairs of annotations. Otherwise, it is 0. 39 */ 40 public int u2elementNameIndex; 41 42 /** 43 * An extra field pointing to the referenced <code>Clazz</code> 44 * object, if applicable. This field is typically filled out by the 45 * <code>{@link proguard.classfile.util.ClassReferenceInitializer}</code>. 46 */ 47 public Clazz referencedClass; 48 49 /** 50 * An extra field pointing to the referenced <code>Method</code> 51 * object, if applicable. This field is typically filled out by the 52 * <code>{@link proguard.classfile.util.ClassReferenceInitializer}</code>. 53 */ 54 public Method referencedMethod; 55 56 /** 57 * An extra field in which visitors can store information. 58 */ 59 public Object visitorInfo; 60 61 62 /** 63 * Creates an uninitialized ElementValue. 64 */ ElementValue()65 protected ElementValue() 66 { 67 } 68 69 70 /** 71 * Creates an initialized ElementValue. 72 */ ElementValue(int u2elementNameIndex)73 protected ElementValue(int u2elementNameIndex) 74 { 75 this.u2elementNameIndex = u2elementNameIndex; 76 } 77 78 79 /** 80 * Returns the element name. 81 */ getMethodName(Clazz clazz)82 public String getMethodName(Clazz clazz) 83 { 84 return clazz.getString(u2elementNameIndex); 85 } 86 87 88 // Abstract methods to be implemented by extensions. 89 90 /** 91 * Returns the tag of this element value. 92 */ getTag()93 public abstract char getTag(); 94 95 96 /** 97 * Accepts the given visitor. 98 */ accept(Clazz clazz, Annotation annotation, ElementValueVisitor elementValueVisitor)99 public abstract void accept(Clazz clazz, Annotation annotation, ElementValueVisitor elementValueVisitor); 100 101 102 103 /** 104 * Applies the given visitor to the referenced method. 105 */ referencedMethodAccept(MemberVisitor memberVisitor)106 public void referencedMethodAccept(MemberVisitor memberVisitor) 107 { 108 if (referencedMethod != null) 109 { 110 referencedMethod.accept(referencedClass, memberVisitor); 111 } 112 } 113 114 115 // Implementations for VisitorAccepter. 116 getVisitorInfo()117 public Object getVisitorInfo() 118 { 119 return visitorInfo; 120 } 121 setVisitorInfo(Object visitorInfo)122 public void setVisitorInfo(Object visitorInfo) 123 { 124 this.visitorInfo = visitorInfo; 125 } 126 } 127