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    /*
058     * (non-Javadoc)
059     *
060     * @see org.apache.commons.compress.harmony.unpack200.BandSet#unpack(java.io.InputStream)
061     */
062    @Override
063    public void read(final InputStream in) throws IOException, Pack200Exception {
064        final int attributeDefinitionCount = header.getAttributeDefinitionCount();
065        attributeDefinitionHeader = decodeBandInt("attr_definition_headers", in, Codec.BYTE1, attributeDefinitionCount);
066        attributeDefinitionName = parseReferences("attr_definition_name", in, Codec.UNSIGNED5, attributeDefinitionCount,
067            cpUTF8);
068        attributeDefinitionLayout = parseReferences("attr_definition_layout", in, Codec.UNSIGNED5,
069            attributeDefinitionCount, cpUTF8);
070
071        attributeDefinitionMap = new AttributeLayoutMap();
072
073        int overflowIndex = 32;
074        if (segment.getSegmentHeader().getOptions().hasClassFlagsHi()) {
075            overflowIndex = 63;
076        }
077        for (int i = 0; i < attributeDefinitionCount; i++) {
078            final int context = attributeDefinitionHeader[i] & 0x03;
079            int index = (attributeDefinitionHeader[i] >> 2) - 1;
080            if (index == -1) {
081                index = overflowIndex++;
082            }
083            final AttributeLayout layout = new AttributeLayout(attributeDefinitionName[i], context,
084                attributeDefinitionLayout[i], index, false);
085            final NewAttributeBands newBands = new NewAttributeBands(segment, layout);
086            attributeDefinitionMap.add(layout, newBands);
087        }
088        attributeDefinitionMap.checkMap();
089        setupDefaultAttributeNames();
090    }
091
092    @Override
093    public void unpack() throws Pack200Exception, IOException {
094
095    }
096
097    private void setupDefaultAttributeNames() {
098        AnnotationDefaultAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("AnnotationDefault")); //$NON-NLS-1$
099        CodeAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("Code")); //$NON-NLS-1$
100        ConstantValueAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("ConstantValue")); //$NON-NLS-1$
101        DeprecatedAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("Deprecated")); //$NON-NLS-1$
102        EnclosingMethodAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("EnclosingMethod")); //$NON-NLS-1$
103        ExceptionsAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("Exceptions")); //$NON-NLS-1$
104        InnerClassesAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("InnerClasses")); //$NON-NLS-1$
105        LineNumberTableAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("LineNumberTable")); //$NON-NLS-1$
106        LocalVariableTableAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("LocalVariableTable")); //$NON-NLS-1$
107        LocalVariableTypeTableAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("LocalVariableTypeTable")); //$NON-NLS-1$
108        SignatureAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("Signature")); //$NON-NLS-1$
109        SourceFileAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("SourceFile")); //$NON-NLS-1$
110        MetadataBandGroup.setRvaAttributeName(segment.getCpBands().cpUTF8Value("RuntimeVisibleAnnotations"));
111        MetadataBandGroup.setRiaAttributeName(segment.getCpBands().cpUTF8Value("RuntimeInvisibleAnnotations"));
112        MetadataBandGroup.setRvpaAttributeName(segment.getCpBands().cpUTF8Value("RuntimeVisibleParameterAnnotations"));
113        MetadataBandGroup
114            .setRipaAttributeName(segment.getCpBands().cpUTF8Value("RuntimeInvisibleParameterAnnotations"));
115    }
116
117    public AttributeLayoutMap getAttributeDefinitionMap() {
118        return attributeDefinitionMap;
119    }
120
121}