List of usage examples for org.eclipse.jdt.core IJavaElement getParent
IJavaElement getParent();
null
if this element has no parent. From source file:org.springframework.ide.eclipse.quickfix.jdt.RequestMappingRenameParticipant.java
License:Open Source License
private ICompilationUnit getCompilationUnit(IJavaElement element) { if (element == null || element instanceof ICompilationUnit) { return (ICompilationUnit) element; }/*from w w w .j a v a2s .c om*/ return getCompilationUnit(element.getParent()); }
From source file:org.whole.lang.artifacts.util.ArtifactsWorkspaceUtils.java
License:Open Source License
public static IEntity toArtifactsPath(IJavaElement fromJavaElement, IJavaElement toJavaElement, IEntity child) { ArtifactsEntityFactory aef = ArtifactsEntityFactory.instance(RegistryConfigurations.RESOLVER); IEntity entity;/* w w w . jav a2 s.c o m*/ String name = toJavaElement.getElementName(); switch (toJavaElement.getElementType()) { case IJavaElement.COMPILATION_UNIT: FileArtifact fileArtifact = aef.createFileArtifact(); fileArtifact.setName(createFileName(name)); entity = fileArtifact; break; case IJavaElement.PACKAGE_FRAGMENT: if (name == IPackageFragment.DEFAULT_PACKAGE_NAME) return fromJavaElement.equals(toJavaElement) ? child : toArtifactsPath(fromJavaElement, toJavaElement.getParent(), child); PackageArtifact packageArtifact = aef.createPackageArtifact(); packageArtifact.setName(aef.createName(name)); entity = packageArtifact; break; case IJavaElement.PACKAGE_FRAGMENT_ROOT: FolderArtifact folderArtifact = aef.createFolderArtifact(); Metadata metadata = aef.createMetadata(); metadata.setAttributes(aef.createAttributes(aef.createAttribute(AttributeEnum.source))); folderArtifact.setMetadata(metadata); folderArtifact.setName(aef.createName(name)); entity = folderArtifact; break; case IJavaElement.JAVA_PROJECT: Project project = aef.createProject(); project.setName(aef.createName(name)); project.setNatures(aef.createNatures(aef.createNature("org.eclipse.jdt.core.javanature"))); project.setArtifacts(aef.createArtifacts(0)); entity = project; break; default: case IJavaElement.JAVA_MODEL: Workspace workspace = aef.createWorkspace(); workspace.setProjects(aef.createProjects(0)); entity = workspace; break; } boolean isWorkspace = Matcher.match(ArtifactsEntityDescriptorEnum.Workspace, entity); if (!EntityUtils.isNull(child)) entity.wGet(isWorkspace ? ArtifactsFeatureDescriptorEnum.projects : ArtifactsFeatureDescriptorEnum.artifacts).wAdd(child); return fromJavaElement.equals(toJavaElement) ? entity : toArtifactsPath(fromJavaElement, toJavaElement.getParent(), entity); }
From source file:org.whole.lang.ui.controls.JavaClassFileFilter.java
License:Open Source License
@Override public boolean select(Viewer viewer, Object parentElement, Object element) { if (!(element instanceof IJavaElement)) return false; IJavaElement javaElement = (IJavaElement) element; if (javaElement.getParent() instanceof IPackageFragment) return element instanceof ITypeRoot; else {// w w w . j a v a 2 s.c o m try { if (element instanceof IPackageFragment && ((IPackageFragment) element).getChildren().length == 0) return false; } catch (Exception e) { } return true; } }
From source file:pl.wroc.pwr.jbehaveplugin.configuration.wizard.NewConfigurationFileWizard.java
License:Open Source License
/** * Odnajduje (rekurencyjnie) nazw pakietu do podanego elementu Java. * //from w w w . ja v a2s . c om * @param element element Java * @return nazwa pakietu, jeeli domylny - <code>null</code> */ private static String findPackage(IJavaElement element) { if (element == null) { return null; } else if (element instanceof IPackageFragment) { // jeeli obiekt jest instancj IPackageFragment to znaczy, e jest // to szukany pakiet return ((IPackageFragment) element).getElementName(); } else { // w innym wypadku rekurencyjne przejcie w gr hierarchii // elementw return findPackage(element.getParent()); } }
From source file:qwickie.util.TypeHelper.java
License:Apache License
public static boolean isWicketJavaElement(final IJavaElement javaElement) throws JavaModelException { Assert.isNotNull(javaElement);//from www.j av a 2 s. c o m if (javaElement != null && javaElement instanceof NamedMember) { if (javaElement.getElementName().equals(DocumentHelper.GET_STRING)) { return true; } else if (javaElement.getElementType() == IJavaElement.TYPE) { final NamedMember method = (NamedMember) javaElement; final IType type = method.getTypeRoot().findPrimaryType(); return hierarchyContainsComponent(type); } else if (javaElement.getElementType() == IJavaElement.METHOD) { return isWicketComponent(javaElement); } return isWicketJavaElement(javaElement.getParent()); } return false; }
From source file:rabbit.tracking.internal.trackers.JavaTracker.java
License:Apache License
/** * Performs filtering of the data before saving. * <p>/*from w w w .j a va 2s .co m*/ * NOTE: Then a user starts to type a new java element, like a method, he/she * knows what the name he/she is going to type for the method, but we have no * way of knowing that, so lots of events may be recorded before he/she * finishes typing the name. For example, if the user want to type "hello" as * the method name, there will be events recorded about the java element * "hel", or "hell", or "hello", we only need one of them ("hello") but we * also want to keep the time about the invalid ones, so before we save the * data, we check for non-existent java elements, and instead of saving the * data under those elements, we save the data under the first existing parent * of the elements, if all parents are missing (e.g. deletes the file), we * save it under the file parent, like "File.java". * </p> */ private void filterData() { Set<JavaEvent> filteredData = Sets.newLinkedHashSet(); for (JavaEvent event : getData()) { IJavaElement e = event.getElement(); // ITypeRoot represents the file, xxx.java. Everything above that is not // modifiable in a JavaEditor, so no need to check them: if (!e.exists()) { for (; !e.exists() && !(e instanceof ITypeRoot); e = e.getParent()) ; filteredData.add(new JavaEvent(event.getInterval(), e)); } else { IJavaElement actual = null; try { actual = filterElement(e); } catch (JavaModelException ex) { actual = null; ex.printStackTrace(); } if (actual == null) { filteredData.add(event); } else { filteredData.add(new JavaEvent(event.getInterval(), actual)); } } } // Replace the old data with the filtered: flushData(); for (JavaEvent event : filteredData) { addData(event); } }
From source file:rabbit.tracking.internal.trackers.JavaTracker.java
License:Apache License
/** * Gets the actual element that we want before saving. One of the following * types is returned:/* w w w . j av a 2s. c om*/ * * <ul> * <li>A type that is not anonymous.</li> * <li>A method that is not enclosed in an anonymous type.</li> * <li>An initializer.</li> * <li>A compilation unit.</li> * <li>A class file.</li> * <li>Null</li> * </ul> * * @param element The element to filter. * @return A filtered element, or null if not found. * @throws JavaModelException If this element does not exist or if an * exception occurs while accessing its corresponding resource. */ private IJavaElement filterElement(@Nullable IJavaElement element) throws JavaModelException { if (element == null) { return null; } switch (element.getElementType()) { case IJavaElement.TYPE: if (((IType) element).isAnonymous()) { return filterElement(element.getParent()); } return element; case IJavaElement.METHOD: if (((IType) element.getParent()).isAnonymous()) { return filterElement(element.getParent()); } return element; case IJavaElement.INITIALIZER: case IJavaElement.COMPILATION_UNIT: case IJavaElement.CLASS_FILE: return element; default: return filterElement(element.getParent()); } }
From source file:rabbit.tracking.internal.trackers.JavaTrackerTest.java
License:Apache License
/** * Tests that if a Java element, a field for example, is deleted from the Java * source file, then when we save the tracker's data, the data about the * deleted field should be store as the parent's data. * <p>//from ww w . j a va2 s . c om * For example: If we have a field called "fieldA" under the class "ClassA" * and the tracker has recorded that the user has spent 20 seconds working on * the field, but the field is then deleted from the class. So when the * tracker saves the data, instead of saving * "The user has spent 20 seconds working on fieldA" we store * "The user has spent 20 seconds working on classA". * </p> * <p> * Another important purpose of this feature is that: Then a user starts to * type a new java element, like a method, he/she knows what the name he/she * is going to type for the method, but we have no way of knowing that, so * lots of events may be recorded before he/she finishes typing the name. For * example, if the user want to type "hello" as the method name, there will be * events recorded about the java element "hel", or "hell", or "hello", we * only need one of them ("hello") but we also want to keep the time about the * invalid ones, so before we save the data, we check for non-existent java * elements, and instead of saving the data under those elements, we save the * data under the first existing parent of the elements, if all parents are * missing (e.g. deletes the file), we save it under the file parent, like * "File.java", even though the file has been deleted. * </p> * * @see #testFilter_deletedElement_mainType() */ @Test public void testFilter_deletedElement_typeMembers() throws Exception { final JavaEditor editor = closeAndOpenEditor(); final IDocument document = getDocument(editor); // Place a field in the body of the class, note that we don't want to add // errors to the class: String field = "private int aVeryUniqueFieldName = 0;"; int offset = document.get().lastIndexOf('}') - 1; int len = 0; document.replace(offset, len, field); // Set the editor to select the field: offset = document.get().indexOf(field); len = field.length(); editor.getSelectionProvider().setSelection(new TextSelection(offset, len)); // Run the tracker to capture the event: long preStart = System.currentTimeMillis(); tracker.setEnabled(true); long postStart = System.currentTimeMillis(); Thread.sleep(25); long preEnd = System.currentTimeMillis(); tracker.setEnabled(false); long postEnd = System.currentTimeMillis(); // Keeps a reference to the field statement first, for testing latter: final IJavaElement element = getElementAtOffset(editor); // Now delete the field statement from the source file, note that there // is no need to save the document (and we should not save the document, // other tests may depend on it): offset = document.get().indexOf(field); len = field.length(); document.replace(offset, len, ""); // Ask the tracker to save the data, the data should be appropriately // filtered tracker.saveData(); // Gets the data, the data is remained in the tracker as long as we don't // enable it again (according to the contract of the tracker): // // One data should be in the collection // (the parent of the selected package declaration): assertEquals(1, tracker.getData().size()); JavaEvent event = tracker.getData().iterator().next(); long start = event.getInterval().getStartMillis(); long end = event.getInterval().getEndMillis(); checkTime(preStart, start, postStart, preEnd, end, postEnd); // Now check that the element is the parent of the package declaration // instead of the deleted package declaration itself: assertEquals(element.getParent(), event.getElement()); }
From source file:rabbit.tracking.internal.trackers.JavaTrackerTest.java
License:Apache License
/** * Test an event on an package declaration. This event should be filtered on * save, so that instead of showing a user spent x amount of time on the * package declaration , we show that a user spent x amount of time on the * main type element.//from ww w .j a v a 2 s . c om */ @Test public void testFilter_existingElement_packageDeclaration() throws Exception { JavaEditor editor = closeAndOpenEditor(); IDocument document = getDocument(editor); int offset = document.get().indexOf(pkg.getElementName()); int len = pkg.getElementName().length(); editor.getSelectionProvider().setSelection(new TextSelection(offset, len)); IJavaElement element = getElementAtOffset(editor); // Make sure we got the selection right: assertEquals(IJavaElement.PACKAGE_DECLARATION, element.getElementType()); long preStart = System.currentTimeMillis(); tracker.setEnabled(true); long postStart = System.currentTimeMillis(); Thread.sleep(30); long preEnd = System.currentTimeMillis(); tracker.setEnabled(false); long postEnd = System.currentTimeMillis(); // Ask the tracker to save the data, the data should be appropriately // filtered tracker.saveData(); assertEquals(1, tracker.getData().size()); JavaEvent event = tracker.getData().iterator().next(); long start = event.getInterval().getStartMillis(); long end = event.getInterval().getEndMillis(); checkTime(preStart, start, postStart, preEnd, end, postEnd); assertEquals(element.getParent(), event.getElement()); }
From source file:rabbit.tracking.internal.trackers.JavaTrackerTest.java
License:Apache License
/** * Test an event on a field. This event should be filtered on save, so that * instead of showing a user spent x amount of time on the field, we show that * a user spent x amount of time on the field's parent type. *//*from w ww . ja v a2s . com*/ @Test public void testFilter_exsitingElement_field() throws Exception { JavaEditor editor = closeAndOpenEditor(); IDocument document = getDocument(editor); String fieldName = "aFieldName"; String methodText = format("private int %s = 1;", fieldName); int offset = document.get().indexOf("{") + 1; int len = 0; document.replace(offset, len, methodText); offset = document.get().indexOf(fieldName); len = fieldName.length(); editor.getSelectionProvider().setSelection(new TextSelection(offset, len)); IJavaElement element = getElementAtOffset(editor); assertEquals(IJavaElement.FIELD, element.getElementType()); long preStart = System.currentTimeMillis(); tracker.setEnabled(true); long postStart = System.currentTimeMillis(); Thread.sleep(20); long preEnd = System.currentTimeMillis(); tracker.setEnabled(false); long postEnd = System.currentTimeMillis(); // Ask the tracker to save the data, the data should be appropriately // filtered tracker.saveData(); assertEquals(1, tracker.getData().size()); JavaEvent event = tracker.getData().iterator().next(); long start = event.getInterval().getStartMillis(); long end = event.getInterval().getEndMillis(); checkTime(preStart, start, postStart, preEnd, end, postEnd); // The filtered event should be on the field's parent, not on the field // itself: assertEquals(element.getParent(), event.getElement()); }