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

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

Introduction

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

Prototype

String getElementName();

Source Link

Document

Returns the name of this element.

Usage

From source file:org.evolizer.famix.importer.ProjectParser.java

License:Apache License

/**
 * Return the list of source packages of an Eclipse Java project.
 * //w  ww. j a  v a  2s .c  o  m
 * @param project
 *            Eclipse Java project.
 * @return List of source packages.
 */
private List<IPackageFragment> getSourcePackages(IJavaProject project) {
    List<IPackageFragment> fragments = new LinkedList<IPackageFragment>();

    try {
        for (IPackageFragment lFragment : project.getPackageFragments()) {
            if (lFragment.getKind() == IPackageFragmentRoot.K_SOURCE) {
                fragments.add(lFragment);
            }
        }
    } catch (JavaModelException e) {
        sLogger.error("Error determining package fragments from Java project " + project.getElementName());
        e.printStackTrace();
    }

    return fragments;
}

From source file:org.evolizer.famix.importer.ui.popup.actions.StoreModelAction.java

License:Apache License

/**
 * Initializes and runs the job to store the last parsed FAMIX model of the selected project.
 * /*  ww  w  .j av  a2  s .  co m*/
 * {@inheritDoc}
 */
public Object execute(ExecutionEvent event) throws ExecutionException {
    ISelection selection = HandlerUtil.getCurrentSelection(event);

    try {
        final IJavaProject javaProject = JavaSelectionHelper.getProject(selection);

        if ((selection != null) && (FamixImporterPlugin.getDefault().getParser() != null)) {
            ProjectParser currentParser = FamixImporterPlugin.getDefault().getParser();
            IJavaProject parsedProject = JavaSelectionHelper
                    .getProject(currentParser.getSelection().iterator().next());
            if (javaProject.equals(parsedProject)) {
                if ((currentParser.getModel() != null)
                        && (currentParser.getModel().getFamixEntities().size() > 0)) {
                    AbstractFamixEntity firstEntity = currentParser.getModel().getFamixEntities().iterator()
                            .next();
                    if (firstEntity.getId() == null) {
                        EvolizerSessionHandler.getHandler().initSessionFactory(javaProject.getProject());

                        String dbUrl = EvolizerSessionHandler.getHandler().getDBUrl(javaProject.getProject());
                        Job mainJob = new FamixStoreModelJob(dbUrl, currentParser.getModel());
                        mainJob.setUser(true);
                        mainJob.schedule();
                    } else {
                        sLogger.warn("FamixModel has already been stored.");
                    }
                } else {
                    sLogger.warn("No elements to store for the selected project. Extract a model first.");
                }
            } else {
                sLogger.warn("Last processed project does not correspond to slected project. Select "
                        + parsedProject.getElementName());
            }
        } else {
            sLogger.warn("The project needs to be parsed first.");
        }
    } catch (EvolizerException ee) {
        throw new ExecutionException(ee.getMessage());
    }

    return null;
}

From source file:org.fastcode.util.SourceUtil.java

License:Open Source License

/**
 *
 * @param project//  w w w.  j a v a2s .c  o  m
 * @param paramType
 * @return
 * @throws Exception
 */
