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 java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33 import org.netbeans.api.java.lexer.JavaTokenId;
34 import static org.netbeans.api.java.lexer.JavaTokenId.BLOCK_COMMENT;
35 import static org.netbeans.api.java.lexer.JavaTokenId.JAVADOC_COMMENT;
36 import static org.netbeans.api.java.lexer.JavaTokenId.LINE_COMMENT;
37 import static org.netbeans.api.java.lexer.JavaTokenId.WHITESPACE;
38 import org.netbeans.api.java.source.CompilationInfo;
39 import org.netbeans.api.java.source.TreeMaker;
40 import org.netbeans.api.lexer.Token;
41 import org.netbeans.api.lexer.TokenSequence;
42 import org.netbeans.spi.editor.hints.ErrorDescription;
43 import org.netbeans.spi.editor.hints.Fix;
44 import org.netbeans.spi.java.hints.ErrorDescriptionFactory;
45 import org.netbeans.spi.java.hints.Hint;
46 import org.netbeans.spi.java.hints.HintContext;
47 import org.netbeans.spi.java.hints.JavaFix;
48 import org.netbeans.spi.java.hints.TriggerTreeKind;
49 import org.openide.util.NbBundle.Messages;
51 @Hint(displayName = "#DN_JSNI2JavaScriptBody", description = "#DESC_JSNI2JavaScriptBody", category = "general")
53 "DN_JSNI2JavaScriptBody=JSNI to @JavaScriptBody",
54 "DESC_JSNI2JavaScriptBody=JSNI to @JavaScriptBody"
56 public class JSNI2JavaScriptBody {
58 private static final Pattern JSNIPattern = Pattern.compile("/\\*-\\{(.*)}-\\*/");
60 @TriggerTreeKind(Kind.METHOD)
61 @Messages("ERR_JSNI2JavaScriptBody=Can convert JSNI to @JavaScriptBody")
62 public static ErrorDescription computeWarning(final HintContext ctx) {
63 Token<JavaTokenId> token = findBlockToken(ctx.getInfo(), ctx.getPath(), ctx);
69 Fix fix = new FixImpl(ctx.getInfo(), ctx.getPath()).toEditorFix();
70 return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_JSNI2JavaScriptBody(), fix);
73 private static Token<JavaTokenId> findBlockToken(CompilationInfo info, TreePath path, HintContext ctx) {
74 int end = (int) info.getTrees().getSourcePositions().getEndPosition(path.getCompilationUnit(), path.getLeaf());
75 TokenSequence<JavaTokenId> ts = info.getTokenHierarchy().tokenSequence(JavaTokenId.language());
77 if (ts == null) return null;
81 if ((ctx != null && ctx.isCanceled()) || !ts.movePrevious() || ts.token().id() != JavaTokenId.SEMICOLON) return null;
83 OUTER: while (ts.movePrevious()) {
84 if (ctx != null && ctx.isCanceled()) return null;
86 switch (ts.token().id()) {
87 case WHITESPACE: break;
88 case LINE_COMMENT: break;
89 case JAVADOC_COMMENT: break;
91 if (JSNIPattern.matcher(ts.token().text()).matches()) {
92 return ts.offsetToken();
103 private static final class FixImpl extends JavaFix {
105 public FixImpl(CompilationInfo info, TreePath tp) {
110 @Messages("FIX_JSNI2JavaScriptBody=Convert JSNI to @JavaScriptBody")
111 protected String getText() {
112 return Bundle.FIX_JSNI2JavaScriptBody();
116 protected void performRewrite(TransformationContext ctx) {
117 Token<JavaTokenId> jsniComment = findBlockToken(ctx.getWorkingCopy(), ctx.getPath(), null);
119 if (jsniComment == null) {
124 Matcher m = JSNIPattern.matcher(jsniComment.text());
126 if (!m.matches()) return ; //XXX: warn?
128 TreeMaker make = ctx.getWorkingCopy().getTreeMaker();
129 MethodTree mt = (MethodTree) ctx.getPath().getLeaf();
130 List<LiteralTree> params = new ArrayList<LiteralTree>();
132 params.add(make.Literal("this"));
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("body"), make.Literal(m.group(1))),
141 make.Assignment(make.Identifier("args"), make.NewArray(null, Collections.<ExpressionTree>emptyList(), params))
146 ctx.getWorkingCopy().rewrite(mt.getModifiers(), make.addModifiersAnnotation(mt.getModifiers(), jsBody));