1 19 package org.netbeans.modules.javacore; 20 21 import java.util.*; 22 import org.netbeans.jmi.javamodel.*; 23 import org.netbeans.modules.javacore.internalapi.GuardedQuery; 24 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 25 import org.netbeans.modules.javacore.internalapi.JavaModelUtil; 26 import org.netbeans.modules.javacore.jmiimpl.javamodel.*; 27 import org.openide.filesystems.FileObject; 28 import org.openide.util.NbBundle; 29 30 34 public class TryWrapper { 35 38 private Element elementToReplace; 39 42 private Element replaceParent; 43 44 private JavaModelPackage model; 45 46 private List tryBody; 47 48 54 public TryWrapper(FileObject fo, int from, int to) { 55 this(JavaModelUtil.getSelectedStatements(JavaMetamodel.getManager().getResource(fo), from, to)); 56 } 57 58 public TryWrapper(List statements) { 59 if (statements==null) { 60 throw new GeneralException(NbBundle.getMessage(TryWrapper.class, "MSG_CannotEncapsulate")); 61 } 62 tryBody = new ArrayList(statements); 63 64 elementToReplace = (Element) tryBody.get(0); 65 if (GuardedQuery.isSectionGuarded(elementToReplace.getResource(), JavaMetamodel.getManager().getElementPosition(elementToReplace))) { 66 throw new GeneralException(NbBundle.getMessage(TryWrapper.class, "MSG_CannotEncapsulateGuardedBlock")); 67 } 68 replaceParent = (Element) elementToReplace.refImmediateComposite(); 69 model = (JavaModelPackage) elementToReplace.refOutermostPackage(); 70 } 71 72 private void check(List body, List declaredVariables) { 73 Iterator it = body.iterator(); 74 while (it.hasNext()) { 75 Object o = it.next(); 76 if (o instanceof LocalVarDeclaration) { 77 LocalVarDeclaration decl = (LocalVarDeclaration) o; 78 declaredVariables.addAll(decl.getVariables()); 79 } 80 } 81 } 82 83 private ExpressionStatement createAssignment(LocalVariable var) { 84 VariableAccess access = model.getVariableAccess().createVariableAccess(var.getName(),null, false); 85 InitialValue initValue = var.getInitialValue(); 86 var.setInitialValue(null); 87 Assignment a = model.getAssignment().createAssignment(access, OperatorEnum.ASSIGN, (Expression) initValue); 88 ExpressionStatement s = model.getExpressionStatement().createExpressionStatement(a); 89 return s; 90 } 91 92 private Set getUsedVariables(List decl, List statements) { 93 Set used = new LinkedHashSet(); 94 LinkedList l = new LinkedList(); 95 l.addAll(statements); 96 while (!l.isEmpty()) { 97 Element e = (Element) l.removeFirst(); 98 if (e instanceof VariableAccess) { 99 Iterator i = decl.iterator(); 100 while (i.hasNext()) { 101 LocalVariable var = (LocalVariable) i.next(); 102 if (((VariableAccess) e).getName().equals(var.getName())) { 106 used.add(var); 107 } 108 } 109 } 110 l.addAll(e.getChildren()); 111 } 112 return used; 113 } 114 115 119 public TryStatement wrap() { 120 List decl = new ArrayList(); 122 check(tryBody, decl); 123 124 List catches = createCatches(getExceptions(tryBody)); 125 126 StatementBlock finallyBlock = !catches.isEmpty()?null:model.getStatementBlock().createStatementBlock(); 128 129 TryStatement tryStatement = model.getTryStatement().createTryStatement(); 130 tryStatement.getCatches().addAll(catches); 131 tryStatement.setFinalizer(finallyBlock); 132 133 if (replaceParent instanceof Feature) { 134 ArrayList l = new ArrayList(); 135 l.add(tryStatement); 136 replaceParent.replaceChild(elementToReplace, model.getStatementBlock().createStatementBlock(l)); 137 StatementBlock tryBlock = model.getStatementBlock().createStatementBlock(tryBody); 138 tryStatement.setBody(tryBlock); 139 } else { 140 List ll = new ArrayList(); 141 ll.addAll(replaceParent.getChildren()); 142 ll.remove(elementToReplace); 143 replaceParent.replaceChild(elementToReplace, tryStatement); 144 ArrayList tryBody2 = new ArrayList(tryBody); 145 tryBody.remove(elementToReplace); 146 for (Iterator i = tryBody.iterator(); i.hasNext() ; ) { 147 Element e = (Element) i.next(); 148 replaceParent.replaceChild(e, null); 149 ll.remove(e); 150 } 151 StatementBlock tryBlock = model.getStatementBlock().createStatementBlock(tryBody2); 152 tryStatement.setBody(tryBlock); 153 Set l = getUsedVariables(decl, ll); 154 List body; 155 if (replaceParent instanceof StatementBlock) { 156 body = ((StatementBlock) replaceParent).getStatements(); 158 } else { 159 body = Collections.singletonList(tryStatement); 163 } 164 int index = body.indexOf(tryStatement); 165 body.addAll(index, separateDeclsAndInits(l, tryBlock)); 166 } 168 return tryStatement; 169 } 170 171 216 private List separateDeclsAndInits(Set usedVars, StatementBlock addInitializersTo) { 217 List decl = new ArrayList(); 218 Iterator varsIt = usedVars.iterator(); 219 Set separated = new HashSet(2); 220 List statements = addInitializersTo.getStatements(); 221 while (varsIt.hasNext()) { 222 LocalVariable var = (LocalVariable) varsIt.next(); 223 LocalVarDeclaration parent = (LocalVarDeclaration) var.refImmediateComposite(); 224 if (!separated.contains(parent)) { 228 separated.add(parent); 229 LocalVarDeclaration lvdCopy = (LocalVarDeclaration) parent.duplicate(); 230 LocalVariable[] vars = (LocalVariable[]) lvdCopy.getVariables().toArray(new LocalVariable[0]); 232 boolean removeInitVals = false; 233 for (int varInDecl = 0; varInDecl < vars.length; varInDecl++) { 234 for (Iterator varsIt2 = usedVars.iterator(); varsIt2.hasNext(); ) { 235 LocalVariable lv_used = (LocalVariable) varsIt2.next(); 236 if (lv_used.getName().equals(vars[varInDecl].getName())) { 237 removeInitVals = true; 242 break; 243 } 244 } 245 if (removeInitVals) { 250 List newAssignments = new ArrayList(); 251 int parentIndex = statements.indexOf(parent); 252 253 for (int j = 0; j < vars.length; j++) { 254 if (vars[j].getInitialValue() != null) { 255 newAssignments.add(createAssignment(vars[j])); 256 vars[j].setInitialValue(null); 257 } 258 } 259 statements.addAll(parentIndex,newAssignments); 260 break; 261 } 262 } 263 if (removeInitVals) { 267 decl.add(lvdCopy); 268 parent.refDelete(); 269 } 270 } 271 } 272 return decl; 273 } 274 275 280 private List createCatches(Collection ex) { 281 List catches = new ArrayList(); 282 String parName = "ex"; 284 for (Iterator i = ex.iterator(); i.hasNext();) { 285 MultipartId e = (MultipartId) i.next(); 286 Parameter par = model.getParameter().createParameter(parName, null, false, e, 0, false); 287 MultipartId var = model.getMultipartId().createMultipartId(parName, null, null); 288 MethodInvocation m = model.getMethodInvocation().createMethodInvocation("printStackTrace", Collections.EMPTY_LIST, var, false); Statement st = model.getExpressionStatement().createExpressionStatement(m); 290 StatementBlock block = model.getStatementBlock().createStatementBlock(Collections.singletonList(st)); 291 292 catches.add(model.getCatch().createCatch(par, block)); 293 } 294 return catches; 295 } 296 297 302 private Collection getExceptions(List b) { 303 Set exSet = JavaModelUtil.getExceptionsFromStatements(b); 304 Map exMap = new TreeMap(); 305 Collection result = new ArrayList(); 306 Collection exceptions; 307 int index = 0; 308 int exs = exSet.size(); 309 310 for (Iterator i = exSet.iterator(); i.hasNext(); index++) { 311 JavaClass c = (JavaClass) i.next(); 312 int id = -(getExceptionLevel(c)*exs+index); 313 314 exMap.put(new Integer (id),c); 315 } 316 exceptions = exMap.values(); 317 for (Iterator i = exceptions.iterator(); i.hasNext();) { 318 JavaClass c = (JavaClass) i.next(); 319 result.add(JavaModelUtil.resolveImportsForClass(replaceParent, c)); 320 } 321 return result; 322 } 323 324 private int getExceptionLevel(JavaClass ex) { 325 if (ex.getName().equals("java.lang.Object")) { return 0; } 328 if (ex.getName().equals("java.lang.Throwable")) { 329 return 0; 330 } 331 return getExceptionLevel(ex.getSuperClass())+1; 332 } 333 334 } 335 | Popular Tags |