List of usage examples for org.eclipse.jdt.core IJavaElement getElementName
String getElementName();
From source file:org.eclipse.linuxtools.changelog.parsers.java.JavaParser.java
License:Open Source License
/** * @see IParserChangeLogContrib#parseCurrentFunction(IEditorPart) *//*from w ww.j a va2 s.c om*/ public String parseCurrentFunction(IEditorInput input, int offset) throws CoreException { String currentElementName; int elementType; // Get the working copy and connect to input. IWorkingCopyManager manager = JavaUI.getWorkingCopyManager(); manager.connect(input); // Retrieve the Java Element in question. // The following internal access is done because the getWorkingCopy() method // for the WorkingCopyManager returns null for StorageEditorInput, however, // there is a working copy available through the ICompilationUnitDocumentProvider. // ICompilationUnit workingCopy = manager.getWorkingCopy(input); ICompilationUnitDocumentProvider x = (ICompilationUnitDocumentProvider) JavaUI.getDocumentProvider(); // Retrieve the Java Element in question. ICompilationUnit workingCopy = x.getWorkingCopy(input); if (workingCopy == null) return ""; IJavaElement method = workingCopy.getElementAt(offset); manager.disconnect(input); // no element selected if (method == null) return ""; // Get the current element name, to test it. currentElementName = method.getElementName(); // Element doesn't have a name. Can go no further. if (currentElementName == null) return ""; // Get the Element Type to test. elementType = method.getElementType(); switch (elementType) { case IJavaElement.METHOD: case IJavaElement.FIELD: break; case IJavaElement.COMPILATION_UNIT: return ""; case IJavaElement.INITIALIZER: return STATIC_INITIALIZER_NAME; // So it's not a method, field, type, or static initializer. Where are we? default: IJavaElement tmpMethodType; if (((tmpMethodType = method.getAncestor(IJavaElement.METHOD)) == null) && ((tmpMethodType = method.getAncestor(IJavaElement.TYPE)) == null)) { return ""; } else { // In a class, but not in a method. Return class name instead. method = tmpMethodType; currentElementName = method.getElementName(); } } // Build all ancestor classes. // Append all ancestor class names to string IJavaElement tmpParent = method.getParent(); boolean firstLoop = true; while (tmpParent != null) { IJavaElement tmpParentClass = tmpParent.getAncestor(IJavaElement.TYPE); if (tmpParentClass != null) { String tmpParentClassName = tmpParentClass.getElementName(); if (tmpParentClassName == null) return ""; currentElementName = tmpParentClassName + "." + currentElementName; } else { // cut root class name int rootClassPos = currentElementName.indexOf("."); if (rootClassPos >= 0) currentElementName = currentElementName.substring(rootClassPos + 1); if (firstLoop) return ""; else return currentElementName; } tmpParent = tmpParentClass.getParent(); firstLoop = false; } return ""; }
From source file:org.eclipse.linuxtools.internal.changelog.parsers.java.JavaParser.java
License:Open Source License
@Override public String parseCurrentFunction(IEditorInput input, int offset) throws CoreException { String currentElementName;/* ww w . j ava2 s. c om*/ int elementType; // Get the working copy and connect to input. IWorkingCopyManager manager = JavaUI.getWorkingCopyManager(); manager.connect(input); // Retrieve the Java Element in question. // The following internal access is done because the getWorkingCopy() // method // for the WorkingCopyManager returns null for StorageEditorInput, // however, // there is a working copy available through the // ICompilationUnitDocumentProvider. ICompilationUnitDocumentProvider x = (ICompilationUnitDocumentProvider) JavaUI.getDocumentProvider(); // Retrieve the Java Element in question. ICompilationUnit workingCopy = x.getWorkingCopy(input); if (workingCopy == null) { return ""; } IJavaElement method = workingCopy.getElementAt(offset); manager.disconnect(input); // no element selected if (method == null) { return ""; } // Get the current element name, to test it. currentElementName = method.getElementName(); // Element doesn't have a name. Can go no further. if (currentElementName == null) { return ""; } // Get the Element Type to test. elementType = method.getElementType(); switch (elementType) { case IJavaElement.METHOD: case IJavaElement.FIELD: break; case IJavaElement.COMPILATION_UNIT: return ""; case IJavaElement.INITIALIZER: return STATIC_INITIALIZER_NAME; // So it's not a method, field, type, or static initializer. Where // are we? default: IJavaElement tmpMethodType; if (((tmpMethodType = method.getAncestor(IJavaElement.METHOD)) == null) && ((tmpMethodType = method.getAncestor(IJavaElement.TYPE)) == null)) { return ""; } else { // In a class, but not in a method. Return class name instead. method = tmpMethodType; currentElementName = method.getElementName(); } } // Build all ancestor classes. // Append all ancestor class names to string IJavaElement tmpParent = method.getParent(); boolean firstLoop = true; while (tmpParent != null) { IJavaElement tmpParentClass = tmpParent.getAncestor(IJavaElement.TYPE); if (tmpParentClass != null) { String tmpParentClassName = tmpParentClass.getElementName(); if (tmpParentClassName == null) { return ""; } currentElementName = tmpParentClassName + "." + currentElementName; } else { // cut root class name int rootClassPos = currentElementName.indexOf('.'); if (rootClassPos >= 0) { currentElementName = currentElementName.substring(rootClassPos + 1); } if (firstLoop) { return ""; } else { return currentElementName; } } tmpParent = tmpParentClass.getParent(); firstLoop = false; } return ""; }
From source file:org.eclipse.modisco.java.discoverer.internal.io.library.binding.JavaModelDelegateBindingFactory.java
License:Open Source License
/** * Returns the MoDisco {@link Binding} corresponding to the Java entity * represented by the Java Model element. * /*from ww w. jav a2 s .c o m*/ * @param element * the Java Model element * @param visitor * the {@code ClassFileParser} * @return the MoDisco {@code Binding} */ public Binding getBindingForElement(final IJavaElement element, final ClassFileParser visitor) { Binding result = null; try { result = getBinding(element, visitor); } catch (JavaModelException e) { IStatus status = new Status(IStatus.WARNING, JavaActivator.PLUGIN_ID, e.getException().getMessage(), e); JavaActivator.getDefault().getLog().log(status); } if (result == null) { result = new UnresolvedBinding(element.getElementName()); } return result; }
From source file:org.eclipse.modisco.java.discoverer.internal.serialization.ElementsToAnalyzeSerializer.java
License:Open Source License
public String serialize(final ElementsToAnalyze elementsToAnalyze) { try {/*from www . j av a2 s . c om*/ StringBuilder builder = new StringBuilder(); IJavaProject javaProject = elementsToAnalyze.getJavaProject(); if (javaProject == null) { return ""; //$NON-NLS-1$ } builder.append(escape(javaProject.getElementName())); builder.append(ElementsToAnalyzeSerializer.SEPARATOR1); Set<Object> elementsToDiscover = elementsToAnalyze.getElementsToDiscover(); boolean first = true; for (Object object : elementsToDiscover) { if (!first) { builder.append(ElementsToAnalyzeSerializer.SEPARATOR1); } first = false; if (object instanceof IJavaProject) { IJavaProject javaProject2 = (IJavaProject) object; builder.append(ElementsToAnalyzeSerializer.PROJECT_PREFIX); builder.append(escape(javaProject2.getElementName())); } else if (object instanceof IJavaElement) { IJavaElement javaElement = (IJavaElement) object; builder.append(ElementsToAnalyzeSerializer.ELEMENT_PREFIX); builder.append(escape(javaElement.getJavaProject().getElementName())); builder.append(ElementsToAnalyzeSerializer.SEPARATOR3); builder.append(escape(javaElement.getElementName())); builder.append(ElementsToAnalyzeSerializer.SEPARATOR3); builder.append(escape(javaElement.getPath().toString())); } else { MoDiscoLogger.logError("Unexpected element: " + object.getClass().getName(), //$NON-NLS-1$ JavaActivator.getDefault()); continue; } Map<String, Object> discoveryOptions = elementsToAnalyze.getDiscoveryOptions(object); for (Entry<String, Object> entry : discoveryOptions.entrySet()) { builder.append(ElementsToAnalyzeSerializer.SEPARATOR2); builder.append(escape(entry.getKey())); builder.append(ElementsToAnalyzeSerializer.SEPARATOR2); ISerializer<?> serializer2 = ISerializationRegistry.INSTANCE .getSerializerFor(entry.getValue().getClass()); if (serializer2 != null) { String serialized2 = ISerializationService.INSTANCE.serialize(entry.getValue()); if (serialized2 != null) { builder.append(escape(serialized2)); } } else { MoDiscoLogger.logError("No serializer for: " + entry.getValue().getClass().getName(), //$NON-NLS-1$ JavaActivator.getDefault()); } } } return builder.toString(); } catch (Exception e) { MoDiscoLogger.logError(e, "Error serializing elements to analyze", //$NON-NLS-1$ JavaActivator.getDefault()); return ""; //$NON-NLS-1$ } }
From source file:org.eclipse.modisco.java.discoverer.internal.serialization.ElementsToAnalyzeSerializer.java
License:Open Source License
public ElementsToAnalyze deserialize(final String serializedValue) { try {/*ww w .j av a2 s. c o m*/ String[] elements = serializedValue.split("\\" + ElementsToAnalyzeSerializer.SEPARATOR1); //$NON-NLS-1$ ElementsToAnalyze elementsToAnalyze = new ElementsToAnalyze(getJavaProject(unescape(elements[0]))); for (int i = 1; i < elements.length; i++) { String element = elements[i]; if (element.startsWith(ElementsToAnalyzeSerializer.PROJECT_PREFIX)) { String[] elementParts = element.substring(ElementsToAnalyzeSerializer.PROJECT_PREFIX.length()) .split("\\" + ElementsToAnalyzeSerializer.SEPARATOR2); //$NON-NLS-1$ String projectName2 = unescape(elementParts[0]); IJavaProject javaProject = getJavaProject(projectName2); elementsToAnalyze.addElementToDiscover(javaProject); fillMapWithRemainingParts(elementParts, elementsToAnalyze.getDiscoveryOptions(javaProject)); } else if (element.startsWith(ElementsToAnalyzeSerializer.ELEMENT_PREFIX)) { String[] elementParts = element.substring(ElementsToAnalyzeSerializer.ELEMENT_PREFIX.length()) .split("\\" + ElementsToAnalyzeSerializer.SEPARATOR2); //$NON-NLS-1$ String elementName = elementParts[0]; String[] parts = elementName.split("\\" //$NON-NLS-1$ + ElementsToAnalyzeSerializer.SEPARATOR3); IJavaProject containingProject = getJavaProject(unescape(parts[0])); for (IJavaElement javaElement : containingProject.getChildren()) { if (unescape(parts[1]).equals(javaElement.getElementName()) && new Path(unescape(parts[2])).equals(javaElement.getPath())) { elementsToAnalyze.addElementToDiscover(javaElement); fillMapWithRemainingParts(elementParts, elementsToAnalyze.getDiscoveryOptions(javaElement)); break; } } } else { MoDiscoLogger.logError("unhandled element", JavaActivator.getDefault()); //$NON-NLS-1$ } } return elementsToAnalyze; } catch (Exception e) { MoDiscoLogger.logError(e, "Error deserializing elements to analyze", //$NON-NLS-1$ JavaActivator.getDefault()); return new ElementsToAnalyze(null); } }
From source file:org.eclipse.mylyn.internal.java.ui.JavaEditingMonitor.java
License:Open Source License
/** * Only public for testing. Note: Two sequential selections on the same element are deemed to be an edit of the * selection as this is the best guess that can be made. See bug 252306. */// w ww . j a v a 2 s . c o m @Override public void handleWorkbenchPartSelection(IWorkbenchPart part, ISelection selection, boolean contributeToContext) { try { IJavaElement selectedElement = null; if (selection instanceof StructuredSelection) { StructuredSelection structuredSelection = (StructuredSelection) selection; if (structuredSelection.equals(currentSelection)) { return; } currentSelection = structuredSelection; for (Iterator<?> iterator = structuredSelection.iterator(); iterator.hasNext();) { Object selectedObject = iterator.next(); if (selectedObject instanceof IJavaElement) { IJavaElement checkedElement = checkIfAcceptedAndPromoteIfNecessary( (IJavaElement) selectedObject); if (checkedElement == null) { return; } else { selectedElement = checkedElement; } } if (selectedElement != null) { super.handleElementSelection(part, selectedElement, contributeToContext); } } } else { if (selection instanceof TextSelection && part instanceof JavaEditor) { currentEditor = (JavaEditor) part; TextSelection textSelection = (TextSelection) selection; selectedElement = SelectionConverter.resolveEnclosingElement(currentEditor, textSelection); if (selectedElement instanceof IPackageDeclaration) { // HACK: ignoring these selections return; } IJavaElement[] resolved = SelectionConverter.codeResolve(currentEditor); if (resolved != null && resolved.length == 1 && !resolved[0].equals(selectedElement)) { lastResolvedElement = resolved[0]; } boolean selectionResolved = false; if (selectedElement instanceof IMethod && lastSelectedElement instanceof IMethod) { // navigation between two elements if (lastResolvedElement != null && lastSelectedElement != null && lastResolvedElement.equals(selectedElement) && !lastSelectedElement.equals(lastResolvedElement)) { super.handleNavigation(part, selectedElement, JavaReferencesProvider.ID, contributeToContext); selectionResolved = true; } else if (lastSelectedElement != null && lastSelectedElement.equals(lastResolvedElement) && !lastSelectedElement.equals(selectedElement)) { super.handleNavigation(part, selectedElement, JavaReferencesProvider.ID, contributeToContext); selectionResolved = true; } } else if (selectedElement != null && lastSelectedElement != null && !lastSelectedElement.equals(selectedElement)) { if (lastSelectedElement.getElementName().equals(selectedElement.getElementName())) { // navigation between two elements if (selectedElement instanceof IMethod && lastSelectedElement instanceof IMethod) { super.handleNavigation(part, selectedElement, JavaImplementorsProvider.ID, contributeToContext); selectionResolved = true; } else if (selectedElement instanceof IType && lastSelectedElement instanceof IType) { super.handleNavigation(part, selectedElement, JavaImplementorsProvider.ID, contributeToContext); selectionResolved = true; } } } if (selectedElement != null) { // selection of an element if (!selectionResolved && selectedElement.equals(lastSelectedElement)) { super.handleElementEdit(part, selectedElement, contributeToContext); } else if (!selectedElement.equals(lastSelectedElement)) { super.handleElementSelection(part, selectedElement, contributeToContext); } } IJavaElement checkedElement = checkIfAcceptedAndPromoteIfNecessary(selectedElement); if (checkedElement == null) { return; } else { selectedElement = checkedElement; } } } if (selectedElement != null) { lastSelectedElement = selectedElement; } } catch (JavaModelException e) { // ignore, fine to fail to resolve an element if the model is not up-to-date } catch (Throwable t) { StatusHandler.log(new Status(IStatus.ERROR, JavaUiBridgePlugin.ID_PLUGIN, "Failed to update model based on selection", t)); //$NON-NLS-1$ } }
From source file:org.eclipse.mylyn.internal.monitor.monitors.SelectionMonitor.java
License:Open Source License
private String obfuscateJavaElementHandle(IJavaElement javaElement) { try {// w w w .ja va 2 s .c o m StringBuffer obfuscatedPath = new StringBuffer(); IResource resource; resource = (IResource) javaElement.getUnderlyingResource(); if (resource != null && (resource instanceof IFile)) { IFile file = (IFile) resource; obfuscatedPath.append(obfuscator.obfuscateResourcePath(file.getProjectRelativePath())); obfuscatedPath.append(':'); obfuscatedPath.append(obfuscator.obfuscateString(javaElement.getElementName())); return obfuscatedPath.toString(); } else { return obfuscator.obfuscateString(javaElement.getHandleIdentifier()); } } catch (JavaModelException e) { // ignore non-existing element } return ID_JAVA_UNKNOWN; }
From source file:org.eclipse.mylyn.internal.sandbox.ui.editors.TaskEditorDropTarget.java
License:Open Source License
@Override public void drop(DropTargetEvent event) { StringBuilder shouldDropText = new StringBuilder(); // this concern may also go to IResourceHyperlinkExtension by adding something like IResourceHyperlinkExtension.drop(DropTargetEvent)? if (TextTransfer.getInstance().isSupportedType(event.currentDataType) && (event.data instanceof String)) { shouldDropText.append((String) event.data); } else if (JavaUI.getJavaElementClipboardTransfer().isSupportedType(event.currentDataType) && (event.data instanceof IJavaElement[])) { IJavaElement[] elements = (IJavaElement[]) event.data; for (IJavaElement element : elements) { shouldDropText.append(ResourceHyperlinkExtensions.getGeneratedPrefix("java") + element.getElementName() + addCommaIfNeeded(elements, element)); }//from w w w . j ava 2 s .co m } else if (ResourceTransfer.getInstance().isSupportedType(event.currentDataType) && (event.data instanceof IResource[])) { IResource[] elements = (IResource[]) event.data; for (IResource element : elements) { String fullName = element.getName(); int sep_pos = fullName.lastIndexOf("."); if (sep_pos != -1) { String fileName = fullName.substring(0, sep_pos); String fileType = fullName.substring(sep_pos + 1); // decide file type by file extension String generatedPrefix = ResourceHyperlinkExtensions.getGeneratedPrefix(fileType.toLowerCase()); if (generatedPrefix != null) { shouldDropText .append(generatedPrefix + " " + fileName + addCommaIfNeeded(elements, element)); } else { // cannot decide the file type, then use default shouldDropText.append(ResourceHyperlinkExtensions.getDefaultPrefix() + " " + element.getFullPath().toString().substring(1) + addCommaIfNeeded(elements, element)); } } } } if (shouldDropText.length() > 0) { dropTextToCurrentPosition(shouldDropText.toString()); } }
From source file:org.eclipse.mylyn.java.ui.views.ProblemsFieldJavaElement.java
License:Open Source License
public String getValue(Object obj) { if (obj == null || !(obj instanceof ConcreteMarker)) { return ""; //$NON-NLS-1$ }// ww w. jav a 2s . c o m ConcreteMarker marker = (ConcreteMarker) obj; IJavaElement javaElement = JavaUiUtil.getJavaElement(marker);//SearchUtil.getJavaElement(marker); if (javaElement == null) { return marker.getResourceName(); } else { String name; if (javaElement instanceof IMember && javaElement.getParent() != null && !(javaElement.getParent() instanceof ICompilationUnit)) { name = javaElement.getParent().getElementName() + "." + javaElement.getElementName(); } else { name = javaElement.getElementName(); } return name; } }
From source file:org.eclipse.mylyn.ui.views.support.DoiDecoratingTableLabelProvider.java
License:Open Source License
public String getColumnText(Object object, int index) { if (object instanceof TaskscapeNode) { TaskscapeNode node = (TaskscapeNode) object; if (node.getKind().equals(ITaskscapeNode.Kind.Java)) { IJavaElement element = JavaCore.create(node.getElementHandle()); if (element != null) { String label = element.getElementName(); return label + " (" + node.getDegreeOfInterest().getDegreeOfInterest().getDoiValue() + ")"; } else { return "<missing element>"; }/*from w w w .j a va2 s. com*/ } else { return node.getElementHandle(); } } else if (object instanceof TaskscapeEdge) { TaskscapeEdge edge = (TaskscapeEdge) object; return edge.toString(); } return "? " + object; }