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