1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import java.util.*; 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.lib.java.parser.ParserTokens; 26 import org.netbeans.mdr.storagemodel.StorableObject; 27 import org.netbeans.modules.javacore.internalapi.JavaModelUtil; 28 import org.netbeans.modules.javacore.parser.MDRParser; 29 30 34 public abstract class MultipartIdImpl extends TypeReferenceImpl implements MultipartId { 35 private LightAttrList typeArguments; 36 37 38 public MultipartIdImpl(StorableObject o) { 39 super(o); 40 } 41 42 void setData(String name, MultipartId parent, List typeArguments) { 43 super.setData(name, parent); 44 this.typeArguments = createChildrenList("typeArguments", typeArguments, CHANGED_TYPE_ARGUMENTS); } 46 47 protected void initChildren() { 48 childrenInited = false; 49 ASTree tree = getASTree(); 50 ASTree[] children = tree.getSubTrees(); 51 typeArguments = createChildrenList(typeArguments, "typeArguments", children == null || children.length < 3 ? null : children[2], ASTreeTypes.TYPE_ARGUMENTS, CHANGED_TYPE_ARGUMENTS, false); super.initChildren(); 53 } 54 55 protected ASTree getNameAST() { 56 ASTree tree = getASTree(); 57 if (tree.getType() == ASTreeTypes.MULTI_PART_ID) { 58 tree = tree.getSubTrees()[1]; 59 } 60 return tree; 61 } 62 63 public List getTypeArguments() { 64 if (!childrenInited) { 65 initChildren(); 66 } 67 return typeArguments; 68 } 69 70 public String getSourceText() { 71 return getRawText(); 72 } 73 74 String getRawText() { 75 StringBuffer buf = new StringBuffer (); 76 MetadataElement parent = (MetadataElement) getParent(); 77 if (parent != null) { 78 buf.append(parent.getSourceText() + '.'); 79 } 80 buf.append(getName()); 81 generateNewTypeArguments(buf); 82 return buf.toString(); 83 } 84 85 private void generateNewTypeArguments(StringBuffer buf) { 86 if (!typeArguments.isEmpty()) { 87 buf.append('<'); 88 Iterator it = typeArguments.iterator(); 89 while (it.hasNext()) { 90 buf.append(((MetadataElement) it.next()).getSourceText()); 91 if (it.hasNext()) { 92 formatElementPart(MetadataElement.COMMA, buf); 93 } 94 } 95 buf.append('>'); 96 } 97 } 98 99 public void getDiff(List diff) { 100 MDRParser parser = getParser(); 101 ASTree tree = getASTree(); 102 ASTree[] children = tree.getSubTrees(); 103 ASTree nameAST = getNameAST(); 104 ASTree parentAST = null; 105 106 if (tree.getType() == ASTreeTypes.MULTI_PART_ID && children != null && children.length > 0) { 107 parentAST = children[0]; 108 } 109 110 if (isChanged(CHANGED_PARENT) && parentAST != null && getParent()==null) { 111 int start = getStartOffset(parser, children[0], false); 113 int end = getEndOffset(parser,children[0]); 114 diff.add(new DiffElement(start, end + 1, "")); 115 } else { 116 getChildDiff(diff, parser, parentAST, (MetadataElement) getParent(), CHANGED_PARENT, getStartOffset(parser, tree, false), ""); 117 } 118 119 if (isChanged(CHANGED_NAME)) { 120 replaceNode(diff, parser, nameAST, getName(), 0, null); 121 } 122 123 if (children == null || children.length < 3 || children[2] == null) { 124 if (isChanged(CHANGED_TYPE_ARGUMENTS)) { 125 StringBuffer buf = new StringBuffer (); 126 generateNewTypeArguments(buf); 127 int endOffset = getEndOffset(parser, getNameAST()); 128 diff.add(new DiffElement(endOffset, endOffset, buf.toString())); 129 } 130 } else if (getTypeArguments().isEmpty()) { 131 if (isChanged(CHANGED_TYPE_ARGUMENTS)) { 132 replaceNode(diff, parser, children[2], "", 0, null); 133 } 134 } else { 135 getCollectionDiff(diff, parser, CHANGED_TYPE_ARGUMENTS, children[2], ASTreeTypes.TYPE_ARGUMENTS, getTypeArguments(), parser.getToken(children[2].getLastToken()).getStartOffset(), ", "); } 137 } 138 139 public NamedElement getElement() { 140 ASTree tree = getASTree(); 141 if (tree == null) { 142 TypeClass typeClass = ((JavaModelPackage) refImmediatePackage()).getType(); 144 NamedElement result = typeClass.resolve(getName()); 145 if (result instanceof UnresolvedClass) { 146 JavaPackage pkg = ((JavaModelPackage) refImmediatePackage()).getJavaPackage().resolvePackage(getName()); 147 if (pkg != null) { 148 return pkg; 149 } 150 } 151 return result; 152 } else { 153 MDRParser parser = (MDRParser) tree.getASTContext(); 154 switch (tree.getType()) { 155 case ASTreeTypes.MULTI_PART_ID: 156 return (NamedElement) parser.getSemanticInfo(tree.getSubTrees()[1], this); 157 case ParserTokens.IDENTIFIER: 158 return (NamedElement) parser.getSemanticInfo(tree, this); 159 case ASTreeTypes.PRIMITIVE_TYPE: 160 return ((JavaModelPackage) refImmediatePackage()).getType().resolve(getName()); 161 default: 162 throw new IllegalArgumentException ("Illegal tree type " + tree.getType() + "."); } 164 } 165 } 166 167 170 public Element duplicate(JavaModelPackage targetExtent) { 171 return targetExtent.getMultipartId().createMultipartId( 172 getName(), 173 (MultipartId) duplicateElement(getParent(), targetExtent), 174 duplicateList(getTypeArguments(), targetExtent) 175 ); 176 } 177 178 protected void _delete() { 179 if (childrenInited) { 181 deleteChildren(typeArguments); 182 } 183 super._delete(); 187 } 188 189 public void replaceChild(Element oldElement,Element newElement) { 190 if (childrenInited) { 191 if (replaceObject(getTypeArguments(),oldElement,newElement)) return; 192 super.replaceChild(oldElement,newElement); 193 } 194 } 195 196 public List getChildren() { 197 List list = super.getChildren(); 198 list.addAll(getTypeArguments()); 199 return list; 200 } 201 202 public void fixImports(Element scope, Element original) { 203 MultipartId mpi=(MultipartId)original; 204 TypeReference resolved=JavaModelUtil.resolveImportsForType(scope,(Type)mpi.getElement()); 205 206 if (resolved!=null) { 207 setName(resolved.getName()); 208 setParent(null); 209 } 210 fixImports(scope,getTypeArguments(),((MultipartId)original).getTypeArguments()); 211 } 212 213 } 214 | Popular Tags |