    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;
    018
    019import java.io.IOException;
    020import java.io.InputStream;
    021
    022import org.apache.commons.compress.harmony.pack200.Codec;
    023import org.apache.commons.compress.harmony.pack200.Pack200Exception;
    024import org.apache.commons.compress.harmony.unpack200.bytecode.AnnotationDefaultAttribute;
    025import org.apache.commons.compress.harmony.unpack200.bytecode.CodeAttribute;
    026import org.apache.commons.compress.harmony.unpack200.bytecode.ConstantValueAttribute;
    027import org.apache.commons.compress.harmony.unpack200.bytecode.DeprecatedAttribute;
    028import org.apache.commons.compress.harmony.unpack200.bytecode.EnclosingMethodAttribute;
    029import org.apache.commons.compress.harmony.unpack200.bytecode.ExceptionsAttribute;
    030import org.apache.commons.compress.harmony.unpack200.bytecode.InnerClassesAttribute;
    031import org.apache.commons.compress.harmony.unpack200.bytecode.LineNumberTableAttribute;
    032import org.apache.commons.compress.harmony.unpack200.bytecode.LocalVariableTableAttribute;
    033import org.apache.commons.compress.harmony.unpack200.bytecode.LocalVariableTypeTableAttribute;
    034import org.apache.commons.compress.harmony.unpack200.bytecode.SignatureAttribute;
    035import org.apache.commons.compress.harmony.unpack200.bytecode.SourceFileAttribute;
    036
    037/**
    038 * Attribute definition bands are the set of bands used to define extra attributes transmitted in the archive.
    039 */
    040public class AttrDefinitionBands extends BandSet {
    041
    042    private int[] attributeDefinitionHeader;
    043
    044    private String[] attributeDefinitionLayout;
    045
    046    private String[] attributeDefinitionName;
    047
    048    private AttributeLayoutMap attributeDefinitionMap;
    049
    050    private final String[] cpUTF8;
    051
    052    public AttrDefinitionBands(final Segment segment) {
    053        super(segment);
    054        this.cpUTF8 = segment.getCpBands().getCpUTF8();
    055    }
    056
    057    public AttributeLayoutMap getAttributeDefinitionMap() {
    058        return attributeDefinitionMap;
    059    }
    060
    061    /*
    062     * (non-Javadoc)
    063     *
    064     * @see org.apache.commons.compress.harmony.unpack200.BandSet#unpack(java.io.InputStream)
    065     */
    066    @Override
    067    public void read(final InputStream in) throws IOException, Pack200Exception {
    068        final int attributeDefinitionCount = header.getAttributeDefinitionCount();
    069        attributeDefinitionHeader = decodeBandInt("attr_definition_headers", in, Codec.BYTE1, attributeDefinitionCount);
    070        attributeDefinitionName = parseReferences("attr_definition_name", in, Codec.UNSIGNED5, attributeDefinitionCount,
    071            cpUTF8);
    072        attributeDefinitionLayout = parseReferences("attr_definition_layout", in, Codec.UNSIGNED5,
    073            attributeDefinitionCount, cpUTF8);
    074
    075        attributeDefinitionMap = new AttributeLayoutMap();
    076
    077        int overflowIndex = 32;
    078        if (segment.getSegmentHeader().getOptions().hasClassFlagsHi()) {
    079            overflowIndex = 63;
    080        }
    081        for (int i = 0; i < attributeDefinitionCount; i++) {
    082            final int context = attributeDefinitionHeader[i] & 0x03;
    083            int index = (attributeDefinitionHeader[i] >> 2) - 1;
    084            if (index == -1) {
    085                index = overflowIndex++;
    086            }
    087            final AttributeLayout layout = new AttributeLayout(attributeDefinitionName[i], context,
    088                attributeDefinitionLayout[i], index, false);
    089            final NewAttributeBands newBands = new NewAttributeBands(segment, layout);
    090            attributeDefinitionMap.add(layout, newBands);
    091        }
    092        attributeDefinitionMap.checkMap();
    093        setupDefaultAttributeNames();
    094    }
    095
    096    private void setupDefaultAttributeNames() {
    097        AnnotationDefaultAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("AnnotationDefault")); //$NON-NLS-1$
    098        CodeAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("Code")); //$NON-NLS-1$
    099        ConstantValueAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("ConstantValue")); //$NON-NLS-1$
    100        DeprecatedAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("Deprecated")); //$NON-NLS-1$
    101        EnclosingMethodAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("EnclosingMethod")); //$NON-NLS-1$
    102        ExceptionsAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("Exceptions")); //$NON-NLS-1$
    103        InnerClassesAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("InnerClasses")); //$NON-NLS-1$
    104        LineNumberTableAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("LineNumberTable")); //$NON-NLS-1$
    105        LocalVariableTableAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("LocalVariableTable")); //$NON-NLS-1$
    106        LocalVariableTypeTableAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("LocalVariableTypeTable")); //$NON-NLS-1$
    107        SignatureAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("Signature")); //$NON-NLS-1$
    108        SourceFileAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("SourceFile")); //$NON-NLS-1$
    109        MetadataBandGroup.setRvaAttributeName(segment.getCpBands().cpUTF8Value("RuntimeVisibleAnnotations"));
    110        MetadataBandGroup.setRiaAttributeName(segment.getCpBands().cpUTF8Value("RuntimeInvisibleAnnotations"));
    111        MetadataBandGroup.setRvpaAttributeName(segment.getCpBands().cpUTF8Value("RuntimeVisibleParameterAnnotations"));
    112        MetadataBandGroup
    113            .setRipaAttributeName(segment.getCpBands().cpUTF8Value("RuntimeInvisibleParameterAnnotations"));
    114    }
    115
    116    @Override
    117    public void unpack() throws Pack200Exception, IOException {
    118
    119    }
    120
    121}