1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package spechelper; 17 18 import dalvik.annotation.TestTargetClass; 19 20 import org.eclipse.core.runtime.IProgressMonitor; 21 import org.eclipse.jdt.ui.text.java.ContentAssistInvocationContext; 22 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer; 23 import org.eclipse.jface.text.BadLocationException; 24 import org.eclipse.jface.text.contentassist.ICompletionProposal; 25 26 import java.util.List; 27 import java.util.Vector; 28 import java.util.regex.Pattern; 29 30 /** 31 * <p> 32 * a plugin to auto-insert the following annotation constructs: 33 * TestInfo.java, TestStatus.java, TestTarget.java, and TestTargetClass.java 34 * under 35 * /android/device/dalvik/libcore/dalvik/src/main/java/dalvik/annotation/ 36 * <p> 37 * usage:<br> 38 * - install export/plugins/spechelper_1.0.0.jar into your eclipse/plugin folder.<br> 39 * - restart eclipse<br> 40 * - open a java file<br> 41 * - insert the TestTargetClass annotation above the class declaration, e.g. 42 * <code>@TestTargetClass(Pattern.class)</code><br> 43 * - insert a ":" one line above the signature of a method to be annotated, 44 * and press ctrl-space for eclipse autocompletion. a popup appears which 45 * lists all target methods. choose one, and the annotation will be filled in 46 * at the cursor position.<br> 47 * <p> 48 * to annotate more than one target method, simply add a comma after the 49 * first TestTarget, press enter and insert a ":", press ctrl-space again. 50 * 51 * <p> 52 * a sample: 53 * 54 <pre> 55 package org.apache.harmony.tests.java.util.regex; 56 57 import dalvik.annotation.TestTargetClass; 58 import dalvik.annotation.TestInfo; 59 import dalvik.annotation.TestTarget; 60 import dalvik.annotation.TestStatus; 61 62 import junit.framework.TestCase; 63 64 import java.util.regex.Pattern; 65 66 @TestTargetClass(Pattern.class) 67 68 public class PatternTest extends TestCase { 69 70 // add ":", press ctrl-space here to let the eclipse plugin generate 71 // the next few lines 72 @TestInfo( 73 status = TestStatus.TBR, 74 notes = "", 75 targets = { 76 @TestTarget( 77 methodName = "compile", 78 methodArgs = {String.class} 79 ) 80 }) 81 public void foo() { 82 // 83 } 84 85 @TestInfo( 86 status = TestStatus.TBR, 87 notes = "", 88 targets = { 89 @TestTarget( 90 methodName = "compile", 91 methodArgs = {String.class} 92 ), 93 // add ":", press ctrl-space here to insert another TestTarget 94 }) 95 public void bar() { 96 // 97 } 98 99 @TestInfo( 100 status = TestStatus.TBR, 101 notes = "", 102 targets = { 103 @TestTarget( 104 methodName = "compile", 105 methodArgs = {String.class} 106 ), 107 @TestTarget( 108 methodName = "split", 109 methodArgs = {CharSequence.class, int.class} 110 ) 111 112 }) 113 public void foobarsample() { 114 // 115 } 116 117 } 118 </pre> 119 * 120 * 121 * 122 */ 123 public class SimpleComputer implements IJavaCompletionProposalComputer { 124 computeCompletionProposals( ContentAssistInvocationContext context, IProgressMonitor monitor)125 public List<ICompletionProposal> computeCompletionProposals( 126 ContentAssistInvocationContext context, IProgressMonitor monitor) { 127 List<ICompletionProposal> ret = new Vector<ICompletionProposal>(); 128 try { 129 int offs = context.getInvocationOffset(); 130 String buffer = context.getDocument().get(0, offs); 131 //System.out.println("buffer:'"+buffer+"'"); 132 //System.out.println("offset:"+offs); 133 String keyWord = ":"; 134 String keyWordInfo = "':': noser: autofills the annotation"; 135 136 int idx = 0; 137 // find the replacement position 138 int klen = keyWord.length(); 139 for (int i = 0; i < klen; i++) { 140 String test = keyWord.substring(0, klen - i); 141 if (buffer.endsWith(test)) { 142 idx = klen - i; 143 break; 144 } 145 } 146 if (idx != 0) { 147 System.out.println("idx:"+idx); 148 String replace ="hi there! a longer sample text\nnew line"; 149 ICompletionProposal ci = new MyCompletion(buffer, replace, 150 context.getInvocationOffset() - idx, idx, replace 151 .length(), null, keyWordInfo, null, null); 152 ret.add(ci); 153 } 154 } catch (BadLocationException e) { 155 e.printStackTrace(); 156 } 157 return ret; 158 } 159 160 computeContextInformation( ContentAssistInvocationContext context, IProgressMonitor monitor)161 public List<ICompletionProposal> computeContextInformation( 162 ContentAssistInvocationContext context, IProgressMonitor monitor) { 163 return new Vector<ICompletionProposal>(); 164 } 165 getErrorMessage()166 public String getErrorMessage() { 167 return "Error from SimpleComputer"; 168 } 169 sessionEnded()170 public void sessionEnded() { 171 //System.out.println("session ended"); 172 } 173 sessionStarted()174 public void sessionStarted() { 175 //System.out.println("session started"); 176 } 177 178 } 179