Search code examples
javaeclipse-plugineclipse-pdeeclipse-jdt

how to identify the modification in a method inside a java file?


I have created a plugin where i am in a need of finding the changes inside the method. I have used JavaCore.addElementChangedListener to listen to the changes happening in the java file. but i cant get the changes inside the method. I want to get the changes inside the method .I have gone through the How to capture changes inside methods in an Eclipse plugin but i am not able to get the subtrees and changes fileds . Actually am not able to get the actual concept . can you please help me to get the changes in the method?


Solution

  • Finally some how I found the solution to identify the changes happening inside the method but it gives simultaneously . I store it in a hash map as specified in the How to capture changes inside methods in an Eclipse plugin answer and by using IResourceChangeEvent.POST_CHANGE we can get the collection of changes happened up to the user saves the file.

    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;
                       }
                   });
    
           }
       }
    }
    

    When the user invokes save option we can get the collection of method name and its location from the hash map

     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; 
        }
    

    }