1 11 package org.eclipse.jdt.internal.debug.core.hcr; 12 13 14 import java.io.BufferedReader ; 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.io.InputStreamReader ; 18 19 import org.eclipse.core.resources.IFile; 20 import org.eclipse.core.resources.IFileState; 21 import org.eclipse.core.resources.ResourcesPlugin; 22 import org.eclipse.core.runtime.CoreException; 23 import org.eclipse.jdt.core.ICompilationUnit; 24 import org.eclipse.jdt.core.dom.AST; 25 import org.eclipse.jdt.core.dom.ASTMatcher; 26 import org.eclipse.jdt.core.dom.ASTParser; 27 import org.eclipse.jdt.core.dom.CompilationUnit; 28 import org.eclipse.jdt.core.dom.MethodDeclaration; 29 30 42 public class CompilationUnitDelta { 43 44 47 private CompilationUnit fCurrentAst; 48 51 private CompilationUnit fPrevAst; 52 53 56 private ASTParser fParser = null; 57 58 61 private ASTMatcher fMatcher = null; 62 63 private boolean fHasHistory= false; 64 65 69 public CompilationUnitDelta(ICompilationUnit cu, long timestamp) throws CoreException { 70 71 if (cu.isWorkingCopy()) { 72 cu= cu.getPrimary(); 73 } 74 75 IFile file= (IFile) cu.getUnderlyingResource(); 77 78 IFileState[] states= file.getHistory(null); 80 if (states == null || states.length <= 0) { 81 return; 82 } 83 fHasHistory= true; 84 85 IFileState found= null; 86 for (int i= 0; i < states.length; i++) { 88 IFileState state= states[i]; 89 long d= state.getModificationTime(); 90 if (d < timestamp) { 91 found= state; 92 break; 93 } 94 } 95 96 if (found == null) { 97 found= states[states.length-1]; 98 } 99 100 InputStream oldContents= null; 101 InputStream newContents= null; 102 try { 103 oldContents= found.getContents(); 104 newContents= file.getContents(); 105 } catch (CoreException ex) { 106 return; 107 } 108 109 fPrevAst = parse(oldContents, cu); 110 fCurrentAst = parse(newContents, cu); 111 } 112 113 122 public boolean hasChanged(String methodName, String signature) { 123 if (!fHasHistory) { 124 return false; } 126 if (fPrevAst == null || fCurrentAst == null) { 127 return true; } 129 MethodSearchVisitor visitor = new MethodSearchVisitor(); 130 MethodDeclaration prev = findMethod(fPrevAst, visitor, methodName, signature); 131 if (prev != null) { 132 MethodDeclaration curr = findMethod(fCurrentAst, visitor, methodName, signature); 133 if (curr != null) { 134 return !getMatcher().match(prev, curr); 135 } 136 } 137 return true; 138 } 139 140 private MethodDeclaration findMethod(CompilationUnit cu, MethodSearchVisitor visitor, String name, String signature) { 141 visitor.setTargetMethod(name, signature); 142 cu.accept(visitor); 143 return visitor.getMatch(); 144 } 145 146 148 152 private CompilationUnit parse(InputStream input, ICompilationUnit cu) { 153 154 char[] buffer= readString(input); 155 if (buffer != null) { 156 if (fParser == null) { 157 fParser = ASTParser.newParser(AST.JLS3); 158 } 159 fParser.setSource(buffer); 160 fParser.setProject(cu.getJavaProject()); 161 fParser.setResolveBindings(true); 162 fParser.setKind(ASTParser.K_COMPILATION_UNIT); 163 fParser.setUnitName(cu.getElementName()); 164 return (CompilationUnit) fParser.createAST(null); 165 } 166 return null; 167 } 168 169 174 private ASTMatcher getMatcher() { 175 if (fMatcher == null) { 176 fMatcher = new ASTMatcher(); 177 } 178 return fMatcher; 179 } 180 181 184 private char[] readString(InputStream is) { 185 if (is == null) { 186 return null; 187 } 188 BufferedReader reader= null; 189 try { 190 StringBuffer buffer= new StringBuffer (); 191 char[] part= new char[2048]; 192 int read= 0; 193 reader= new BufferedReader (new InputStreamReader (is, ResourcesPlugin.getEncoding())); 194 195 while ((read= reader.read(part)) != -1) { 196 buffer.append(part, 0, read); 197 } 198 199 char[] b= new char[buffer.length()]; 200 buffer.getChars(0, b.length, b, 0); 201 return b; 202 203 } catch (IOException ex) { 204 } finally { 205 if (reader != null) { 206 try { 207 reader.close(); 208 } catch (IOException ex) { 209 } 210 } 211 } 212 return null; 213 } 214 } 215 | Popular Tags |