public static Object getElementFromProject(final IJavaProject project, final String paramType,
        final String filter) throws Exception {

    IType type = project.findType(paramType);
    if (type != null && type.exists()) {
        return type;
    }

    IFile file = project.getProject().getFile(paramType);
    if (file != null && file.exists()) {
        return file;
    }

    final IPackageFragmentRoot[] roots = project.getAllPackageFragmentRoots();
    int nClasses = 0;

    for (final IPackageFragmentRoot root : roots) {
        if (root.getElementName() != null && filter != null && root.getElementName().endsWith(filter)) {
            continue;
        }
        final IJavaElement[] elements = root.getChildren();
        for (int i = 0; i < elements.length; i++) {
            final IJavaElement element = elements[i];
            final IPackageFragment fragment = (IPackageFragment) element.getAdapter(IPackageFragment.class);
            if (fragment == null) {
                continue;
            }

            // TODO redo this logic later with JavaCore.create(file)
            String path = fragment.getPath().toString();
            if (path.length() > root.getPath().toString().length()) {
                path = path.substring(root.getPath().toString().length() + 1);
            }

            final String newpath = path.replaceAll(FORWARD_SLASH, "\\.");

            if (paramType.startsWith(newpath)) {
                String tmpName = paramType.replaceFirst(newpath, path);
                tmpName = tmpName.replaceFirst("\\.", FORWARD_SLASH);
                tmpName = root.getPath().toString() + FORWARD_SLASH + tmpName;
                tmpName = tmpName.substring(project.getProject().getName().length() + 1);
                final IResource resource = project.getProject().findMember(tmpName);
                // JavaCore.create(file);

                if (resource != null && resource.exists()) {
                    return resource;
                }
            }

            final IJavaElement fes[] = fragment.getChildren();

            for (int j = 0; j < fes.length; j++) {
                final String className = fes[j].getElementName();

                final int elementType = fes[j].getElementType();
                if (elementType == TYPE) {
                    type = (IType) fes[j];
                } else if (elementType == COMPILATION_UNIT) {
                    type = ((ICompilationUnit) fes[j]).findPrimaryType();
                } else if (elementType == CLASS_FILE) {
                    type = ((IClassFile) fes[j]).findPrimaryType();
                } else if (elementType == IResource.FILE) {
                    file = (IFile) fes[j];
                }

                if (type != null && type.getFullyQualifiedName().equals(paramType)) {
                    return type;
                } else if (file != null
                        && paramType.equals(fragment.getElementName() + DOT + fes[j].getElementName())) {
                    return file;
                }

                nClasses++;
            }
        }
    }
    final String projectName = project.getElementName();
    return null;
}

From source file:org.fastcode.util.SourceUtil.java

License:Open Source License

/**
 *
 * @param project/*from  www  . j a v  a2  s . c o  m*/
 * @param srcPath
 * @return
 * @throws Exception
 */
public static IPackageFragmentRoot getPackageRootFromProject(final IJavaProject project, final String srcPath)
        throws Exception {
    final IPackageFragmentRoot[] roots = getPackageRootsFromProject(project);
    for (final IPackageFragmentRoot root : roots) {
        // if (root.getPath().toString().equals("/" +
        // project.getElementName() + srcPath)) {
        if (root.getPath().toString().equals(FORWARD_SLASH + project.getElementName() + srcPath)) {
            return root;
        }
    }
    return null;
}

From source file:org.fastcode.util.SourceUtil.java

License:Open Source License

/**
 *
 * @param project//from  ww  w  .j  ava2  s  . c  o  m
 * @param srcPath
 * @return
 * @throws Exception
 */
public static IPackageFragmentRoot getPackageRootFromProject(final String project, final String srcPath)
        throws Exception {
    final IWorkspace workspace = ResourcesPlugin.getWorkspace();
    final IJavaModel javaModel = JavaCore.create(workspace.getRoot());
    final IJavaProject projects[] = javaModel.getJavaProjects();
    for (final IJavaProject javaProject : projects) {
        if (javaProject.getElementName().equals(project)) {
            return getPackageRootFromProject(javaProject, srcPath);
        }
    }
    return null;
}

From source file:org.fastcode.util.SourceUtil.java

License:Open Source License

/**
 * @param prjName/*from   ww  w . j a va  2s. c om*/
 * @return
 * @throws Exception
 */
public static IJavaProject getIJavaProjectFromName(final String prjName) throws Exception {

    for (final IJavaProject prj : getProjectsFromWorkspace()) {
        if (prj.getElementName().equals(prjName)) {
            return prj;
        }
    }
    return null;

}

From source file:org.fastcode.util.SourceUtil.java

License:Open Source License

/**
 * @author Gautam/*  ww w  . jav a 2  s  .  c  o  m*/
 * @param srcPath
 * @return
 * @throws JavaModelException
 */
