1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * https://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.bcel.classfile;
20
21 import java.io.DataInput;
22 import java.io.DataOutputStream;
23 import java.io.IOException;
24 import java.util.HashMap;
25 import java.util.LinkedHashMap;
26 import java.util.Map;
27 import java.util.Objects;
28
29 import org.apache.bcel.Const;
30
31 /**
32 * Extends the abstract {@link Constant} to represent a reference to a UTF-8 encoded string.
33 * <p>
34 * The following system properties govern caching this class performs.
35 * </p>
36 * <ul>
37 * <li>{@link #SYS_PROP_CACHE_MAX_ENTRIES} (since 6.4): The size of the cache, by default 0, meaning caching is
38 * disabled.</li>
39 * <li>{@link #SYS_PROP_CACHE_MAX_ENTRY_SIZE} (since 6.0): The maximum size of the values to cache, by default 200, 0
40 * disables caching. Values larger than this are <em>not</em> cached.</li>
41 * <li>{@link #SYS_PROP_STATISTICS} (since 6.0): Prints statistics on the console when the JVM exits.</li>
42 * </ul>
43 * <p>
44 * Here is a sample Maven invocation with caching disabled:
45 * </p>
46 *
47 * <pre>
48 * mvn test -Dbcel.statistics=true -Dbcel.maxcached.size=0 -Dbcel.maxcached=0
49 * </pre>
50 * <p>
51 * Here is a sample Maven invocation with caching enabled:
52 * </p>
53 *
54 * <pre>
55 * mvn test -Dbcel.statistics=true -Dbcel.maxcached.size=100000 -Dbcel.maxcached=5000000
56 * </pre>
57 *
58 * @see Constant
59 */
60 public final class ConstantUtf8 extends Constant {
61
62 private static final class Cache {
63
64 private static final boolean BCEL_STATISTICS = Boolean.getBoolean(SYS_PROP_STATISTICS);
65 private static final int MAX_ENTRIES = Integer.getInteger(SYS_PROP_CACHE_MAX_ENTRIES, 0).intValue();
66 private static final int INITIAL_CAPACITY = (int) (MAX_ENTRIES / 0.75);
67
68 private static final HashMap<String, ConstantUtf8> CACHE = new LinkedHashMap<String, ConstantUtf8>(INITIAL_CAPACITY, 0.75f, true) {
69
70 private static final long serialVersionUID = -8506975356158971766L;
71
72 @Override
73 protected boolean removeEldestEntry(final Map.Entry<String, ConstantUtf8> eldest) {
74 return size() > MAX_ENTRIES;
75 }
76 };
77
78 // Set the size to 0 or below to skip caching entirely
79 private static final int MAX_ENTRY_SIZE = Integer.getInteger(SYS_PROP_CACHE_MAX_ENTRY_SIZE, 200).intValue();
80
81 static boolean isEnabled() {
82 return MAX_ENTRIES > 0 && MAX_ENTRY_SIZE > 0;
83 }
84
85 }
86
87 // TODO these should perhaps be AtomicInt?
88 private static volatile int considered;
89 private static volatile int created;
90 private static volatile int hits;
91 private static volatile int skipped;
92
93 private static final String SYS_PROP_CACHE_MAX_ENTRIES = "bcel.maxcached";
94 private static final String SYS_PROP_CACHE_MAX_ENTRY_SIZE = "bcel.maxcached.size";
95 private static final String SYS_PROP_STATISTICS = "bcel.statistics";
96
97 static {
98 if (Cache.BCEL_STATISTICS) {
99 Runtime.getRuntime().addShutdownHook(new Thread(ConstantUtf8::printStats));
100 }
101 }
102
103 /**
104 * Clears the cache.
105 *
106 * @since 6.4.0
107 */
108 public static synchronized void clearCache() {
109 Cache.CACHE.clear();
110 }
111
112 // for access by test code
113 static synchronized void clearStats() {
114 hits = considered = skipped = created = 0;
115 }
116
117 // Avoid Spotbugs complaint about Write to static field
118 private static synchronized void countCreated() {
119 created++;
120 }
121
122 /**
123 * Gets a new or cached instance of the given value.
124 * <p>
125 * See {@link ConstantUtf8} class Javadoc for details.
126 * </p>
127 *
128 * @param value the value.
129 * @return a new or cached instance of the given value.
130 * @since 6.0
131 */
132 public static synchronized ConstantUtf8 getCachedInstance(final String value) {
133 if (value.length() > Cache.MAX_ENTRY_SIZE) {
134 skipped++;
135 return new ConstantUtf8(value);
136 }
137 considered++;
138 synchronized (ConstantUtf8.class) { // might be better with a specific lock object
139 ConstantUtf8 result = Cache.CACHE.get(value);
140 if (result != null) {
141 hits++;
142 return result;
143 }
144 result = new ConstantUtf8(value);
145 Cache.CACHE.put(value, result);
146 return result;
147 }
148 }
149
150 /**
151 * Gets a new or cached instance of the given value.
152 * <p>
153 * See {@link ConstantUtf8} class Javadoc for details.
154 * </p>
155 *
156 * @param dataInput the value.
157 * @return a new or cached instance of the given value.
158 * @throws IOException if an I/O error occurs.
159 * @since 6.0
160 */
161 public static ConstantUtf8 getInstance(final DataInput dataInput) throws IOException {
162 return getInstance(dataInput.readUTF());
163 }
164
165 /**
166 * Gets a new or cached instance of the given value.
167 * <p>
168 * See {@link ConstantUtf8} class Javadoc for details.
169 * </p>
170 *
171 * @param value the value.
172 * @return a new or cached instance of the given value.
173 * @since 6.0
174 */
175 public static ConstantUtf8 getInstance(final String value) {
176 return Cache.isEnabled() ? getCachedInstance(value) : new ConstantUtf8(value);
177 }
178
179 // for access by test code
180 static void printStats() {
181 final String prefix = "[Apache Commons BCEL]";
182 System.err.printf("%s Cache hit %,d/%,d, %d skipped.%n", prefix, hits, considered, skipped);
183 System.err.printf("%s Total of %,d ConstantUtf8 objects created.%n", prefix, created);
184 System.err.printf("%s Configuration: %s=%,d, %s=%,d.%n", prefix, SYS_PROP_CACHE_MAX_ENTRIES, Cache.MAX_ENTRIES, SYS_PROP_CACHE_MAX_ENTRY_SIZE,
185 Cache.MAX_ENTRY_SIZE);
186 }
187
188 private final String value;
189
190 /**
191 * Initializes from another object.
192 *
193 * @param constantUtf8 the value.
194 */
195 public ConstantUtf8(final ConstantUtf8 constantUtf8) {
196 this(constantUtf8.getBytes());
197 }
198
199 /**
200 * Initializes instance from file data.
201 *
202 * @param dataInput Input stream
203 * @throws IOException if an I/O error occurs.
204 */
205 ConstantUtf8(final DataInput dataInput) throws IOException {
206 super(Const.CONSTANT_Utf8);
207 value = dataInput.readUTF();
208 countCreated();
209 }
210
211 /**
212 * @param value Data
213 */
214 public ConstantUtf8(final String value) {
215 super(Const.CONSTANT_Utf8);
216 this.value = Objects.requireNonNull(value, "value");
217 countCreated();
218 }
219
220 /**
221 * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
222 * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
223 *
224 * @param v Visitor object
225 */
226 @Override
227 public void accept(final Visitor v) {
228 v.visitConstantUtf8(this);
229 }
230
231 /**
232 * Dumps String in Utf8 format to file stream.
233 *
234 * @param file Output file stream
235 * @throws IOException if an I/O error occurs.
236 */
237 @Override
238 public void dump(final DataOutputStream file) throws IOException {
239 file.writeByte(super.getTag());
240 file.writeUTF(value);
241 }
242
243 /**
244 * @return Data converted to string.
245 */
246 public String getBytes() {
247 return value;
248 }
249
250 /**
251 * @param bytes the raw bytes of this UTF-8
252 * @deprecated (since 6.0)
253 */
254 @java.lang.Deprecated
255 public void setBytes(final String bytes) {
256 throw new UnsupportedOperationException();
257 }
258
259 /**
260 * @return String representation
261 */
262 @Override
263 public String toString() {
264 return super.toString() + "(\"" + Utility.replace(value, "\n", "\\n") + "\")";
265 }
266 }