001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.bcel.classfile; 018 019import java.io.DataInput; 020import java.io.DataOutputStream; 021import java.io.IOException; 022 023import org.apache.bcel.Const; 024import org.apache.bcel.util.Args; 025 026/** 027 * This attribute exists for local or anonymous classes and ... there can be only one. 028 * 029 * @since 6.0 030 */ 031public class EnclosingMethod extends Attribute { 032 033 // Pointer to the CONSTANT_Class_info structure representing the 034 // innermost class that encloses the declaration of the current class. 035 private int classIndex; 036 037 // If the current class is not immediately enclosed by a method or 038 // constructor, then the value of the method_index item must be zero. 039 // Otherwise, the value of the method_index item must point to a 040 // CONSTANT_NameAndType_info structure representing the name and the 041 // type of a method in the class referenced by the class we point 042 // to in the class_index. *It is the compiler responsibility* to 043 // ensure that the method identified by this index is the closest 044 // lexically enclosing method that includes the local/anonymous class. 045 private int methodIndex; 046 047 // Ctors - and code to read an attribute in. 048 EnclosingMethod(final int nameIndex, final int len, final DataInput input, final ConstantPool cpool) throws IOException { 049 this(nameIndex, len, input.readUnsignedShort(), input.readUnsignedShort(), cpool); 050 } 051 052 private EnclosingMethod(final int nameIndex, final int len, final int classIndex, final int methodIndex, final ConstantPool cpool) { 053 super(Const.ATTR_ENCLOSING_METHOD, nameIndex, Args.require(len, 4, "EnclosingMethod attribute length"), cpool); 054 this.classIndex = Args.requireU2(classIndex, 0, cpool.getLength(), "EnclosingMethod class index"); 055 this.methodIndex = Args.requireU2(methodIndex, "EnclosingMethod method index"); 056 } 057 058 @Override 059 public void accept(final Visitor v) { 060 v.visitEnclosingMethod(this); 061 } 062 063 @Override 064 public Attribute copy(final ConstantPool constantPool) { 065 return (Attribute) clone(); 066 } 067 068 @Override 069 public final void dump(final DataOutputStream file) throws IOException { 070 super.dump(file); 071 file.writeShort(classIndex); 072 file.writeShort(methodIndex); 073 } 074 075 public final ConstantClass getEnclosingClass() { 076 return super.getConstantPool().getConstant(classIndex, Const.CONSTANT_Class, ConstantClass.class); 077 } 078 079 // Accessors 080 public final int getEnclosingClassIndex() { 081 return classIndex; 082 } 083 084 public final ConstantNameAndType getEnclosingMethod() { 085 if (methodIndex == 0) { 086 return null; 087 } 088 return super.getConstantPool().getConstant(methodIndex, Const.CONSTANT_NameAndType, ConstantNameAndType.class); 089 } 090 091 public final int getEnclosingMethodIndex() { 092 return methodIndex; 093 } 094 095 public final void setEnclosingClassIndex(final int idx) { 096 classIndex = idx; 097 } 098 099 public final void setEnclosingMethodIndex(final int idx) { 100 methodIndex = idx; 101 } 102}