Pack200ClassReader.java

  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. import org.objectweb.asm.ClassReader;

  19. /**
  20.  * Wrapper for ClassReader that enables pack200 to obtain extra class file information
  21.  */
  22. public class Pack200ClassReader extends ClassReader {

  23.     private boolean lastConstantHadWideIndex;
  24.     private int lastUnsignedShort;
  25.     private boolean anySyntheticAttributes;
  26.     private String fileName;

  27.     /**
  28.      * @param b the contents of class file in the format of bytes
  29.      */
  30.     public Pack200ClassReader(final byte[] b) {
  31.         super(b);
  32.     }

  33.     public String getFileName() {
  34.         return fileName;
  35.     }

  36.     public boolean hasSyntheticAttributes() {
  37.         return anySyntheticAttributes;
  38.     }

  39.     public boolean lastConstantHadWideIndex() {
  40.         return lastConstantHadWideIndex;
  41.     }

  42.     @Override
  43.     public Object readConst(final int item, final char[] buf) {
  44.         lastConstantHadWideIndex = item == lastUnsignedShort;
  45.         return super.readConst(item, buf);
  46.     }

  47.     @Override
  48.     public int readUnsignedShort(final int index) {
  49.         // Doing this to check whether last load-constant instruction was ldc (18) or ldc_w (19)
  50.         // TODO: Assess whether this impacts on performance
  51.         final int unsignedShort = super.readUnsignedShort(index);
  52.         if (index > 0 && b[index - 1] == 19) {
  53.             lastUnsignedShort = unsignedShort;
  54.         } else {
  55.             lastUnsignedShort = Short.MIN_VALUE;
  56.         }
  57.         return unsignedShort;
  58.     }

  59.     @Override
  60.     public String readUTF8(final int arg0, final char[] arg1) {
  61.         final String utf8 = super.readUTF8(arg0, arg1);
  62.         if (!anySyntheticAttributes && "Synthetic".equals(utf8)) {
  63.             anySyntheticAttributes = true;
  64.         }
  65.         return utf8;
  66.     }

  67.     public void setFileName(final String name) {
  68.         this.fileName = name;
  69.     }

  70. }