001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.compress.harmony.unpack200;
020
021import java.io.IOException;
022import java.io.InputStream;
023
024import org.apache.commons.compress.harmony.pack200.Codec;
025import org.apache.commons.compress.harmony.pack200.Pack200Exception;
026import org.apache.commons.compress.harmony.unpack200.bytecode.AnnotationDefaultAttribute;
027import org.apache.commons.compress.harmony.unpack200.bytecode.CodeAttribute;
028import org.apache.commons.compress.harmony.unpack200.bytecode.ConstantValueAttribute;
029import org.apache.commons.compress.harmony.unpack200.bytecode.DeprecatedAttribute;
030import org.apache.commons.compress.harmony.unpack200.bytecode.EnclosingMethodAttribute;
031import org.apache.commons.compress.harmony.unpack200.bytecode.ExceptionsAttribute;
032import org.apache.commons.compress.harmony.unpack200.bytecode.InnerClassesAttribute;
033import org.apache.commons.compress.harmony.unpack200.bytecode.LineNumberTableAttribute;
034import org.apache.commons.compress.harmony.unpack200.bytecode.LocalVariableTableAttribute;
035import org.apache.commons.compress.harmony.unpack200.bytecode.LocalVariableTypeTableAttribute;
036import org.apache.commons.compress.harmony.unpack200.bytecode.SignatureAttribute;
037import org.apache.commons.compress.harmony.unpack200.bytecode.SourceFileAttribute;
038
039/**
040 * Attribute definition bands are the set of bands used to define extra attributes transmitted in the archive.
041 */
042public class AttrDefinitionBands extends BandSet {
043
044    private int[] attributeDefinitionHeader;
045
046    private String[] attributeDefinitionLayout;
047
048    private String[] attributeDefinitionName;
049
050    private AttributeLayoutMap attributeDefinitionMap;
051
052    private final String[] cpUTF8;
053
054    /**
055     * Constructs a new instance.
056     *
057     * @param segment the segment.
058     */
059    public AttrDefinitionBands(final Segment segment) {
060        super(segment);
061        this.cpUTF8 = segment.getCpBands().getCpUTF8();
062    }
063
064    /**
065     * Gets the attribute layout map.
066     *
067     * @return the attribute layout map.
068     */
069    public AttributeLayoutMap getAttributeDefinitionMap() {
070        return attributeDefinitionMap;
071    }
072
073    /*
074     * (non-Javadoc)
075     *
076     * @see org.apache.commons.compress.harmony.unpack200.BandSet#unpack(java.io.InputStream)
077     */
078    @Override
079    public void read(final InputStream in) throws IOException, Pack200Exception {
080        final int attributeDefinitionCount = header.getAttributeDefinitionCount();
081        attributeDefinitionHeader = decodeBandInt("attr_definition_headers", in, Codec.BYTE1, attributeDefinitionCount);
082        attributeDefinitionName = parseReferences("attr_definition_name", in, Codec.UNSIGNED5, attributeDefinitionCount, cpUTF8);
083        attributeDefinitionLayout = parseReferences("attr_definition_layout", in, Codec.UNSIGNED5, attributeDefinitionCount, cpUTF8);
084
085        attributeDefinitionMap = new AttributeLayoutMap();
086
087        int overflowIndex = 32;
088        if (segment.getSegmentHeader().getOptions().hasClassFlagsHi()) {
089            overflowIndex = 63;
090        }
091        for (int i = 0; i < attributeDefinitionCount; i++) {
092            final int context = attributeDefinitionHeader[i] & 0x03;
093            int index = (attributeDefinitionHeader[i] >> 2) - 1;
094            if (index == -1) {
095                index = overflowIndex++;
096            }
097            final AttributeLayout layout = new AttributeLayout(attributeDefinitionName[i], context, attributeDefinitionLayout[i], index, false);
098            final NewAttributeBands newBands = new NewAttributeBands(segment, layout);
099            attributeDefinitionMap.add(layout, newBands);
100        }
101        attributeDefinitionMap.checkMap();
102        setupDefaultAttributeNames();
103    }
104
105    private void setupDefaultAttributeNames() {
106        AnnotationDefaultAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("AnnotationDefault")); //$NON-NLS-1$
107        CodeAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("Code")); //$NON-NLS-1$
108        ConstantValueAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("ConstantValue")); //$NON-NLS-1$
109        DeprecatedAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("Deprecated")); //$NON-NLS-1$
110        EnclosingMethodAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("EnclosingMethod")); //$NON-NLS-1$
111        ExceptionsAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("Exceptions")); //$NON-NLS-1$
112        InnerClassesAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("InnerClasses")); //$NON-NLS-1$
113        LineNumberTableAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("LineNumberTable")); //$NON-NLS-1$
114        LocalVariableTableAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("LocalVariableTable")); //$NON-NLS-1$
115        LocalVariableTypeTableAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("LocalVariableTypeTable")); //$NON-NLS-1$
116        SignatureAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("Signature")); //$NON-NLS-1$
117        SourceFileAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("SourceFile")); //$NON-NLS-1$
118        MetadataBandGroup.setRvaAttributeName(segment.getCpBands().cpUTF8Value("RuntimeVisibleAnnotations"));
119        MetadataBandGroup.setRiaAttributeName(segment.getCpBands().cpUTF8Value("RuntimeInvisibleAnnotations"));
120        MetadataBandGroup.setRvpaAttributeName(segment.getCpBands().cpUTF8Value("RuntimeVisibleParameterAnnotations"));
121        MetadataBandGroup.setRipaAttributeName(segment.getCpBands().cpUTF8Value("RuntimeInvisibleParameterAnnotations"));
122    }
123
124    @Override
125    public void unpack() throws Pack200Exception, IOException {
126
127    }
128
129}