1 11 package org.eclipse.jdt.internal.ui.compare; 12 13 import java.util.Iterator ; 14 import java.util.Stack ; 15 16 import org.eclipse.jdt.core.dom.ASTNode; 17 import org.eclipse.jdt.core.dom.ASTVisitor; 18 import org.eclipse.jdt.core.dom.AnnotationTypeDeclaration; 19 import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration; 20 import org.eclipse.jdt.core.dom.CompilationUnit; 21 import org.eclipse.jdt.core.dom.EnumConstantDeclaration; 22 import org.eclipse.jdt.core.dom.EnumDeclaration; 23 import org.eclipse.jdt.core.dom.FieldDeclaration; 24 import org.eclipse.jdt.core.dom.ImportDeclaration; 25 import org.eclipse.jdt.core.dom.Initializer; 26 import org.eclipse.jdt.core.dom.MethodDeclaration; 27 import org.eclipse.jdt.core.dom.PackageDeclaration; 28 import org.eclipse.jdt.core.dom.SingleVariableDeclaration; 29 import org.eclipse.jdt.core.dom.Type; 30 import org.eclipse.jdt.core.dom.TypeDeclaration; 31 import org.eclipse.jdt.core.dom.VariableDeclarationFragment; 32 33 34 class JavaParseTreeBuilder extends ASTVisitor { 35 36 private char[] fBuffer; 37 private Stack fStack= new Stack (); 38 private JavaNode fImportContainer; 39 private boolean fShowCU; 40 41 45 JavaParseTreeBuilder(JavaNode root, char[] buffer, boolean showCU) { 46 fBuffer= buffer; 47 fShowCU= showCU; 48 fStack.clear(); 49 fStack.push(root); 50 } 51 52 public boolean visit(PackageDeclaration node) { 53 new JavaNode(getCurrentContainer(), JavaNode.PACKAGE, null, node.getStartPosition(), node.getLength()); 54 return false; 55 } 56 57 public boolean visit(CompilationUnit node) { 58 if (fShowCU) 59 push(JavaNode.CU, null, node.getStartPosition(), node.getLength()); 60 return true; 61 } 62 63 public void endVisit(CompilationUnit node) { 64 if (fShowCU) 65 pop(); 66 } 67 68 public boolean visit(TypeDeclaration node) { 69 push(node.isInterface() ? JavaNode.INTERFACE : JavaNode.CLASS, node.getName().toString(), node.getStartPosition(), node.getLength()); 70 return true; 71 } 72 73 public void endVisit(TypeDeclaration node) { 74 pop(); 75 } 76 77 public boolean visit(EnumDeclaration node) { 78 push(JavaNode.ENUM, node.getName().toString(), node.getStartPosition(), node.getLength()); 79 return true; 80 } 81 82 public void endVisit(EnumDeclaration node) { 83 pop(); 84 } 85 86 public boolean visit(AnnotationTypeDeclaration node) { 87 push(JavaNode.ANNOTATION, node.getName().toString(), node.getStartPosition(), node.getLength()); 88 return true; 89 } 90 91 public void endVisit(AnnotationTypeDeclaration node) { 92 pop(); 93 } 94 95 public boolean visit(AnnotationTypeMemberDeclaration node) { 96 push(JavaNode.METHOD, getSignature(node), node.getStartPosition(), node.getLength()); 97 return true; 98 } 99 100 public void endVisit(AnnotationTypeMemberDeclaration node) { 101 pop(); 102 } 103 104 public boolean visit(MethodDeclaration node) { 105 String signature= getSignature(node); 106 push(node.isConstructor() ? JavaNode.CONSTRUCTOR : JavaNode.METHOD, signature, node.getStartPosition(), node.getLength()); 107 return false; 108 } 109 110 public void endVisit(MethodDeclaration node) { 111 pop(); 112 } 113 114 public boolean visit(Initializer node) { 115 push(JavaNode.INIT, getCurrentContainer().getInitializerCount(), node.getStartPosition(), node.getLength()); 116 return false; 117 } 118 119 public void endVisit(Initializer node) { 120 pop(); 121 } 122 123 public boolean visit(ImportDeclaration node) { 124 int s= node.getStartPosition(); 125 int l= node.getLength(); 126 int declarationEnd= s + l; 127 if (fImportContainer == null) 128 fImportContainer= new JavaNode(getCurrentContainer(), JavaNode.IMPORT_CONTAINER, null, s, l); 129 String nm= node.getName().toString(); 130 if (node.isOnDemand()) 131 nm+= ".*"; new JavaNode(fImportContainer, JavaNode.IMPORT, nm, s, l); 133 fImportContainer.setLength(declarationEnd - fImportContainer.getRange().getOffset() + 1); 134 fImportContainer.setAppendPosition(declarationEnd + 2); return false; 136 } 137 138 public boolean visit(VariableDeclarationFragment node) { 139 String name= getFieldName(node); 140 ASTNode parent= node.getParent(); 141 push(JavaNode.FIELD, name, parent.getStartPosition(), parent.getLength()); 142 return false; 143 } 144 145 public void endVisit(VariableDeclarationFragment node) { 146 pop(); 147 } 148 149 public boolean visit(EnumConstantDeclaration node) { 150 push(JavaNode.FIELD, node.getName().toString(), node.getStartPosition(), node.getLength()); 151 return false; 152 } 153 154 public void endVisit(EnumConstantDeclaration node) { 155 pop(); 156 } 157 158 160 164 private void push(int type, String name, int declarationStart, int length) { 165 166 while (declarationStart > 0) { 167 char c= fBuffer[declarationStart - 1]; 168 if (c != ' ' && c != '\t') 169 break; 170 declarationStart--; 171 length++; 172 } 173 174 JavaNode node= new JavaNode(getCurrentContainer(), type, name, declarationStart, length); 175 if (type == JavaNode.CU) 176 node.setAppendPosition(declarationStart + length + 1); 177 else 178 node.setAppendPosition(declarationStart + length); 179 180 fStack.push(node); 181 } 182 183 187 private void pop() { 188 fStack.pop(); 189 } 190 191 private JavaNode getCurrentContainer() { 192 return (JavaNode) fStack.peek(); 193 } 194 195 private String getFieldName(VariableDeclarationFragment node) { 196 StringBuffer buffer= new StringBuffer (); 197 buffer.append(node.getName().toString()); 198 ASTNode parent= node.getParent(); 199 if (parent instanceof FieldDeclaration) { 200 FieldDeclaration fd= (FieldDeclaration) parent; 201 buffer.append(" : "); buffer.append(getType(fd.getType())); 203 } 204 return buffer.toString(); 205 } 206 207 private String getSignature(MethodDeclaration node) { 208 StringBuffer buffer= new StringBuffer (); 209 buffer.append(node.getName().toString()); 210 buffer.append('('); 211 boolean first= true; 212 Iterator iterator= node.parameters().iterator(); 213 while (iterator.hasNext()) { 214 Object parameterDecl= iterator.next(); 215 if (parameterDecl instanceof SingleVariableDeclaration) { 216 SingleVariableDeclaration svd= (SingleVariableDeclaration) parameterDecl; 217 if (!first) 218 buffer.append(", "); buffer.append(getType(svd.getType())); 220 if (svd.isVarargs()) 221 buffer.append("..."); first= false; 223 } 224 } 225 buffer.append(')'); 226 return buffer.toString(); 227 } 228 229 private String getSignature(AnnotationTypeMemberDeclaration node) { 230 StringBuffer buffer= new StringBuffer (); 231 buffer.append(node.getName().toString()); 232 buffer.append('('); 233 buffer.append(')'); 234 return buffer.toString(); 235 } 236 237 private String getType(Type type) { 238 String name= type.toString(); 239 int pos= name.lastIndexOf('.'); 240 if (pos >= 0) 241 name= name.substring(pos + 1); 242 return name; 243 } 244 } 245 | Popular Tags |