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

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

Introduction

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

Prototype

IJavaProject getJavaProject();

Source Link

Document

Returns the Java project this element is contained in, or null if this element is not contained in any Java project (for instance, the IJavaModel is not contained in any Java project).

Usage

From source file:net.rim.ejde.internal.packaging.PackagingManager.java

License:Open Source License

static private void getCompileImportsRecusively(IClasspathEntry[] entries, IJavaProject jProject,
        Vector<ImportedJar> imports, boolean isMainProject) throws CoreException {
    if (imports == null) {
        imports = new Vector<ImportedJar>();
    }/*from   w  ww.j av a 2 s  . com*/
    // Workspace imports; if there aren't any specified, default to
    // using the runtime libraries.
    IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
    // String jarPathString;
    try {
        BlackBerryProperties properties = null;
        boolean needAddBBJar = false;
        IPath jarPath = null;
        ImportedJar importedJar = null;
        for (IClasspathEntry entry : entries) {
            switch (entry.getEntryKind()) {
            case IClasspathEntry.CPE_CONTAINER: {
                // libraries
                IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(),
                        jProject.getJavaProject());
                if (container == null) {
                    continue;
                }

                IVMInstall containerVM;
                if (!(container instanceof JREContainer)) {
                    // We need to verify the type of the container because the path of Maven container only has one
                    // segment and JavaRuntime.getVMInstall(IPath) return the default VM install if the entry path has one
                    // segment.
                    containerVM = null;
                } else {
                    containerVM = JavaRuntime.getVMInstall(entry.getPath());
                }

                try {
                    if (containerVM != null) {
                        if (containerVM.getVMInstallType().getId().equals(BlackBerryVMInstallType.VM_ID)) {
                            if (isMainProject) {
                                // Add jars to a list
                                IClasspathEntry[] classpathEntries = container.getClasspathEntries();
                                if (classpathEntries != null && classpathEntries.length > 0) {
                                    getCompileImportsRecusively(classpathEntries, jProject, imports, false);
                                }
                            }
                        } else {
                            if (!jProject.getProject().hasNature(BlackBerryProjectCoreNature.NATURE_ID)) {
                                needAddBBJar = true;
                                continue;
                            }
                        }
                    } else {
                        // Add jars to a list
                        IClasspathEntry[] classpathEntries = container.getClasspathEntries();
                        if (classpathEntries != null && classpathEntries.length > 0) {
                            getCompileImportsRecusively(classpathEntries, jProject, imports, false);
                        }
                    }
                } catch (CoreException e) {
                    _log.error(e.getMessage());
                    continue;
                }
                break;
            }
            case IClasspathEntry.CPE_LIBRARY: {
                // imported jars
                jarPath = PackageUtils.getAbsoluteEntryPath(entry);
                // the jar path can be null if the jar file does not exist
                if (jarPath == null) {
                    throw new CoreException(StatusFactory.createErrorStatus(
                            NLS.bind(Messages.PackagingManager_Entry_Not_Found_MSG, entry.getPath())));
                }
                if (jarPath.lastSegment().equals(IConstants.RIM_API_JAR) && needAddBBJar) {
                    needAddBBJar = false;
                }

                importedJar = null;
                if (PackagingUtils.getPackagExportedJar()) {
                    if (entry.isExported()) {
                        if (isMainProject) {
                            // if the exported jar is not in the main project but a dependent project, the classes it
                            // contains are packaged into the dependent project jar. We don't add it to classpath.
                            importedJar = new ImportedJar(jarPath.toOSString(), true,
                                    getJarFileType(jarPath.toFile()));
                        }
                    } else {
                        importedJar = new ImportedJar(jarPath.toOSString(), false,
                                getJarFileType(jarPath.toFile()));
                    }
                } else {
                    importedJar = new ImportedJar(jarPath.toOSString(), false,
                            getJarFileType(jarPath.toFile()));
                }
                if (importedJar != null && !existingJar(imports, importedJar)) {
                    imports.add(importedJar);
                }
                break;
            }
            case IClasspathEntry.CPE_PROJECT: {
                // dependency projects
                IProject project = workspaceRoot.getProject(entry.getPath().toString());
                IJavaProject javaProject = JavaCore.create(project);
                try {
                    if (project.hasNature(BlackBerryProjectCoreNature.NATURE_ID)) {
                        properties = ContextManager.PLUGIN.getBBProperties(javaProject.getProject().getName(),
                                false);
                        if (properties == null) {
                            _log.error("BlackBerry properties is null");
                            break;
                        }
                    } else {
                        properties = BlackBerryPropertiesFactory.createBlackBerryProperties(javaProject);
                    }
                } catch (CoreException e) {
                    _log.error(e.getMessage());
                    continue;
                }
                if (PackagingManager.getProjectTypeID(properties._application.getType()) == Project.LIBRARY) {
                    IPath absoluteJarPath = PackagingUtils
                            .getAbsoluteStandardOutputFilePath(new BlackBerryProject(javaProject, properties));
                    File jarFile = new File(
                            absoluteJarPath.toOSString() + IConstants.DOT_MARK + IConstants.JAR_EXTENSION);
                    importedJar = new ImportedJar(jarFile.getAbsolutePath(), false, getJarFileType(jarFile));
                    if (!existingJar(imports, importedJar)) {
                        imports.add(importedJar);
                    }
                    IClasspathEntry[] subEntries = javaProject.getRawClasspath();
                    if (subEntries != null && subEntries.length > 0) {
                        getCompileImportsRecusively(subEntries, javaProject, imports, false);
                    }
                }
                break;
            }
            case IClasspathEntry.CPE_VARIABLE: {
                // variables
                String e = entry.getPath().toString();
                int index = e.indexOf('/');
                if (index == -1) {
                    index = e.indexOf('\\');
                }
                String variable = e;
                IPath cpvar = JavaCore.getClasspathVariable(variable);
                if (cpvar == null) {
                    String msg = NLS.bind(Messages.PackagingManager_Variable_Not_Defined_MSG, variable);
                    throw new CoreException(StatusFactory.createErrorStatus(msg));
                }
                if (cpvar.lastSegment().equals(IConstants.RIM_API_JAR) && needAddBBJar) {
                    needAddBBJar = false;
                }
                // TODO RAPC does not support a class folder. We may support it later on
                if (cpvar.lastSegment().endsWith("." + IConstants.JAR_EXTENSION)) {
                    importedJar = new ImportedJar(cpvar.toOSString(), false, getJarFileType(cpvar.toFile()));
                    if (!existingJar(imports, importedJar)) {
                        imports.add(importedJar);
                    }
                }
                break;
            }
            }
        }
        if (needAddBBJar && isMainProject) {
            // insert the default BB jre lib if needed
            IVMInstall bbVM = VMUtils.getDefaultBBVM();
            if (bbVM != null) {
                LibraryLocation[] libLocations = bbVM.getLibraryLocations();
                if (libLocations != null) {
                    for (LibraryLocation location : libLocations) {
                        importedJar = new ImportedJar(location.getSystemLibraryPath().toOSString(), false,
                                getJarFileType(location.getSystemLibraryPath().toFile()));
                        if (!existingJar(imports, importedJar)) {
                            imports.add(importedJar);
                        }
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        _log.error(e.getMessage());
    }
}

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

License:Open Source License

/**
 * @param workingJavaProject//from w w  w  . j av a  2 s  . c  om
 * @param defaultPath
 * @return
 * @throws Exception
 */
public static IPackageFragment[] getPackagesInProject(IJavaProject workingJavaProject, String defaultPath,
        final String type) throws Exception {
    final List<IPackageFragment> allPackages = new ArrayList<IPackageFragment>();
    if (workingJavaProject.getElementName().equals(FC_PLUGIN)) {
        workingJavaProject = getWorkingJavaProjectFromUser();
        defaultPath = getDefaultPathFromProject(workingJavaProject, type, EMPTY_STR);
    }
    final IPackageFragmentRoot[] packageFragmentRootArray = workingJavaProject.getJavaProject()
            .getAllPackageFragmentRoots();
    getPackagesForFragment(workingJavaProject, packageFragmentRootArray, allPackages, defaultPath);
    final String reqdPrjs[] = workingJavaProject.getRequiredProjectNames();

    for (final String rqdPrj : reqdPrjs) {
        final IJavaProject javaPrj = getJavaProject(rqdPrj);
        if (javaPrj == null) {
            MessageDialog.openInformation(new Shell(), "Dependent Project Missing",
                    "Dependent project is missing, going ahead with the available ones.");
            continue;
        }
        final IPackageFragmentRoot[] pkgFrgmRoots = javaPrj.getAllPackageFragmentRoots();
        getPackagesForFragment(getJavaProject(rqdPrj), pkgFrgmRoots, allPackages, defaultPath);
    }
    final GlobalSettings globalSettings = getInstance();
    if (allPackages.isEmpty()) {
        if (globalSettings.isUseDefaultForPath()) {
            MessageDialog.openInformation(new Shell(), "No Packages Found",
                    "There are no packages in the path " + globalSettings.getSourcePathJava());
            return new IPackageFragment[0];
        } else {
            MessageDialog.openInformation(new Shell(), "No Packages Found",
                    "There are no packages found in the project " + workingJavaProject.getElementName());
            return new IPackageFragment[0];
        }
    }

    return allPackages.toArray(new IPackageFragment[0]);
}

From source file:tools.vitruv.views.java.projumled4j.annotations.ClasspathInjection.java

License:Open Source License

public static void addAnnotationsToClasspath(IJavaProject project) throws JavaModelException {
    String annotationsPath = null;
    try {/*  w  w w. j  av a 2 s  . c  om*/
        annotationsPath = new File(FileLocator.resolve(getClassFolderURL()).getFile()).toString();
    } catch (IOException e) {
    }

    if (annotationsPath == null) {
        logger.error("Something went wrong generation the path for the annotation classes.");
        throw new IllegalStateException("Path to annotations folder could not be generated.");
    }

    IClasspathEntry[] currentClasspath = project.getRawClasspath();
    IClasspathEntry[] newClasspath = Arrays.copyOf(currentClasspath, currentClasspath.length + 1);
    IClasspathEntry newClasspathEntry = JavaCore.newLibraryEntry(new Path(annotationsPath), null, null, false);
    newClasspath[newClasspath.length - 1] = newClasspathEntry;

    // Check if the classpath entry already exists, then we do not have to add it again
    boolean alreadyExisting = false;
    for (IClasspathEntry entry : currentClasspath) {
        if (entry.equals(newClasspathEntry)) {
            alreadyExisting = true;
        }
    }

    if (!alreadyExisting) {
        logger.info("Add annotations to classpath: " + annotationsPath);
        project.getJavaProject().setRawClasspath(newClasspath, new NullProgressMonitor());
        /*try {
           javaPackage.getJavaProject().getProject().refreshLocal(IResource.DEPTH_INFINITE, progressMonitor);
        } catch (CoreException e) {
           logger.error("Project could not be refreshed.");
        }*/
    }
}