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