public static IPackageFragmentRoot getJavaElementFromSourcePathFromWorkspace(final String srcPath,
        final IJavaProject javaProject) throws JavaModelException {
    // Try to find it from the current project first

    IPackageFragmentRoot packageFragmentRoot = getJavaElementFromSourcePathFromProject(srcPath, javaProject,
            false);
    if (packageFragmentRoot != null) {
        return packageFragmentRoot;
    }

    // Try to find it from the workspace

    final IProject[] javaProjects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
    for (final IProject project : javaProjects) {
        if (project == null || !project.exists() || !project.isOpen()) {
            continue;
        }

        final IJavaProject javaProj = JavaCore.create(project);
        if (javaProj == null || javaProject.exists() || !srcPath.startsWith("/" + javaProj.getElementName())) {
            continue;
        }

        packageFragmentRoot = getJavaElementFromSourcePathFromProject(srcPath, javaProj, true);
        if (packageFragmentRoot != null) {
            return packageFragmentRoot;
        }
    }
    return null;
}

From source file:org.fastcode.util.SourceUtil.java

License:Open Source License

/**
 *
 * @param srcPath/*from w w  w  .j a va 2  s . com*/
 * @param checkFullPath
 *
 * @return
 * @throws JavaModelException
 */
public static IPackageFragmentRoot getJavaElementFromSourcePathFromProject(final String srcPath,
        final IJavaProject javaProject, final boolean checkFullPath) throws JavaModelException {
    for (final IPackageFragmentRoot packageFragmentRoot : javaProject.getAllPackageFragmentRoots()) {
        final String rootPath = packageFragmentRoot.getPath().toString();
        if (srcPath.equals(
                checkFullPath ? rootPath : rootPath.substring(javaProject.getElementName().length() + 1))) {
            return packageFragmentRoot;
        }
    }
    return null;
}

From source file:org.fastcode.util.SourceUtil.java

License:Open Source License

/**
 *
 * @param project/*from   w  ww.  ja  v  a 2 s  .  com*/
 * @return
 */
public static IJavaProject getJavaProject(final String project) {
    final IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
    for (final IProject prjct : projects) {
        if (prjct.isOpen() && !prjct.isHidden() && prjct.isAccessible()) {
            final IJavaProject javaProj = JavaCore.create(prjct);
            if (javaProj != null && javaProj.exists() && javaProj.getElementName().equals(project)) {
                return javaProj;
            }
        }
    }
    return null;
}

From source file:org.fastcode.util.SourceUtil.java

License:Open Source License

/**
 *
 * @param project/*from  www  . ja va2s  .  c  o  m*/
 * @return
 */
public static String[][] getSourcePathsForProject(final String project) {
    final IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();

    final List<String> paths = new ArrayList<String>();
    try {
        for (final IProject prjct : projects) {
            if (project == null || prjct.getName().equals(project)) {
                final IJavaProject javaProject = JavaCore.create(prjct);
                if (javaProject == null || !javaProject.exists()) {
                    continue;
                }
                if (isProjectBinary(javaProject)) {
                    continue;
                }
                for (final IPackageFragmentRoot packageFragmentRoot : javaProject
                        .getAllPackageFragmentRoots()) {
                    if (packageFragmentRoot == null || !packageFragmentRoot.exists()
                            || packageFragmentRoot.isArchive() || packageFragmentRoot.isExternal()) {
                        continue;
                    }
                    if (!packageFragmentRoot.getParent().equals(javaProject)) { // discard
                        // roots
                        // which
                        // come
                        // from
                        // another
                        // project.
                        continue;
                    }
                    final String fullSrcPath = packageFragmentRoot.getPath().toString();
                    final String srcPath = fullSrcPath
                            .replaceFirst(FORWARD_SLASH + javaProject.getElementName(), EMPTY_STR);
                    if (!paths.contains(srcPath)) {
                        paths.add(srcPath);
                    }
                }
            }
        }
    } catch (final Exception ex) {
        ex.printStackTrace();
    }

    return getStringArrayFromList(paths, true);
}