List of usage examples for org.eclipse.jdt.core IJavaElement getAncestor
IJavaElement getAncestor(int ancestorType);
From source file:edu.buffalo.cse.green.editor.model.RootModel.java
License:Open Source License
/** * @param kind - The kind of element to find. * @return All elements in the editor of the specified kind. *///from w w w .j ava2s . c o m public List<IJavaElement> getElementsOfKind(int kind) { Set<String> ids = new HashSet<String>(); List<IJavaElement> elements = new ArrayList<IJavaElement>(); for (IJavaElement element : _cache.getElements()) { if (element == null) continue; IJavaElement ancestor = element.getAncestor(kind); if (ancestor != null) { ids.add(ancestor.getHandleIdentifier()); } } for (String id : ids) { elements.add(JavaCore.create(id)); } return elements; }
From source file:edu.buffalo.cse.green.JavaModelListener.java
License:Open Source License
/** * @see edu.buffalo.cse.green.RefactorHandler#handleRemove(edu.buffalo.cse.green.editor.model.RootModel, org.eclipse.jdt.core.IJavaElement) *//*from w w w.jav a 2 s .c om*/ public void handleRemove(RootModel root, E element) { List<IJavaElement> packages = root.getElementsOfKind(PACKAGE_FRAGMENT); for (IJavaElement pack : packages) { // get the project that contains the package IJavaElement proj = pack.getAncestor(JAVA_PROJECT); // if the package belongs to the project, remove it if (JavaModelListener.sameElements(proj, element)) { PackageRefactorHandler.instance().handleRemove(root, (IPackageFragment) pack); } } }
From source file:edu.buffalo.cse.green.JavaModelListener.java
License:Open Source License
/** * @see edu.buffalo.cse.green.RefactorHandler#handleRemove(edu.buffalo.cse.green.editor.model.RootModel, org.eclipse.jdt.core.IJavaElement) *//*from www . ja v a2 s . co m*/ public void handleRemove(RootModel root, E element) { List<IJavaElement> cus = root.getElementsOfKind(COMPILATION_UNIT); for (IJavaElement cu : cus) { if (JavaModelListener.sameElements(cu.getAncestor(PACKAGE_FRAGMENT), element)) { CompilationUnitRefactorHandler.instance().handleRemove(root, (ICompilationUnit) cu); } } }
From source file:edu.buffalo.cse.green.JavaModelListener.java
License:Open Source License
/** * @see edu.buffalo.cse.green.RefactorHandler#handleMove(edu.buffalo.cse.green.editor.model.RootModel, org.eclipse.jdt.core.IJavaElement, org.eclipse.jdt.core.IJavaElement) *//* w ww. j a v a 2 s . c o m*/ public void handleMove(RootModel root, E sourceElement, E targetElement) { // get all types represented in the diagram String scuId = sourceElement.getHandleIdentifier(); String tcuId = targetElement.getHandleIdentifier(); String scuName = scuId.substring(scuId.indexOf('{') + 1, scuId.indexOf(".java")); String tcuName = tcuId.substring(tcuId.indexOf('{') + 1, tcuId.indexOf(".java")); List<IJavaElement> cuTypes = root.getElementsOfKind(TYPE); // see if any types belong to the compilation unit // that is undergoing the move event for (IJavaElement oType : cuTypes) { if (JavaModelListener.sameElements(sourceElement, oType.getAncestor(COMPILATION_UNIT))) { String oId = oType.getHandleIdentifier(); String oName = oId.substring(oId.indexOf('[')); oName = oName.replaceAll("\\[" + scuName, "[" + tcuName); IJavaElement nType = JavaCore.create(tcuId + oName); TypeModel oModel = (TypeModel) root.getModelFromElement(oType); TypeModel nModel = root.createTypeModel((IType) nType); if (oModel != null) { // TODO We tried to catch a ResourceException, // but it is caught in MemberModel oModel.removeFromParent(); nModel.setLocation(oModel.getLocation()); nModel.setSize(oModel.getSize()); } } } }
From source file:edu.buffalo.cse.green.JavaModelListener.java
License:Open Source License
/** * @see edu.buffalo.cse.green.RefactorHandler#handleRemove(edu.buffalo.cse.green.editor.model.RootModel, org.eclipse.jdt.core.IJavaElement) *//*from ww w .j a va 2 s . c om*/ public void handleRemove(RootModel root, E element) { // ignore this if the CU removed is a working copy (not actual removal) if (element.isWorkingCopy()) return; List<IJavaElement> types = new ArrayList<IJavaElement>(root.getElementsOfKind(TYPE)); // remove any types that belong to the compilation unit for (IJavaElement type : types) { if (JavaModelListener.sameElements(element, type.getAncestor(COMPILATION_UNIT))) { if (root.getModelFromElement(type) != null) { root.getModelFromElement(type).removeFromParent(); } } } }
From source file:edu.washington.cs.cupid.jdt.compiler.CompilationUnit.java
License:Open Source License
@Override public LinearJob<IJavaElement, ICompilationUnit> getJob(final IJavaElement input) { return new LinearJob<IJavaElement, ICompilationUnit>(this, input) { @Override//from w w w .j a v a2s. c o m protected LinearStatus<ICompilationUnit> run(final IProgressMonitor monitor) { try { monitor.beginTask(getName(), 1); IJavaElement cu = input.getAncestor(IJavaElement.COMPILATION_UNIT); if (cu != null) { return LinearStatus.<ICompilationUnit>makeError( new RuntimeException("No associated compilation unit")); } else { return LinearStatus.makeOk(getCapability(), (ICompilationUnit) cu); } } catch (Exception ex) { return LinearStatus.<ICompilationUnit>makeError(ex); } finally { monitor.done(); } } }; }
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 w w.ja va2 s .co 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); } } } }
From source file:es.bsc.servicess.ide.wizards.ServiceSsCommonWizardPage.java
License:Apache License
protected void initPage(IJavaElement elem) { IJavaProject project = null;/* w w w . j a v a2 s . c o m*/ IPackageFragment pack = null; IType typeName = null; if (elem != null) { // evaluate the enclosing type project = elem.getJavaProject(); pack = (IPackageFragment) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT); System.out.println("Element name:" + elem.getElementName() + "type: " + elem.getElementType()); if (elem.getElementType() == JavaElement.COMPILATION_UNIT) { typeName = ((ICompilationUnit) elem).findPrimaryType(); } else if (elem.getElementType() == JavaElement.TYPE) { typeName = (IType) elem; } } setPackageFragment(pack, true); setClassName(typeName, true); }
From source file:es.bsc.servicess.ide.wizards.ServiceSsNewElementWizardPage.java
License:Apache License
protected void initPage(IJavaElement elem) { IJavaProject project = null;//w w w.jav a 2s.com IPackageFragment pack = null; IType typeName = null; if (elem != null) { // evaluate the enclosing type project = elem.getJavaProject(); pack = (IPackageFragment) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT); System.out.println("Element name:" + elem.getElementName()); if (elem.getElementType() == JavaElement.COMPILATION_UNIT) { typeName = ((ICompilationUnit) elem).findPrimaryType(); } else if (elem.getElementType() == JavaElement.TYPE) { typeName = (IType) elem; } } setPackageFragment(pack, true); setClassName(typeName, true); }
From source file:es.bsc.servicess.ide.wizards.ServiceSsNewServiceClassPage.java
License:Apache License
/** * Initialize the class part in the wizard page. * @param elem Selected java element/*from ww w . j av a 2 s.c om*/ */ protected void initTypePage(IJavaElement elem) { IJavaProject project = null; IPackageFragment pack = null; if (elem != null) { // evaluate the enclosing type project = elem.getJavaProject(); IStatus status = Checker.validateServiceSsProject(project.getProject()); if (!status.isOK()) { setPackageFragment(null, false); setTypeName("", false); fContainerStatus = status; } pack = (IPackageFragment) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT); } String typeName = ""; //$NON-NLS-1$ ITextSelection selection = getCurrentTextSelection(); if (selection != null) { String text = selection.getText(); if (text != null && validateJavaTypeName(text, project).isOK()) { typeName = text; } } setPackageFragment(pack, true); setTypeName(typeName, true); }