1 11 package org.eclipse.jdt.internal.core.jdom; 12 13 import org.eclipse.jdt.core.IJavaElement; 14 import org.eclipse.jdt.core.IType; 15 import org.eclipse.jdt.core.jdom.*; 16 import org.eclipse.jdt.internal.core.util.Messages; 17 import org.eclipse.jdt.internal.core.util.CharArrayBuffer; 18 import org.eclipse.jdt.internal.core.util.Util; 19 28 class DOMInitializer extends DOMMember implements IDOMInitializer { 29 30 35 protected String fBody; 36 37 41 protected int[] fBodyRange; 42 43 46 DOMInitializer() { 47 } 49 76 DOMInitializer(char[] document, int[] sourceRange, int[] commentRange, int flags, int[] modifierRange, int bodyStartPosition) { 77 super(document, sourceRange, null, new int[]{-1, -1}, commentRange, flags, modifierRange); 78 fBodyRange= new int[2]; 79 fBodyRange[0]= bodyStartPosition; 80 fBodyRange[1]= sourceRange[1]; 81 setHasBody(true); 82 setMask(MASK_DETAILED_SOURCE_INDEXES, true); 83 } 84 97 DOMInitializer(char[] document, int[] sourceRange, int flags) { 98 this(document, sourceRange, new int[] {-1, -1}, flags, new int[] {-1, -1}, -1); 99 setMask(MASK_DETAILED_SOURCE_INDEXES, false); 100 101 } 102 105 protected void appendMemberBodyContents(CharArrayBuffer buffer) { 106 if (hasBody()) { 107 buffer 108 .append(getBody()) 109 .append(fDocument, fBodyRange[1] + 1, fSourceRange[1] - fBodyRange[1]); 110 } else { 111 buffer.append("{}").append(Util.getLineSeparator(buffer.toString(), null)); } 113 } 114 117 protected void appendMemberDeclarationContents(CharArrayBuffer buffer) { 118 } 120 123 protected void appendSimpleContents(CharArrayBuffer buffer) { 124 buffer.append(fDocument, fSourceRange[0], fNameRange[0] - fSourceRange[0]); 126 buffer.append(fName); 128 buffer.append(fDocument, fNameRange[1] + 1, fSourceRange[1] - fNameRange[1]); 130 } 131 134 public String getBody() { 135 becomeDetailed(); 136 if (hasBody()) { 137 if (fBody != null) { 138 return fBody; 139 } else { 140 return new String (fDocument, fBodyRange[0], fBodyRange[1] + 1 - fBodyRange[0]); 141 } 142 } else { 143 return null; 144 } 145 } 146 149 protected DOMNode getDetailedNode() { 150 return (DOMNode)getFactory().createInitializer(getContents()); 151 } 152 155 public IJavaElement getJavaElement(IJavaElement parent) throws IllegalArgumentException { 156 if (parent.getElementType() == IJavaElement.TYPE) { 157 int count = 1; 158 IDOMNode previousNode = getPreviousNode(); 159 while (previousNode != null) { 160 if (previousNode instanceof DOMInitializer) { 161 count++; 162 } 163 previousNode = previousNode.getPreviousNode(); 164 } 165 return ((IType) parent).getInitializer(count); 166 } else { 167 throw new IllegalArgumentException (Messages.element_illegalParent); 168 } 169 } 170 173 protected int getMemberDeclarationStartPosition() { 174 return fBodyRange[0]; 175 } 176 179 public int getNodeType() { 180 return IDOMNode.INITIALIZER; 181 } 182 188 public boolean isSignatureEqual(IDOMNode node) { 189 return false; 190 } 191 194 protected DOMNode newDOMNode() { 195 return new DOMInitializer(); 196 } 197 200 protected void offset(int offset) { 201 super.offset(offset); 202 offsetRange(fBodyRange, offset); 203 } 204 207 public void setBody(String body) { 208 becomeDetailed(); 209 fBody= body; 210 setHasBody(body != null); 211 fragment(); 212 } 213 216 public void setName(String name) { 217 } 219 222 protected void shareContents(DOMNode node) { 223 super.shareContents(node); 224 DOMInitializer init= (DOMInitializer)node; 225 fBody= init.fBody; 226 fBodyRange= rangeCopy(init.fBodyRange); 227 } 228 231 public String toString() { 232 return "INITIALIZER"; } 234 } 235 | Popular Tags |