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., 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 name_index Index in constant pool 064 * @param length Content length in bytes 065 * @param bytes Attribute contents 066 * @param constant_pool Array of constants 067 */ 068 public Unknown(final int name_index, final int length, final byte[] bytes, final ConstantPool constant_pool) { 069 super(Const.ATTR_UNKNOWN, name_index, length, constant_pool); 070 this.bytes = bytes; 071 name = constant_pool.getConstantUtf8(name_index).getBytes(); 072 UNKNOWN_ATTRIBUTES.put(name, this); 073 } 074 075 /** 076 * Construct object from input stream. 077 * 078 * @param name_index Index in constant pool 079 * @param length Content length in bytes 080 * @param input Input stream 081 * @param constant_pool Array of constants 082 * @throws IOException if an I/O error occurs. 083 */ 084 Unknown(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool) throws IOException { 085 this(name_index, length, (byte[]) null, constant_pool); 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 = new byte[bytes.length]; 119 System.arraycopy(bytes, 0, c.bytes, 0, bytes.length); 120 } 121 c.setConstantPool(constantPool); 122 return c; 123 } 124 125 /** 126 * Dump unknown bytes to file stream. 127 * 128 * @param file Output file stream 129 * @throws IOException if an I/O error occurs. 130 */ 131 @Override 132 public void dump(final DataOutputStream file) throws IOException { 133 super.dump(file); 134 if (super.getLength() > 0) { 135 file.write(bytes, 0, super.getLength()); 136 } 137 } 138 139 /** 140 * @return data bytes. 141 */ 142 public byte[] getBytes() { 143 return bytes; 144 } 145 146 /** 147 * @return name of attribute. 148 */ 149 @Override 150 public String getName() { 151 return name; 152 } 153 154 /** 155 * @param bytes the bytes to set 156 */ 157 public void setBytes(final byte[] bytes) { 158 this.bytes = bytes; 159 } 160 161 /** 162 * @return String representation. 163 */ 164 @Override 165 public String toString() { 166 if (super.getLength() == 0 || bytes == null) { 167 return "(Unknown attribute " + name + ")"; 168 } 169 String hex; 170 if (super.getLength() > 10) { 171 final byte[] tmp = new byte[10]; 172 System.arraycopy(bytes, 0, tmp, 0, 10); 173 hex = Utility.toHexString(tmp) + "... (truncated)"; 174 } else { 175 hex = Utility.toHexString(bytes); 176 } 177 return "(Unknown attribute " + name + ": " + hex + ")"; 178 } 179}