Search in sources :

Example 91 with IJavaElement

use of org.eclipse.jdt.core.IJavaElement in project xtext-xtend by eclipse.

the class DetectImplHyperlinksTest method testComputeHyperlink_2.

@Test
public void testComputeHyperlink_2() throws Exception {
    String content = "package foo class Foo { def bar(String a) { fo|o() } def foo(){}}";
    XtextEditor xtextEditor = openEditor(content.replace("|", ""));
    int offset = content.indexOf("|");
    IHyperlink[] detectHyperlinks = hyperlinkDetector.detectHyperlinks(xtextEditor.getInternalSourceViewer(), new Region(offset, 1), true);
    assertEquals(3, detectHyperlinks.length);
    XbaseImplementatorsHyperlink hyperlink = Iterables.filter(Lists.newArrayList(detectHyperlinks), XbaseImplementatorsHyperlink.class).iterator().next();
    assertEquals(offset - 2, hyperlink.getHyperlinkRegion().getOffset());
    assertEquals(3, hyperlink.getHyperlinkRegion().getLength());
    IJavaElement element = hyperlink.getElement();
    assertTrue(element instanceof IMethod);
    assertEquals("foo", element.getElementName());
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) XbaseImplementatorsHyperlink(org.eclipse.xtext.xbase.ui.navigation.XbaseImplementatorsHyperlink) Region(org.eclipse.jface.text.Region) IMethod(org.eclipse.jdt.core.IMethod) Test(org.junit.Test) AbstractEditorTest(org.eclipse.xtext.ui.testing.AbstractEditorTest)

Example 92 with IJavaElement

use of org.eclipse.jdt.core.IJavaElement in project AutoRefactor by JnRouvignac.

the class SuperCallRatherThanUselessOverridingRefactoring method isMethodUsedInItsPackage.

/**
 * This method is extremely expensive.
 */
private boolean isMethodUsedInItsPackage(IMethodBinding methodBinding, MethodDeclaration node) {
    final IPackageBinding methodPackage = methodBinding.getDeclaringClass().getPackage();
    final AtomicBoolean methodIsUsedInPackage = new AtomicBoolean(false);
    final SearchRequestor requestor = new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) {
            methodIsUsedInPackage.set(true);
        }
    };
    try {
        final SearchEngine searchEngine = new SearchEngine();
        searchEngine.search(createPattern(methodBinding.getJavaElement(), REFERENCES, R_EXACT_MATCH), new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, SearchEngine.createJavaSearchScope(new IJavaElement[] { methodPackage.getJavaElement() }), requestor, ctx.getProgressMonitor());
        return methodIsUsedInPackage.get();
    } catch (CoreException e) {
        throw new UnhandledException(node, e);
    }
}
Also used : SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) UnhandledException(org.autorefactor.util.UnhandledException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IJavaElement(org.eclipse.jdt.core.IJavaElement) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) IPackageBinding(org.eclipse.jdt.core.dom.IPackageBinding)

Example 93 with IJavaElement

use of org.eclipse.jdt.core.IJavaElement in project AutoRefactor by JnRouvignac.

the class PrepareApplyRefactoringsJob method addAll.

private void addAll(Queue<RefactoringUnit> results, List<IJavaElement> javaElements, IProgressMonitor monitor) throws JavaModelException {
    final SubMonitor subMonitor = SubMonitor.convert(monitor, javaElements.size());
    for (IJavaElement javaElement : javaElements) {
        final SubMonitor child = subMonitor.newChild(1);
        final JavaProjectOptions options = getJavaProjectOptions(javaElement);
        if (javaElement instanceof ICompilationUnit) {
            add(results, (ICompilationUnit) javaElement, options);
        } else if (javaElement instanceof IPackageFragment) {
            final IPackageFragment pf = (IPackageFragment) javaElement;
            addAll(results, getSubPackages(pf), child);
            addAll(results, pf.getCompilationUnits(), options);
        } else if (javaElement instanceof IPackageFragmentRoot) {
            final IPackageFragmentRoot pfr = (IPackageFragmentRoot) javaElement;
            addAll(results, Arrays.asList(pfr.getChildren()), child);
        } else if (javaElement instanceof IJavaProject) {
            IJavaProject javaProject = (IJavaProject) javaElement;
            for (IPackageFragment pf : javaProject.getPackageFragments()) {
                addAll(results, pf.getCompilationUnits(), options);
            }
        }
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IJavaProject(org.eclipse.jdt.core.IJavaProject) SubMonitor(org.eclipse.core.runtime.SubMonitor) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 94 with IJavaElement

use of org.eclipse.jdt.core.IJavaElement in project AutoRefactor by JnRouvignac.

the class PrepareApplyRefactoringsJob method getSubPackages.

private List<IJavaElement> getSubPackages(IPackageFragment motherPackage) throws JavaModelException {
    List<IJavaElement> subPackages = new ArrayList<IJavaElement>();
    String packageName = motherPackage.getElementName();
    IJavaElement[] packages = ((IPackageFragmentRoot) motherPackage.getParent()).getChildren();
    for (IJavaElement onePackage : packages) {
        if (onePackage instanceof IPackageFragment && onePackage.getElementName().startsWith(packageName + ".")) {
            subPackages.add(onePackage);
        }
    }
    return subPackages;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ArrayList(java.util.ArrayList) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 95 with IJavaElement

use of org.eclipse.jdt.core.IJavaElement in project xtext-xtend by eclipse.

the class DispatchMethodRenameStrategy method initialize.

@Override
public boolean initialize(EObject xtendMethod, IRenameElementContext context) {
    Assert.isLegal(xtendMethod instanceof XtendFunction);
    Assert.isLegal(((XtendFunction) xtendMethod).isDispatch());
    Assert.isLegal(context instanceof DispatchMethodRenameContext);
    ResourceSet resourceSet = xtendMethod.eResource().getResourceSet();
    Map<URI, IJavaElement> jvm2JavaElements = ((DispatchMethodRenameContext) context).getJvm2JavaElements();
    for (URI dispatchOperationURI : jvm2JavaElements.keySet()) {
        JvmOperation dispatchOperation = (JvmOperation) resourceSet.getEObject(dispatchOperationURI, true);
        XtendFunction xtendDispatchMethod = associations.getXtendFunction(dispatchOperation);
        if (xtendDispatchMethod != null) {
            if (equal(xtendDispatchMethod.getName(), dispatchOperation.getSimpleName())) {
                // synthetic dispatcher
                dispatchers.add(dispatchOperation);
            } else {
                // xtend dispatch method
                XtendDispatchMethodChildStrategy xtendChildStrategy = childStrategyProvider.get();
                xtendChildStrategy.initialize(xtendDispatchMethod, context);
                children.add(xtendChildStrategy);
            }
        } else {
            // a dispatch method form a Java class
            JavaDispatchMethodChildStrategy jvmChildStrategy = javaStrategyProvider.get();
            jvmChildStrategy.initialize(dispatchOperation, context);
            children.add(jvmChildStrategy);
        }
    }
    return !children.isEmpty();
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) IJavaElement(org.eclipse.jdt.core.IJavaElement) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URI(org.eclipse.emf.common.util.URI)

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