如何识别java文件中方法的修改?
我已经创建了一个插件,需要在其中查找方法内部的更改。我已经使用JavaCore.addElementChangedListener来监听java文件中发生的更改。但是我无法获得方法内部的更改。我想获取方法内部的更改。我已经完成了,但无法获取子树和更改文件。实际上我无法得到实际的概念。您能帮我获取方法中的更改吗?最后,我找到了一些解决方案来识别方法中发生的更改,但它同时给出了一些信息。我将其存储在答案中指定的哈希映射中,通过使用IResourceChangeEvent.POST_CHANGE,我们可以获得用户保存文件之前发生的更改集合如何识别java文件中方法的修改?,java,eclipse-plugin,eclipse-pde,eclipse-jdt,Java,Eclipse Plugin,Eclipse Pde,Eclipse Jdt,我已经创建了一个插件,需要在其中查找方法内部的更改。我已经使用JavaCore.addElementChangedListener来监听java文件中发生的更改。但是我无法获得方法内部的更改。我想获取方法内部的更改。我已经完成了,但无法获取子树和更改文件。实际上我无法得到实际的概念。您能帮我获取方法中的更改吗?最后,我找到了一些解决方案来识别方法中发生的更改,但它同时给出了一些信息。我将其存储在答案中指定的哈希映射中,通过使用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;
}
}具体来说,您希望找到哪些更改?你可能很清楚,但我们不清楚。你的意思是修改本地字段吗?到数据模型的某些部分?如果用户更改了方法中的某些逻辑,我需要更改发生的方法名。@AndrzejDoyle:有没有办法获得方法名?我现在非常需要它。请帮我解决这个问题你是什么意思?字段值还是源代码?我认为您试图做的没有多大意义。为了运行应用程序,源代码被编译成字节码,并执行那些特定的指令。如果源文件过去是不同的,那么运行的程序不可能知道这一点。它只执行最新版本代码中的指令。