Skip to content

Commit 2e5caee

Browse files
committed
java.editor.overridden package cleanup
- jdk 17 code improvements and cleanup - deprecation fixes
1 parent bf9ce11 commit 2e5caee

12 files changed

+299
-424
lines changed

java/java.editor/src/org/netbeans/modules/java/editor/overridden/AnnotationsHolder.java

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class AnnotationsHolder implements PropertyChangeListener {
4242

4343
private static final Logger LOGGER = Logger.getLogger(AnnotationsHolder.class.getName());
4444
private static final RequestProcessor WORKER = new RequestProcessor(AnnotationsHolder.class.getName(), 1, false, false);
45-
private static final Map<DataObject, AnnotationsHolder> file2Annotations = new HashMap<DataObject, AnnotationsHolder>();
45+
private static final Map<DataObject, AnnotationsHolder> file2Annotations = new HashMap<>();
4646

4747
public static synchronized AnnotationsHolder get(FileObject file) {
4848
try {
@@ -59,12 +59,11 @@ public static synchronized AnnotationsHolder get(FileObject file) {
5959
return null;
6060
}
6161

62-
file2Annotations.put(od, a = new AnnotationsHolder(od, ec));
63-
62+
a = new AnnotationsHolder(od, ec);
63+
file2Annotations.put(od, a);
6464
return a;
6565
} catch (IOException ex) {
6666
LOGGER.log(Level.INFO, null, ex);
67-
6867
return null;
6968
}
7069
}
@@ -75,19 +74,16 @@ public static synchronized AnnotationsHolder get(FileObject file) {
7574
private AnnotationsHolder(DataObject file, EditorCookie.Observable ec) {
7675
this.file = file;
7776
this.ec = ec;
78-
this.annotations = new ArrayList<IsOverriddenAnnotation>();
77+
this.annotations = new ArrayList<>();
7978

8079
ec.addPropertyChangeListener(this);
8180

82-
SwingUtilities.invokeLater(new Runnable() {
83-
public void run() {
84-
checkForReset();
85-
}
86-
});
81+
SwingUtilities.invokeLater(this::checkForReset);
8782

8883
Logger.getLogger("TIMER").log(Level.FINE, "Overridden AnnotationsHolder", new Object[] {file.getPrimaryFile(), this}); //NOI18N
8984
}
9085

86+
@Override
9187
public void propertyChange(PropertyChangeEvent evt) {
9288
if (EditorCookie.Observable.PROP_OPENED_PANES.endsWith(evt.getPropertyName()) || evt.getPropertyName() == null) {
9389
checkForReset();
@@ -111,26 +107,24 @@ private void checkForReset() {
111107
private final List<IsOverriddenAnnotation> annotations;
112108

113109
public void setNewAnnotations(final List<IsOverriddenAnnotation> as) {
114-
Runnable doAttachDetach = new Runnable() {
115-
public void run() {
116-
final List<IsOverriddenAnnotation> toRemove;
117-
final List<IsOverriddenAnnotation> toAdd;
118-
119-
synchronized (AnnotationsHolder.this) {
120-
toRemove = new ArrayList<IsOverriddenAnnotation>(annotations);
121-
toAdd = new ArrayList<IsOverriddenAnnotation>(as);
122-
123-
annotations.clear();
124-
annotations.addAll(as);
125-
}
126-
127-
for (IsOverriddenAnnotation a : toRemove) {
128-
a.detachImpl();
129-
}
110+
Runnable doAttachDetach = () -> {
111+
List<IsOverriddenAnnotation> toRemove;
112+
List<IsOverriddenAnnotation> toAdd;
113+
114+
synchronized (AnnotationsHolder.this) {
115+
toRemove = new ArrayList<>(annotations);
116+
toAdd = new ArrayList<>(as);
130117

131-
for (IsOverriddenAnnotation a : toAdd) {
132-
a.attach();
133-
}
118+
annotations.clear();
119+
annotations.addAll(as);
120+
}
121+
122+
for (IsOverriddenAnnotation a : toRemove) {
123+
a.detachImpl();
124+
}
125+
126+
for (IsOverriddenAnnotation a : toAdd) {
127+
a.attach();
134128
}
135129
};
136130

@@ -140,6 +134,6 @@ public void run() {
140134
}
141135

142136
public synchronized List<IsOverriddenAnnotation> getAnnotations() {
143-
return new ArrayList<IsOverriddenAnnotation>(annotations);
137+
return new ArrayList<>(annotations);
144138
}
145139
}

java/java.editor/src/org/netbeans/modules/java/editor/overridden/ComputeAnnotations.java

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
import com.sun.source.tree.MethodTree;
2323
import com.sun.source.tree.Tree;
2424
import java.util.Collection;
25-
import java.util.Collections;
2625
import java.util.LinkedList;
2726
import java.util.List;
2827
import java.util.Map;
2928
import java.util.Map.Entry;
29+
import java.util.Set;
3030
import java.util.concurrent.atomic.AtomicBoolean;
3131
import java.util.logging.Level;
3232
import java.util.logging.Logger;
@@ -98,7 +98,7 @@ public void run(Result result, SchedulerEvent event) {
9898
}
9999

100100
List<IsOverriddenAnnotation> computeAnnotations(CompilationInfo info, StyledDocument doc) {
101-
List<IsOverriddenAnnotation> annotations = new LinkedList<IsOverriddenAnnotation>();
101+
List<IsOverriddenAnnotation> annotations = new LinkedList<>();
102102

103103
createAnnotations(info, doc, new ComputeOverriding(cancel).process(info), false, annotations);
104104
createAnnotations(info, doc, new ComputeOverriders(cancel).process(info, null, null, false), true, annotations);
@@ -140,7 +140,7 @@ private void createAnnotations(CompilationInfo info, StyledDocument doc, Map<Ele
140140
if (kb != null)
141141
dn += NbBundle.getMessage(ComputeAnnotations.class, "LBL_shortcut_promotion", kb, choice); //NOI18N
142142
} else {
143-
StringBuffer tooltip = new StringBuffer();
143+
StringBuilder tooltip = new StringBuilder();
144144
boolean wasOverrides = false;
145145

146146
boolean newline = false;
@@ -171,22 +171,14 @@ private void createAnnotations(CompilationInfo info, StyledDocument doc, Map<Ele
171171
dn += NbBundle.getMessage(ComputeAnnotations.class, "LBL_shortcut_promotion", kb, 3); //NOI18N
172172
}
173173

174-
int[] elementNameSpan;
175-
176-
switch (t.getKind()) {
177-
case ANNOTATION_TYPE:
178-
case CLASS:
179-
case ENUM:
180-
case INTERFACE:
181-
elementNameSpan = info.getTreeUtilities().findNameSpan((ClassTree) t);
182-
break;
183-
case METHOD:
184-
elementNameSpan = info.getTreeUtilities().findNameSpan((MethodTree) t);
185-
break;
186-
default:
187-
elementNameSpan = new int[] {(int) info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), t), -1};
188-
break;
189-
}
174+
int[] elementNameSpan = switch (t.getKind()) {
175+
case ANNOTATION_TYPE, CLASS, ENUM, INTERFACE ->
176+
info.getTreeUtilities().findNameSpan((ClassTree) t);
177+
case METHOD ->
178+
info.getTreeUtilities().findNameSpan((MethodTree) t);
179+
default ->
180+
new int[]{(int) info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), t), -1};
181+
};
190182

191183
if (elementNameSpan == null) continue;
192184

@@ -220,6 +212,7 @@ public void cancel() {
220212
private static Position getPosition(final StyledDocument doc, final int offset) {
221213
class Impl implements Runnable {
222214
private Position pos;
215+
@Override
223216
public void run() {
224217
if (offset < 0 || offset >= doc.getLength())
225218
return ;
@@ -255,7 +248,7 @@ public static final class FactoryImpl extends TaskFactory {
255248

256249
@Override
257250
public Collection<? extends SchedulerTask> create(Snapshot snapshot) {
258-
return Collections.singleton(new ComputeAnnotations());
251+
return Set.of(new ComputeAnnotations());
259252
}
260253

261254
}

0 commit comments

Comments
 (0)