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.pack200;
20  
21  import org.objectweb.asm.ClassReader;
22  
23  /**
24   * Wrapper for ClassReader that enables pack200 to obtain extra class file information
25   */
26  public class Pack200ClassReader extends ClassReader {
27  
28      private boolean lastConstantHadWideIndex;
29      private int lastUnsignedShort;
30      private boolean anySyntheticAttributes;
31      private String fileName;
32  
33      /**
34       * @param b the contents of class file in the format of bytes
35       */
36      public Pack200ClassReader(final byte[] b) {
37          super(b);
38      }
39  
40      public String getFileName() {
41          return fileName;
42      }
43  
44      public boolean hasSyntheticAttributes() {
45          return anySyntheticAttributes;
46      }
47  
48      public boolean lastConstantHadWideIndex() {
49          return lastConstantHadWideIndex;
50      }
51  
52      @Override
53      public Object readConst(final int item, final char[] buf) {
54          lastConstantHadWideIndex = item == lastUnsignedShort;
55          return super.readConst(item, buf);
56      }
57  
58      @Override
59      public int readUnsignedShort(final int index) {
60          // Doing this to check whether last load-constant instruction was ldc (18) or ldc_w (19)
61          // TODO: Assess whether this impacts on performance
62          final int unsignedShort = super.readUnsignedShort(index);
63          if (index > 0 && b[index - 1] == 19) {
64              lastUnsignedShort = unsignedShort;
65          } else {
66              lastUnsignedShort = Short.MIN_VALUE;
67          }
68          return unsignedShort;
69      }
70  
71      @Override
72      public String readUTF8(final int arg0, final char[] arg1) {
73          final String utf8 = super.readUTF8(arg0, arg1);
74          if (!anySyntheticAttributes && "Synthetic".equals(utf8)) {
75              anySyntheticAttributes = true;
76          }
77          return utf8;
78      }
79  
80      public void setFileName(final String name) {
81          this.fileName = name;
82      }
83  
84  }