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