    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.HashMap;
    024import java.util.Map;
    025
    026import org.apache.bcel.Const;
    027
    028/**
    029 * This class represents a reference to an unknown (i.e.,
    030 * application-specific) attribute of a class.  It is instantiated from the
    031 * {@link Attribute#readAttribute(java.io.DataInput, ConstantPool)} method.
    032 * Applications that need to read in application-specific attributes should create an
    033 * {@link UnknownAttributeReader} implementation and attach it via
    034 * {@link Attribute#addAttributeReader(String, UnknownAttributeReader)}.
    035
    036 *
    037 * @version $Id: Unknown.java 1806200 2017-08-25 16:33:06Z ggregory $
    038 * @see Attribute
    039 * @see UnknownAttributeReader
    040 */
    041public final class Unknown extends Attribute {
    042
    043    private byte[] bytes;
    044    private final String name;
    045    private static final Map<String, Unknown> unknown_attributes = new HashMap<>();
    046
    047
    048    /** @return array of unknown attributes, but just one for each kind.
    049     */
    050    static Unknown[] getUnknownAttributes() {
    051        final Unknown[] unknowns = new Unknown[unknown_attributes.size()];
    052        unknown_attributes.values().toArray(unknowns);
    053        unknown_attributes.clear();
    054        return unknowns;
    055    }
    056
    057
    058    /**
    059     * Initialize from another object. Note that both objects use the same
    060     * references (shallow copy). Use clone() for a physical copy.
    061     */
    062    public Unknown(final Unknown c) {
    063        this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool());
    064    }
    065
    066
    067    /**
    068     * Create a non-standard attribute.
    069     *
    070     * @param name_index Index in constant pool
    071     * @param length Content length in bytes
    072     * @param bytes Attribute contents
    073     * @param constant_pool Array of constants
    074     */
    075    public Unknown(final int name_index, final int length, final byte[] bytes, final ConstantPool constant_pool) {
    076        super(Const.ATTR_UNKNOWN, name_index, length, constant_pool);
    077        this.bytes = bytes;
    078        name = ((ConstantUtf8) constant_pool.getConstant(name_index, Const.CONSTANT_Utf8))
    079                .getBytes();
    080        unknown_attributes.put(name, this);
    081    }
    082
    083
    084    /**
    085     * Construct object from input stream.
    086     *
    087     * @param name_index Index in constant pool
    088     * @param length Content length in bytes
    089     * @param input Input stream
    090     * @param constant_pool Array of constants
    091     * @throws IOException
    092     */
    093    Unknown(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)
    094            throws IOException {
    095        this(name_index, length, (byte[]) null, constant_pool);
    096        if (length > 0) {
    097            bytes = new byte[length];
    098            input.readFully(bytes);
    099        }
    100    }
    101
    102
    103    /**
    104     * Called by objects that are traversing the nodes of the tree implicitely
    105     * defined by the contents of a Java class. I.e., the hierarchy of methods,
    106     * fields, attributes, etc. spawns a tree of objects.
    107     *
    108     * @param v Visitor object
    109     */
    110    @Override
    111    public void accept( final Visitor v ) {
    112        v.visitUnknown(this);
    113    }
    114
    115
    116    /**
    117     * Dump unknown bytes to file stream.
    118     *
    119     * @param file Output file stream
    120     * @throws IOException
    121     */
    122    @Override
    123    public final void dump( final DataOutputStream file ) throws IOException {
    124        super.dump(file);
    125        if (super.getLength() > 0) {
    126            file.write(bytes, 0, super.getLength());
    127        }
    128    }
    129
    130
    131    /**
    132     * @return data bytes.
    133     */
    134    public final byte[] getBytes() {
    135        return bytes;
    136    }
    137
    138
    139    /**
    140     * @return name of attribute.
    141     */
    142    @Override
    143    public final String getName() {
    144        return name;
    145    }
    146
    147
    148    /**
    149     * @param bytes the bytes to set
    150     */
    151    public final void setBytes( final byte[] bytes ) {
    152        this.bytes = bytes;
    153    }
    154
    155
    156    /**
    157     * @return String representation.
    158     */
    159    @Override
    160    public final String toString() {
    161        if (super.getLength() == 0 || bytes == null) {
    162            return "(Unknown attribute " + name + ")";
    163        }
    164        String hex;
    165        if (super.getLength() > 10) {
    166            final byte[] tmp = new byte[10];
    167            System.arraycopy(bytes, 0, tmp, 0, 10);
    168            hex = Utility.toHexString(tmp) + "... (truncated)";
    169        } else {
    170            hex = Utility.toHexString(bytes);
    171        }
    172        return "(Unknown attribute " + name + ": " + hex + ")";
    173    }
    174
    175
    176    /**
    177     * @return deep copy of this attribute
    178     */
    179    @Override
    180    public Attribute copy( final ConstantPool _constant_pool ) {
    181        final Unknown c = (Unknown) clone();
    182        if (bytes != null) {
    183            c.bytes = new byte[bytes.length];
    184            System.arraycopy(bytes, 0, c.bytes, 0, bytes.length);
    185        }
    186        c.setConstantPool(_constant_pool);
    187        return c;
    188    }
    189}