Debian Codegraph @

src/com/bulenkov/iconloader/util/StringFactory.java

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * Copyright 2000-2014 JetBrains s.r.o.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.bulenkov.iconloader.util;
18
19 import sun.reflect.ConstructorAccessor;
20
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.Method;
23
24 /**
25 * @author Konstantin Bulenkov
26 */
27 public class StringFactory {
28 // String(char[], boolean). Works since JDK1.7, earlier JDKs have too slow reflection anyway
29 private static final ConstructorAccessor ourConstructorAccessor;
30
31 static {
32 ConstructorAccessor constructorAccessor = null;
33 try {
34 Constructor<String> newC = String.class.getDeclaredConstructor(char[].class, boolean.class);
35 newC.setAccessible(true);
36 // it is faster to invoke constructor via sun.reflect.ConstructorAccessor; it avoids AccessibleObject.checkAccess()
37 Method accessor = Constructor.class.getDeclaredMethod("acquireConstructorAccessor");
38 accessor.setAccessible(true);
39 constructorAccessor = (ConstructorAccessor) accessor.invoke(newC);
40 } catch (Exception ignored) {
41 }
42 ourConstructorAccessor = constructorAccessor;
43 }
44
45
46 /**
47 * @return new instance of String which backed by 'chars' array.
48 * <p/>
49 * CAUTION. EXTREMELY DANGEROUS.
50 * DO NOT USE THIS METHOD UNLESS YOU ARE TOO DESPERATE
51 */
52 public static String createShared(char[] chars) {
53 if (ourConstructorAccessor != null) {
54 try {
55 return (String) ourConstructorAccessor.newInstance(new Object[]{chars, Boolean.TRUE});
56 } catch (Exception e) {
57 throw new RuntimeException(e);
58 }
59 }
60 return new String(chars);
61 }
62 }
63