ide/editor/src/main/java/org/apidesign/bck2brwsr/ide/editor/JSNI2JavaScriptBody.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 13 Feb 2013 12:51:31 +0100
branchide
changeset 719 72a100b59e88
parent 718 b93760cedf02
permissions -rw-r--r--
Removing unused imports
     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 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;
    48 
    49 @Hint(displayName = "#DN_JSNI2JavaScriptBody", description = "#DESC_JSNI2JavaScriptBody", category = "general")
    50 @Messages({
    51     "DN_JSNI2JavaScriptBody=JSNI to @JavaScriptBody",
    52     "DESC_JSNI2JavaScriptBody=JSNI to @JavaScriptBody"
    53 })
    54 public class JSNI2JavaScriptBody {
    55 
    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);
    60 
    61         if (token == null) {
    62             return null;
    63         }
    64 
    65         Fix fix = new FixImpl(ctx.getInfo(), ctx.getPath()).toEditorFix();
    66         return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_JSNI2JavaScriptBody(), fix);
    67     }
    68 
    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());
    72 
    73         if (ts == null) return null;
    74 
    75         ts.move(end);
    76 
    77         if ((ctx != null && ctx.isCanceled()) || !ts.movePrevious() || ts.token().id() != JavaTokenId.SEMICOLON) return null;
    78 
    79         OUTER: while (ts.movePrevious()) {
    80             if (ctx != null && ctx.isCanceled()) return null;
    81 
    82             switch (ts.token().id()) {
    83                 case WHITESPACE: break;
    84                 case LINE_COMMENT: break;
    85                 case JAVADOC_COMMENT: break;
    86                 case BLOCK_COMMENT:
    87                     final CharSequence tok = ts.token().text();
    88                     final int l = tok.length(); 
    89                     if (l > 4 
    90                         && tok.subSequence(0, 4).toString().equals("/*-{") // NOI18N
    91                         && tok.subSequence(l - 4, l).toString().equals("}-*/") // NOI18N
    92                     ) {
    93                         return ts.offsetToken();
    94                     }
    95                     break;
    96                 default:
    97                     break OUTER;
    98             }
    99         }
   100 
   101         return null;
   102     }
   103 
   104     private static final class FixImpl extends JavaFix {
   105 
   106         public FixImpl(CompilationInfo info, TreePath tp) {
   107             super(info, tp);
   108         }
   109 
   110         @Override
   111         @Messages("FIX_JSNI2JavaScriptBody=Convert JSNI to @JavaScriptBody")
   112         protected String getText() {
   113             return Bundle.FIX_JSNI2JavaScriptBody();
   114         }
   115 
   116         @Override
   117         protected void performRewrite(TransformationContext ctx) {
   118             Token<JavaTokenId> jsniComment = findBlockToken(ctx.getWorkingCopy(), ctx.getPath(), null);
   119 
   120             if (jsniComment == null) {
   121                 //XXX: warn?
   122                 return ;
   123             }
   124             
   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);
   129 
   130             TreeMaker make = ctx.getWorkingCopy().getTreeMaker();
   131             MethodTree mt = (MethodTree) ctx.getPath().getLeaf();
   132             List<LiteralTree> params = new ArrayList<LiteralTree>();
   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("args"), make.NewArray(null, Collections.<ExpressionTree>emptyList(), params)),
   141                     make.Assignment(make.Identifier("body"), make.Literal(ms.out.toString()))
   142                 )
   143             );
   144 
   145 
   146             ctx.getWorkingCopy().rewrite(mt.getModifiers(), make.addModifiersAnnotation(mt.getModifiers(), jsBody));
   147         }
   148     }
   149 }