1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import java.util.*; 22 import org.netbeans.jmi.javamodel.Element; 23 import org.netbeans.jmi.javamodel.JavaModelPackage; 24 import org.netbeans.jmi.javamodel.StatementBlock; 25 import org.netbeans.jmi.javamodel.TryStatement; 26 import org.netbeans.lib.java.parser.ASTree; 27 import org.netbeans.lib.java.parser.ASTreeTypes; 28 import org.netbeans.mdr.storagemodel.StorableObject; 29 import org.netbeans.modules.javacore.parser.ASTProvider; 30 31 35 public abstract class TryStatementImpl extends StatementImpl implements TryStatement { 36 private StatementBlock body = null; 37 private LightAttrList catches = null; 38 private StatementBlock finalizer = null; 39 40 41 public TryStatementImpl(StorableObject o) { 42 super(o); 43 } 44 45 public void setBody(StatementBlock body) { 46 objectChanged(CHANGED_BODY); 47 changeChild(getBody(), body); 48 this.body = body; 49 } 50 51 public StatementBlock getBody() { 52 if (!childrenInited) { 53 initChildren(); 54 } 55 return body; 56 } 57 58 public void setFinalizer(StatementBlock finalizer) { 59 objectChanged(CHANGED_FINALIZER); 60 changeChild(getFinalizer(), finalizer); 61 this.finalizer = finalizer; 62 } 63 64 public StatementBlock getFinalizer() { 65 if (!childrenInited) { 66 initChildren(); 67 } 68 return finalizer; 69 } 70 71 public List getCatches() { 72 if (!childrenInited) { 73 initChildren(); 74 } 75 return catches; 76 } 77 78 public List getChildren() { 79 List list = new ArrayList(2 + getCatches().size()); 80 addIfNotNull(list, getBody()); 81 list.addAll(getCatches()); 82 addIfNotNull(list, getFinalizer()); 83 return list; 84 } 85 86 protected void initChildren() { 87 childrenInited = false; 88 ASTree tree = getASTree(); 89 if (tree != null) { 90 ASTree[] parts = tree.getSubTrees(); 91 body = (StatementBlock) initOrCreate(body, parts[0]); 92 catches = createChildrenList(catches, "catches", parts[1], ASTreeTypes.CATCHES, CHANGED_CATCHES, false); finalizer = (StatementBlock) initOrCreate(finalizer, parts[2]); 94 } 95 childrenInited = true; 96 } 97 98 String getRawText() { 99 StringBuffer buf = new StringBuffer (); 100 StatementImpl body = (StatementImpl) getBody(); 101 List catches = getCatches(); 102 TransientElement finalizer = (TransientElement)getFinalizer(); 103 formatElementPart(TRY_KEYWORD, buf); 104 buf.append(body.getSourceText()); 105 for (Iterator iter = catches.iterator(); iter.hasNext();) { 106 TransientElement cas = (TransientElement) iter.next(); 107 buf.append(cas.getSourceText()); 108 } 109 if (finalizer != null) { 110 formatElementPart(FINALLY_KEYWORD, buf); 111 buf.append(finalizer.getSourceText()); 112 } 113 return buf.toString(); 114 } 115 116 public void getDiff(List diff) { 117 ASTProvider parser = getParser(); 118 ASTree tree = getASTree(); 119 ASTree[] children = tree.getSubTrees(); 120 121 getChildDiff(diff, parser, children[0], (MetadataElement) getBody(), CHANGED_BODY); 122 int pos; 123 if (children[2] == null) { 124 pos = parser.getToken(tree.getLastToken()).getEndOffset(); 125 } else { 126 pos = parser.getToken(children[2].getFirstToken()).getStartOffset(); 127 } 128 getCollectionDiff(diff, parser, CHANGED_CATCHES, children[1], ASTreeTypes.CATCHES, getCatches(), pos, "\n"); getChildDiff(diff, parser, children[2], (MetadataElement) getFinalizer(), CHANGED_FINALIZER, pos, "\n"); } 131 132 void setData(StatementBlock body, List catches, StatementBlock finalizer) { 133 changeChild(null, body); 134 this.body = body; 135 this.catches = createChildrenList("catches", catches, CHANGED_CATCHES); changeChild(null, finalizer); 137 this.finalizer = finalizer; 138 } 139 140 protected void _delete() { 141 if (childrenInited) { 143 deleteChild(body); 144 deleteChild(finalizer); 145 deleteChildren(catches); 146 } 147 super._delete(); 151 } 152 153 public void replaceChild(Element oldElement,Element newElement) { 154 if (childrenInited) { 155 if (oldElement.equals(body)) { 156 setBody((StatementBlock)newElement); 157 } else if (oldElement.equals(finalizer)) { 158 setFinalizer((StatementBlock)newElement); 159 } else 160 replaceObject(getCatches(),oldElement,newElement); 161 } 162 } 163 164 public Element duplicate(JavaModelPackage targetExtent) { 165 return targetExtent.getTryStatement().createTryStatement( 166 (StatementBlock) duplicateElement(getBody(), targetExtent), 167 duplicateList(getCatches(), targetExtent), 168 (StatementBlock) duplicateElement(getFinalizer(), targetExtent) 169 ); 170 } 171 } 172 | Popular Tags |