1 11 package org.eclipse.jdt.internal.core; 12 13 import java.util.Arrays ; 14 import java.util.Hashtable ; 15 16 import org.eclipse.jdt.core.JavaCore; 17 import org.eclipse.jdt.core.dom.ASTNode; 18 import org.eclipse.jdt.internal.core.SortElementBuilder.SortElement; 19 20 24 public abstract class SortJavaElement implements Comparable { 25 26 public static final int COMPILATION_UNIT = 1; 27 public static final int TYPE = 2; 28 public static final int CLASS = 4; 29 public static final int INTERFACE = 8; 30 public static final int FIELD = 16; 31 public static final int INITIALIZER = 32; 32 public static final int METHOD = 64; 33 public static final int CONSTRUCTOR = 128; 34 public static final int MULTIPLE_FIELD = 256; 35 36 SortElementBuilder builder; 37 38 protected static final String LINE_SEPARATOR = System.getProperty("line.separator"); public static final String CORRESPONDING_ELEMENT = "corresponding_element"; 41 Hashtable options; 42 43 protected int id; 44 protected int sourceStart; 45 protected int newSourceStart; 46 protected int modifiers; 47 protected String superclass; 48 protected String [] superInterfaces; 49 50 protected String [] parametersNames; 51 protected String [] parametersTypes; 52 protected String [] thrownExceptions; 53 protected String returnType; 54 protected String name; 55 protected String type; 56 protected int fieldCounter; 57 protected SortElementBuilder.SortFieldDeclaration[] innerFields; 58 protected ASTNode[] astNodes; 59 60 protected int sourceEnd; 61 protected int nameSourceStart; 62 protected SortElement[] children; 63 protected int children_count; 64 protected SortElement firstChildBeforeSorting; 65 protected SortElement lastChildBeforeSorting; 66 protected int declarationStart; 67 protected int declarationSourceEnd; 68 69 SortJavaElement(SortElementBuilder builder) { 70 this.builder = builder; 71 this.options = JavaCore.getOptions(); 72 } 73 76 public int compareTo(Object o) { 77 return this.builder.comparator.compare(this, o); 78 } 79 80 protected void addChild(SortElement sortElement) { 81 if (this.children_count == 0) { 82 this.children = new SortElement[3]; 83 } else if (this.children_count == this.children.length) { 84 System.arraycopy(this.children, 0, this.children = new SortElement[this.children_count * 2], 0, this.children_count); 85 } 86 this.children[this.children_count++] = sortElement; 87 } 88 89 protected void closeCollections() { 90 int length = this.children_count; 91 if (length != 0 && length != this.children.length) { 92 System.arraycopy(this.children, 0, this.children= new SortElement[length], 0, length); 93 } 94 } 95 96 abstract void display(StringBuffer buffer, int tab); 97 98 protected void generateSource(StringBuffer buffer) { 99 this.newSourceStart = buffer.length(); 100 } 101 102 abstract void mapPositions(); 103 104 public String toString(int tab) { 105 StringBuffer buffer = new StringBuffer (); 106 display(buffer, tab); 107 if (this.children != null) { 108 buffer 109 .append(tab(tab)) 110 .append("CHILDREN ------------------------------" + LINE_SEPARATOR); for (int i = 0; i < this.children_count; i++) { 112 buffer.append(this.children[i].toString(tab + 1)); 113 buffer.append(LINE_SEPARATOR); 114 } 115 } 116 return buffer.toString(); 117 } 118 119 protected char[] tab(int tab) { 120 char[] tabs = new char[tab]; 121 Arrays.fill(tabs, '\t'); 122 return tabs; 123 } 124 125 public String toString() { 126 return toString(0); 127 } 128 129 protected void sort() { 130 if (this.children != null) { 131 this.firstChildBeforeSorting = children[0]; 132 this.lastChildBeforeSorting = children[this.children_count - 1]; 133 switch(this.id) { 134 case CLASS | TYPE : 135 case INTERFACE | TYPE : 136 case COMPILATION_UNIT : 137 this.astNodes = convertChildren(); 138 Arrays.sort(astNodes, this.builder.comparator); 139 } 140 for (int i = 0, max = this.children_count; i < max; i++) { 141 children[i].sort(); 142 } 143 } 144 } 145 146 private ASTNode[] convertChildren() { 147 ASTNode[] convertedNodes = new ASTNode[this.children_count]; 148 for (int i = 0, max = this.children_count; i < max; i++) { 149 SortElementBuilder.SortElement currentElement = this.children[i]; 150 ASTNode newNode = currentElement.convert(); 151 newNode.setProperty(CORRESPONDING_ELEMENT, currentElement); 152 convertedNodes[i] = newNode; 153 } 154 return convertedNodes; 155 } 156 } 157 | Popular Tags |