Example usage for org.eclipse.jdt.core IJavaElement getPath

List of usage examples for org.eclipse.jdt.core IJavaElement getPath

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaElement getPath.

Prototype

IPath getPath();

Source Link

Document

Returns the path to the innermost resource enclosing this element.

Usage

From source file:org.jboss.mapper.eclipse.internal.util.JavaUtil.java

License:Open Source License

/**
 * Returns the resource path relative to its containing
 * IPackageFragmentRoot. If the resource is not located within a Java source
 * directory, the project name is stripped from the path.
 * /* w ww  .ja va 2s  . c  om*/
 * @param resource the resource.
 * 
 * @return the relative path.
 */
public static IPath getJavaPathForResource(final IResource resource) {
    if (resource == null || resource.getType() == IResource.PROJECT || resource.getType() == IResource.ROOT) {
        return null;
    }
    IJavaProject project = JavaCore.create(resource.getProject());
    if (project == null) {
        // just remove the project segment.
        return resource.getFullPath().removeFirstSegments(1);
    }
    IResource container = resource;
    if (container.getType() == IResource.FILE) {
        container = container.getParent();
    }
    IJavaElement element = null;
    for (; element == null && container != null; container = container.getParent()) {
        element = JavaCore.create(container, project);
    }
    if (element == null) {
        return resource.getFullPath().removeFirstSegments(1);
    } else if (element.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
        return resource.getFullPath().makeRelativeTo(element.getParent().getPath());
    }
    return resource.getFullPath().makeRelativeTo(element.getPath());
}

From source file:org.jboss.mapper.eclipse.internal.util.Util.java

License:Open Source License

private static void populateClasses(final Shell shell, final IParent parent, final List<IType> types,
        final Filter filter) {
    try {//from w  w w. j  a  v a  2 s.  c  o m
        for (final IJavaElement element : parent.getChildren()) {
            if (element instanceof IType) {
                final IType type = (IType) element;
                if (type.isClass() && type.isStructureKnown() && !type.isAnonymous() && !type.isLocal()
                        && !Flags.isAbstract(type.getFlags()) && Flags.isPublic(type.getFlags())
                        && (filter == null || filter.accept(type))) {
                    types.add(type);
                }
            } else if (element instanceof IParent && !element.getPath().toString().contains("/test/")
                    && (!(element instanceof IPackageFragmentRoot)
                            || !((IPackageFragmentRoot) element).isExternal())) {
                populateClasses(shell, (IParent) element, types, filter);
            }
        }
    } catch (final JavaModelException e) {
        Activator.error(e);
    }
}

From source file:org.jboss.tools.cdi.ui.search.InjectionPointQueryParticipant.java

License:Open Source License

public void search(ISearchRequestor requestor, QuerySpecification querySpecification, IProgressMonitor monitor)
        throws CoreException {
    objects.clear();//  ww w. j a va  2  s. c om

    if (querySpecification instanceof ElementQuerySpecification) {
        if (!isSearchForReferences(querySpecification.getLimitTo()))
            return;

        ElementQuerySpecification qs = (ElementQuerySpecification) querySpecification;
        IJavaElement element = qs.getElement();
        if (element instanceof IMethod || element instanceof IField || element instanceof ILocalVariable) {
            IProject project = element.getJavaProject().getProject();
            if (project == null)
                return;

            CDICoreNature cdiNature = CDICorePlugin.getCDI(project, true);

            if (cdiNature == null)
                return;

            ICDIProject cdiProject = cdiNature.getDelegate();

            if (cdiProject == null)
                return;

            Collection<IBean> beans = cdiProject.getBeans(element.getPath());

            IInjectionPoint injectionPoint = CDIUtil.findInjectionPoint(beans, element, 0);
            if (injectionPoint != null) {
                List<IBean> resultBeanList = CDIUtil.sortBeans(cdiProject.getBeans(false, injectionPoint));
                for (IBean bean : resultBeanList) {
                    if (bean != null && containsInSearchScope(querySpecification, bean)) {
                        CDIMatch match = new CDIMatch(bean);
                        if (!objects.contains(match.getPath())) {
                            requestor.reportMatch(match);
                            objects.add(match.getPath());
                        }
                    }
                }
                resolveObserverMethods(cdiProject, injectionPoint, requestor, querySpecification);
            }
            if (element instanceof IMethod) {
                IParameter param = findObserverParameter(beans, (IMethod) element);
                if (param != null) {
                    findObservedEvents(cdiProject, param, requestor, querySpecification);
                }
            }
        }
    }
}

