List of usage examples for org.eclipse.jdt.core IJavaElementDelta getMovedToElement
public IJavaElement getMovedToElement();
null if the #F_MOVED_TO change flag is not set. From source file:edu.buffalo.cse.green.JavaModelListener.java
License:Open Source License
/** * Finds pairs of element changes that triggered "move" events. The types of * elements that are considered eligible for such an event are: * <code>IJavaProject</code> * <code>IPackageFragment</code> * <code>ICompilationUnit</code> * /*from w ww .j av a 2 s .c o m*/ * This is checked using the getMovedFromElement() and getMovedToElement() * methods in the delta. * * There are other <code>IJavaElement</code> subclasses that may trigger * move events; however, in that context, move events do not pertain to the * <code>DiagramEditor</code>. * * @param added - A list of elements that were added. * @param removed - A list of elements that were removed. * @return A mapping of all elements that triggered "move" events in the * <code>IJavaElementDelta</code> hierarchy. */ private HashMap<IJavaElement, IJavaElement> extractMovedElements(List<IJavaElementDelta> added, List<IJavaElementDelta> removed) { HashMap<IJavaElement, IJavaElement> moved = new HashMap<IJavaElement, IJavaElement>(); List<IJavaElementDelta> notAdded = new ArrayList<IJavaElementDelta>(); List<IJavaElementDelta> notRemoved = new ArrayList<IJavaElementDelta>(); for (IJavaElementDelta toDelta : added) { // ignore this element if it wasn't moved to somewhere if (toDelta.getMovedFromElement() != null) { IJavaElement newElement = toDelta.getElement(); // see if there's an element that has // the "newElement" returned by // toDelta.getMovedToElement() for (IJavaElementDelta fromDelta : removed) { // if this element was moved from "newElement"... if (sameElements(fromDelta.getMovedToElement(), newElement)) { IJavaElement oldElement = fromDelta.getElement(); // and this element was moved from "oldElement" if (sameElements(toDelta.getMovedFromElement(), oldElement)) { /* * remove both elements from their respective lists * and put them in the move map */ moved.put(oldElement, newElement); notAdded.add(toDelta); notRemoved.add(fromDelta); } } } } } added.removeAll(notAdded); removed.removeAll(notRemoved); return moved; }
From source file:edu.wpi.cs.jburge.SEURAT.SEURATElementChangedManager.java
License:Open Source License
/** * Determines whether a change occurred to an associated artifact, and if it did, * handles the change. It basically works by saving the first element it gets and * mapping it to a corresponding element later on (provided that the elements are actually * associated with some alternative). Two exceptions come up: one, if it's a compilation unit, * we just use the getMovedToElement() method to take care of the mapping for us (we don't have * access to this for single methods due to the confusing/arcane way they decided to code * java element changes). The other exception is also for compilation units- we have to manually check * the java elements attached to it such as methods and fields to see if any of those are associated * and handle the changes if they are, because we don't get deltas for them. * // w ww .j a va 2 s.c o m * @param change - the possible change, an IJavaElementDelta */ private void handleChange(IJavaElementDelta change) { System.out .println("HandleChange: " + change.getElement() + " " + change.toString() + " " + change.getKind()); //System.out.println("Saved Delta: " + savedDelta.getElement() + " " + savedDelta.toString() + " " + savedDelta.getKind()); IJavaElement movedTo = change.getMovedToElement(); if (change.getKind() == IJavaElementDelta.ADDED || change.getKind() == IJavaElementDelta.REMOVED) { if (change.getKind() == IJavaElementDelta.REMOVED && savedDelta == null && movedTo == null) { checkDeltaAndSave(change); } else if (change.getKind() == IJavaElementDelta.ADDED && savedDelta == null && movedTo == null) { // always save this kind of delta, can't check if in DB without reference to removed delta savedDelta = change; System.out.println("Saved a delta"); } else { IJavaElement oldElt = null; IJavaElement newElt = null; if (change.getKind() == IJavaElementDelta.ADDED) { // get the saved added delta oldElt = savedDelta.getElement(); newElt = change.getElement(); } else { // change.getKind() == IJavaElementDelta.REMOVED if (movedTo != null) { // process the move checkDeltaAndSave(change); // in this case we're not really saving, just checking newElt = movedTo; oldElt = change.getElement(); } else { // check the removed delta, then get the saved added delta checkDeltaAndSave(change); if (savedDelta != null) { // sanity check newElt = savedDelta.getElement(); oldElt = change.getElement(); } } } // Get the saved association and the old artName Association ourAssoc = savedAssoc; String oldArtName = oldElt.getElementName(); // Reset the saved delta and association savedDelta = null; savedAssoc = null; // Make sure the association is in the database if (ourAssoc != null && ourAssoc.getAlt() != -1) { // Find the artifact itself IResource newResource = null; int cstart = 0; IField[] fields = null; IMethod[] methods = null; try { ICompilationUnit compUnit = null; if (newElt.getElementType() != IJavaElement.COMPILATION_UNIT) { newResource = newElt.getUnderlyingResource(); compUnit = (ICompilationUnit) newElt.getAncestor(IJavaElement.COMPILATION_UNIT); } else { newResource = newElt.getCorrespondingResource(); compUnit = (ICompilationUnit) newElt; } IType[] myTypes = compUnit.getTypes(); boolean found = false; int i = 0; while ((!found) && i < myTypes.length) { //selected item was the class itself if (newElt.getElementType() == IJavaElement.COMPILATION_UNIT) { if (myTypes[i].isClass()) { found = true; cstart = myTypes[i].getNameRange().getOffset(); fields = myTypes[i].getFields(); methods = myTypes[i].getMethods(); } } else if (newElt.getElementType() == IJavaElement.FIELD) { IField[] myFields = myTypes[i].getFields(); for (int j = 0; j < myFields.length; j++) { if (myFields[j].getElementName().compareTo(newElt.getElementName()) == 0) { found = true; cstart = myFields[j].getNameRange().getOffset(); } } } else if (newElt.getElementType() == IJavaElement.METHOD) { IMethod[] myMethods = myTypes[i].getMethods(); for (int j = 0; j < myMethods.length; j++) { if (myMethods[j].getElementName().compareTo(newElt.getElementName()) == 0) { found = true; cstart = myMethods[j].getNameRange().getOffset(); } } } i++; } //end while } catch (JavaModelException jme) { System.err.println(jme.toString()); } System.out.println("DEBUG: newresource " + newResource.getName() + " cstart " + cstart); // Get new values for association data String newRes = newResource.getName(); String newArtName = newElt.getElementName(); String newArt = newElt.getHandleIdentifier(); IResource oldResource = null; // Now, the time-consuming part. If this is a compilation unit, we need to go through // all of its fields and methods and if they have associations, update them. if (newElt.getElementType() == IJavaElement.COMPILATION_UNIT) { for (int i = 0; i < fields.length; i++) { checkAssocAndUpdate(fields[i], newRes, null); } for (int i = 0; i < methods.length; i++) { String oldSubArtName = methods[i].getElementName(); String oldSubArtNameJava = oldSubArtName + ".java"; if ((oldSubArtName == oldArtName || oldSubArtNameJava == oldArtName) && oldArtName != newArtName) { // Names of old comp unit and old method are same; this is a constructor, and its name has changed checkAssocAndUpdate(methods[i], newRes, oldSubArtName); } else { checkAssocAndUpdate(methods[i], newRes, null); } } } // If we're not dealing with a compilation unit, a single method/field was renamed or moved. // If it was moved we need to know the old resource to change resource properties (it might not // have rationale anymore) so we save that now. else { oldResource = oldElt.getResource(); } // Update them ourAssoc.setArtifact(newArt); ourAssoc.setResource(newRes); ourAssoc.setArtName(newArtName); System.out.println("oldArt became " + newArt); System.out.println("oldRes became " + newRes); System.out.println(oldArtName + " became " + newArtName); // Send the update to the DB ourAssoc.toDatabase(oldArtName); // Update the marker- this will sometimes give us "resource tree locked" exceptions. // However, in the cases where it does, we're making changes to a compilation unit // and the markers will stay where they are. try { IMarker ratM = newResource.createMarker("SEURAT.ratmarker"); String markD = ourAssoc.getMsg(); ratM.setAttribute(IMarker.MESSAGE, markD); ratM.setAttribute(IMarker.CHAR_START, cstart); ratM.setAttribute(IMarker.CHAR_END, cstart + 1); ratM.setAttribute(IMarker.SEVERITY, 0); System.out.println(cstart); Alternative ourAlt = (Alternative) RationaleDB.getRationaleElement(ourAssoc.getAlt(), RationaleElementType.ALTERNATIVE); ratM.setAttribute("alternative", ourAlt.getName()); } catch (CoreException e) { System.err.println(e.toString()); } // If the element was moved, remove the persistent property from the old resource. // This is a hack, because if there is associated rationale remaining in the old resource // it still removes the decorator, but the next time restoreAssociations is called it will // be back. I couldn't see a better way to do it short of rewriting the decorator and/or resource properties manager. if (oldResource != null) { // it will be null for a compilation unit because we didn't set it SEURATResourcePropertiesManager.addPersistentProperty(oldResource, "Rat", "false"); SEURATDecoratorManager.addSuccessResources(oldResource); } // add property for the new resource in all cases SEURATResourcePropertiesManager.addPersistentProperty(newResource, "Rat", "true"); SEURATDecoratorManager.addSuccessResources(newResource); } } } }