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, cpUTF8);
071        attributeDefinitionLayout = parseReferences("attr_definition_layout", in, Codec.UNSIGNED5, attributeDefinitionCount, cpUTF8);
072
073        attributeDefinitionMap = new AttributeLayoutMap();
074
075        int overflowIndex = 32;
076        if (segment.getSegmentHeader().getOptions().hasClassFlagsHi()) {
077            overflowIndex = 63;
078        }
079        for (int i = 0; i < attributeDefinitionCount; i++) {
080            final int context = attributeDefinitionHeader[i] & 0x03;
081            int index = (attributeDefinitionHeader[i] >> 2) - 1;
082            if (index == -1) {
083                index = overflowIndex++;
084            }
085            final AttributeLayout layout = new AttributeLayout(attributeDefinitionName[i], context, attributeDefinitionLayout[i], index, false);
086            final NewAttributeBands newBands = new NewAttributeBands(segment, layout);
087            attributeDefinitionMap.add(layout, newBands);
088        }
089        attributeDefinitionMap.checkMap();
090        setupDefaultAttributeNames();
091    }
092
093    private void setupDefaultAttributeNames() {
094        AnnotationDefaultAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("AnnotationDefault")); //$NON-NLS-1$
095        CodeAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("Code")); //$NON-NLS-1$
096        ConstantValueAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("ConstantValue")); //$NON-NLS-1$
097        DeprecatedAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("Deprecated")); //$NON-NLS-1$
098        EnclosingMethodAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("EnclosingMethod")); //$NON-NLS-1$
099        ExceptionsAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("Exceptions")); //$NON-NLS-1$
100        InnerClassesAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("InnerClasses")); //$NON-NLS-1$
101        LineNumberTableAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("LineNumberTable")); //$NON-NLS-1$
102        LocalVariableTableAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("LocalVariableTable")); //$NON-NLS-1$
103        LocalVariableTypeTableAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("LocalVariableTypeTable")); //$NON-NLS-1$
104        SignatureAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("Signature")); //$NON-NLS-1$
105        SourceFileAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("SourceFile")); //$NON-NLS-1$
106        MetadataBandGroup.setRvaAttributeName(segment.getCpBands().cpUTF8Value("RuntimeVisibleAnnotations"));
107        MetadataBandGroup.setRiaAttributeName(segment.getCpBands().cpUTF8Value("RuntimeInvisibleAnnotations"));
108        MetadataBandGroup.setRvpaAttributeName(segment.getCpBands().cpUTF8Value("RuntimeVisibleParameterAnnotations"));
109        MetadataBandGroup.setRipaAttributeName(segment.getCpBands().cpUTF8Value("RuntimeInvisibleParameterAnnotations"));
110    }
111
112    @Override
113    public void unpack() throws Pack200Exception, IOException {
114
115    }
116
117}