From source file:org.jboss.tools.fuse.transformation.editor.internal.util.Util.java

License:Open Source License

private static void populateClasses(final Shell shell, final IParent parent, final List<IType> types,
        final Filter filter) {
    try {//www .  j a va  2s. co m
        for (final IJavaElement element : parent.getChildren()) {
            if (element instanceof IType) {
                final IType type = (IType) element;
                if (type.isClass() && type.isStructureKnown() && !type.isAnonymous() && !type.isLocal()
                        && !Flags.isAbstract(type.getFlags()) && Flags.isPublic(type.getFlags())
                        && (filter == null || filter.accept(type))) {
                    types.add(type);
                }
            } else if (element instanceof IParent) {
                String path = element.getPath().toString();
                if (!path.contains("/test/") //$NON-NLS-1$
                        && !path.endsWith("/.functions") && !path.endsWith("/" + TRANSFORMATIONS_FOLDER) //$NON-NLS-1$ //$NON-NLS-2$
                        && (!(element instanceof IPackageFragmentRoot)
                                || !((IPackageFragmentRoot) element).isExternal()))
                    populateClasses(shell, (IParent) element, types, filter);
            }
        }
    } catch (final JavaModelException e) {
        Activator.error(e);
    }
}

From source file:org.jboss.tools.ws.creation.core.utils.JBossWSCreationUtils.java

License:Open Source License

/**
 * find compilationunit by annotation/*w ww .  j  av a2s.  com*/
 * 
 * @param project
 * @param annotation
 * @return
 */
public static List<ICompilationUnit> findJavaUnitsByAnnotation(IJavaProject project, String annotation,
        String packageName) {
    List<ICompilationUnit> units = new LinkedList<ICompilationUnit>();
    try {
        IPath path = addPackagetoPath(project, packageName);
        if (path == null) {
            IResource[] resources = JBossWSCreationUtils.getJavaSourceRoots(project);
            if (resources != null && resources.length > 0) {
                IJavaElement[] elements = project.getPackageFragmentRoot(resources[0]).getChildren();
                for (IJavaElement element : elements) {
                    if (IJavaElement.PACKAGE_FRAGMENT == element.getElementType()) {
                        findInPackageFragment(units, element.getPath(), project, annotation);
                    }
                }
            }
        } else {
            findInPackageFragment(units, path, project, annotation);
        }
    } catch (JavaModelException e) {
        JBossWSCreationCorePlugin.getDefault().logError(e);
    }
    return units;
}

From source file:org.jboss.tools.ws.jaxws.ui.utils.JBossWSCreationUtils.java

License:Open Source License

/**
 * find compilationunit by annotation// w  w w.  ja v  a2  s. c  o  m
 * 
 * @param project
 * @param annotation
 * @return
 */
public static List<ICompilationUnit> findJavaUnitsByAnnotation(IJavaProject project, String annotation,
        String packageName) {
    List<ICompilationUnit> units = new LinkedList<ICompilationUnit>();
    try {
        IPath path = addPackagetoPath(project, packageName);
        if (path == null) {
            IResource[] resources = JBossWSCreationUtils.getJavaSourceRoots(project);
            if (resources != null && resources.length > 0) {
                IJavaElement[] elements = project.getPackageFragmentRoot(resources[0]).getChildren();
                for (IJavaElement element : elements) {
                    if (IJavaElement.PACKAGE_FRAGMENT == element.getElementType()) {
                        findInPackageFragment(units, element.getPath(), project, annotation);
                    }
                }
            }
        } else {
            findInPackageFragment(units, path, project, annotation);
        }
    } catch (JavaModelException e) {
        JBossJAXWSUIPlugin.getDefault().logError(e);
    }
    return units;
}

From source file:org.jetbrains.kotlin.core.resolve.lang.java.structure.EclipseJavaElementUtil.java

License:Apache License

