Skip to content

Commit 10921f5

Browse files
committed
Simplify JavaCodeTemplateFilter#accept
list.contains() should be more efficient compared to copying the list into EnumSet + HashSet first just to call contains on them. Simpler code which doesn't catch IAEs during regular execution which would show up in JFR logs during auto completion.
1 parent 7a33402 commit 10921f5

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

java/java.editor/src/org/netbeans/modules/editor/java/JavaCodeTemplateFilter.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import com.sun.source.tree.Tree;
2424
import java.util.ArrayList;
2525
import java.util.Collections;
26-
import java.util.EnumSet;
27-
import java.util.HashSet;
2826
import java.util.List;
2927
import java.util.concurrent.atomic.AtomicBoolean;
3028
import javax.swing.text.Document;
@@ -34,6 +32,7 @@
3432
import com.sun.source.tree.ExpressionTree;
3533
import com.sun.source.util.SourcePositions;
3634
import com.sun.source.util.TreePath;
35+
import java.util.Set;
3736
import org.netbeans.api.java.lexer.JavaTokenId;
3837
import org.netbeans.api.java.source.CompilationController;
3938
import org.netbeans.api.java.source.JavaSource.Phase;
@@ -74,7 +73,7 @@ private JavaCodeTemplateFilter(Document doc, int startOffset, int endOffset) {
7473
final AtomicBoolean cancel = new AtomicBoolean();
7574
BaseProgressUtils.runOffEventDispatchThread(() -> {
7675
try {
77-
ParserManager.parse(Collections.singleton(source), new UserTask() {
76+
ParserManager.parse(Set.of(source), new UserTask() {
7877
@Override
7978
public void run(ResultIterator resultIterator) throws Exception {
8079
if (cancel.get()) {
@@ -205,22 +204,12 @@ public synchronized boolean accept(CodeTemplate template) {
205204
if (treeKindCtx == null && stringCtx == null) {
206205
return false;
207206
}
208-
EnumSet<Tree.Kind> treeKindContexts = EnumSet.noneOf(Tree.Kind.class);
209-
HashSet stringContexts = new HashSet();
210-
getTemplateContexts(template, treeKindContexts, stringContexts);
211-
return treeKindContexts.isEmpty() && stringContexts.isEmpty() && treeKindCtx != Tree.Kind.STRING_LITERAL || treeKindContexts.contains(treeKindCtx) || stringContexts.contains(stringCtx);
212-
}
213-
214-
private void getTemplateContexts(CodeTemplate template, EnumSet<Tree.Kind> treeKindContexts, HashSet<String> stringContexts) {
215207
List<String> contexts = template.getContexts();
216-
if (contexts != null) {
217-
for(String context : contexts) {
218-
try {
219-
treeKindContexts.add(Tree.Kind.valueOf(context));
220-
} catch (IllegalArgumentException iae) {
221-
stringContexts.add(context);
222-
}
223-
}
208+
if (contexts == null || contexts.isEmpty()) {
209+
return treeKindCtx != Tree.Kind.STRING_LITERAL;
210+
} else {
211+
return (treeKindCtx != null && contexts.contains(treeKindCtx.name()))
212+
|| (stringCtx != null && contexts.contains(stringCtx));
224213
}
225214
}
226215

0 commit comments

Comments
 (0)