001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.bytecode;
031:
032: import com.caucho.log.Log;
033:
034: import java.io.IOException;
035: import java.util.logging.Logger;
036:
037: /**
038: * Represents a method ref in the constant pool.
039: */
040: public class MethodRefConstant extends ConstantPoolEntry {
041: static private final Logger log = Log.open(MethodRefConstant.class);
042:
043: private int _classIndex;
044: private int _nameAndTypeIndex;
045:
046: /**
047: * Creates a new field ref constant.
048: */
049: MethodRefConstant(ConstantPool pool, int index, int classIndex,
050: int nameAndTypeIndex) {
051: super (pool, index);
052:
053: _classIndex = classIndex;
054: _nameAndTypeIndex = nameAndTypeIndex;
055: }
056:
057: /**
058: * Returns the class index.
059: */
060: public int getClassIndex() {
061: return _classIndex;
062: }
063:
064: /**
065: * Sets the class index.
066: */
067: public void setClassIndex(int index) {
068: _classIndex = index;
069: }
070:
071: /**
072: * Returns the class name
073: */
074: public String getClassName() {
075: return getConstantPool().getClass(_classIndex).getName();
076: }
077:
078: /**
079: * Returns the method name
080: */
081: public String getName() {
082: return getConstantPool().getNameAndType(_nameAndTypeIndex)
083: .getName();
084: }
085:
086: /**
087: * Returns the method type
088: */
089: public String getType() {
090: return getConstantPool().getNameAndType(_nameAndTypeIndex)
091: .getType();
092: }
093:
094: /**
095: * Sets the method name and type
096: */
097: public void setNameAndType(String name, String type) {
098: _nameAndTypeIndex = getConstantPool()
099: .addNameAndType(name, type).getIndex();
100: }
101:
102: /**
103: * Writes the contents of the pool entry.
104: */
105: void write(ByteCodeWriter out) throws IOException {
106: out.write(ConstantPool.CP_METHOD_REF);
107: out.writeShort(_classIndex);
108: out.writeShort(_nameAndTypeIndex);
109: }
110:
111: /**
112: * Exports to the target pool.
113: */
114: public int export(ConstantPool target) {
115: return target
116: .addMethodRef(getClassName(), getName(), getType())
117: .getIndex();
118: }
119:
120: public String toString() {
121: return "MethodRefConstant[" + getClassName() + "." + getName()
122: + "(" + getType() + ")]";
123: }
124: }
|