Example usage for org.eclipse.jdt.core IJavaProject findElement

List of usage examples for org.eclipse.jdt.core IJavaProject findElement

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaProject findElement.

Prototype

IJavaElement findElement(IPath path) throws JavaModelException;

Source Link

Document

Returns the IJavaElement corresponding to the given classpath-relative path, or null if no such IJavaElement is found.

Usage

From source file:org.gw4e.eclipse.test.facade.ResourceManagerTest.java

License:Open Source License

@Test
public void testGetPackageFragmentRoot() throws Exception {
    IJavaProject project = ProjectHelper.getOrCreateSimpleGW4EProject(PROJECT_NAME, true, false);
    ResourceManager.createFile(project.getProject().getName(), "src/test/resources", "mypkg/subpkg",
            "test.java", "");
    IPackageFragment frag = (IPackageFragment) project.findElement(new Path("mypkg/subpkg"));

    IPackageFragmentRoot pfr = ResourceManager.getPackageFragmentRoot(project.getProject(), frag);
    assertNotNull(pfr);/*ww w  .  j  a v a 2  s  .co m*/
}

From source file:org.gw4e.eclipse.test.facade.ResourceManagerTest.java

License:Open Source License

@Test
public void testGetPackageFragment() throws Exception {
    IJavaProject project = ProjectHelper.getOrCreateSimpleGW4EProject(PROJECT_NAME, true, false);
    ResourceManager.createFile(project.getProject().getName(), "src/test/resources", "mypkg/subpkg",
            "test.java", "");
    IPackageFragment pf = ResourceManager.getPackageFragment(project.getProject(),
            new Path("src/test/resources/mypkg/subpkg"));
    assertNotNull(pf);/*from  w  w  w  . jav a 2  s .co  m*/

    IPackageFragment frag = (IPackageFragment) project.findElement(new Path("mypkg/subpkg"));
    IPackageFragmentRoot pfr = ResourceManager.getPackageFragmentRoot(project.getProject(), frag);
    pf = ResourceManager.getPackageFragment(project.getProject(), pfr.getPath().removeFirstSegments(1));
    assertNotNull(pf);
}

From source file:org.gw4e.eclipse.test.facade.ResourceManagerTest.java

License:Open Source License

@Test
public void testGetSelectedPathInProject() throws Exception {
    IJavaProject project = ProjectHelper.getOrCreateSimpleGW4EProject(PROJECT_NAME, true, false);
    ResourceManager.createFile(project.getProject().getName(), "src/test/resources", "mypkg/subpkg",
            "test.java", "");

    IPackageFragment frag = (IPackageFragment) project.findElement(new Path("mypkg/subpkg"));
    IPackageFragmentRoot pfr = ResourceManager.getPackageFragmentRoot(project.getProject(), frag);
    String s = ResourceManager.getSelectedPathInProject(pfr);
    assertEquals("src/test/resources", s);
}

From source file:org.gw4e.eclipse.test.facade.ResourceManagerTest.java

License:Open Source License

@Test
public void testIsPackageFragmentRoot() throws Exception {
    IJavaProject project = ProjectHelper.getOrCreateSimpleGW4EProject(PROJECT_NAME, true, false);
    ResourceManager.createFile(project.getProject().getName(), "src/test/resources", "mypkg/subpkg",
            "test.java", "");

    IPackageFragment frag = (IPackageFragment) project.findElement(new Path("mypkg/subpkg"));
    IPackageFragmentRoot pfr = ResourceManager.getPackageFragmentRoot(project.getProject(), frag);

    boolean b = ResourceManager.isPackageFragmentRoot(pfr);
    assertTrue(b);/* w ww  . j a  v  a  2 s .co m*/

    b = ResourceManager.isPackageFragmentRoot(project);
    assertFalse(b);
}

From source file:org.hibernate.eclipse.jdt.ui.internal.jpa.common.Utils.java

License:Open Source License

static public ICompilationUnit[] findCompilationUnits(IJavaProject javaProject, IPath path) {
    IJavaElement javaElement = null;// w  ww . j a v  a  2s .c o  m
    try {
        javaElement = javaProject.findElement(path.makeRelative());
    } catch (JavaModelException e) {
        // just ignore it!
        //HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e); //$NON-NLS-1$
    }
    ICompilationUnit[] res = null;
    if (javaElement != null) {
        if (javaElement instanceof ICompilationUnit) {
            res = new ICompilationUnit[] { (ICompilationUnit) javaElement };
        } else if (javaElement instanceof IPackageFragment) {
            try {
                res = ((IPackageFragment) javaElement).getCompilationUnits();
            } catch (JavaModelException e) {
                // just ignore it!
                //HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e); //$NON-NLS-1$
            }
        } else if (javaElement instanceof IClassFile) {
        }
    }
    return res;
}

