PMGClass.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.bcel.classfile;

  18. import java.io.DataInput;
  19. import java.io.DataOutputStream;
  20. import java.io.IOException;

  21. import org.apache.bcel.Const;

  22. /**
  23.  * This class is derived from <em>Attribute</em> and represents a reference to a PMG attribute.
  24.  *
  25.  * @see Attribute
  26.  */
  27. public final class PMGClass extends Attribute {

  28.     private int pmgClassIndex;
  29.     private int pmgIndex;

  30.     /**
  31.      * Constructs object from input stream.
  32.      *
  33.      * @param nameIndex Index in constant pool to CONSTANT_Utf8
  34.      * @param length Content length in bytes
  35.      * @param input Input stream
  36.      * @param constantPool Array of constants
  37.      * @throws IOException if an I/O error occurs.
  38.      */
  39.     PMGClass(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
  40.         this(nameIndex, length, input.readUnsignedShort(), input.readUnsignedShort(), constantPool);
  41.     }

  42.     /**
  43.      * @param nameIndex Index in constant pool to CONSTANT_Utf8
  44.      * @param length Content length in bytes
  45.      * @param pmgIndex index in constant pool for source file name
  46.      * @param pmgClassIndex Index in constant pool to CONSTANT_Utf8
  47.      * @param constantPool Array of constants
  48.      */
  49.     public PMGClass(final int nameIndex, final int length, final int pmgIndex, final int pmgClassIndex, final ConstantPool constantPool) {
  50.         super(Const.ATTR_PMG, nameIndex, length, constantPool);
  51.         this.pmgIndex = pmgIndex;
  52.         this.pmgClassIndex = pmgClassIndex;
  53.     }

  54.     /**
  55.      * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a
  56.      * physical copy.
  57.      *
  58.      * @param pgmClass Source to copy.
  59.      */
  60.     public PMGClass(final PMGClass pgmClass) {
  61.         this(pgmClass.getNameIndex(), pgmClass.getLength(), pgmClass.getPMGIndex(), pgmClass.getPMGClassIndex(), pgmClass.getConstantPool());
  62.     }

  63.     /**
  64.      * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
  65.      * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
  66.      *
  67.      * @param v Visitor object
  68.      */
  69.     @Override
  70.     public void accept(final Visitor v) {
  71.         println("Visiting non-standard PMGClass object");
  72.     }

  73.     /**
  74.      * @return deep copy of this attribute
  75.      */
  76.     @Override
  77.     public Attribute copy(final ConstantPool constantPool) {
  78.         return (Attribute) clone();
  79.     }

  80.     /**
  81.      * Dump source file attribute to file stream in binary format.
  82.      *
  83.      * @param file Output file stream
  84.      * @throws IOException if an I/O error occurs.
  85.      */
  86.     @Override
  87.     public void dump(final DataOutputStream file) throws IOException {
  88.         super.dump(file);
  89.         file.writeShort(pmgIndex);
  90.         file.writeShort(pmgClassIndex);
  91.     }

  92.     /**
  93.      * @return Index in constant pool of source file name.
  94.      */
  95.     public int getPMGClassIndex() {
  96.         return pmgClassIndex;
  97.     }

  98.     /**
  99.      * @return PMG class name.
  100.      */
  101.     public String getPMGClassName() {
  102.         return super.getConstantPool().getConstantUtf8(pmgClassIndex).getBytes();
  103.     }

  104.     /**
  105.      * @return Index in constant pool of source file name.
  106.      */
  107.     public int getPMGIndex() {
  108.         return pmgIndex;
  109.     }

  110.     /**
  111.      * @return PMG name.
  112.      */
  113.     public String getPMGName() {
  114.         return super.getConstantPool().getConstantUtf8(pmgIndex).getBytes();
  115.     }

  116.     /**
  117.      * @param pmgClassIndex
  118.      */
  119.     public void setPMGClassIndex(final int pmgClassIndex) {
  120.         this.pmgClassIndex = pmgClassIndex;
  121.     }

  122.     /**
  123.      * @param pmgIndex
  124.      */
  125.     public void setPMGIndex(final int pmgIndex) {
  126.         this.pmgIndex = pmgIndex;
  127.     }

  128.     /**
  129.      * @return String representation
  130.      */
  131.     @Override
  132.     public String toString() {
  133.         return "PMGClass(" + getPMGName() + ", " + getPMGClassName() + ")";
  134.     }
  135. }