Search in sources :

Example 76 with IJavaElement

use of org.eclipse.jdt.core.IJavaElement in project che by eclipse.

the class JavaSearchScopeFactory method addJavaElements.

private void addJavaElements(Set<IJavaElement> javaElements, IWorkingSet workingSet) {
    if (workingSet == null)
        return;
    if (workingSet.isAggregateWorkingSet() && workingSet.isEmpty()) {
        try {
            IJavaProject[] projects = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
            javaElements.addAll(Arrays.asList(projects));
        } catch (JavaModelException e) {
            JavaPlugin.log(e);
        }
        return;
    }
    IAdaptable[] elements = workingSet.getElements();
    for (int i = 0; i < elements.length; i++) {
        IJavaElement javaElement = (IJavaElement) elements[i].getAdapter(IJavaElement.class);
        if (javaElement != null) {
            addJavaElements(javaElements, javaElement);
            continue;
        }
        IResource resource = (IResource) elements[i].getAdapter(IResource.class);
        if (resource != null) {
            addJavaElements(javaElements, resource);
        }
    // else we don't know what to do with it, ignore.
    }
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) IResource(org.eclipse.core.resources.IResource)

Example 77 with IJavaElement

use of org.eclipse.jdt.core.IJavaElement in project che by eclipse.

the class JavaSearchScopeFactory method addJavaElements.

private void addJavaElements(Set<IJavaElement> javaElements, IResource resource) {
    IJavaElement javaElement = (IJavaElement) resource.getAdapter(IJavaElement.class);
    if (javaElement == null)
        // not a Java resource
        return;
    if (javaElement.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
        // add other possible package fragments
        try {
            addJavaElements(javaElements, ((IFolder) resource).members());
        } catch (CoreException ex) {
        // don't add elements
        }
    }
    javaElements.add(javaElement);
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) CoreException(org.eclipse.core.runtime.CoreException)

Example 78 with IJavaElement

use of org.eclipse.jdt.core.IJavaElement in project che by eclipse.

the class Rename18Test method renameLocalVariable.

private void renameLocalVariable(String newFieldName, boolean updateReferences) throws Exception {
    ParticipantTesting.reset();
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), "A");
    ISourceRange selection = getSelection(cu);
    IJavaElement[] elements = cu.codeSelect(selection.getOffset(), selection.getLength());
    assertEquals(1, elements.length);
    RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_LOCAL_VARIABLE);
    descriptor.setJavaElement(elements[0]);
    descriptor.setNewName(newFieldName);
    descriptor.setUpdateReferences(updateReferences);
    descriptor.setUpdateTextualOccurrences(false);
    RenameRefactoring refactoring = (RenameRefactoring) createRefactoring(descriptor);
    List list = new ArrayList();
    list.add(elements[0]);
    List args = new ArrayList();
    args.add(new RenameArguments(newFieldName, updateReferences));
    String[] renameHandles = ParticipantTesting.createHandles(list.toArray());
    RefactoringStatus result = performRefactoring(refactoring);
    assertEquals("was supposed to pass", null, result);
    assertEqualLines("invalid renaming", getFileContents(getOutputTestFileName("A")), cu.getSource());
    ParticipantTesting.testRename(renameHandles, (RenameArguments[]) args.toArray(new RenameArguments[args.size()]));
    assertTrue("anythingToUndo", RefactoringCore.getUndoManager().anythingToUndo());
    assertTrue("! anythingToRedo", !RefactoringCore.getUndoManager().anythingToRedo());
    RefactoringCore.getUndoManager().performUndo(null, new NullProgressMonitor());
    assertEqualLines("invalid undo", getFileContents(getInputTestFileName("A")), cu.getSource());
    assertTrue("! anythingToUndo", !RefactoringCore.getUndoManager().anythingToUndo());
    assertTrue("anythingToRedo", RefactoringCore.getUndoManager().anythingToRedo());
    RefactoringCore.getUndoManager().performRedo(null, new NullProgressMonitor());
    assertEqualLines("invalid redo", getFileContents(getOutputTestFileName("A")), cu.getSource());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) RenameArguments(org.eclipse.ltk.core.refactoring.participants.RenameArguments) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) ArrayList(java.util.ArrayList) List(java.util.List) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 79 with IJavaElement

