List of usage examples for org.eclipse.jdt.core IJavaElement getHandleIdentifier
String getHandleIdentifier();
From source file:edu.brown.cs.bubbles.bedrock.BedrockEditor.java
License:Open Source License
private IJavaElement findElementForKey(IJavaElement elt, String key) { if (key.equals(elt.getHandleIdentifier())) return elt; if (elt instanceof IParent) { IParent ip = (IParent) elt;/*from ww w . j a v a2 s .c o m*/ try { if (ip.hasChildren()) { for (IJavaElement je : ip.getChildren()) { IJavaElement re = findElementForKey(je, key); if (re != null) return re; } } } catch (JavaModelException e) { } } return null; }
From source file:edu.brown.cs.bubbles.bedrock.BedrockRenamer.java
License:Open Source License
/********************************************************************************/ void rename(String proj, String bid, String file, int start, int end, String name, String handle, String newname, boolean keeporig, boolean getters, boolean setters, boolean dohier, boolean qual, boolean refs, boolean dosimilar, boolean textocc, boolean doedit, String filespat, IvyXmlWriter xw) throws BedrockException { ICompilationUnit icu = our_plugin.getCompilationUnit(proj, file); IJavaElement[] elts;//from w w w . j a va 2 s. co m try { elts = icu.codeSelect(start, end - start); } catch (JavaModelException e) { throw new BedrockException("Bad location: " + e, e); } IJavaElement relt = null; for (IJavaElement ije : elts) { if (handle != null && !handle.equals(ije.getHandleIdentifier())) continue; if (name != null && !name.equals(ije.getElementName())) continue; relt = ije; break; } if (relt == null) throw new BedrockException("Item to rename not found"); BedrockPlugin.logD("RENAME CHECK " + relt.getElementType() + " " + relt.getParent().getElementType()); switch (relt.getElementType()) { case IJavaElement.COMPILATION_UNIT: throw new BedrockException("Compilation unit renaming not supported yet"); case IJavaElement.PACKAGE_FRAGMENT_ROOT: case IJavaElement.PACKAGE_FRAGMENT: throw new BedrockException("Package renaming not supported yet"); case IJavaElement.FIELD: case IJavaElement.LOCAL_VARIABLE: case IJavaElement.TYPE_PARAMETER: break; case IJavaElement.METHOD: IMethod mthd = (IMethod) relt; try { if (mthd.isConstructor()) throw new BedrockException("Constructor renaming not supported yet"); } catch (JavaModelException e) { } break; case IJavaElement.TYPE: IJavaElement pelt = relt.getParent(); if (pelt.getElementType() == IJavaElement.COMPILATION_UNIT) { ITypeRoot xcu = (ITypeRoot) pelt; if (relt == xcu.findPrimaryType()) { throw new BedrockException("Compilation unit renaming based on type not supported yet"); } } break; default: throw new BedrockException("Invalid element type to rename"); } SearchPattern sp = SearchPattern.createPattern(relt, IJavaSearchConstants.ALL_OCCURRENCES, SearchPattern.R_EXACT_MATCH); List<ICompilationUnit> worku = new ArrayList<ICompilationUnit>(); for (IJavaElement je : BedrockJava.getAllProjects()) { our_plugin.getWorkingElements(je, worku); } ICompilationUnit[] work = new ICompilationUnit[worku.size()]; work = worku.toArray(work); int fg = IJavaSearchScope.SOURCES | IJavaSearchScope.REFERENCED_PROJECTS; IJavaSearchScope scp = SearchEngine.createJavaSearchScope(work, fg); SearchEngine se = new SearchEngine(work); SearchParticipant[] parts = new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }; FindHandler fh = new FindHandler(xw, null); try { se.search(sp, parts, scp, fh, null); } catch (CoreException e) { throw new BedrockException("Problem doing find all search: " + e, e); } BedrockPlugin.logD("RENAME RESULT = " + xw.toString()); }
From source file:edu.brown.cs.bubbles.bedrock.BedrockUtil.java
License:Open Source License
private static void outputSymbol(IJavaElement elt, String what, String nm, String key, IvyXmlWriter xw) { if (what == null || nm == null) return;/*from ww w . java 2 s. com*/ xw.begin("ITEM"); xw.field("TYPE", what); xw.field("NAME", nm); xw.field("HANDLE", elt.getHandleIdentifier()); xw.field("WORKING", (elt.getPrimaryElement() != elt)); ICompilationUnit cu = (ICompilationUnit) elt.getAncestor(IJavaElement.COMPILATION_UNIT); if (cu != null) { xw.field("CUWORKING", cu.isWorkingCopy()); } try { xw.field("KNOWN", elt.isStructureKnown()); } catch (JavaModelException e) { } if (elt instanceof ISourceReference) { try { ISourceRange rng = ((ISourceReference) elt).getSourceRange(); if (rng != null) { xw.field("STARTOFFSET", rng.getOffset()); xw.field("ENDOFFSET", rng.getOffset() + rng.getLength()); xw.field("LENGTH", rng.getLength()); } } catch (JavaModelException e) { BedrockPlugin.logE("Problem getting source range: " + e); } } if (elt instanceof ILocalVariable) { ILocalVariable lcl = (ILocalVariable) elt; xw.field("SIGNATURE", lcl.getTypeSignature()); } if (elt instanceof IMember) { try { IMember mem = ((IMember) elt); int fgs = mem.getFlags(); if (mem.getParent() instanceof IType && !(elt instanceof IType)) { IType par = (IType) mem.getParent(); if (par.isInterface()) { if (elt instanceof IMethod) fgs |= Flags.AccAbstract; fgs |= Flags.AccPublic; } xw.field("QNAME", par.getFullyQualifiedName() + "." + nm); } xw.field("FLAGS", fgs); } catch (JavaModelException e) { } } if (elt instanceof IPackageFragment || elt instanceof IType) { Display d = BedrockApplication.getDisplay(); if (d != null) { JavadocUrl ju = new JavadocUrl(elt); d.syncExec(ju); URL u = ju.getResult(); if (u != null) { xw.field("JAVADOC", u.toString()); } } } xw.field("SOURCE", "USERSOURCE"); if (key != null) xw.field("KEY", key); boolean havepath = false; for (IJavaElement pe = elt.getParent(); pe != null; pe = pe.getParent()) { if (pe.getElementType() == IJavaElement.COMPILATION_UNIT) { IProject ip = elt.getJavaProject().getProject(); File f = BedrockUtil.getFileForPath(elt.getPath(), ip); xw.field("PATH", f.getAbsolutePath()); havepath = true; break; } } IJavaProject ijp = elt.getJavaProject(); if (ijp != null) xw.field("PROJECT", ijp.getProject().getName()); IPath p = elt.getPath(); if (p != null) { xw.field("XPATH", p); if (!havepath) { IProject ip = elt.getJavaProject().getProject(); File f = getFileForPath(elt.getPath(), ip); xw.field("PATH", f.getAbsolutePath()); } } if (elt instanceof IMethod) { IMethod m = (IMethod) elt; try { xw.field("RESOLVED", m.isResolved()); ISourceRange rng = m.getNameRange(); if (rng != null) { xw.field("NAMEOFFSET", rng.getOffset()); xw.field("NAMELENGTH", rng.getLength()); } xw.field("RETURNTYPE", m.getReturnType()); xw.field("NUMPARAM", m.getNumberOfParameters()); String[] pnms = m.getParameterNames(); String[] ptys = m.getParameterTypes(); for (int i = 0; i < ptys.length; ++i) { xw.begin("PARAMETER"); if (i < pnms.length) xw.field("NAME", pnms[i]); xw.field("TYPE", ptys[i]); xw.end(); } for (String ex : m.getExceptionTypes()) { xw.begin("EXCEPTION"); xw.field("TYPE", ex); xw.end(); } } catch (JavaModelException e) { } } // TODO: output parameters as separate elements with type and name if (elt instanceof IAnnotatable) { IAnnotatable ann = (IAnnotatable) elt; try { IAnnotation[] ans = ann.getAnnotations(); for (IAnnotation an : ans) { xw.begin("ANNOTATION"); xw.field("NAME", an.getElementName()); xw.field("COUNT", an.getOccurrenceCount()); try { for (IMemberValuePair mvp : an.getMemberValuePairs()) { xw.begin("VALUE"); xw.field("NAME", mvp.getMemberName()); if (mvp.getValue() != null) xw.field("VALUE", mvp.getValue().toString()); xw.field("KIND", mvp.getValueKind()); xw.end("VALUE"); } } catch (JavaModelException e) { } xw.end("ANNOTATION"); } } catch (JavaModelException e) { } } xw.end("ITEM"); }
From source file:edu.buffalo.cse.green.editor.action.refactor.RefactorRenameParticipant.java
License:Open Source License
@Override public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException { if (!(getProcessor() instanceof RenameProcessor)) { return null; }//from w w w . j a v a 2 s . com CompositeChange change = null; //Allows the return of a null change if no applicable changes are found. IJavaElement element = ((IJavaElement) (getProcessor()).getElements()[0]); String oldName = element.getElementName(); oldName = oldName.replace(".java", ""); String handle = element.getHandleIdentifier(); String newName = ((JavaRenameProcessor) getProcessor()).getNewElementName(); newName = newName.replace(".java", ""); ArrayList<HandleNode> handleList = new ArrayList<HandleNode>(); String handleCopy = handle; char prefix = handleCopy.charAt(0); handleCopy = handleCopy.substring(1); for (int j = 0; j < handleCopy.length(); j++) { if (HANDLE_PREFIXES.contains(handleCopy.charAt(j) + "")) { handleList.add(new HandleNode(prefix + handleCopy.substring(0, j))); prefix = handleCopy.charAt(j); handleCopy = handleCopy.substring(j + 1); j = 0; } } handleList.add(new HandleNode(prefix + handleCopy)); handleCopy = ""; Iterator<HandleNode> itr = handleList.iterator(); while (itr.hasNext()) { HandleNode node = itr.next(); // if(node.getName().contains(".")) { // if((node.getName()).substring(0, node.getName().indexOf('.')) == oldName) { // node.setName(newName + ".java"); // } // } if (node.getName().equals(oldName)) { node.setName(newName); } } Iterator<HandleNode> itr2 = handleList.iterator(); String newHandle = ""; while (itr2.hasNext()) { HandleNode node = itr2.next(); newHandle += node.toString(); } // if(newName.indexOf('.') != -1) { //To handle type names that include .java // if(oldName.contains(".java")) { // oldName = oldName.substring(0, oldName.indexOf(".java")); // } // newName = newName.substring(0, newName.indexOf('.')); // } //Assumes project is open and exists (which should be the case if the element //was able to be selected) IProject project = element.getResource().getProject(); ArrayList<IFile> greenFiles = ResourceUtil.getGreenFiles(project); for (IFile file : greenFiles) { BufferedReader br = new BufferedReader(new InputStreamReader(file.getContents())); String fileText = ""; String line = null; try { while ((line = br.readLine()) != null) { fileText += line + '\n'; } } catch (IOException e) { e.printStackTrace(); } //Starting index of element handle int[] handleIndices = ResourceUtil.findAllOccurrences(fileText, handle); // ArrayList<Integer> offsets = new ArrayList<Integer>(); if (change == null) { //Creates a new Change object if necessary change = new CompositeChange("Change reference '" + oldName + "' to '" + newName + "'."); } TextFileChange result = new TextFileChange(file.getName(), file); MultiTextEdit fileChangeRootEdit = new MultiTextEdit(); result.setEdit(fileChangeRootEdit); for (int i = 0; i < handleIndices.length; i++) { ReplaceEdit edit = new ReplaceEdit(handleIndices[i], handle.length(), newHandle); fileChangeRootEdit.addChild(edit); change.add(result); } // for(int i = 0; i < handleIndices.length; i++) { // int[] tempOffsets = ResourceUtil.findAllOccurrences(handle, oldName); // for(int j = 0; j < tempOffsets.length; j++) { // offsets.add(new Integer(handleIndices[i] + tempOffsets[j])); // } // } // if(!offsets.isEmpty()) { //Changes exist // if(change == null) { //Creates a new Change object if necessary // change = new CompositeChange("Change reference '" + oldName + "' to '" + newName + "'."); // } // // TextFileChange result = new TextFileChange(file.getName(), file); // MultiTextEdit fileChangeRootEdit = new MultiTextEdit(); // result.setEdit(fileChangeRootEdit); // // for(Integer offset : offsets) { // ReplaceEdit edit = new ReplaceEdit(offset.intValue(), oldName.length(), newName); // fileChangeRootEdit.addChild(edit); // } // change.add(result); // } } return change; }
From source file:edu.buffalo.cse.green.editor.controller.MemberPart.java
License:Open Source License
/** * Compares two <code>IJavaElement</code>s for equality. * /* w w w. j a v a 2 s .co m*/ * @param element - The element to compare the current element to. * @return true if the elements are equal; false otherwise. */ public boolean compareElements(IJavaElement element) { return (_element.getHandleIdentifier().equals(element.getHandleIdentifier())); }
From source file:edu.buffalo.cse.green.editor.DiagramEditor.java
License:Open Source License
/** * Finds all relationships that have the given element as their source. * /* w w w. j a v a 2s.c o m*/ * @param element - The element to find relationships for. */ private void findRelationships(IJavaElement element) { long modified; // if the element contains errors, quit try { if (!element.exists() || !element.isStructureKnown()) return; } catch (JavaModelException e) { e.printStackTrace(); return; } CompilationUnit cu; String id = element.getHandleIdentifier(); // generate AST if necessary - check modification stamp Long modifiedStore = _cuMap.getModificationStamp(id); IResource resource = element.getResource(); if (resource == null) { if (_cuMap.getCompilationUnit(id) != null) { modifiedStore = new Long(0); } modified = 0; } else { modified = resource.getModificationStamp(); } // if there isn't an up-to-date AST, create one if ((modifiedStore == null) || (modified != modifiedStore)) { ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setResolveBindings(true); if (element instanceof ICompilationUnit) { parser.setSource((ICompilationUnit) element); } else if (element instanceof IClassFile) { // only search through the class if it has source code attached IClassFile classFile = (IClassFile) element; try { if (classFile.getSource() == null) { return; } } catch (JavaModelException e) { e.printStackTrace(); } parser.setSource(classFile); } else { GreenException.illegalOperation("Illegal element type: " + element.getClass()); } cu = (CompilationUnit) parser.createAST(null); _cuMap.put(element, cu); } else { cu = _cuMap.getCompilationUnit(id); } // run the recognizers for (Class klass : PlugIn.getRelationships()) { RelationshipRecognizer recognizer = PlugIn.getRelationshipGroup(klass).getRecognizer(); // run the recognizer recognizer.run(cu, getRootModel().getRelationshipCache()); } }
From source file:edu.buffalo.cse.green.editor.DiagramEditor.java
License:Open Source License
/** * Refreshes the relationships in the editor * // w w w. j a v a 2 s. c o m * @param force - If true, will run the relationship recognizers. If false, * will run the relationship recognizers only if they are not disabled in * the <code>PlugIn</code> instance. */ private void refreshRelationships(boolean force) { List<String> visitedElements = new ArrayList<String>(); List<String> outdated = new ArrayList<String>(); if (!force && !PlugIn.isRecognizersEnabled()) { return; } // get a list of all classes and compilation units in the editor List<IJavaElement> elements = new ArrayList<IJavaElement>(); elements.addAll(getRootModel().getElementsOfKind(IJavaElement.COMPILATION_UNIT)); elements.addAll(getRootModel().getElementsOfKind(IJavaElement.CLASS_FILE)); // find relationships attached to those elements for (IJavaElement element : elements) { findRelationships(element); visitedElements.add(element.getHandleIdentifier()); } // remove outdated CompilationUnit objects from the map for (String cu : _cuMap.keySet()) { if (!(visitedElements.contains(cu))) { outdated.add(cu); } } for (String obsolete : outdated) { _cuMap.remove(obsolete); } Set<RelationshipModel> toRemove = new HashSet<RelationshipModel>(); _relationshipChanges = getRootModel().getRelationshipCache().processChanges(); // update the relationships as appropriate for (RelationshipModel rModel : _relationshipChanges) { if (rModel.getRelationships().size() == 0) { // removal rModel.removeFromParent(); toRemove.add(rModel); } else { rModel.setParent(getRootModel()); if (rModel.getSourceModel() != null && rModel.getTargetModel() != null) { if (!getRootModel().getRelationships().contains(rModel)) { getRootModel().addChild(rModel); toRemove.add(rModel); } } } } // update the cardinality labels of all updated relationships for (RelationshipModel model : toRemove) { model.updateCardinality(); } _relationshipChanges.removeAll(toRemove); }
From source file:edu.buffalo.cse.green.editor.DiagramEditor.java
License:Open Source License
/** * Gets an AST <code>CompilationUnit</code> from the mapping using the given * <code>IJavaElement</code>, which should be either an * <code>IClassFile</code> or an <code>ICompilationUnit</code> * /*from ww w. j a va 2 s .c o m*/ * @param element - The element to find in the mapping. * @return The AST <code>CompilationUnit</code> the represents the structure * of the given element */ public CompilationUnit getCompilationUnit(IJavaElement element) { return _cuMap.getCompilationUnit(element.getHandleIdentifier()); }
From source file:edu.buffalo.cse.green.editor.DiagramEditor.java
License:Open Source License
/** * Maps an element to its corresponding <code>CompilationUnit</code>. * /* w w w. j av a 2 s.c om*/ * @param element - The element. * @param cu - Its corresponding <code>CompilationUnit</code>. */ public void put(IJavaElement element, CompilationUnit cu) { String id = element.getHandleIdentifier(); _map.put(id, cu); if (!(element.isReadOnly())) { _cuModMap.put(id, element.getResource().getModificationStamp()); } }
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. *///w w w . ja va 2s.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; }