ide/editor/src/main/java/org/apidesign/bck2brwsr/ide/editor/JSNI2JavaScriptBody.java
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.ide.editor;
20 import com.sun.source.tree.AnnotationTree;
21 import com.sun.source.tree.ExpressionTree;
22 import com.sun.source.tree.LiteralTree;
23 import com.sun.source.tree.MethodTree;
24 import com.sun.source.tree.Tree.Kind;
25 import com.sun.source.tree.VariableTree;
26 import com.sun.source.util.TreePath;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.List;
31 import org.netbeans.api.java.lexer.JavaTokenId;
32 import static org.netbeans.api.java.lexer.JavaTokenId.BLOCK_COMMENT;
33 import static org.netbeans.api.java.lexer.JavaTokenId.JAVADOC_COMMENT;
34 import static org.netbeans.api.java.lexer.JavaTokenId.LINE_COMMENT;
35 import static org.netbeans.api.java.lexer.JavaTokenId.WHITESPACE;
36 import org.netbeans.api.java.source.CompilationInfo;
37 import org.netbeans.api.java.source.TreeMaker;
38 import org.netbeans.api.lexer.Token;
39 import org.netbeans.api.lexer.TokenSequence;
40 import org.netbeans.spi.editor.hints.ErrorDescription;
41 import org.netbeans.spi.editor.hints.Fix;
42 import org.netbeans.spi.java.hints.ErrorDescriptionFactory;
43 import org.netbeans.spi.java.hints.Hint;
44 import org.netbeans.spi.java.hints.HintContext;
45 import org.netbeans.spi.java.hints.JavaFix;
46 import org.netbeans.spi.java.hints.TriggerTreeKind;
47 import org.openide.util.NbBundle.Messages;
49 @Hint(displayName = "#DN_JSNI2JavaScriptBody", description = "#DESC_JSNI2JavaScriptBody", category = "general")
51 "DN_JSNI2JavaScriptBody=JSNI to @JavaScriptBody",
52 "DESC_JSNI2JavaScriptBody=JSNI to @JavaScriptBody"
54 public class JSNI2JavaScriptBody {
56 @TriggerTreeKind(Kind.METHOD)
57 @Messages("ERR_JSNI2JavaScriptBody=Can convert JSNI to @JavaScriptBody")
58 public static ErrorDescription computeWarning(final HintContext ctx) {
59 Token<JavaTokenId> token = findBlockToken(ctx.getInfo(), ctx.getPath(), ctx);
65 Fix fix = new FixImpl(ctx.getInfo(), ctx.getPath()).toEditorFix();
66 return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_JSNI2JavaScriptBody(), fix);
69 private static Token<JavaTokenId> findBlockToken(CompilationInfo info, TreePath path, HintContext ctx) {
70 int end = (int) info.getTrees().getSourcePositions().getEndPosition(path.getCompilationUnit(), path.getLeaf());
71 TokenSequence<JavaTokenId> ts = info.getTokenHierarchy().tokenSequence(JavaTokenId.language());
73 if (ts == null) return null;
77 if ((ctx != null && ctx.isCanceled()) || !ts.movePrevious() || ts.token().id() != JavaTokenId.SEMICOLON) return null;
79 OUTER: while (ts.movePrevious()) {
80 if (ctx != null && ctx.isCanceled()) return null;
82 switch (ts.token().id()) {
83 case WHITESPACE: break;
84 case LINE_COMMENT: break;
85 case JAVADOC_COMMENT: break;
87 final CharSequence tok = ts.token().text();
88 final int l = tok.length();
90 && tok.subSequence(0, 4).toString().equals("/*-{") // NOI18N
91 && tok.subSequence(l - 4, l).toString().equals("}-*/") // NOI18N
93 return ts.offsetToken();
104 private static final class FixImpl extends JavaFix {
106 public FixImpl(CompilationInfo info, TreePath tp) {
111 @Messages("FIX_JSNI2JavaScriptBody=Convert JSNI to @JavaScriptBody")
112 protected String getText() {
113 return Bundle.FIX_JSNI2JavaScriptBody();
117 protected void performRewrite(TransformationContext ctx) {
118 Token<JavaTokenId> jsniComment = findBlockToken(ctx.getWorkingCopy(), ctx.getPath(), null);
120 if (jsniComment == null) {
125 JsniCommentTokenizer tok = new JsniCommentTokenizer();
126 ManglingSink ms = new ManglingSink();
127 final CharSequence cmnt = jsniComment.text();
128 tok.process(cmnt.subSequence(4, cmnt.length() - 4), ms);
130 TreeMaker make = ctx.getWorkingCopy().getTreeMaker();
131 MethodTree mt = (MethodTree) ctx.getPath().getLeaf();
132 List<LiteralTree> params = new ArrayList<LiteralTree>();
134 for (VariableTree p : mt.getParameters()) {
135 params.add(make.Literal(p.getName().toString()));
138 AnnotationTree jsBody = make.Annotation(make.QualIdent("org.apidesign.bck2brwsr.core.JavaScriptBody"),
139 Arrays.<ExpressionTree>asList(
140 make.Assignment(make.Identifier("args"), make.NewArray(null, Collections.<ExpressionTree>emptyList(), params)),
141 make.Assignment(make.Identifier("body"), make.Literal(ms.out.toString()))
146 ctx.getWorkingCopy().rewrite(mt.getModifiers(), make.addModifiersAnnotation(mt.getModifiers(), jsBody));