001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * https://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.commons.compress.harmony.unpack200.bytecode; 020 021import java.io.DataOutputStream; 022import java.io.IOException; 023 024/** 025 * Enclosing method class file attribute. 026 */ 027public class EnclosingMethodAttribute extends Attribute { 028 029 private static CPUTF8 attributeName; 030 031 public static void setAttributeName(final CPUTF8 cpUTF8Value) { 032 attributeName = cpUTF8Value; 033 } 034 035 private int classIndex; 036 private int methodIndex; 037 private final CPClass cpClass; 038 039 private final CPNameAndType method; 040 041 /** 042 * Constructs a new instance. 043 * 044 * @param cpClass a constant pool class. 045 * @param method a constant pool name and type. 046 */ 047 public EnclosingMethodAttribute(final CPClass cpClass, final CPNameAndType method) { 048 super(attributeName); 049 this.cpClass = cpClass; 050 this.method = method; 051 } 052 053 /* 054 * (non-Javadoc) 055 * 056 * @see org.apache.commons.compress.harmony.unpack200.bytecode.Attribute#getLength() 057 */ 058 @Override 059 protected int getLength() { 060 return 4; 061 } 062 063 @Override 064 protected ClassFileEntry[] getNestedClassFileEntries() { 065 if (method != null) { 066 return new ClassFileEntry[] { attributeName, cpClass, method }; 067 } 068 return new ClassFileEntry[] { attributeName, cpClass }; 069 } 070 071 @Override 072 protected void resolve(final ClassConstantPool pool) { 073 super.resolve(pool); 074 cpClass.resolve(pool); 075 classIndex = pool.indexOf(cpClass); 076 if (method != null) { 077 method.resolve(pool); 078 methodIndex = pool.indexOf(method); 079 } else { 080 methodIndex = 0; 081 } 082 } 083 084 /* 085 * (non-Javadoc) 086 * 087 * @see org.apache.commons.compress.harmony.unpack200.bytecode.ClassFileEntry#toString() 088 */ 089 @Override 090 public String toString() { 091 return "EnclosingMethod"; 092 } 093 094 /* 095 * (non-Javadoc) 096 * 097 * @see org.apache.commons.compress.harmony.unpack200.bytecode.Attribute#writeBody(java.io.DataOutputStream) 098 */ 099 @Override 100 protected void writeBody(final DataOutputStream dos) throws IOException { 101 dos.writeShort(classIndex); 102 dos.writeShort(methodIndex); 103 } 104 105}