我创建了一个插件,我需要在方法中找到更改。我使用JavaCore.addElementChangedListener来监听java文件中发生的更改。但我无法得到方法内的变化。我想在方法内部进行更改。我已经完成了How to capture changes inside methods in an Eclipse plugin,但我无法获得子树和更改文件。实际上我无法得到实际的概念。你能帮我改变方法吗?
答案 0 :(得分:1)
最后,我找到了一些解决方案,以确定方法内部发生的变化,但同时给出了。我将它存储在How to capture changes inside methods in an Eclipse plugin答案中指定的哈希映射中,并且通过使用IResourceChangeEvent.POST_CHANGE,我们可以获得在用户保存文件之前发生的更改集合。
public void elementChanged(ElementChangedEvent event) {
IJavaElementDelta delta= event.getDelta();
if (delta != null) {
delta.getCompilationUnitAST().accept(new ASTVisitor() {
@Override
public boolean visit(MethodDeclaration node) {
String name = ( (TypeDeclaration) node.getParent()).getName()
.getFullyQualifiedName() + "." + node.getName().getFullyQualifiedName();
boolean changedmethodvalue = false;
if (subtrees.containsKey(name)){
changedmethodvalue = !node.subtreeMatch(new ASTMatcher(),subtrees.get(Name));
if(changedmethodvalue){
System.out.println("method changed"+Name+":"+changedmethodvalue);
/**
* Store the changed method inside the hash map for future reflection.
*/
changed.put(Name, (IMethod) node.resolveBinding().getJavaElement());
/**
* setting up the hash map value each time changes happened.
*
*/
ModificationStore.setChanged(changed);
}
}
else{
// No earlier entry found, definitely changed
methodHasChanged = true;
System.out.println("A new method is added");
}
}
/**
* updating the subtree structure
*/
subtrees.put(mName, node);
return true;
}
});
}
}
}
当用户调用save选项时,我们可以从哈希映射中获取方法名称及其位置的集合
public class InvokeSynchronizer implements IResourceDeltaVisitor{
private static HashMap<String, IMethod> methodtoinvoke = new HashMap<String, IMethod>();
public boolean visit(IResourceDelta delta) {
IResource res = delta.getResource();
switch (delta.getKind()) {
case IResourceDelta.ADDED:
System.out.println("ADDED: ");
break;
case IResourceDelta.CHANGED:
/**
* methodtoinvoke is a hash map values got from the modification store class.
*/
methodtoinvoke=ModificationStore.getChanged();
Iterator it = methodtoinvoke.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
// System.out.println(pairs.getKey() + " = " + pairs.getValue());
IMethod methods=(IMethod) pairs.getValue();
//IResource resource=(IResource) methods;
System.out.println("I resource value"+res);
System.out.println("\nlocation of the method:"+methods.getParent().getResource().toString());
System.out.println("\n\nmethod name ::"+methods.getElementName());
it.remove(); // avoids a ConcurrentModificationException
}}
return true;
}
}