jaroslav@716
|
1 |
/**
|
jaroslav@716
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@716
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@716
|
4 |
*
|
jaroslav@716
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@716
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@716
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@716
|
8 |
*
|
jaroslav@716
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@716
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@716
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@716
|
12 |
* GNU General Public License for more details.
|
jaroslav@716
|
13 |
*
|
jaroslav@716
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@716
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@716
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@716
|
17 |
*/
|
jaroslav@716
|
18 |
package org.apidesign.bck2brwsr.ide.editor;
|
jaroslav@716
|
19 |
|
jaroslav@716
|
20 |
import com.sun.source.tree.AnnotationTree;
|
jaroslav@716
|
21 |
import com.sun.source.tree.ExpressionTree;
|
jaroslav@716
|
22 |
import com.sun.source.tree.LiteralTree;
|
jaroslav@716
|
23 |
import com.sun.source.tree.MethodTree;
|
jaroslav@716
|
24 |
import com.sun.source.tree.Tree.Kind;
|
jaroslav@716
|
25 |
import com.sun.source.tree.VariableTree;
|
jaroslav@716
|
26 |
import com.sun.source.util.TreePath;
|
jaroslav@716
|
27 |
import java.util.ArrayList;
|
jaroslav@716
|
28 |
import java.util.Arrays;
|
jaroslav@716
|
29 |
import java.util.Collections;
|
jaroslav@716
|
30 |
import java.util.List;
|
jaroslav@716
|
31 |
import java.util.regex.Matcher;
|
jaroslav@716
|
32 |
import java.util.regex.Pattern;
|
jaroslav@716
|
33 |
import org.netbeans.api.java.lexer.JavaTokenId;
|
jaroslav@716
|
34 |
import static org.netbeans.api.java.lexer.JavaTokenId.BLOCK_COMMENT;
|
jaroslav@716
|
35 |
import static org.netbeans.api.java.lexer.JavaTokenId.JAVADOC_COMMENT;
|
jaroslav@716
|
36 |
import static org.netbeans.api.java.lexer.JavaTokenId.LINE_COMMENT;
|
jaroslav@716
|
37 |
import static org.netbeans.api.java.lexer.JavaTokenId.WHITESPACE;
|
jaroslav@716
|
38 |
import org.netbeans.api.java.source.CompilationInfo;
|
jaroslav@716
|
39 |
import org.netbeans.api.java.source.TreeMaker;
|
jaroslav@716
|
40 |
import org.netbeans.api.lexer.Token;
|
jaroslav@716
|
41 |
import org.netbeans.api.lexer.TokenSequence;
|
jaroslav@716
|
42 |
import org.netbeans.spi.editor.hints.ErrorDescription;
|
jaroslav@716
|
43 |
import org.netbeans.spi.editor.hints.Fix;
|
jaroslav@716
|
44 |
import org.netbeans.spi.java.hints.ErrorDescriptionFactory;
|
jaroslav@716
|
45 |
import org.netbeans.spi.java.hints.Hint;
|
jaroslav@716
|
46 |
import org.netbeans.spi.java.hints.HintContext;
|
jaroslav@716
|
47 |
import org.netbeans.spi.java.hints.JavaFix;
|
jaroslav@716
|
48 |
import org.netbeans.spi.java.hints.TriggerTreeKind;
|
jaroslav@716
|
49 |
import org.openide.util.NbBundle.Messages;
|
jaroslav@716
|
50 |
|
jaroslav@716
|
51 |
@Hint(displayName = "#DN_JSNI2JavaScriptBody", description = "#DESC_JSNI2JavaScriptBody", category = "general")
|
jaroslav@716
|
52 |
@Messages({
|
jaroslav@716
|
53 |
"DN_JSNI2JavaScriptBody=JSNI to @JavaScriptBody",
|
jaroslav@716
|
54 |
"DESC_JSNI2JavaScriptBody=JSNI to @JavaScriptBody"
|
jaroslav@716
|
55 |
})
|
jaroslav@716
|
56 |
public class JSNI2JavaScriptBody {
|
jaroslav@716
|
57 |
|
jaroslav@716
|
58 |
private static final Pattern JSNIPattern = Pattern.compile("/\\*-\\{(.*)}-\\*/");
|
jaroslav@716
|
59 |
|
jaroslav@716
|
60 |
@TriggerTreeKind(Kind.METHOD)
|
jaroslav@716
|
61 |
@Messages("ERR_JSNI2JavaScriptBody=Can convert JSNI to @JavaScriptBody")
|
jaroslav@716
|
62 |
public static ErrorDescription computeWarning(final HintContext ctx) {
|
jaroslav@716
|
63 |
Token<JavaTokenId> token = findBlockToken(ctx.getInfo(), ctx.getPath(), ctx);
|
jaroslav@716
|
64 |
|
jaroslav@716
|
65 |
if (token == null) {
|
jaroslav@716
|
66 |
return null;
|
jaroslav@716
|
67 |
}
|
jaroslav@716
|
68 |
|
jaroslav@716
|
69 |
Fix fix = new FixImpl(ctx.getInfo(), ctx.getPath()).toEditorFix();
|
jaroslav@716
|
70 |
return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_JSNI2JavaScriptBody(), fix);
|
jaroslav@716
|
71 |
}
|
jaroslav@716
|
72 |
|
jaroslav@716
|
73 |
private static Token<JavaTokenId> findBlockToken(CompilationInfo info, TreePath path, HintContext ctx) {
|
jaroslav@716
|
74 |
int end = (int) info.getTrees().getSourcePositions().getEndPosition(path.getCompilationUnit(), path.getLeaf());
|
jaroslav@716
|
75 |
TokenSequence<JavaTokenId> ts = info.getTokenHierarchy().tokenSequence(JavaTokenId.language());
|
jaroslav@716
|
76 |
|
jaroslav@716
|
77 |
if (ts == null) return null;
|
jaroslav@716
|
78 |
|
jaroslav@716
|
79 |
ts.move(end);
|
jaroslav@716
|
80 |
|
jaroslav@716
|
81 |
if ((ctx != null && ctx.isCanceled()) || !ts.movePrevious() || ts.token().id() != JavaTokenId.SEMICOLON) return null;
|
jaroslav@716
|
82 |
|
jaroslav@716
|
83 |
OUTER: while (ts.movePrevious()) {
|
jaroslav@716
|
84 |
if (ctx != null && ctx.isCanceled()) return null;
|
jaroslav@716
|
85 |
|
jaroslav@716
|
86 |
switch (ts.token().id()) {
|
jaroslav@716
|
87 |
case WHITESPACE: break;
|
jaroslav@716
|
88 |
case LINE_COMMENT: break;
|
jaroslav@716
|
89 |
case JAVADOC_COMMENT: break;
|
jaroslav@716
|
90 |
case BLOCK_COMMENT:
|
jaroslav@716
|
91 |
if (JSNIPattern.matcher(ts.token().text()).matches()) {
|
jaroslav@716
|
92 |
return ts.offsetToken();
|
jaroslav@716
|
93 |
}
|
jaroslav@716
|
94 |
break;
|
jaroslav@716
|
95 |
default:
|
jaroslav@716
|
96 |
break OUTER;
|
jaroslav@716
|
97 |
}
|
jaroslav@716
|
98 |
}
|
jaroslav@716
|
99 |
|
jaroslav@716
|
100 |
return null;
|
jaroslav@716
|
101 |
}
|
jaroslav@716
|
102 |
|
jaroslav@716
|
103 |
private static final class FixImpl extends JavaFix {
|
jaroslav@716
|
104 |
|
jaroslav@716
|
105 |
public FixImpl(CompilationInfo info, TreePath tp) {
|
jaroslav@716
|
106 |
super(info, tp);
|
jaroslav@716
|
107 |
}
|
jaroslav@716
|
108 |
|
jaroslav@716
|
109 |
@Override
|
jaroslav@716
|
110 |
@Messages("FIX_JSNI2JavaScriptBody=Convert JSNI to @JavaScriptBody")
|
jaroslav@716
|
111 |
protected String getText() {
|
jaroslav@716
|
112 |
return Bundle.FIX_JSNI2JavaScriptBody();
|
jaroslav@716
|
113 |
}
|
jaroslav@716
|
114 |
|
jaroslav@716
|
115 |
@Override
|
jaroslav@716
|
116 |
protected void performRewrite(TransformationContext ctx) {
|
jaroslav@716
|
117 |
Token<JavaTokenId> jsniComment = findBlockToken(ctx.getWorkingCopy(), ctx.getPath(), null);
|
jaroslav@716
|
118 |
|
jaroslav@716
|
119 |
if (jsniComment == null) {
|
jaroslav@716
|
120 |
//XXX: warn?
|
jaroslav@716
|
121 |
return ;
|
jaroslav@716
|
122 |
}
|
jaroslav@716
|
123 |
|
jaroslav@716
|
124 |
Matcher m = JSNIPattern.matcher(jsniComment.text());
|
jaroslav@716
|
125 |
|
jaroslav@716
|
126 |
if (!m.matches()) return ; //XXX: warn?
|
jaroslav@716
|
127 |
|
jaroslav@716
|
128 |
TreeMaker make = ctx.getWorkingCopy().getTreeMaker();
|
jaroslav@716
|
129 |
MethodTree mt = (MethodTree) ctx.getPath().getLeaf();
|
jaroslav@716
|
130 |
List<LiteralTree> params = new ArrayList<LiteralTree>();
|
jaroslav@716
|
131 |
|
jaroslav@716
|
132 |
params.add(make.Literal("this"));
|
jaroslav@716
|
133 |
|
jaroslav@716
|
134 |
for (VariableTree p : mt.getParameters()) {
|
jaroslav@716
|
135 |
params.add(make.Literal(p.getName().toString()));
|
jaroslav@716
|
136 |
}
|
jaroslav@716
|
137 |
|
jaroslav@716
|
138 |
AnnotationTree jsBody = make.Annotation(make.QualIdent("org.apidesign.bck2brwsr.core.JavaScriptBody"),
|
jaroslav@716
|
139 |
Arrays.<ExpressionTree>asList(
|
jaroslav@716
|
140 |
make.Assignment(make.Identifier("body"), make.Literal(m.group(1))),
|
jaroslav@716
|
141 |
make.Assignment(make.Identifier("args"), make.NewArray(null, Collections.<ExpressionTree>emptyList(), params))
|
jaroslav@716
|
142 |
)
|
jaroslav@716
|
143 |
);
|
jaroslav@716
|
144 |
|
jaroslav@716
|
145 |
|
jaroslav@716
|
146 |
ctx.getWorkingCopy().rewrite(mt.getModifiers(), make.addModifiersAnnotation(mt.getModifiers(), jsBody));
|
jaroslav@716
|
147 |
}
|
jaroslav@716
|
148 |
}
|
jaroslav@716
|
149 |
}
|