Repository navigation
Expand file tree
/
Copy pathMisc.hint
More file actions
181 lines (145 loc) · 4.14 KB
/
Copy pathMisc.hint
File metadata and controls
181 lines (145 loc) · 4.14 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// faster and shorter
$enum.class.getEnumConstants() :: $enum instanceof Enum =>
$enum.values()
;;
"Remove unessesary null check before instanceof.":
$a != null && $a instanceof $A =>
$a instanceof $A
;;
// shorter null check
if ($o == null) {
throw new NullPointerException($msg$);
}
=>
java.util.Objects.requireNonNull($o, $msg$);
;;
// TODO: doesn't work, jackpot ignores the body. bug?
/*
if ($o != null) {
$body$;
} else {
throw new NullPointerException($msg$);
}
=>
java.util.Objects.requireNonNull($o, $msg$);
$body$;
;;
*/
// shorter equals
$obj != null ? $obj.equals($other) : $other == null =>
java.util.Objects.equals($obj, $other)
;;
$obj != null ? !$obj.equals($other) : $other != null =>
!java.util.Objects.equals($obj, $other)
;;
($obj == $other) || ($obj != null && $obj.equals($other)) =>
java.util.Objects.equals($obj, $other)
;;
($obj != $other) || ($obj != null && !$obj.equals($other)) =>
!java.util.Objects.equals($obj, $other)
;;
// 10+
// does what it says
$o.get() :: $o instanceof java.util.Optional && sourceVersionGE(10) =>
$o.orElseThrow()
;;
// 11+
!$o.isPresent() :: $o instanceof java.util.Optional && sourceVersionGE(11) =>
$o.isEmpty()
;;
!$o.isEmpty() :: $o instanceof java.util.Optional =>
$o.isPresent()
;;
// 11+
// similar performance, strip is unicode aware, trim is not.
$str.trim() :: $str instanceof java.lang.String && sourceVersionGE(11) =>
$str.strip()
;;
// currently compiles into invokedynamic / makeConcatWithConstants
// valueOf is more direct, needs no warmup and has specialized variants.
// javac RFE https://bugs.openjdk.java.net/browse/JDK-8276951
"" + $val =>
String.valueOf($val)
;;
$val + "" =>
String.valueOf($val)
;;
$str.substring($begin, $end).indexOf($other) + $begin :: $str instanceof String && $begin instanceof int && $end instanceof int && sourceVersionGE(21) =>
$str.indexOf($other, $begin, $end)
;;
"can use formatted":
String.format($str, $args$) :: $str instanceof String && sourceVersionGE(15) =>
$str.formatted($args$)
;;
"can remove redundant toString()":
$sb1.append($sb2.toString()) :: $sb1 instanceof StringBuilder && $sb2 instanceof StringBuilder =>
$sb1.append($sb2)
;;
/* TODO: doesn't work unless there is a way to create constants
"extract Pattern constant.":
$str.matches($regex) :: $str instanceof java.lang.String && $regex instanceof java.lang.String =>
final java.util.regex.Pattern pattern = java.util.regex.Pattern.compile($regex);
pattern.matcher($str).matches()
;;
*/
// shorter toString()
$o != null ? $o.toString() : $default =>
java.util.Objects.toString($o, $default)
;;
$o == null ? $default : $o.toString() =>
java.util.Objects.toString($o, $default)
;;
System.getProperty("line.separator") =>
System.lineSeparator()
;;
System.getProperty("file.separator") =>
java.io.File.separator
;;
System.getProperty("path.separator") =>
java.io.File.pathSeparator
;;
System.getProperty("java.runtime.version") :: sourceVersionGE(9) =>
Runtime.version().toString()
;;
System.getProperty("java.specification.version") :: sourceVersionGE(10) =>
Runtime.version().feature()
;;
// no exact match unfortunately since it contains optional values
System.getProperty("java.version") :: sourceVersionGE(9) =>
Runtime.version().toString()
;;
System.getProperty("java.class.version") :: sourceVersionGE(20) =>
java.lang.reflect.ClassFileFormatVersion.latest().major()
;;
"URL.equals() resolves the host and compares the target, this is a blocking operation. Consider comparing URIs or Strings instead.":
$url1.equals($url2) :: $url1 instanceof java.net.URL && $url2 instanceof java.net.URL;;
java.util.StringJoiner $joiner = new java.util.StringJoiner($delim);
for (String $s : $iter) {
$joiner.add($s);
}
String $joined = $joiner.toString();
=>
String $joined = String.join($delim, $iter);
;;
/* todo: should be a hint but no warning
"use Long::hashCode for better hash code distribution.":
$anno$ public int hashCode() {
return (int) $id;
} :: $id instanceof long && elementKindMatches($id, ElementKind.FIELD)
=>
$anno$ public int hashCode() {
return Long.hashCode($id);
}
;;
*/
// code style
/*
for (int $i = $s; $i < $n; ++$i) {
$body$;
}
=>
for (int $i = $s; $i < $n; $i++) {
$body$;
}
;;
*/