1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import java.util.ArrayList ; 22 import org.netbeans.jmi.javamodel.*; 23 import org.netbeans.lib.java.parser.ASTree; 24 import org.netbeans.lib.java.parser.ASTreeTypes; 25 import org.netbeans.mdr.storagemodel.StorableObject; 26 import org.netbeans.modules.javacore.internalapi.JavaModelUtil; 27 import org.netbeans.modules.javacore.parser.ASTProvider; 28 import org.netbeans.modules.javacore.parser.ClassInfo; 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 33 37 public abstract class NewClassExpressionImpl extends InvocationImpl implements NewClassExpression { 38 private ClassDefinition classDefinition; 39 private PrimaryExpression enclosingClass; 40 private MultipartId className; 41 42 43 public NewClassExpressionImpl(StorableObject o) { 44 super(o); 45 } 46 47 public void setEnclosingClass(PrimaryExpression expression) { 48 objectChanged(CHANGED_ENCLOSING_CLASS); 49 changeChild(getEnclosingClass(), expression); 50 this.enclosingClass = expression; 51 } 52 53 public PrimaryExpression getEnclosingClass() { 54 if (!childrenInited) { 55 initChildren(); 56 } 57 return enclosingClass; 58 } 59 60 public Type getType() { 61 if (isChanged(CHANGED_TYPE)) 62 return type; 63 else { 64 return (Type) getParser().getSemanticInfo(getNameAST(), this); 65 } 66 } 67 68 public void setType(Type type) { 69 this.type = type; 70 objectChanged(CHANGED_TYPE); 71 } 72 73 public ClassDefinition getClassDefinition() { 74 if (!childrenInited) { 75 initChildren(); 76 } 77 return classDefinition; 78 } 79 80 public void setClassDefinition(ClassDefinition classDefinition) { 81 objectChanged(CHANGED_CLASS_DEFINITION); 82 changeChild(getClassDefinition(), classDefinition); 83 this.classDefinition = classDefinition; 84 } 85 86 public MultipartId getClassName() { 87 if (!childrenInited) { 88 initChildren(); 89 } 90 return className; 91 } 92 93 public void setClassName(MultipartId className) { 94 objectChanged(CHANGED_CLASS_NAME); 95 changeChild(getClassName(), className); 96 this.className = className; 97 } 98 99 protected ASTree getNameAST() { 100 return getASTree().getSubTrees()[2]; 101 } 102 103 public List getChildren() { 104 List list = new ArrayList (6); 105 addIfNotNull(list, getEnclosingClass()); 106 addIfNotNull(list, getClassName()); 107 list.addAll(super.getChildren()); 108 addIfNotNull(list, getClassDefinition()); 109 return list; 110 } 111 112 protected void initChildren() { 113 childrenInited = false; 114 ASTree tree = getASTree(); 115 if (tree != null) { 116 ASTree[] parts = tree.getSubTrees(); 117 enclosingClass = (PrimaryExpression) initOrCreate(enclosingClass, parts[0]); 118 className = (MultipartId) initOrCreate(className, parts[2]); 119 parameters = createChildrenList(parameters, "parameters", parts[3], ASTreeTypes.ARGUMENT_LIST, CHANGED_PARAMETERS, false); ClassInfo newClassInfo = null; 121 if (parts[4] != null) { 122 newClassInfo = (ClassInfo)getParser().getSemanticInfo(parts[4], this); 123 } 124 if (classDefinition != null) { 125 if (newClassInfo == null) { 126 changeChild(classDefinition, null); 127 classDefinition.refDelete(); 128 classDefinition = null; 129 } else { 130 ClassDefinitionImpl defImpl = (ClassDefinitionImpl)classDefinition; 131 132 defImpl.updatePersistent(newClassInfo); 133 defImpl.setElementInfo(newClassInfo); 134 } 135 } else if (newClassInfo != null) { 136 SemiPersistentElement spe=(SemiPersistentElement)JavaModelUtil.getDeclaringFeature(this); 137 classDefinition = (ClassDefinition)spe.createElement(newClassInfo); 138 changeChild(null, classDefinition); 139 } 140 } 141 childrenInited = true; 142 } 143 144 public String getSourceText() { 145 return getRawText(); 147 } 148 149 String getRawText() { 150 StringBuffer buf = new StringBuffer (); 151 if (enclosingClass != null) { 152 buf.append(((MetadataElement) enclosingClass).getSourceText()); 154 buf.append("."); } 156 buf.append("new "); MultipartId className = getClassName(); 158 if (className != null) { 159 buf.append(((MetadataElement) className).getSourceText()); 160 } else { 161 buf.append(getName()); 162 } 163 formatElementPart(PAR_OPEN_BRACKET, buf); 164 Iterator iter = getParameters().iterator(); 165 while (iter.hasNext()) { 166 StatementImpl par = (StatementImpl) iter.next(); 168 buf.append(par.getSourceText()); 169 if (iter.hasNext()) { 170 formatElementPart(MetadataElement.COMMA, buf); 171 } 172 } 173 formatElementPart(PAR_CLOSE_BRACKET, buf); 174 175 ClassDefinition classDefinition = getClassDefinition(); 176 if (classDefinition != null) { 177 buf.append(((ClassDefinitionImpl) classDefinition).getSourceText()); 179 } 180 return buf.toString(); 181 } 182 183 private static final int ENCLOSING = 0; 184 private static final int NAME = 2; 185 private static final int PARAMS = 3; 186 private static final int BODY = 4; 187 188 public void getDiff(List diffList) { 189 ASTProvider parser = getParser(); 190 ASTree[] children = getASTree().getSubTrees(); 191 192 if (isChanged(CHANGED_ENCLOSING_CLASS) || ((enclosingClass != null) && ((MetadataElement) enclosingClass).isChanged())) { 195 if (children[ENCLOSING] != null) { 196 if (enclosingClass != null) { 197 ((MetadataElement) enclosingClass).getDiff(diffList); 199 } else { 200 diffList.add( new DiffElement( 202 parser.getToken(children[ENCLOSING].getFirstToken()).getStartOffset(), 203 parser.getToken(children[ENCLOSING].getLastToken()+1).getEndOffset(), 204 "") 205 ); 206 } 207 } else { 208 int startPos = parser.getToken(children[NAME].getFirstToken()-1).getStartOffset(); 210 diffList.add(new DiffElement(startPos, startPos, ((MetadataElement) enclosingClass).getSourceText() + ".")); } 212 } 213 getChildDiff(diffList, parser, children[NAME], (MetadataElement) getClassName(), CHANGED_CLASS_NAME); 214 215 int endToken = children[PARAMS] != null ? children[PARAMS].getLastToken() : children[NAME].getLastToken() + 1; 216 int endOffset = parser.getToken(endToken).getEndOffset(); 217 getCollectionDiff(diffList, parser, CHANGED_PARAMETERS, children[PARAMS], ASTreeTypes.ARGUMENT_LIST, getParameters(), endOffset, formatElementPart(MetadataElement.COMMA)); 218 int startToken = getASTree().getLastToken(); 219 getChildDiff(diffList, parser, children[BODY], (MetadataElement)getClassDefinition(), CHANGED_CLASS_DEFINITION, parser.getToken(startToken).getEndOffset(), null); 220 } 221 222 void setData(String name, List parameters, PrimaryExpression enclosingClass, ClassDefinition classDefinition, MultipartId className) { 223 setData(name, parameters); 224 changeChild(null, enclosingClass); 225 this.enclosingClass = enclosingClass; 226 changeChild(null, classDefinition); 227 this.classDefinition = classDefinition; 228 changeChild(null, className); 229 this.className = className; 230 } 231 232 protected void _delete() { 233 if (childrenInited) { 235 deleteChild(enclosingClass); 236 deleteChild(classDefinition); 237 deleteChild(className); 238 } 239 super._delete(); 243 } 244 245 public void replaceChild(Element oldElement,Element newElement) { 246 if (childrenInited) { 247 if (oldElement.equals(classDefinition)) { 248 setClassDefinition((ClassDefinition)newElement); 249 } else if (oldElement.equals(enclosingClass)) { 250 setEnclosingClass((PrimaryExpression)newElement); 251 } else if (oldElement.equals(className)) { 252 setClassName((MultipartId)newElement); 253 } else 254 super.replaceChild(oldElement,newElement); 255 } 256 } 257 258 public NamedElement getElement() { 259 NamedElement el=(NamedElement) getParser().getSemanticInfo(getASTree(), this); 260 261 if (el instanceof Constructor) { 262 return el; 263 } 264 return null; 265 } 266 267 public Element duplicate(JavaModelPackage targetExtent) { 268 return targetExtent.getNewClassExpression().createNewClassExpression( 269 getName(), 270 duplicateList(getParameters(), targetExtent), 271 (PrimaryExpression) duplicateElement(getEnclosingClass(), targetExtent), 272 (MultipartId) duplicateElement(getClassName(), targetExtent), 273 (ClassDefinition) duplicateElement(getClassDefinition(), targetExtent) 274 ); 275 } 276 } 277 | Popular Tags |