View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.commons.compress.harmony.unpack200;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  
22  import org.apache.commons.compress.harmony.pack200.Codec;
23  import org.apache.commons.compress.harmony.pack200.Pack200Exception;
24  import org.apache.commons.compress.harmony.unpack200.bytecode.AnnotationDefaultAttribute;
25  import org.apache.commons.compress.harmony.unpack200.bytecode.CodeAttribute;
26  import org.apache.commons.compress.harmony.unpack200.bytecode.ConstantValueAttribute;
27  import org.apache.commons.compress.harmony.unpack200.bytecode.DeprecatedAttribute;
28  import org.apache.commons.compress.harmony.unpack200.bytecode.EnclosingMethodAttribute;
29  import org.apache.commons.compress.harmony.unpack200.bytecode.ExceptionsAttribute;
30  import org.apache.commons.compress.harmony.unpack200.bytecode.InnerClassesAttribute;
31  import org.apache.commons.compress.harmony.unpack200.bytecode.LineNumberTableAttribute;
32  import org.apache.commons.compress.harmony.unpack200.bytecode.LocalVariableTableAttribute;
33  import org.apache.commons.compress.harmony.unpack200.bytecode.LocalVariableTypeTableAttribute;
34  import org.apache.commons.compress.harmony.unpack200.bytecode.SignatureAttribute;
35  import org.apache.commons.compress.harmony.unpack200.bytecode.SourceFileAttribute;
36  
37  /**
38   * Attribute definition bands are the set of bands used to define extra attributes transmitted in the archive.
39   */
40  public class AttrDefinitionBands extends BandSet {
41  
42      private int[] attributeDefinitionHeader;
43  
44      private String[] attributeDefinitionLayout;
45  
46      private String[] attributeDefinitionName;
47  
48      private AttributeLayoutMap attributeDefinitionMap;
49  
50      private final String[] cpUTF8;
51  
52      public AttrDefinitionBands(final Segment segment) {
53          super(segment);
54          this.cpUTF8 = segment.getCpBands().getCpUTF8();
55      }
56  
57      public AttributeLayoutMap getAttributeDefinitionMap() {
58          return attributeDefinitionMap;
59      }
60  
61      /*
62       * (non-Javadoc)
63       *
64       * @see org.apache.commons.compress.harmony.unpack200.BandSet#unpack(java.io.InputStream)
65       */
66      @Override
67      public void read(final InputStream in) throws IOException, Pack200Exception {
68          final int attributeDefinitionCount = header.getAttributeDefinitionCount();
69          attributeDefinitionHeader = decodeBandInt("attr_definition_headers", in, Codec.BYTE1, attributeDefinitionCount);
70          attributeDefinitionName = parseReferences("attr_definition_name", in, Codec.UNSIGNED5, attributeDefinitionCount, cpUTF8);
71          attributeDefinitionLayout = parseReferences("attr_definition_layout", in, Codec.UNSIGNED5, attributeDefinitionCount, cpUTF8);
72  
73          attributeDefinitionMap = new AttributeLayoutMap();
74  
75          int overflowIndex = 32;
76          if (segment.getSegmentHeader().getOptions().hasClassFlagsHi()) {
77              overflowIndex = 63;
78          }
79          for (int i = 0; i < attributeDefinitionCount; i++) {
80              final int context = attributeDefinitionHeader[i] & 0x03;
81              int index = (attributeDefinitionHeader[i] >> 2) - 1;
82              if (index == -1) {
83                  index = overflowIndex++;
84              }
85              final AttributeLayout layout = new AttributeLayout(attributeDefinitionName[i], context, attributeDefinitionLayout[i], index, false);
86              final NewAttributeBands newBands = new NewAttributeBands(segment, layout);
87              attributeDefinitionMap.add(layout, newBands);
88          }
89          attributeDefinitionMap.checkMap();
90          setupDefaultAttributeNames();
91      }
92  
93      private void setupDefaultAttributeNames() {
94          AnnotationDefaultAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("AnnotationDefault")); //$NON-NLS-1$
95          CodeAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("Code")); //$NON-NLS-1$
96          ConstantValueAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("ConstantValue")); //$NON-NLS-1$
97          DeprecatedAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("Deprecated")); //$NON-NLS-1$
98          EnclosingMethodAttribute.setAttributeName(segment.getCpBands().cpUTF8Value("EnclosingMethod")); //$NON-NLS-1$
99          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 }