001: /*
002: * ProGuard -- shrinking, optimization, obfuscation, and preverification
003: * of Java bytecode.
004: *
005: * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
006: *
007: * This library is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the Free
009: * Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This library is distributed in the hope that it will be useful, but WITHOUT
013: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this library; if not, write to the Free Software Foundation,
019: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: package proguard.classfile;
022:
023: import proguard.classfile.attribute.*;
024: import proguard.classfile.attribute.visitor.AttributeVisitor;
025: import proguard.classfile.visitor.MemberVisitor;
026:
027: /**
028: * Representation of a field or method from a program class.
029: *
030: * @author Eric Lafortune
031: */
032: public abstract class ProgramMember implements Member {
033: public int u2accessFlags;
034: public int u2nameIndex;
035: public int u2descriptorIndex;
036: public int u2attributesCount;
037: public Attribute[] attributes;
038:
039: /**
040: * An extra field in which visitors can store information.
041: */
042: public Object visitorInfo;
043:
044: protected ProgramMember() {
045: }
046:
047: /**
048: * Returns the line number range of the given class member as "m:n",
049: * if it can find it, or <code>null</code> otherwise.
050: */
051: public String getLineNumberRange(Clazz clazz) {
052: CodeAttribute codeAttribute = (CodeAttribute) getAttribute(
053: clazz, ClassConstants.ATTR_Code);
054: if (codeAttribute == null) {
055: return null;
056: }
057:
058: LineNumberTableAttribute lineNumberTableAttribute = (LineNumberTableAttribute) codeAttribute
059: .getAttribute(clazz,
060: ClassConstants.ATTR_LineNumberTable);
061: if (lineNumberTableAttribute == null) {
062: return null;
063: }
064:
065: return ""
066: + lineNumberTableAttribute.getLineNumber(0)
067: + ":"
068: + lineNumberTableAttribute
069: .getLineNumber(Integer.MAX_VALUE);
070: }
071:
072: /**
073: * Returns the (first) attribute with the given name.
074: */
075: private Attribute getAttribute(Clazz clazz, String name) {
076: for (int index = 0; index < u2attributesCount; index++) {
077: Attribute attribute = attributes[index];
078: if (attribute.getAttributeName(clazz).equals(name)) {
079: return attribute;
080: }
081: }
082:
083: return null;
084: }
085:
086: /**
087: * Accepts the given member info visitor.
088: */
089: public abstract void accept(ProgramClass programClass,
090: MemberVisitor memberVisitor);
091:
092: /**
093: * Lets the given attribute info visitor visit all the attributes of
094: * this member info.
095: */
096: public abstract void attributesAccept(ProgramClass programClass,
097: AttributeVisitor attributeVisitor);
098:
099: // Implementations for Member.
100:
101: public int getAccessFlags() {
102: return u2accessFlags;
103: }
104:
105: public String getName(Clazz clazz) {
106: return clazz.getString(u2nameIndex);
107: }
108:
109: public String getDescriptor(Clazz clazz) {
110: return clazz.getString(u2descriptorIndex);
111: }
112:
113: public void accept(Clazz clazz, MemberVisitor memberVisitor) {
114: accept((ProgramClass) clazz, memberVisitor);
115: }
116:
117: // Implementations for VisitorAccepter.
118:
119: public Object getVisitorInfo() {
120: return visitorInfo;
121: }
122:
123: public void setVisitorInfo(Object visitorInfo) {
124: this.visitorInfo = visitorInfo;
125: }
126: }
|