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