1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.bcel.internal.classfile;
  23 
  24 import java.io.DataInput;
  25 import java.io.DataOutputStream;
  26 import java.io.IOException;
  27 
  28 import com.sun.org.apache.bcel.internal.Const;
  29 
  30 /**
  31  * This class is derived from <em>Attribute</em> and represents a reference
  32  * to a PMG attribute.
  33  *
  34  * @version $Id$
  35  * @see     Attribute
  36  */
  37 public final class PMGClass extends Attribute {
  38 
  39     private int pmg_class_index;
  40     private int pmg_index;
  41 
  42 
  43     /**
  44      * Initialize from another object. Note that both objects use the same
  45      * references (shallow copy). Use copy() for a physical copy.
  46      */
  47     public PMGClass(final PMGClass c) {
  48         this(c.getNameIndex(), c.getLength(), c.getPMGIndex(), c.getPMGClassIndex(), c
  49                 .getConstantPool());
  50     }
  51 
  52 
  53     /**
  54      * Construct object from input stream.
  55      * @param name_index Index in constant pool to CONSTANT_Utf8
  56      * @param length Content length in bytes
  57      * @param input Input stream
  58      * @param constant_pool Array of constants
  59      * @throws IOException
  60      */
  61     PMGClass(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)
  62             throws IOException {
  63         this(name_index, length, input.readUnsignedShort(), input.readUnsignedShort(), constant_pool);
  64     }
  65 
  66 
  67     /**
  68      * @param name_index Index in constant pool to CONSTANT_Utf8
  69      * @param length Content length in bytes
  70      * @param pmg_index index in constant pool for source file name
  71      * @param pmg_class_index Index in constant pool to CONSTANT_Utf8
  72      * @param constant_pool Array of constants
  73      */
  74     public PMGClass(final int name_index, final int length, final int pmg_index, final int pmg_class_index,
  75             final ConstantPool constant_pool) {
  76         super(Const.ATTR_PMG, name_index, length, constant_pool);
  77         this.pmg_index = pmg_index;
  78         this.pmg_class_index = pmg_class_index;
  79     }
  80 
  81 
  82     /**
  83      * Called by objects that are traversing the nodes of the tree implicitely
  84      * defined by the contents of a Java class. I.e., the hierarchy of methods,
  85      * fields, attributes, etc. spawns a tree of objects.
  86      *
  87      * @param v Visitor object
  88      */
  89     @Override
  90     public void accept( final Visitor v ) {
  91         System.err.println("Visiting non-standard PMGClass object");
  92     }
  93 
  94 
  95     /**
  96      * Dump source file attribute to file stream in binary format.
  97      *
  98      * @param file Output file stream
  99      * @throws IOException
 100      */
 101     @Override
 102     public final void dump( final DataOutputStream file ) throws IOException {
 103         super.dump(file);
 104         file.writeShort(pmg_index);
 105         file.writeShort(pmg_class_index);
 106     }
 107 
 108 
 109     /**
 110      * @return Index in constant pool of source file name.
 111      */
 112     public final int getPMGClassIndex() {
 113         return pmg_class_index;
 114     }
 115 
 116 
 117     /**
 118      * @param pmg_class_index
 119      */
 120     public final void setPMGClassIndex( final int pmg_class_index ) {
 121         this.pmg_class_index = pmg_class_index;
 122     }
 123 
 124 
 125     /**
 126      * @return Index in constant pool of source file name.
 127      */
 128     public final int getPMGIndex() {
 129         return pmg_index;
 130     }
 131 
 132 
 133     /**
 134      * @param pmg_index
 135      */
 136     public final void setPMGIndex( final int pmg_index ) {
 137         this.pmg_index = pmg_index;
 138     }
 139 
 140 
 141     /**
 142      * @return PMG name.
 143      */
 144     public final String getPMGName() {
 145         final ConstantUtf8 c = (ConstantUtf8) super.getConstantPool().getConstant(pmg_index,
 146                 Const.CONSTANT_Utf8);
 147         return c.getBytes();
 148     }
 149 
 150 
 151     /**
 152      * @return PMG class name.
 153      */
 154     public final String getPMGClassName() {
 155         final ConstantUtf8 c = (ConstantUtf8) super.getConstantPool().getConstant(pmg_class_index,
 156                 Const.CONSTANT_Utf8);
 157         return c.getBytes();
 158     }
 159 
 160 
 161     /**
 162      * @return String representation
 163      */
 164     @Override
 165     public final String toString() {
 166         return "PMGClass(" + getPMGName() + ", " + getPMGClassName() + ")";
 167     }
 168 
 169 
 170     /**
 171      * @return deep copy of this attribute
 172      */
 173     @Override
 174     public Attribute copy( final ConstantPool _constant_pool ) {
 175         return (Attribute) clone();
 176     }
 177 }