    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.commons.compress.harmony.unpack200.bytecode;
    018
    019import java.io.DataOutputStream;
    020import java.io.IOException;
    021
    022/**
    023 * Enclosing method class file attribute.
    024 */
    025public class EnclosingMethodAttribute extends Attribute {
    026
    027    private int class_index;
    028    private int method_index;
    029    private final CPClass cpClass;
    030    private final CPNameAndType method;
    031    private static CPUTF8 attributeName;
    032
    033    public static void setAttributeName(final CPUTF8 cpUTF8Value) {
    034        attributeName = cpUTF8Value;
    035    }
    036
    037    public EnclosingMethodAttribute(final CPClass cpClass, final CPNameAndType method) {
    038        super(attributeName);
    039        this.cpClass = cpClass;
    040        this.method = method;
    041    }
    042
    043    @Override
    044    protected ClassFileEntry[] getNestedClassFileEntries() {
    045        if (method != null) {
    046            return new ClassFileEntry[] {attributeName, cpClass, method};
    047        }
    048        return new ClassFileEntry[] {attributeName, cpClass};
    049    }
    050
    051    /*
    052     * (non-Javadoc)
    053     *
    054     * @see org.apache.commons.compress.harmony.unpack200.bytecode.Attribute#getLength()
    055     */
    056    @Override
    057    protected int getLength() {
    058        return 4;
    059    }
    060
    061    @Override
    062    protected void resolve(final ClassConstantPool pool) {
    063        super.resolve(pool);
    064        cpClass.resolve(pool);
    065        class_index = pool.indexOf(cpClass);
    066        if (method != null) {
    067            method.resolve(pool);
    068            method_index = pool.indexOf(method);
    069        } else {
    070            method_index = 0;
    071        }
    072    }
    073
    074    /*
    075     * (non-Javadoc)
    076     *
    077     * @see org.apache.commons.compress.harmony.unpack200.bytecode.Attribute#writeBody(java.io.DataOutputStream)
    078     */
    079    @Override
    080    protected void writeBody(final DataOutputStream dos) throws IOException {
    081        dos.writeShort(class_index);
    082        dos.writeShort(method_index);
    083    }
    084
    085    /*
    086     * (non-Javadoc)
    087     *
    088     * @see org.apache.commons.compress.harmony.unpack200.bytecode.ClassFileEntry#toString()
    089     */
    090    @Override
    091    public String toString() {
    092        return "EnclosingMethod";
    093    }
    094
    095}