001: /*
002: * Javassist, a Java-bytecode translator toolkit.
003: * Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved.
004: *
005: * The contents of this file are subject to the Mozilla Public License Version
006: * 1.1 (the "License"); you may not use this file except in compliance with
007: * the License. Alternatively, the contents of this file may be used under
008: * the terms of the GNU Lesser General Public License Version 2.1 or later.
009: *
010: * Software distributed under the License is distributed on an "AS IS" basis,
011: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
012: * for the specific language governing rights and limitations under the
013: * License.
014: */
015:
016: package javassist.bytecode;
017:
018: import java.io.DataInputStream;
019: import java.io.IOException;
020: import java.util.Map;
021:
022: /**
023: * <code>EnclosingMethod_attribute</code>.
024: */
025: public class EnclosingMethodAttribute extends AttributeInfo {
026: /**
027: * The name of this attribute <code>"EnclosingMethod"</code>.
028: */
029: public static final String tag = "EnclosingMethod";
030:
031: EnclosingMethodAttribute(ConstPool cp, int n, DataInputStream in)
032: throws IOException {
033: super (cp, n, in);
034: }
035:
036: /**
037: * Constructs an EnclosingMethod attribute.
038: *
039: * @param cp a constant pool table.
040: * @param className the name of the innermost enclosing class.
041: * @param methodName the name of the enclosing method.
042: * @param methodDesc the descriptor of the enclosing method.
043: */
044: public EnclosingMethodAttribute(ConstPool cp, String className,
045: String methodName, String methodDesc) {
046: super (cp, tag);
047: int ci = cp.addClassInfo(className);
048: int ni = cp.addNameAndTypeInfo(methodName, methodDesc);
049: byte[] bvalue = new byte[4];
050: bvalue[0] = (byte) (ci >>> 8);
051: bvalue[1] = (byte) ci;
052: bvalue[2] = (byte) (ni >>> 8);
053: bvalue[3] = (byte) ni;
054: set(bvalue);
055: }
056:
057: /**
058: * Constructs an EnclosingMethod attribute.
059: * The value of <code>method_index</code> is set to 0.
060: *
061: * @param cp a constant pool table.
062: * @param className the name of the innermost enclosing class.
063: */
064: public EnclosingMethodAttribute(ConstPool cp, String className) {
065: super (cp, tag);
066: int ci = cp.addClassInfo(className);
067: int ni = 0;
068: byte[] bvalue = new byte[4];
069: bvalue[0] = (byte) (ci >>> 8);
070: bvalue[1] = (byte) ci;
071: bvalue[2] = (byte) (ni >>> 8);
072: bvalue[3] = (byte) ni;
073: set(bvalue);
074: }
075:
076: /**
077: * Returns the value of <code>class_index</code>.
078: */
079: public int classIndex() {
080: return ByteArray.readU16bit(get(), 0);
081: }
082:
083: /**
084: * Returns the value of <code>method_index</code>.
085: */
086: public int methodIndex() {
087: return ByteArray.readU16bit(get(), 2);
088: }
089:
090: /**
091: * Returns the name of the class specified by <code>class_index</code>.
092: */
093: public String className() {
094: return getConstPool().getClassInfo(classIndex());
095: }
096:
097: /**
098: * Returns the method name specified by <code>method_index</code>.
099: */
100: public String methodName() {
101: ConstPool cp = getConstPool();
102: int mi = methodIndex();
103: int ni = cp.getNameAndTypeName(mi);
104: return cp.getUtf8Info(ni);
105: }
106:
107: /**
108: * Returns the method descriptor specified by <code>method_index</code>.
109: */
110: public String methodDescriptor() {
111: ConstPool cp = getConstPool();
112: int mi = methodIndex();
113: int ti = cp.getNameAndTypeDescriptor(mi);
114: return cp.getUtf8Info(ti);
115: }
116:
117: /**
118: * Makes a copy. Class names are replaced according to the
119: * given <code>Map</code> object.
120: *
121: * @param newCp the constant pool table used by the new copy.
122: * @param classnames pairs of replaced and substituted
123: * class names.
124: */
125: public AttributeInfo copy(ConstPool newCp, Map classnames) {
126: if (methodIndex() == 0)
127: return new EnclosingMethodAttribute(newCp, className());
128: else
129: return new EnclosingMethodAttribute(newCp, className(),
130: methodName(), methodDescriptor());
131: }
132: }
|