ide/editor/src/main/java/org/apidesign/bck2brwsr/ide/editor/JSNI2JavaScriptBody.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 13 Feb 2013 11:12:55 +0100
branchide
changeset 716 8d7f8719d29c
child 718 b93760cedf02
permissions -rw-r--r--
Lahvac's hint to convert from JSNI to @JavaScriptBody
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.bck2brwsr.ide.editor;
    19 
    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;
    50 
    51 @Hint(displayName = "#DN_JSNI2JavaScriptBody", description = "#DESC_JSNI2JavaScriptBody", category = "general")
    52 @Messages({
    53     "DN_JSNI2JavaScriptBody=JSNI to @JavaScriptBody",
    54     "DESC_JSNI2JavaScriptBody=JSNI to @JavaScriptBody"
    55 })
    56 public class JSNI2JavaScriptBody {
    57 
    58     private static final Pattern JSNIPattern = Pattern.compile("/\\*-\\{(.*)}-\\*/");
    59 
    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);
    64 
    65         if (token == null) {
    66             return null;
    67         }
    68 
    69         Fix fix = new FixImpl(ctx.getInfo(), ctx.getPath()).toEditorFix();
    70         return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_JSNI2JavaScriptBody(), fix);
    71     }
    72 
    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());
    76 
    77         if (ts == null) return null;
    78 
    79         ts.move(end);
    80 
    81         if ((ctx != null && ctx.isCanceled()) || !ts.movePrevious() || ts.token().id() != JavaTokenId.SEMICOLON) return null;
    82 
    83         OUTER: while (ts.movePrevious()) {
    84             if (ctx != null && ctx.isCanceled()) return null;
    85 
    86             switch (ts.token().id()) {
    87                 case WHITESPACE: break;
    88                 case LINE_COMMENT: break;
    89                 case JAVADOC_COMMENT: break;
    90                 case BLOCK_COMMENT:
    91                     if (JSNIPattern.matcher(ts.token().text()).matches()) {
    92                         return ts.offsetToken();
    93                     }
    94                     break;
    95                 default:
    96                     break OUTER;
    97             }
    98         }
    99 
   100         return null;
   101     }
   102 
   103     private static final class FixImpl extends JavaFix {
   104 
   105         public FixImpl(CompilationInfo info, TreePath tp) {
   106             super(info, tp);
   107         }
   108 
   109         @Override
   110         @Messages("FIX_JSNI2JavaScriptBody=Convert JSNI to @JavaScriptBody")
   111         protected String getText() {
   112             return Bundle.FIX_JSNI2JavaScriptBody();
   113         }
   114 
   115         @Override
   116         protected void performRewrite(TransformationContext ctx) {
   117             Token<JavaTokenId> jsniComment = findBlockToken(ctx.getWorkingCopy(), ctx.getPath(), null);
   118 
   119             if (jsniComment == null) {
   120                 //XXX: warn?
   121                 return ;
   122             }
   123 
   124             Matcher m = JSNIPattern.matcher(jsniComment.text());
   125 
   126             if (!m.matches()) return ; //XXX: warn?
   127 
   128             TreeMaker make = ctx.getWorkingCopy().getTreeMaker();
   129             MethodTree mt = (MethodTree) ctx.getPath().getLeaf();
   130             List<LiteralTree> params = new ArrayList<LiteralTree>();
   131 
   132             params.add(make.Literal("this"));
   133 
   134             for (VariableTree p : mt.getParameters()) {
   135                 params.add(make.Literal(p.getName().toString()));
   136             }
   137 
   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))
   142                 )
   143             );
   144 
   145 
   146             ctx.getWorkingCopy().rewrite(mt.getModifiers(), make.addModifiersAnnotation(mt.getModifiers(), jsBody));
   147         }
   148     }
   149 }