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.classfile; 22 23 24import proguard.classfile.attribute.*; 25import proguard.classfile.attribute.visitor.AttributeVisitor; 26import proguard.classfile.visitor.MemberVisitor; 27 28/** 29 * Representation of a field or method from a program class. 30 * 31 * @author Eric Lafortune 32 */ 33public abstract class ProgramMember implements Member 34{ 35 public int u2accessFlags; 36 public int u2nameIndex; 37 public int u2descriptorIndex; 38 public int u2attributesCount; 39 public Attribute[] attributes; 40 41 /** 42 * An extra field in which visitors can store information. 43 */ 44 public Object visitorInfo; 45 46 47 /** 48 * Creates an uninitialized ProgramMember. 49 */ 50 protected ProgramMember() 51 { 52 } 53 54 55 /** 56 * Creates an initialized ProgramMember. 57 */ 58 protected ProgramMember(int u2accessFlags, 59 int u2nameIndex, 60 int u2descriptorIndex, 61 int u2attributesCount, 62 Attribute[] attributes) 63 { 64 this.u2accessFlags = u2accessFlags; 65 this.u2nameIndex = u2nameIndex; 66 this.u2descriptorIndex = u2descriptorIndex; 67 this.u2attributesCount = u2attributesCount; 68 this.attributes = attributes; 69 } 70 71 72 /** 73 * Returns the line number range of the given class member as "m:n", 74 * if it can find it, or <code>null</code> otherwise. 75 */ 76 public String getLineNumberRange(Clazz clazz) 77 { 78 CodeAttribute codeAttribute = 79 (CodeAttribute)getAttribute(clazz, ClassConstants.ATTR_Code); 80 if (codeAttribute == null) 81 { 82 return null; 83 } 84 85 LineNumberTableAttribute lineNumberTableAttribute = 86 (LineNumberTableAttribute)codeAttribute.getAttribute(clazz, 87 ClassConstants.ATTR_LineNumberTable); 88 if (lineNumberTableAttribute == null) 89 { 90 return null; 91 } 92 93 return "" + 94 lineNumberTableAttribute.getLineNumber(0) + 95 ":" + 96 lineNumberTableAttribute.getLineNumber(Integer.MAX_VALUE); 97 } 98 99 100 /** 101 * Returns the (first) attribute with the given name. 102 */ 103 private Attribute getAttribute(Clazz clazz, String name) 104 { 105 for (int index = 0; index < u2attributesCount; index++) 106 { 107 Attribute attribute = attributes[index]; 108 if (attribute.getAttributeName(clazz).equals(name)) 109 { 110 return attribute; 111 } 112 } 113 114 return null; 115 } 116 117 118 /** 119 * Accepts the given member info visitor. 120 */ 121 public abstract void accept(ProgramClass programClass, 122 MemberVisitor memberVisitor); 123 124 125 126 /** 127 * Lets the given attribute info visitor visit all the attributes of 128 * this member info. 129 */ 130 public abstract void attributesAccept(ProgramClass programClass, 131 AttributeVisitor attributeVisitor); 132 133 134 // Implementations for Member. 135 136 public int getAccessFlags() 137 { 138 return u2accessFlags; 139 } 140 141 public String getName(Clazz clazz) 142 { 143 return clazz.getString(u2nameIndex); 144 } 145 146 public String getDescriptor(Clazz clazz) 147 { 148 return clazz.getString(u2descriptorIndex); 149 } 150 151 public void accept(Clazz clazz, MemberVisitor memberVisitor) 152 { 153 accept((ProgramClass)clazz, memberVisitor); 154 } 155 156 157 // Implementations for VisitorAccepter. 158 159 public Object getVisitorInfo() 160 { 161 return visitorInfo; 162 } 163 164 public void setVisitorInfo(Object visitorInfo) 165 { 166 this.visitorInfo = visitorInfo; 167 } 168} 169