1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import java.util.*; 22 import javax.jmi.reflect.RefFeatured; 23 import org.netbeans.jmi.javamodel.ArrayInitialization; 24 import org.netbeans.jmi.javamodel.AttributeValue; 25 import org.netbeans.jmi.javamodel.Element; 26 import org.netbeans.jmi.javamodel.JavaModelPackage; 27 import org.netbeans.lib.java.parser.ASTree; 28 import org.netbeans.lib.java.parser.ASTreeTypes; 29 import org.netbeans.mdr.storagemodel.StorableObject; 30 import org.netbeans.modules.javacore.parser.ASTProvider; 31 import org.netbeans.modules.javacore.parser.AnnotationInfo; 32 import org.netbeans.modules.javacore.parser.AnnotationValueInfo; 33 34 38 public abstract class ArrayInitializationImpl extends TransientElement implements ArrayInitialization { 39 private LightAttrList elementValues = null; 40 41 42 public ArrayInitializationImpl(StorableObject o) { 43 super(o); 44 } 45 46 public List getElementValues() { 47 if (!childrenInited) { 48 initChildren(); 49 } 50 return elementValues; 51 } 52 53 public List getChildren() { 54 return new ArrayList(getElementValues()); 55 } 56 57 protected void initChildren() { 58 childrenInited = false; 59 ASTree tree = getASTree(); 60 if (tree != null) { 61 ASTree[] parts = tree.getSubTrees(); 62 elementValues = createChildrenList(elementValues, "elementValues", parts[0], ASTreeTypes.VARIABLE_INITIALIZERS, CHANGED_ELEMENT_VALUES, false); } 64 childrenInited = true; 65 } 66 67 protected MetadataElement createElement(ASTree tree) { 68 if (tree == null) return null; 69 if (tree.getType() == ASTreeTypes.ANNOTATION) { 70 RefFeatured parent = refImmediateComposite(); 71 if (parent instanceof AttributeValueImpl) { 72 AttributeValueImpl attrVal = (AttributeValueImpl)parent; 73 AnnotationValueInfo info = (AnnotationValueInfo) attrVal.getElementInfo(); 74 Object [] arrEls = (Object [])info.value; 75 76 for (int i=0;i<arrEls.length;i++) { 77 Object el=arrEls[i]; 78 79 if (el instanceof AnnotationInfo) { 80 AnnotationInfo annInfo = (AnnotationInfo) el; 81 82 Object infoTree = annInfo.getASTree().get(); 83 if (infoTree == null) { 84 infoTree = annInfo.refreshASTree(); 85 } 86 if (infoTree == tree) { 87 return createElement(annInfo); 88 } 89 } 90 } 91 throw new IllegalArgumentException ("Annotation tree not found; resource: " + getResource().getName()); } 93 } 94 return super.createElement(tree); 95 } 96 97 public String getRawText() { 98 StringBuffer buf = new StringBuffer (); 99 List values = getElementValues(); 100 formatElementPart(ARRAY_OPEN_CURLY, buf); 101 boolean isAttributeValue = refImmediateComposite() instanceof AttributeValue; 102 MetadataElement composite = (MetadataElement) refImmediateComposite(); 103 if (isAttributeValue) { 104 buf.append('\n'); 105 buf.append(composite.getIndentation()); 106 } 107 Iterator iter = values.iterator(); 108 while (iter.hasNext()) { 109 MetadataElement val = (MetadataElement) iter.next(); 110 buf.append(val.getSourceText()); 111 if (iter.hasNext()) { 112 if (isAttributeValue) { 113 buf.append(",\n"); 114 buf.append(composite.getIndentation()); 115 } else { 116 formatElementPart(COMMA, buf); 117 } 118 } 119 } 120 if (isAttributeValue) { 121 buf.append('\n'); 122 buf.append(((MetadataElement) composite.refImmediateComposite()).getIndentation()); 123 } 124 formatElementPart(ARRAY_CLOSE_CURLY, buf); 125 return buf.toString(); 126 } 127 128 public void getDiff(List diff) { 129 ASTProvider parser = getParser(); 130 ASTree tree = getASTree(); 131 ASTree[] children = tree.getSubTrees(); 132 133 getCollectionDiff(diff, parser, CHANGED_ELEMENT_VALUES, children[0], ASTreeTypes.VARIABLE_INITIALIZERS, getElementValues(), parser.getToken(tree.getLastToken()).getStartOffset(), formatElementPart(COMMA)); 134 } 135 136 void setData(List elementValues) { 137 this.elementValues = createChildrenList("elementValues", elementValues, CHANGED_ELEMENT_VALUES); } 139 140 protected void _delete() { 141 if (childrenInited) { 143 deleteChildren(elementValues); 144 } 145 super._delete(); 149 } 150 151 public void replaceChild(Element oldElement,Element newElement) { 152 if (childrenInited) { 153 replaceObject(getElementValues(),oldElement,newElement); 154 } 155 } 156 157 public Element duplicate(JavaModelPackage targetExtent) { 158 return targetExtent.getArrayInitialization().createArrayInitialization( 159 duplicateList(getElementValues(), targetExtent)); 160 } 161 } 162 | Popular Tags |