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