1 19 package org.netbeans.api.java.source.gen; 20 21 import com.sun.source.tree.ExpressionTree; 22 import com.sun.source.tree.IdentifierTree; 23 import com.sun.source.tree.MethodTree; 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 import javax.lang.model.element.Element; 28 import org.netbeans.api.java.source.JavaSource; 29 import org.netbeans.api.java.source.SourceUtilsTestUtil2; 30 import org.netbeans.api.java.source.WorkingCopy; 31 import org.netbeans.api.java.source.transform.Transformer; 32 import org.netbeans.junit.NbTestSuite; 33 import junit.textui.TestRunner; 34 import org.netbeans.api.java.source.CancellableTask; 35 import org.netbeans.api.java.source.ClasspathInfo; 36 import org.netbeans.api.java.source.CompilationInfo; 37 import org.openide.filesystems.FileObject; 38 import org.openide.filesystems.FileUtil; 39 40 45 public class MethodTest3 extends GeneratorTest { 46 47 48 public MethodTest3(String name) { 49 super(name); 50 } 51 52 public static NbTestSuite suite() { 53 NbTestSuite suite = new NbTestSuite(); 54 suite.addTest(new MethodTest3("testMethodThrows")); 55 return suite; 56 } 57 58 protected void setUp() throws Exception { 59 super.setUp(); 60 testFile = getFile(getSourceDir(), getSourcePckg() + "MethodTest3.java"); 61 i = ClasspathInfo.create(testFile); 62 } 63 64 ClasspathInfo i; 65 66 70 public void testMethodThrows() throws IOException { 71 FileObject fo = FileUtil.toFileObject(testFile); 72 JavaSource js = JavaSource.forFileObject(fo); 73 js.runModificationTask(new CancellableTask<WorkingCopy>() { 74 public void cancel() { 75 } 76 public void run(WorkingCopy wc) { 77 RemoveException remove = new RemoveException(0); 78 SourceUtilsTestUtil2.run(wc, remove); 79 80 AddException add = new AddException(wc, "java.lang.IllegalMonitorStateException"); 81 SourceUtilsTestUtil2.run(wc, add); 82 } 83 }).commit(); 84 85 assertFiles("testMethodThrows.pass"); 86 } 87 88 92 public static void main(String [] args) { 93 TestRunner.run(suite()); 94 } 95 96 String getSourcePckg() { 97 return "org/netbeans/test/codegen/"; 98 } 99 100 String getGoldenPckg() { 101 return "org/netbeans/jmi/javamodel/codegen/MethodTest3/MethodTest3/"; 102 } 103 104 private class RemoveException extends Transformer<Void , Object > { 105 106 int index; 107 public RemoveException(int index) { 108 this.index = index; 109 } 110 111 public Void visitMethod(MethodTree node, Object p) { 112 super.visitMethod(node, p); 113 Element al = model.getElement(node); 114 if ("foo()".equals(al.toString())) { 115 List <ExpressionTree> l = new ArrayList <ExpressionTree>(); 116 int i=0; 117 for (ExpressionTree n: node.getThrows()) { 118 if (i!=index) { 119 l.add(n); 120 } 121 } 122 MethodTree njuMethod = make.Method( 123 node.getModifiers(), 124 node.getName(), 125 (ExpressionTree) node.getReturnType(), 126 node.getTypeParameters(), 127 node.getParameters(), 128 l, 129 node.getBody(), 130 (ExpressionTree) node.getDefaultValue() 131 ); 132 model.setElement(njuMethod, al); 133 changes.rewrite(node, njuMethod); 134 } 135 return null; 136 } 137 } 138 private class AddException extends Transformer<Void , Object > { 139 140 private CompilationInfo info; 141 ExpressionTree tr; 142 String ex; 143 public AddException(CompilationInfo info, String ex) { 144 this.info = info; 145 this.ex = ex; 146 } 147 148 public Void visitMethod(MethodTree node, Object p) { 149 super.visitMethod(node, p); 150 Element al = model.getElement(node); 151 if ("foo()".equals(al.toString())) { 152 tr = make.Identifier(ex); 153 List <ExpressionTree> l = new ArrayList <ExpressionTree>(); 154 l.addAll(node.getThrows()); 155 l.add(tr); 156 MethodTree njuMethod = make.Method( 157 node.getModifiers(), 158 node.getName(), 159 (ExpressionTree) node.getReturnType(), 160 node.getTypeParameters(), 161 node.getParameters(), 162 l, 163 node.getBody(), 164 (ExpressionTree) node.getDefaultValue() 165 ); 166 Element el = info.getElements().getTypeElement(((IdentifierTree) tr).getName().toString()); 167 168 model.setElement(tr, el); 169 model.setType(tr, el.asType()); 170 changes.rewrite(node, njuMethod); 171 } 172 return null; 173 } 174 } 175 176 } 177 | Popular Tags |