From source file:org.jactr.eclipse.core.parser.ProjectSensitiveParserImportDelegate.java

License:Open Source License

/**
 * we've found the valid class name from the previous call to isValidClassName
 * if the super implementation can't find it, we check for any extensions that
 * provide one..//from ww w .ja  v a 2  s. c om
 * 
 * @param moduleClassName
 * @return
 */
@Override
protected IASTParticipant getASTParticipant(String moduleClassName) {
    IASTParticipant participant = super.getASTParticipant(moduleClassName);
    if (participant == null) {
        boolean participantIsDefined = false;

        /*
         * find one ourselves..
         */
        for (ASTParticipantDescriptor descriptor : org.jactr.eclipse.core.bundles.registry.ASTParticipantRegistry
                .getRegistry().getDescriptors(_project))
            if (descriptor.getContributingClassName().equals(moduleClassName)) {
                participantIsDefined = true;

                // the code block below is handled by jactr.io.activator.Activator
                // /*
                // * if we can instantiate this, let's do so.. im not sure if this
                // * should actually be installed since we are project specific.. if
                // we
                // * had different versions active, bad things could happen
                // */
                // try
                // {
                // participant = (IASTParticipant) descriptor.instantiate();
                // if (participant != null) return participant;
                // }
                // catch (Exception e)
                // {
                // CorePlugin.debug("Could not instantiate "
                // + descriptor.getContributingClassName() + " for "
                // + moduleClassName, e);
                // }

                /*
                 * construct and return. We know we can't use participantClass since
                 * this is a workspace extension, we have to use BasicASTParticipat
                 * and content location
                 */
                String contentLocation = descriptor.getContentLocation();
                IPath location = new Path(contentLocation);
                try {
                    IProject contributorProject = ResourcesPlugin.getWorkspace().getRoot()
                            .getProject(descriptor.getContributor());

                    // CorePlugin.debug(String.format("Checking %s(%s)",
                    // contributorProject.getName(), contributorProject.exists()));

                    IJavaProject project = JavaCore.create(contributorProject);
                    IResource resource = null;

                    IJavaElement containingPackage = project.findElement(location.removeLastSegments(1));

                    if (containingPackage != null
                            && containingPackage.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
                        if (LOGGER.isDebugEnabled())
                            LOGGER.debug("Found package that contains " + location + " at "
                                    + containingPackage.getPath());

                        // CorePlugin.debug("Found package that contains " + location
                        // + " at " + containingPackage.getPath());

                        IResource tmp = containingPackage.getUnderlyingResource();
                        if (tmp instanceof IContainer) {
                            IContainer container = (IContainer) tmp;
                            resource = container.findMember(location.lastSegment());
                        }
                    }
                    // else
                    // CorePlugin.debug(location + " Containing package = "
                    // + containingPackage);

                    if (resource == null)
                        throw new RuntimeException(String.format("Could not find %s (%s) within %s' classpath",
                                contentLocation, location, _project.getName()));

                    URL contentURL = resource.getLocationURI().toURL();

                    // CorePlugin.debug(String.format(
                    // "Creating participant for content @ %s",
                    // contentURL.toExternalForm()));

                    participant = new IDEBasicASTParticipant(contentURL);
                } catch (Exception e) {
                    String message = "Could not create basic ast participant referencing " + contentLocation
                            + " in " + _project.getName();
                    if (LOGGER.isWarnEnabled())
                        LOGGER.warn(message, e);
                    CorePlugin.warn(message, e);
                    return null;
                }
            }

        if (participant == null && participantIsDefined) {
            /*
             * warn and log
             */
            String message = "Could not find a valid IASTParticipant in any dependencies for "
                    + moduleClassName;
            if (LOGGER.isWarnEnabled())
                LOGGER.warn(message);
            CorePlugin.warn(message);
        }
    }

    return participant;
}

From source file:org.jboss.tools.common.model.handlers.OpenJavaEditorHandler.java

License:Open Source License

