/
InternalMethodHint.java
134 lines (111 loc) · 4.58 KB
/
InternalMethodHint.java
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
package com.jme3.gde.codecheck.hints;
import com.jme3.system.Annotations.Internal;
import com.sun.source.tree.Tree;
import com.sun.source.tree.Tree.Kind;
import com.sun.source.util.SourcePositions;
import com.sun.source.util.TreePath;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import javax.lang.model.element.Element;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import org.netbeans.api.editor.EditorRegistry;
import org.netbeans.api.java.source.CompilationInfo;
import org.netbeans.modules.java.hints.spi.AbstractHint;
import org.netbeans.spi.editor.hints.ChangeInfo;
import org.netbeans.spi.editor.hints.EnhancedFix;
import org.netbeans.spi.editor.hints.ErrorDescription;
import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
import org.netbeans.spi.editor.hints.Fix;
import org.openide.awt.StatusDisplayer;
import org.openide.util.NbBundle;
public class InternalMethodHint extends AbstractHint {
//This hint does not enable the IDE to fix the problem:
private static final List<Fix> NO_FIXES = Collections.<Fix>emptyList();
//This hint applies to method invocations:
private static final Set<Tree.Kind> TREE_KINDS =
EnumSet.<Tree.Kind>of(Tree.Kind.METHOD_INVOCATION);
public InternalMethodHint() {
super(true, true, AbstractHint.HintSeverity.WARNING);
}
//Specify the kind of code that the hint applies to, in this case,
//the hint applies to method invocations:
@Override
public Set<Kind> getTreeKinds() {
return TREE_KINDS;
}
@Override
public List<ErrorDescription> run(CompilationInfo info, TreePath treePath) {
Tree t = treePath.getLeaf();
Element el = info.getTrees().getElement(treePath);
if (el.getAnnotation(Internal.class) != null) {
//prepare selection for removing
JTextComponent editor = EditorRegistry.lastFocusedComponent();
Document doc = editor.getDocument();
SourcePositions sp = info.getTrees().getSourcePositions();
int start = (int) sp.getStartPosition(info.getCompilationUnit(), t);
int end = (int) sp.getEndPosition(info.getCompilationUnit(), t);
String bodyText = info.getText().substring(start, end);
//prepare fix
List<Fix> fixes = new ArrayList<Fix>();
fixes.add(new MessagesFix(doc, start, bodyText));
return Collections.<ErrorDescription>singletonList(
ErrorDescriptionFactory.createErrorDescription(
getSeverity().toEditorSeverity(),
getDisplayName(),
fixes,
info.getFileObject(),
(int) info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), t),
(int) info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), t)));
}
return null;
}
//This is called if/when the hint processing is cancelled:
@Override
public void cancel() {
}
//Message that the user sees in the left sidebar:
@Override
public String getDisplayName() {
return NbBundle.getMessage(InternalMethodHint.class, "InternalMethodHint.display-name");
}
//Name of the hint in the Options window:
@Override
public String getId() {
return NbBundle.getMessage(InternalMethodHint.class, "InternalMethodHint.id");
}
//Description of the hint in the Options window:
@Override
public String getDescription() {
return NbBundle.getMessage(InternalMethodHint.class, "InternalMethodHint.description");
}
class MessagesFix implements EnhancedFix {
Document doc = null;
int start = 0;
String bodyText = null;
public MessagesFix(Document doc, int start, String bodyText) {
this.doc = doc;
this.start = start;
this.bodyText = bodyText;
}
@Override
public CharSequence getSortText() {
return "charsequence";
}
@Override
public String getText() {
return NbBundle.getMessage(InternalMethodHint.class, "InternalMethodHint.fix-text");
}
@Override
public ChangeInfo implement() throws Exception {
//Add 1 character, for the semi-colon:
doc.remove(start, bodyText.length() + 1);
//Display message to user in status bar:
StatusDisplayer.getDefault().setStatusText("Removed: " + bodyText);
return null;
}
}
}