ide/editor/src/main/java/org/apidesign/bck2brwsr/ide/editor/JSNI2JavaScriptBody.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/ide/editor/src/main/java/org/apidesign/bck2brwsr/ide/editor/JSNI2JavaScriptBody.java Wed Feb 13 11:12:55 2013 +0100
1.3 @@ -0,0 +1,149 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.bck2brwsr.ide.editor;
1.22 +
1.23 +import com.sun.source.tree.AnnotationTree;
1.24 +import com.sun.source.tree.ExpressionTree;
1.25 +import com.sun.source.tree.LiteralTree;
1.26 +import com.sun.source.tree.MethodTree;
1.27 +import com.sun.source.tree.Tree.Kind;
1.28 +import com.sun.source.tree.VariableTree;
1.29 +import com.sun.source.util.TreePath;
1.30 +import java.util.ArrayList;
1.31 +import java.util.Arrays;
1.32 +import java.util.Collections;
1.33 +import java.util.List;
1.34 +import java.util.regex.Matcher;
1.35 +import java.util.regex.Pattern;
1.36 +import org.netbeans.api.java.lexer.JavaTokenId;
1.37 +import static org.netbeans.api.java.lexer.JavaTokenId.BLOCK_COMMENT;
1.38 +import static org.netbeans.api.java.lexer.JavaTokenId.JAVADOC_COMMENT;
1.39 +import static org.netbeans.api.java.lexer.JavaTokenId.LINE_COMMENT;
1.40 +import static org.netbeans.api.java.lexer.JavaTokenId.WHITESPACE;
1.41 +import org.netbeans.api.java.source.CompilationInfo;
1.42 +import org.netbeans.api.java.source.TreeMaker;
1.43 +import org.netbeans.api.lexer.Token;
1.44 +import org.netbeans.api.lexer.TokenSequence;
1.45 +import org.netbeans.spi.editor.hints.ErrorDescription;
1.46 +import org.netbeans.spi.editor.hints.Fix;
1.47 +import org.netbeans.spi.java.hints.ErrorDescriptionFactory;
1.48 +import org.netbeans.spi.java.hints.Hint;
1.49 +import org.netbeans.spi.java.hints.HintContext;
1.50 +import org.netbeans.spi.java.hints.JavaFix;
1.51 +import org.netbeans.spi.java.hints.TriggerTreeKind;
1.52 +import org.openide.util.NbBundle.Messages;
1.53 +
1.54 +@Hint(displayName = "#DN_JSNI2JavaScriptBody", description = "#DESC_JSNI2JavaScriptBody", category = "general")
1.55 +@Messages({
1.56 + "DN_JSNI2JavaScriptBody=JSNI to @JavaScriptBody",
1.57 + "DESC_JSNI2JavaScriptBody=JSNI to @JavaScriptBody"
1.58 +})
1.59 +public class JSNI2JavaScriptBody {
1.60 +
1.61 + private static final Pattern JSNIPattern = Pattern.compile("/\\*-\\{(.*)}-\\*/");
1.62 +
1.63 + @TriggerTreeKind(Kind.METHOD)
1.64 + @Messages("ERR_JSNI2JavaScriptBody=Can convert JSNI to @JavaScriptBody")
1.65 + public static ErrorDescription computeWarning(final HintContext ctx) {
1.66 + Token<JavaTokenId> token = findBlockToken(ctx.getInfo(), ctx.getPath(), ctx);
1.67 +
1.68 + if (token == null) {
1.69 + return null;
1.70 + }
1.71 +
1.72 + Fix fix = new FixImpl(ctx.getInfo(), ctx.getPath()).toEditorFix();
1.73 + return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_JSNI2JavaScriptBody(), fix);
1.74 + }
1.75 +
1.76 + private static Token<JavaTokenId> findBlockToken(CompilationInfo info, TreePath path, HintContext ctx) {
1.77 + int end = (int) info.getTrees().getSourcePositions().getEndPosition(path.getCompilationUnit(), path.getLeaf());
1.78 + TokenSequence<JavaTokenId> ts = info.getTokenHierarchy().tokenSequence(JavaTokenId.language());
1.79 +
1.80 + if (ts == null) return null;
1.81 +
1.82 + ts.move(end);
1.83 +
1.84 + if ((ctx != null && ctx.isCanceled()) || !ts.movePrevious() || ts.token().id() != JavaTokenId.SEMICOLON) return null;
1.85 +
1.86 + OUTER: while (ts.movePrevious()) {
1.87 + if (ctx != null && ctx.isCanceled()) return null;
1.88 +
1.89 + switch (ts.token().id()) {
1.90 + case WHITESPACE: break;
1.91 + case LINE_COMMENT: break;
1.92 + case JAVADOC_COMMENT: break;
1.93 + case BLOCK_COMMENT:
1.94 + if (JSNIPattern.matcher(ts.token().text()).matches()) {
1.95 + return ts.offsetToken();
1.96 + }
1.97 + break;
1.98 + default:
1.99 + break OUTER;
1.100 + }
1.101 + }
1.102 +
1.103 + return null;
1.104 + }
1.105 +
1.106 + private static final class FixImpl extends JavaFix {
1.107 +
1.108 + public FixImpl(CompilationInfo info, TreePath tp) {
1.109 + super(info, tp);
1.110 + }
1.111 +
1.112 + @Override
1.113 + @Messages("FIX_JSNI2JavaScriptBody=Convert JSNI to @JavaScriptBody")
1.114 + protected String getText() {
1.115 + return Bundle.FIX_JSNI2JavaScriptBody();
1.116 + }
1.117 +
1.118 + @Override
1.119 + protected void performRewrite(TransformationContext ctx) {
1.120 + Token<JavaTokenId> jsniComment = findBlockToken(ctx.getWorkingCopy(), ctx.getPath(), null);
1.121 +
1.122 + if (jsniComment == null) {
1.123 + //XXX: warn?
1.124 + return ;
1.125 + }
1.126 +
1.127 + Matcher m = JSNIPattern.matcher(jsniComment.text());
1.128 +
1.129 + if (!m.matches()) return ; //XXX: warn?
1.130 +
1.131 + TreeMaker make = ctx.getWorkingCopy().getTreeMaker();
1.132 + MethodTree mt = (MethodTree) ctx.getPath().getLeaf();
1.133 + List<LiteralTree> params = new ArrayList<LiteralTree>();
1.134 +
1.135 + params.add(make.Literal("this"));
1.136 +
1.137 + for (VariableTree p : mt.getParameters()) {
1.138 + params.add(make.Literal(p.getName().toString()));
1.139 + }
1.140 +
1.141 + AnnotationTree jsBody = make.Annotation(make.QualIdent("org.apidesign.bck2brwsr.core.JavaScriptBody"),
1.142 + Arrays.<ExpressionTree>asList(
1.143 + make.Assignment(make.Identifier("body"), make.Literal(m.group(1))),
1.144 + make.Assignment(make.Identifier("args"), make.NewArray(null, Collections.<ExpressionTree>emptyList(), params))
1.145 + )
1.146 + );
1.147 +
1.148 +
1.149 + ctx.getWorkingCopy().rewrite(mt.getModifiers(), make.addModifiersAnnotation(mt.getModifiers(), jsBody));
1.150 + }
1.151 + }
1.152 +}