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.MethodInvocation; 25 import org.netbeans.jmi.javamodel.PrimaryExpression; 26 import org.netbeans.jmi.javamodel.Type; 27 import org.netbeans.jmi.javamodel.Method; 28 import org.netbeans.jmi.javamodel.MethodInvocationClass; 29 import org.netbeans.lib.java.parser.ASTree; 30 import org.netbeans.lib.java.parser.ASTreeTypes; 31 import org.netbeans.mdr.storagemodel.StorableObject; 32 import org.netbeans.modules.javacore.parser.ASTProvider; 33 34 35 39 public abstract class MethodInvocationImpl extends InvocationImpl implements MethodInvocation { 40 private boolean hasSuper = false; 41 private PrimaryExpression parentClass; 42 43 44 public MethodInvocationImpl(StorableObject o) { 45 super(o); 46 } 47 48 public void setParentClass(PrimaryExpression expression) { 49 objectChanged(CHANGED_PARENT_CLASS); 50 changeChild(getParentClass(), expression); 51 this.parentClass = expression; 52 } 53 54 public PrimaryExpression getParentClass() { 55 if (!childrenInited) { 56 initChildren(); 57 } 58 return parentClass; 59 } 60 61 public boolean getHasSuper() { 62 if (isChanged(CHANGED_HAS_SUPER)) { 63 return hasSuper; 64 } else { 65 return getASTree().getSubTrees()[1] != null; 66 } 67 } 68 69 public void setHasSuper(boolean hasSuper) { 70 objectChanged(CHANGED_HAS_SUPER); 71 this.hasSuper = hasSuper; 72 } 73 74 public List getChildren() { 75 List list = new ArrayList(); 76 addIfNotNull(list, getParentClass()); 77 list.addAll(super.getChildren()); 78 return list; 79 } 80 81 protected void initChildren() { 82 childrenInited = false; 83 ASTree tree = getASTree(); 84 if (tree != null) { 85 ASTree[] parts = tree.getSubTrees(); 86 parentClass = (PrimaryExpression) initOrCreate(parentClass, parts[0]); 87 parameters = createChildrenList(parameters, "parameters", parts[4], ASTreeTypes.ARGUMENT_LIST, CHANGED_PARAMETERS, false); } 89 childrenInited = true; 90 } 91 92 96 public Type getType() { 97 Object semInfo = getParser().getSemanticInfo(getASTree(), this); 98 99 if (semInfo instanceof Method) 100 return ((Method)semInfo).getType(); 101 return null; 102 } 103 104 String getRawText() { 105 StringBuffer buf = new StringBuffer (); 106 boolean hasSuper = getHasSuper (); 107 StatementImpl parentClass = (StatementImpl) getParentClass(); 108 List params = getParameters(); 109 String methodName = getName(); 110 if (parentClass != null) { 111 buf.append(parentClass.getSourceText()); 112 buf.append("."); } 114 if (hasSuper) { 115 buf.append("super."); } 117 buf.append(methodName); 118 ASTree tree = getASTree(); 120 if (tree != null && tree.getSubTrees()[3] != null) { 121 IndentUtil.printTokenWithAllAround(tree.getSubTrees()[3].getLastToken()+1, getASTree().getFirstToken(), this, buf); 122 } else { 123 formatElementPart(STMT_OPEN_BRACKET, buf); 124 } 125 Iterator iter = params.iterator(); 126 while (iter.hasNext()) { 127 StatementImpl par = (StatementImpl) iter.next(); 128 buf.append(par.getSourceText()); 129 if (iter.hasNext()) { 130 ASTree t = par.getASTree(); 131 if (t != null) { 132 int commaToken = t.getLastToken() + 1; 133 IndentUtil.printTokenWithAllAround(commaToken, getASTree().getFirstToken(), this, buf); 134 } else { 135 formatElementPart(MetadataElement.COMMA, buf); 136 } 137 } 138 } 139 if (tree != null) { 140 IndentUtil.printTokenWithAllAround(tree.getLastToken(), getASTree().getFirstToken(), this, buf); 141 } else { 142 formatElementPart(STMT_CLOSE_BRACKET, buf); 143 } 144 return buf.toString(); 145 } 146 147 public void getDiff(List diff) { 148 ASTProvider parser = getParser(); 149 ASTree tree = getASTree(); 150 ASTree[] children = tree.getSubTrees(); 151 152 if (isChanged(CHANGED_HAS_SUPER)) { 153 int endOffset = parser.getToken(children[3].getFirstToken()-1).getEndOffset(); 154 int startOffset = children[0]!=null?parser.getToken(children[1].getLastToken()).getEndOffset():endOffset; 155 diff.add(new DiffElement(startOffset, endOffset, isHasSuper()?"super":"")); } 157 158 MetadataElement parentClass = (MetadataElement) getParentClass(); 159 if (isChanged(CHANGED_PARENT_CLASS) || (parentClass != null && parentClass.isChanged())) { 160 getChildDiff(diff, parser, children[0], parentClass, CHANGED_PARENT_CLASS); 161 } 163 if (isChanged(CHANGED_NAME)) { 164 replaceNode(diff, parser, children[3], getName(), 0, null); 165 } 166 getCollectionDiff(diff, parser, CHANGED_PARAMETERS, children[4], ASTreeTypes.ARGUMENT_LIST, getParameters(), 167 parser.getToken(tree.getLastToken()).getStartOffset(), formatElementPart(MetadataElement.COMMA)); 168 } 169 170 void setData(String name, List parameters, PrimaryExpression parentClass, boolean hasSuper) { 171 setData(name, parameters); 172 changeChild(null, parentClass); 173 this.parentClass = parentClass; 174 this.hasSuper = hasSuper; 175 } 176 177 protected ASTree getNameAST() { 178 return getASTree().getSubTrees()[3]; 179 } 180 181 protected void _delete() { 182 if (childrenInited) { 184 deleteChild(parentClass); 185 } 186 super._delete(); 190 } 191 192 public void replaceChild(Element oldElement,Element newElement) { 193 if (childrenInited) { 194 if (oldElement.equals(parentClass)) { 195 setParentClass((PrimaryExpression)newElement); 196 } else 197 super.replaceChild(oldElement,newElement); 198 } 199 } 200 201 public Element duplicate(JavaModelPackage targetExtent) { 202 return targetExtent.getMethodInvocation().createMethodInvocation( 203 getName(), 204 duplicateList(getParameters(), targetExtent), 205 (PrimaryExpression) duplicateElement(getParentClass(), targetExtent), 206 getHasSuper() 207 ); 208 } 209 210 protected String getIndentation() { 211 return ((MetadataElement) refImmediateComposite()).getIndentation(); 212 } 213 } 214 | Popular Tags |