001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.bcel.classfile;
020
021import java.io.DataInput;
022import java.io.DataOutputStream;
023import java.io.IOException;
024import java.util.HashMap;
025import java.util.LinkedHashMap;
026import java.util.Map;
027import java.util.Objects;
028
029import org.apache.bcel.Const;
030
031/**
032 * Extends the abstract {@link Constant} to represent a reference to a UTF-8 encoded string.
033 * <p>
034 * The following system properties govern caching this class performs.
035 * </p>
036 * <ul>
037 * <li>{@link #SYS_PROP_CACHE_MAX_ENTRIES} (since 6.4): The size of the cache, by default 0, meaning caching is
038 * disabled.</li>
039 * <li>{@link #SYS_PROP_CACHE_MAX_ENTRY_SIZE} (since 6.0): The maximum size of the values to cache, by default 200, 0
040 * disables caching. Values larger than this are <em>not</em> cached.</li>
041 * <li>{@link #SYS_PROP_STATISTICS} (since 6.0): Prints statistics on the console when the JVM exits.</li>
042 * </ul>
043 * <p>
044 * Here is a sample Maven invocation with caching disabled:
045 * </p>
046 *
047 * <pre>
048 * mvn test -Dbcel.statistics=true -Dbcel.maxcached.size=0 -Dbcel.maxcached=0
049 * </pre>
050 * <p>
051 * Here is a sample Maven invocation with caching enabled:
052 * </p>
053 *
054 * <pre>
055 * mvn test -Dbcel.statistics=true -Dbcel.maxcached.size=100000 -Dbcel.maxcached=5000000
056 * </pre>
057 *
058 * @see Constant
059 */
060public final class ConstantUtf8 extends Constant {
061
062    private static final class Cache {
063
064        private static final boolean BCEL_STATISTICS = Boolean.getBoolean(SYS_PROP_STATISTICS);
065        private static final int MAX_ENTRIES = Integer.getInteger(SYS_PROP_CACHE_MAX_ENTRIES, 0).intValue();
066        private static final int INITIAL_CAPACITY = (int) (MAX_ENTRIES / 0.75);
067
068        private static final HashMap<String, ConstantUtf8> CACHE = new LinkedHashMap<String, ConstantUtf8>(INITIAL_CAPACITY, 0.75f, true) {
069
070            private static final long serialVersionUID = -8506975356158971766L;
071
072            @Override
073            protected boolean removeEldestEntry(final Map.Entry<String, ConstantUtf8> eldest) {
074                return size() > MAX_ENTRIES;
075            }
076        };
077
078        // Set the size to 0 or below to skip caching entirely
079        private static final int MAX_ENTRY_SIZE = Integer.getInteger(SYS_PROP_CACHE_MAX_ENTRY_SIZE, 200).intValue();
080
081        static boolean isEnabled() {
082            return MAX_ENTRIES > 0 && MAX_ENTRY_SIZE > 0;
083        }
084
085    }
086
087    // TODO these should perhaps be AtomicInt?
088    private static volatile int considered;
089    private static volatile int created;
090    private static volatile int hits;
091    private static volatile int skipped;
092
093    private static final String SYS_PROP_CACHE_MAX_ENTRIES = "bcel.maxcached";
094    private static final String SYS_PROP_CACHE_MAX_ENTRY_SIZE = "bcel.maxcached.size";
095    private static final String SYS_PROP_STATISTICS = "bcel.statistics";
096
097    static {
098        if (Cache.BCEL_STATISTICS) {
099            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     * Constructs a ConstantUtf8.
213     *
214     * @param value Data.
215     */
216    public ConstantUtf8(final String value) {
217        super(Const.CONSTANT_Utf8);
218        this.value = Objects.requireNonNull(value, "value");
219        countCreated();
220    }
221
222    /**
223     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
224     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
225     *
226     * @param v Visitor object.
227     */
228    @Override
229    public void accept(final Visitor v) {
230        v.visitConstantUtf8(this);
231    }
232
233    /**
234     * Dumps String in Utf8 format to file stream.
235     *
236     * @param file Output file stream.
237     * @throws IOException if an I/O error occurs.
238     */
239    @Override
240    public void dump(final DataOutputStream file) throws IOException {
241        file.writeByte(super.getTag());
242        file.writeUTF(value);
243    }
244
245    /**
246     * Gets the data converted to string.
247     *
248     * @return Data converted to string.
249     */
250    public String getBytes() {
251        return value;
252    }
253
254    /**
255     * @param bytes the raw bytes of this UTF-8.
256     * @deprecated (since 6.0)
257     */
258    @java.lang.Deprecated
259    public void setBytes(final String bytes) {
260        throw new UnsupportedOperationException();
261    }
262
263    /**
264     * @return String representation.
265     */
266    @Override
267    public String toString() {
268        return super.toString() + "(\"" + Utility.replace(value, "\n", "\\n") + "\")";
269    }
270}