use of org.eclipse.jdt.core.IJavaElement in project che by eclipse.

the class RenameTypeTest method helperWithTextual.

private String[] helperWithTextual(String oldCuName, String oldName, String newName, String newCUName, boolean updateReferences, boolean updateTextualMatches) throws Exception {
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), oldCuName);
    IType classA = getType(cu, oldName);
    IJavaElement[] classAMembers = classA.getChildren();
    IPackageFragment pack = (IPackageFragment) cu.getParent();
    String[] renameHandles = null;
    if (classA.getDeclaringType() == null && cu.getElementName().startsWith(classA.getElementName())) {
        renameHandles = ParticipantTesting.createHandles(classA, cu, cu.getResource());
    } else {
        renameHandles = ParticipantTesting.createHandles(classA);
    }
    RenameJavaElementDescriptor descriptor = createRefactoringDescriptor(classA, newName);
    descriptor.setUpdateReferences(updateReferences);
    descriptor.setUpdateTextualOccurrences(updateTextualMatches);
    Refactoring refactoring = createRefactoring(descriptor);
    assertEquals("was supposed to pass", null, performRefactoring(refactoring));
    ICompilationUnit newcu = pack.getCompilationUnit(newCUName + ".java");
    TestCase.assertTrue("cu " + newcu.getElementName() + " does not exist", newcu.exists());
    assertEqualLines("invalid renaming", getFileContents(getOutputTestFileName(newCUName)), newcu.getSource());
    INameUpdating nameUpdating = ((INameUpdating) refactoring.getAdapter(INameUpdating.class));
    IType newElement = (IType) nameUpdating.getNewElement();
    TestCase.assertTrue("new element does not exist:\n" + newElement.toString(), newElement.exists());
    checkMappers(refactoring, classA, newCUName + ".java", classAMembers);
    return renameHandles;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) INameUpdating(org.eclipse.jdt.internal.corext.refactoring.tagging.INameUpdating) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) Refactoring(org.eclipse.ltk.core.refactoring.Refactoring) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) IType(org.eclipse.jdt.core.IType)

Example 80 with IJavaElement

use of org.eclipse.jdt.core.IJavaElement in project che by eclipse.

the class RenameTypeTest method checkMappedSimilarElementsExist.

private void checkMappedSimilarElementsExist(Refactoring ref) {
    RenameTypeProcessor rtp = (RenameTypeProcessor) ((RenameRefactoring) ref).getProcessor();
    IJavaElementMapper mapper = (IJavaElementMapper) rtp.getAdapter(IJavaElementMapper.class);
    IJavaElement[] similarElements = rtp.getSimilarElements();
    if (similarElements == null)
        return;
    for (int i = 0; i < similarElements.length; i++) {
        IJavaElement element = similarElements[i];
        if (!(element instanceof ILocalVariable)) {
            IJavaElement newElement = mapper.getRefactoredJavaElement(element);
            TestCase.assertTrue(newElement.exists());
            Assert.assertFalse(element.exists());
        }
    }
}
Also used : ILocalVariable(org.eclipse.jdt.core.ILocalVariable) IJavaElement(org.eclipse.jdt.core.IJavaElement) RenameTypeProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.RenameTypeProcessor) IJavaElementMapper(org.eclipse.jdt.core.refactoring.IJavaElementMapper)

Aggregations

IJavaElement (org.eclipse.jdt.core.IJavaElement)226 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)49 IType (org.eclipse.jdt.core.IType)44 ArrayList (java.util.ArrayList)35 IJavaProject (org.eclipse.jdt.core.IJavaProject)35 IMethod (org.eclipse.jdt.core.IMethod)34 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)32 JavaModelException (org.eclipse.jdt.core.JavaModelException)32 IResource (org.eclipse.core.resources.IResource)30 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)28 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)23 IField (org.eclipse.jdt.core.IField)16 IMember (org.eclipse.jdt.core.IMember)14 Test (org.junit.Test)14 CoreException (org.eclipse.core.runtime.CoreException)13 StringTokenizer (java.util.StringTokenizer)11 ISourceRange (org.eclipse.jdt.core.ISourceRange)11 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)11 IPath (org.eclipse.core.runtime.IPath)10 HashMap (java.util.HashMap)9