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