    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one
    003 * or more contributor license agreements.  See the NOTICE file
    004 * distributed with this work for additional information
    005 * regarding copyright ownership.  The ASF licenses this file
    006 * to you under the Apache License, Version 2.0 (the
    007 * "License"); you may not use this file except in compliance
    008 * with the License.  You may obtain a copy of the License at
    009 *
    010 *   https://www.apache.org/licenses/LICENSE-2.0
    011 *
    012 * Unless required by applicable law or agreed to in writing,
    013 * software distributed under the License is distributed on an
    014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    015 * KIND, either express or implied.  See the License for the
    016 * specific language governing permissions and limitations
    017 * under the License.
    018 */
    019
    020package org.apache.bcel.classfile;
    021
    022import java.io.DataInput;
    023import java.io.DataOutputStream;
    024import java.io.IOException;
    025import java.util.Arrays;
    026
    027import org.apache.bcel.Const;
    028import org.apache.bcel.util.Args;
    029import org.apache.commons.lang3.ArrayUtils;
    030
    031/**
    032 * This class is derived from <em>Attribute</em> and records the classes and interfaces that are authorized to claim
    033 * membership in the nest hosted by the current class or interface. There may be at most one NestMembers attribute in a
    034 * ClassFile structure.
    035 *
    036 * @see Attribute
    037 */
    038public final class NestMembers extends Attribute {
    039
    040    private int[] classes;
    041
    042    /**
    043     * Constructs object from input stream.
    044     *
    045     * @param nameIndex Index in constant pool
    046     * @param length Content length in bytes
    047     * @param input Input stream
    048     * @param constantPool Array of constants
    049     * @throws IOException if an I/O error occurs.
    050     */
    051    NestMembers(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
    052        this(nameIndex, length, (int[]) null, constantPool);
    053        final int classCount = input.readUnsignedShort();
    054        classes = new int[classCount];
    055        for (int i = 0; i < classCount; i++) {
    056            classes[i] = input.readUnsignedShort();
    057        }
    058    }
    059
    060    /**
    061     * @param nameIndex Index in constant pool
    062     * @param length Content length in bytes
    063     * @param classes Table of indices in constant pool
    064     * @param constantPool Array of constants
    065     */
    066    public NestMembers(final int nameIndex, final int length, final int[] classes, final ConstantPool constantPool) {
    067        super(Const.ATTR_NEST_MEMBERS, nameIndex, length, constantPool);
    068        this.classes = ArrayUtils.nullToEmpty(classes);
    069        Args.requireU2(this.classes.length, "classes.length");
    070    }
    071
    072    /**
    073     * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a
    074     * physical copy.
    075     *
    076     * @param c Source to copy.
    077     */
    078    public NestMembers(final NestMembers c) {
    079        this(c.getNameIndex(), c.getLength(), c.getClasses(), c.getConstantPool());
    080    }
    081
    082    /**
    083     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
    084     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
    085     *
    086     * @param v Visitor object
    087     */
    088    @Override
    089    public void accept(final Visitor v) {
    090        v.visitNestMembers(this);
    091    }
    092
    093    /**
    094     * @return deep copy of this attribute
    095     */
    096    @Override
    097    public Attribute copy(final ConstantPool constantPool) {
    098        final NestMembers c = (NestMembers) clone();
    099        if (classes.length > 0) {
    100            c.classes = classes.clone();
    101        }
    102        c.setConstantPool(constantPool);
    103        return c;
    104    }
    105
    106    /**
    107     * Dump NestMembers attribute to file stream in binary format.
    108     *
    109     * @param file Output file stream
    110     * @throws IOException if an I/O error occurs.
    111     */
    112    @Override
    113    public void dump(final DataOutputStream file) throws IOException {
    114        super.dump(file);
    115        file.writeShort(classes.length);
    116        for (final int index : classes) {
    117            file.writeShort(index);
    118        }
    119    }
    120
    121    /**
    122     * @return array of indices into constant pool of class names.
    123     */
    124    public int[] getClasses() {
    125        return classes;
    126    }
    127
    128    /**
    129     * @return string array of class names
    130     */
    131    public String[] getClassNames() {
    132        final String[] names = new String[classes.length];
    133        Arrays.setAll(names, i -> Utility.pathToPackage(super.getConstantPool().getConstantString(classes[i], Const.CONSTANT_Class)));
    134        return names;
    135    }
    136
    137    /**
    138     * @return Length of classes table.
    139     */
    140    public int getNumberClasses() {
    141        return classes.length;
    142    }
    143
    144    /**
    145     * @param classes the list of class indexes Also redefines number_of_classes according to table length.
    146     */
    147    public void setClasses(final int[] classes) {
    148        this.classes = ArrayUtils.nullToEmpty(classes);
    149    }
    150
    151    /**
    152     * @return String representation, i.e., a list of classes.
    153     */
    154    @Override
    155    public String toString() {
    156        final StringBuilder buf = new StringBuilder();
    157        buf.append("NestMembers(");
    158        buf.append(classes.length);
    159        buf.append("):\n");
    160        for (final int index : classes) {
    161            final String className = super.getConstantPool().getConstantString(index, Const.CONSTANT_Class);
    162            buf.append("  ").append(Utility.compactClassName(className, false)).append("\n");
    163        }
    164        return buf.substring(0, buf.length() - 1); // remove the last newline
    165    }
    166}