1 11 package org.eclipse.jdt.internal.debug.core.hcr; 12 13 14 import java.util.Stack ; 15 16 import org.eclipse.jdt.internal.compiler.SourceElementRequestorAdapter; 17 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit; 18 19 class JavaParseTreeBuilder extends SourceElementRequestorAdapter implements ICompilationUnit { 20 21 private static final boolean SHOW_COMPILATIONUNIT= true; 22 23 private char[] fBuffer; 24 private JavaNode fImportContainer; 25 private Stack fStack= new Stack (); 26 27 31 JavaParseTreeBuilder(JavaNode root, char[] buffer) { 32 fImportContainer= null; 33 fStack.clear(); 34 fStack.push(root); 35 fBuffer= buffer; 36 } 37 38 42 public char[] getContents() { 43 return fBuffer; 44 } 45 46 49 public char[] getFileName() { 50 return new char[0]; 51 } 52 53 56 public char[] getMainTypeName() { 57 return new char[0]; 58 } 59 60 63 public char[][] getPackageName() { 64 return null; 65 } 66 67 69 public void enterCompilationUnit() { 70 if (SHOW_COMPILATIONUNIT) 71 push(JavaNode.CU, null, 0); 72 } 73 74 public void exitCompilationUnit(int declarationEnd) { 75 if (SHOW_COMPILATIONUNIT) 76 pop(declarationEnd); 77 } 78 79 public void acceptPackage(int declarationStart, int declarationEnd, char[] p3) { 80 push(JavaNode.PACKAGE, null, declarationStart); 81 pop(declarationEnd); 82 } 83 84 public void acceptImport(int declarationStart, int declarationEnd, char[] name, boolean onDemand, int modifiers) { 85 int length= declarationEnd-declarationStart+1; 86 if (fImportContainer == null) 87 fImportContainer= new JavaNode(getCurrentContainer(), JavaNode.IMPORT_CONTAINER, null, declarationStart, length); 88 String nm= new String (name); 89 if (onDemand) 90 nm+= ".*"; new JavaNode(fImportContainer, JavaNode.IMPORT, nm, declarationStart, length); 92 fImportContainer.setLength(declarationEnd-fImportContainer.getStart()+1); 93 } 94 95 public void enterClass(int declarationStart, int p2, char[] name, int p4, int p5, char[] p6, char[][] p7) { 96 push(JavaNode.CLASS, new String (name), declarationStart); 97 } 98 99 public void exitClass(int declarationEnd) { 100 pop(declarationEnd); 101 } 102 103 public void enterInterface(int declarationStart, int p2, char[] name, int p4, int p5, char[][] p6) { 104 push(JavaNode.INTERFACE, new String (name), declarationStart); 105 } 106 107 public void exitInterface(int declarationEnd) { 108 pop(declarationEnd); 109 } 110 111 public void enterInitializer(int declarationSourceStart, int modifiers) { 112 push(JavaNode.INIT, getCurrentContainer().getInitializerCount(), declarationSourceStart); 113 } 114 115 public void enterConstructor(int declarationStart, int p2, char[] name, int p4, int p5, char[][] parameterTypes, char[][] p7, char[][] p8) { 116 push(JavaNode.CONSTRUCTOR, getSignature(name, parameterTypes), declarationStart); 117 } 118 119 public void exitConstructor(int declarationEnd) { 120 pop(declarationEnd); 121 } 122 123 public void enterMethod(int declarationStart, int p2, char[] p3, char[] name, int p5, int p6, char[][] parameterTypes, char[][] p8, char[][] p9){ 124 push(JavaNode.METHOD, getSignature(name, parameterTypes), declarationStart); 125 } 126 127 public void exitMethod(int declarationEnd) { 128 pop(declarationEnd); 129 } 130 131 public void enterField(int declarationStart, int p2, char[] p3, char[] name, int p5, int p6) { 132 push(JavaNode.FIELD, new String (name), declarationStart); 133 } 134 135 public void exitField(int initializationStart, int declarationEnd, int declarationSourceEnd) { 136 pop(declarationEnd); 137 } 138 139 private JavaNode getCurrentContainer() { 140 return (JavaNode) fStack.peek(); 141 } 142 143 146 private void push(int type, String name, int declarationStart) { 147 148 while (declarationStart > 0) { 149 char c= fBuffer[declarationStart-1]; 150 if (c != ' ' && c != '\t') 151 break; 152 declarationStart--; 153 } 154 155 fStack.push(new JavaNode(getCurrentContainer(), type, name, declarationStart, 0)); 156 } 157 158 162 private void pop(int declarationEnd) { 163 JavaNode current= getCurrentContainer(); 164 current.setLength(declarationEnd - current.getStart() + 1); 165 fStack.pop(); 166 } 167 168 171 public String getSignature(char[] name, char[][] parameterTypes) { 172 StringBuffer buffer= new StringBuffer (); 173 buffer.append(name); 174 buffer.append('('); 175 if (parameterTypes != null) { 176 for (int p= 0; p < parameterTypes.length; p++) { 177 String parameterType= new String (parameterTypes[p]); 178 179 int pos= parameterType.lastIndexOf('.'); 182 if (pos >= 0) 183 parameterType= parameterType.substring(pos+1); 184 186 buffer.append(parameterType); 187 if (p < parameterTypes.length-1) 188 buffer.append(", "); } 190 } 191 buffer.append(')'); 192 return buffer.toString(); 193 } 194 195 198 public void exitInitializer(int declarationEnd) { 199 pop(declarationEnd); 200 } 201 202 } 203 | Popular Tags |