1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import java.util.*; 22 import javax.jmi.reflect.ConstraintViolationException; 23 import org.netbeans.jmi.javamodel.ConstructorInvocation; 24 import org.netbeans.jmi.javamodel.ConstructorInvocationClass; 25 import org.netbeans.jmi.javamodel.Element; 26 import org.netbeans.jmi.javamodel.JavaModelPackage; 27 import org.netbeans.jmi.javamodel.PrimaryExpression; 28 import org.netbeans.lib.java.parser.ASTree; 29 import org.netbeans.lib.java.parser.ASTreeTypes; 30 import org.netbeans.lib.java.parser.ParserTokens; 31 import org.netbeans.mdr.storagemodel.StorableObject; 32 import org.netbeans.modules.javacore.parser.ASTProvider; 33 34 38 public abstract class ConstructorInvocationImpl extends InvocationImpl implements ConstructorInvocation { 39 private boolean hasSuper = false; 40 private PrimaryExpression parentClass; 41 42 43 public ConstructorInvocationImpl(StorableObject o) { 44 super(o); 45 } 46 47 public String getName() { 48 return null; 49 } 50 51 public void setName(String name) { 52 throw new ConstraintViolationException(null, null, "Cannot change name of constructor invocation."); } 54 55 public void setParentClass(PrimaryExpression expression) { 56 objectChanged(CHANGED_PARENT_CLASS); 57 changeChild(getParentClass(), expression); 58 this.parentClass = expression; 59 } 60 61 public PrimaryExpression getParentClass() { 62 if (!childrenInited) { 63 initChildren(); 64 } 65 return parentClass; 66 } 67 68 public boolean isHasSuper() { 69 if (isChanged(CHANGED_HAS_SUPER)) { 70 return hasSuper; 71 } else { 72 return getASTree().getSubTrees()[2].getType() != ParserTokens.THIS; 73 } 74 } 75 76 public void setHasSuper(boolean hasSuper) { 77 objectChanged(CHANGED_HAS_SUPER); 78 this.hasSuper = hasSuper; 79 } 80 81 public List getChildren() { 82 List list = new ArrayList(4); 83 addIfNotNull(list, getParentClass()); 84 list.addAll(super.getChildren()); 85 return list; 86 } 87 88 protected void initChildren() { 89 childrenInited = false; 90 ASTree tree = getASTree(); 91 if (tree != null) { 92 ASTree[] parts = tree.getSubTrees(); 93 parentClass = (PrimaryExpression) initOrCreate(parentClass, parts[0]); 94 parameters = createChildrenList(parameters, "parameters", parts[3], ASTreeTypes.ARGUMENT_LIST, CHANGED_PARAMETERS, false); } 96 childrenInited = true; 97 } 98 99 public String getSourceText() { 100 String origElem; 101 if ((origElem = checkChange()) != null) 102 return origElem; 103 StringBuffer buf = new StringBuffer (); 104 boolean hasSuper = isHasSuper(); 105 StatementImpl parentClass = (StatementImpl) getParentClass(); 106 List params = getParameters(); 107 buf.append('\n'); 108 buf.append(getIndentation()); 109 if (parentClass != null) { 110 buf.append(parentClass.getSourceText()); 111 buf.append('.'); 112 } 113 if (hasSuper) { 114 buf.append("super"); } else { 116 buf.append("this"); } 118 formatElementPart(PAR_OPEN_BRACKET, buf); 119 Iterator iter = params.iterator(); 120 while (iter.hasNext()) { 121 StatementImpl par = (StatementImpl) iter.next(); 122 buf.append(par.getSourceText()); 123 if (iter.hasNext()) { 124 formatElementPart(MetadataElement.COMMA, buf); 125 } 126 } 127 formatElementPart(PAR_CLOSE_BRACKET, buf); 128 buf.append(';'); 129 return buf.toString(); 130 } 131 132 public void getDiff(List diff) { 133 ASTProvider parser = getParser(); 134 ASTree tree = getASTree(); 135 ASTree[] children = tree.getSubTrees(); 136 137 MetadataElement parentClass = (MetadataElement) getParentClass(); 138 if ((isChanged(CHANGED_PARENT_CLASS) && children[0] != null && parentClass != null) || isChanged(CHANGED_CHILDREN)) { 139 getChildDiff(diff, parser, children[0], parentClass, CHANGED_PARENT_CLASS); 140 } else if (isChanged(CHANGED_PARENT_CLASS)) { 141 int endOffset = parser.getToken(children[2].getFirstToken()).getStartOffset(); 142 if (children[0] == null) { 143 diff.add(new DiffElement(endOffset, endOffset, parentClass.getSourceText().concat("."))); 144 } else { 145 int startOffset = parser.getToken(children[0].getFirstToken()).getStartOffset(); 146 diff.add(new DiffElement(startOffset, endOffset, "")); 147 } 148 } 149 150 if (isChanged(CHANGED_HAS_SUPER)) { 151 replaceNode(diff, parser, children[2], isHasSuper() ? "super" : "this", 0, null); 152 } 153 154 getCollectionDiff(diff, parser, CHANGED_PARAMETERS, children[3], ASTreeTypes.ARGUMENT_LIST, getParameters(), parser.getToken(tree.getLastToken() - 1).getStartOffset(), formatElementPart(MetadataElement.COMMA)); 155 } 156 157 void setData(boolean hasSuper, PrimaryExpression parentClass, List parameters) { 158 setData(name, parameters); 160 changeChild(null, parentClass); 161 this.parentClass = parentClass; 162 this.hasSuper = hasSuper; 163 } 164 165 protected void _delete() { 166 if (childrenInited) { 168 deleteChild(parentClass); 169 } 170 super._delete(); 174 } 175 176 public void replaceChild(Element oldElement,Element newElement) { 177 if (childrenInited) { 178 if (oldElement.equals(parentClass)) { 179 setParentClass((PrimaryExpression)newElement); 180 } 181 } 182 } 183 184 public Element duplicate(JavaModelPackage targetExtent) { 185 return targetExtent.getConstructorInvocation().createConstructorInvocation( 186 null, 187 duplicateList(getParameters(), targetExtent), 188 isHasSuper(), 189 (PrimaryExpression) duplicateElement(getParentClass(), targetExtent) 190 ); 191 } 192 } 193 | Popular Tags |