1 19 20 package org.netbeans.test.j2ee.refactoring.changesignature; 21 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.List ; 25 import org.netbeans.jmi.javamodel.CallableFeature; 26 import org.netbeans.jmi.javamodel.Parameter; 27 import org.netbeans.jmi.javamodel.Type; 28 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 29 import org.netbeans.modules.refactoring.api.ChangeParametersRefactoring; 30 import org.netbeans.modules.refactoring.api.RefactoringSession; 31 import org.netbeans.modules.refactoring.api.ChangeParametersRefactoring.ParameterInfo; 32 import org.netbeans.test.j2ee.refactoring.RefactoringTestCase; 33 import org.netbeans.test.j2ee.refactoring.Utility; 34 35 36 40 public class ChangeSignatureTestCase extends RefactoringTestCase { 41 42 43 public ChangeSignatureTestCase(String name) { 44 super(name); 45 } 46 47 protected void changeSignature(String className, String name, String [] arguments, String [][] newArguments, int access, boolean undo) { 48 setJavaClass(className); 49 Utility.prepareTest(); 50 String args=""; 51 ArrayList list=new ArrayList (); 52 boolean finish=false; 53 HashMap files=null; 54 RefactoringSession result = RefactoringSession.create("Change method signature of " + name); 55 try { 56 if (arguments != null) { 57 for (int i=0;i < arguments.length;i++) { 58 list.add(Utility.findType(arguments[i], typeProxy, jc)); 59 args+=arguments[i]; 60 if (i < arguments.length-1) { 61 args+=", "; 62 } 63 } 64 } 65 CallableFeature method; 67 if (name == null) method=jc.getConstructor(list, false); 69 else 70 method=jc.getMethod(name, list, false); 71 72 ref("Change signature of "+((name==null)?"constructor ":"method ")+ 73 getModifier(method.getModifiers())+" "+ 74 ((method.getName() == null)?"":method.getName())+"("+args+") of class "+className); 75 ref(((name==null)?"constructor ":"method ")+getModifier(access)+" "+ 76 ((method.getName() == null)?"":method.getName())); 77 ref("Arguments: "); 78 for (int i=0;i < newArguments.length;i++) { 79 if (newArguments[i][1] == null) { 80 ref(newArguments[i][0]); 81 } else { 82 ref(newArguments[i][1]+" "+newArguments[i][0]+" = "+newArguments[i][2]); 83 } 84 } 85 HashMap indicies=new HashMap (); 87 List origArguments = method.getParameters(); 88 for (int i=0;i < origArguments.size();i++) { 89 Parameter par=(Parameter)(origArguments.get(i)); 90 for (int j=0;j < newArguments.length;j++) { 91 if (newArguments[j][0].equals(par.getName())) { 92 indicies.put(par.getName(), new Integer (i)); 93 } 94 } 95 } 96 ParameterInfo[] argTable = new ParameterInfo[newArguments.length]; 97 for (int i=0;i < argTable.length;i++) { 98 Type type=null; 99 if (newArguments[i][1] != null) { 100 type=typeProxy.resolve(newArguments[i][1]); 101 argTable[i]=new ParameterInfo(-1, newArguments[i][0], type, newArguments[i][2]); 102 } else { 103 argTable[i]=new ParameterInfo(((Integer )(indicies.get(newArguments[i][0]))).intValue()); 104 } 105 } 106 ChangeParametersRefactoring refactoring = new ChangeParametersRefactoring(method); 108 if (refProblems(refactoring.preCheck())) throw new Exception ("Fatal problem"); 109 refactoring.setParameterInfo(argTable); 110 refactoring.setModifiers(access); 111 if (refProblems(refactoring.checkParameters())) throw new Exception ("Fatal problem"); 112 if (refProblems(refactoring.prepare(result))) throw new Exception ("Fatal problem"); 113 files=getResources(result); 114 if (refProblems(result.doRefactoring(true))) throw new Exception ("Fatal problem"); 115 finish=true; 116 } catch (Throwable t) { 117 if (t.getMessage() == null || !t.getMessage().equals("Fatal problem")) { 118 t.printStackTrace(getLogStream()); 119 assertTrue(t.getMessage(), false); 120 } 121 } finally { 122 Utility.finishTest(); 123 } 124 if (finish) { 125 try { 126 if (undo) { 127 JavaMetamodel.getUndoManager().undo(); 128 } 129 compareResources(files, result, className, className); 130 } catch (Exception ex) { 131 ex.printStackTrace(getLogStream()); 132 assertTrue(ex.getMessage(), false); 133 } 134 } 135 } 136 } 137 | Popular Tags |