1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package org.spf4j.base;
33
34 import com.google.common.escape.Escaper;
35 import com.google.common.html.HtmlEscapers;
36 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
37 import gnu.trove.map.hash.THashMap;
38 import java.io.IOException;
39 import java.io.Writer;
40 import java.util.Map;
41 import javax.annotation.concurrent.Immutable;
42 import org.spf4j.base.avro.Converters;
43 import org.spf4j.base.avro.Method;
44
45
46
47
48 @Immutable
49
50 @SuppressFBWarnings("JCIP_FIELD_ISNT_FINAL_IN_IMMUTABLE_CLASS")
51 public final class Methods {
52
53 public static final Method ROOT = Converters.ROOT;
54
55 private static final Map<String, Map<String, Method>> INSTANCE_REPO = new THashMap<>(1024);
56
57
58 private Methods() { }
59
60 public static void writeHtml(final Method m, final Writer w) throws IOException {
61 Escaper htmlEscaper = HtmlEscapers.htmlEscaper();
62 w.append(htmlEscaper.escape(m.getName())).append(htmlEscaper.escape("@")).
63 append(htmlEscaper.escape(m.getDeclaringClass()));
64 }
65
66 public static Method getMethod(final StackTraceElement elem) {
67 return getMethod(elem.getClassName(), elem.getMethodName());
68 }
69
70
71
72
73
74 @SuppressFBWarnings("PMB_POSSIBLE_MEMORY")
75 public static synchronized Method getMethod(final String className, final String methodName) {
76 Map<String, Method> mtom = INSTANCE_REPO.get(className);
77 Method result;
78 if (mtom == null) {
79 mtom = new THashMap<>(5);
80 result = new Method(className, methodName);
81 mtom.put(methodName, result);
82 INSTANCE_REPO.put(className, mtom);
83 } else {
84 result = mtom.get(methodName);
85 if (result == null) {
86 result = new Method(className, methodName);
87 mtom.put(methodName, result);
88 }
89 }
90 return result;
91 }
92
93 public static void writeTo(final Method m, final Appendable w) throws IOException {
94 w.append(m.getName()).append('@').append(m.getDeclaringClass());
95 }
96
97 public static void writeTo(final Method m, final StringBuilder w) {
98 w.append(m.getName()).append('@').append(m.getDeclaringClass());
99 }
100
101 public static CharSequence toCharSequence(final Method m) {
102 StringBuilder sb = new StringBuilder(32);
103 writeTo(m, sb);
104 return sb;
105 }
106
107 public static String toString(final Method m) {
108 return toCharSequence(m).toString();
109 }
110
111 public static Method from(final CharSequence cs) {
112 return from(cs, 0, cs.length());
113 }
114
115 public static Method from(final CharSequence cs, final int start, final int end) {
116 int idx = CharSequences.indexOf(cs, start, end, '@');
117 if (idx < 0) {
118 throw new IllegalArgumentException("Invalid method representation: " + cs);
119 }
120 return getMethod(cs.subSequence(idx + 1, end).toString(), cs.subSequence(start, idx).toString());
121 }
122
123 }