public void executeHandler(XModelObject object, Properties p) throws XModelException {
    IProject project = EclipseResourceUtil.getProject(object);
    try {//from   www.  ja v a  2 s.  c  o m
        IJavaProject javaProject = (IJavaProject) project.getNature(JavaCore.NATURE_ID);

        String className = EclipseResourceUtil.getJavaClassQualifiedName(object).replace('.', '/') + "." //$NON-NLS-1$
                + object.getAttributeValue(XModelObjectConstants.ATTR_NAME_EXTENSION);

        IJavaElement javaElement = javaProject.findElement(new Path(className));
        if (javaElement != null) {
            JavaUI.openInEditor(javaElement);
        } else {
            IFile f = (IFile) EclipseResourceUtil.getResource(object);
            if (f != null)
                IDE.openEditor(getWorkbenchPage(), f);
        }
    } catch (CoreException e) {
        throw new XModelException(e);
    }
}

From source file:org.jboss.tools.common.model.java.handlers.OpenJavaSourceHandler.java

License:Open Source License

public static void open(XModel model, String type, Properties p) throws XModelException, CoreException {
    IProject project = (IProject) model.getProperties().get(XModelObjectConstants.PROJECT);
    IJavaProject javaProject = (IJavaProject) project.getNature(JavaCore.NATURE_ID);
    IJavaElement javaElement = javaProject.findElement(new Path(type));
    if (javaElement == null) {
        String message = "Cannot find java source.";
        if (p != null && XModelObjectConstants.TRUE.equals(p.getProperty("ignoreWarning"))) { //$NON-NLS-1$
            p.setProperty("error", message); //$NON-NLS-1$
        } else {/*from w  w  w.  j a  v a  2 s .  co  m*/
            ServiceDialog d = model.getService();
            d.showDialog(ModelMessages.WARNING, message, new String[] { "Close" }, null, ServiceDialog.WARNING);
        }
    } else {
        if (p != null && XModelObjectConstants.TRUE.equals(p.getProperty("onlySelectIfOpen"))) { //$NON-NLS-1$
            IEditorInput ii = EditorUtility.getEditorInput(javaElement);
            IWorkbenchPage page = getWorkbenchPage();
            if (page == null)
                return;
            IEditorPart editor = page.findEditor(ii);
            if (editor == null)
                return;
            else
                page.bringToTop(editor);
        }
        IJavaElement child = getElement(javaElement, p);
        if (child != null) {
            JavaUI.revealInEditor(JavaUI.openInEditor(javaElement), child);
        } else {
            JavaUI.openInEditor(javaElement);
        }
    }
}

From source file:org.jboss.tools.common.model.util.EclipseResourceUtil.java

License:Open Source License

public static IJavaElement findJavaElement(XModelObject object) {
    int type = object.getFileType();
    IResource resource = getResource(object);
    if (resource == null)
        return null;
    IProject project = resource.getProject();
    IJavaProject javaProject = getJavaProject(project);
    if (javaProject == null)
        return null;
    try {/*  w  w  w .j a  v a2 s  . c  om*/
        if (type == XFileObject.SYSTEM)
            return javaProject.findPackageFragmentRoot(resource.getFullPath());
        Path path = null;
        if (type == XFileObject.FOLDER) {
            String p = getJavaPackageName(object);
            if (p == null)
                return null;
            path = new Path(p.replace('.', '/'));
        } else {
            String p = getJavaClassQualifiedName(object);
            if (p == null)
                return null;
            path = new Path(p.replace('.', '/') + ".java"); //$NON-NLS-1$
        }
        return javaProject.findElement(path);
    } catch (CoreException ex) {
        ModelPlugin.getPluginLog().logError(ex);
    }
    return null;
}

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

License:Open Source License

private boolean isPackageInsideSource(String packageName) {
    IJavaProject jProject = JavaCore.create(project);
    try {/*from  ww  w  . jav a 2  s  . c  o m*/
        IJavaElement findElement = jProject
                .findElement(Path.fromPortableString(packageName.replaceAll("\\.", "/"))); //$NON-NLS-1$ //$NON-NLS-2$
        if (findElement instanceof IPackageFragment) {
            return IPackageFragmentRoot.K_SOURCE == ((IPackageFragment) findElement).getKind();
        }
    } catch (Exception e) {
        Activator.log(new Status(IStatus.WARNING, Activator.PLUGIN_ID,
                "Cannot determine where the package " + packageName + " comes from.", e)); //$NON-NLS-1$ //$NON-NLS-2$
    }
    return true;
}