Source code

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