public static boolean isKotlinLightClass(@NotNull IJavaElement element) {
    IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(element.getPath());
    if (resource == null) {
        return false;
    }/*w  w w  . j a  v  a  2s . co m*/

    IContainer parent = resource.getParent();
    while (parent != null) {
        if (KotlinFileSystem.SCHEME.equals(parent.getLocationURI().getScheme())) {
            return true;
        }
        parent = parent.getParent();
    }

    return false;
}

From source file:org.jetbrains.kotlin.wizards.NewUnitWizardPage.java

License:Apache License

private String getSourceFolderFromSelection() {
    String defaultFolder = DEFAULT_SOURCE_FOLDER;

    if (selection.isEmpty()) {
        return defaultFolder;
    }/*from w w w  . j  a  va 2  s  .co m*/

    Object selectedObject = selection.getFirstElement();

    if (selectedObject instanceof IJavaElement) {
        IJavaElement selectedJavaElement = (IJavaElement) selectedObject;
        switch (selectedJavaElement.getElementType()) {
        case IJavaElement.JAVA_PROJECT:
            return getDefaultSrcByProject((IJavaProject) selectedJavaElement);

        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            return selectedJavaElement.getPath().toPortableString();

        case IJavaElement.PACKAGE_FRAGMENT:
        case IJavaElement.COMPILATION_UNIT:
            return selectedJavaElement.getPath().uptoSegment(2).toPortableString();
        }
    } else if (selectedObject instanceof IResource) {
        IResource selectedResource = (IResource) selectedObject;
        switch (selectedResource.getType()) {
        case IResource.FOLDER:
            return getDefaultSrcByProject(JavaCore.create(selectedResource.getProject()));

        case IResource.FILE:
            return selectedResource.getFullPath().uptoSegment(2).toPortableString();
        }
    }

    return defaultFolder;
}

From source file:org.jetbrains.kotlin.wizards.NewUnitWizardPage.java

License:Apache License

private String getPackageFromSelection() {
    String defaultPackage = DEFAULT_PACKAGE;

    if (selection.isEmpty()) {
        return defaultPackage;
    }/*w  w w.  j  a v  a 2s. c o  m*/

    Object selectedObject = selection.getFirstElement();

    if (selectedObject instanceof IJavaElement) {
        IJavaElement selectedJavaElement = (IJavaElement) selectedObject;
        switch (selectedJavaElement.getElementType()) {
        case IJavaElement.PACKAGE_FRAGMENT:
            return selectedJavaElement.getElementName();

        case IJavaElement.COMPILATION_UNIT:
            try {
                return selectedJavaElement.getJavaProject()
                        .findPackageFragment(selectedJavaElement.getPath().makeAbsolute().removeLastSegments(1))
                        .getElementName();
            } catch (Exception e) {
                KotlinLogger.logAndThrow(e);
            }
            break;
        }
    } else if (selectedObject instanceof IResource) {
        IResource selectedResource = (IResource) selectedObject;
        switch (selectedResource.getType()) {
        case IResource.FILE:
            try {
                return JavaCore.create(selectedResource.getProject())
                        .findPackageFragment(
                                selectedResource.getFullPath().makeAbsolute().removeLastSegments(1))
                        .getElementName();
            } catch (Exception e) {
                KotlinLogger.logAndThrow(e);
            }
            break;
        }
    }

    return defaultPackage;
}

From source file:org.key_project.key4eclipse.starter.core.util.KeYUtil.java

License:Open Source License

/**
 * <p>/*  ww w.  j av a  2  s  . co  m*/
 * Opens an {@link IDocument} for the source file defined by the given {@link IJavaElement}
 * and executes some code on it defined via the give {@link IRunnableWithDocument}.
 * </p>
 * <p>
 * If the {@link IDocument} is already opened its {@link IDocument} is used.
 * If it is not opened it is opened and closed after executing the {@link IRunnableWithDocument}.
 * </p>
 * @param element The {@link IJavaElement} to open its source file.
 * @param run The {@link IRunnableWithDocument} to execute on the opened {@link IDocument}.
 * @return {@code true} {@link IRunnableWithDocument} was executed, {@code false} {@link IRunnableWithDocument} was not executed.
 * @throws CoreException Occurred Exception.
 */

public static boolean runOnDocument(IJavaElement element, IRunnableWithDocument run) throws CoreException {
    if (element != null) {
        return runOnDocument(element.getPath(), run);
    } else {
        return false;
    }
}