List of usage examples for org.eclipse.jdt.core IClasspathEntry getPath
IPath getPath();
From source file:com.redhat.ceylon.eclipse.core.classpath.CeylonProjectModulesContainer.java
License:Apache License
/** * Resolves the classpath entries for this container. * @param monitor//from w w w . j a v a2 s . com * @param reparse * @return true if the classpath was changed, false otherwise. */ public boolean resolveClasspath(IProgressMonitor monitor, boolean reparse) { IJavaProject javaProject = getJavaProject(); IProject project = javaProject.getProject(); try { TypeChecker typeChecker = null; if (!reparse) { typeChecker = getProjectTypeChecker(project); } IClasspathEntry[] oldEntries = classpathEntries; if (typeChecker == null) { IClasspathEntry explodeFolderEntry = null; if (oldEntries != null) { for (IClasspathEntry entry : oldEntries) { if (entry.getPath() != null && entry.getPath().equals(getCeylonClassesOutputFolder(project).getFullPath())) { explodeFolderEntry = entry; break; } } } IClasspathEntry[] resetEntries = explodeFolderEntry == null ? new IClasspathEntry[] {} : new IClasspathEntry[] { explodeFolderEntry }; JavaCore.setClasspathContainer(getPath(), new IJavaProject[] { javaProject }, new IClasspathContainer[] { new CeylonProjectModulesContainer(javaProject, getPath(), resetEntries, attributes) }, monitor); typeChecker = parseCeylonModel(project, monitor); } IFolder explodedModulesFolder = getCeylonClassesOutputFolder(project); if (isExplodeModulesEnabled(project)) { if (!explodedModulesFolder.exists()) { CoreUtility.createDerivedFolder(explodedModulesFolder, true, true, monitor); } else { if (!explodedModulesFolder.isDerived()) { explodedModulesFolder.setDerived(true, monitor); } } } else { if (explodedModulesFolder.exists()) { explodedModulesFolder.delete(true, monitor); } } final Collection<IClasspathEntry> paths = findModuleArchivePaths(javaProject, project, typeChecker); CeylonProjectModulesContainer currentContainer = (CeylonProjectModulesContainer) JavaCore .getClasspathContainer(path, javaProject); if (oldEntries == null || oldEntries != currentContainer.classpathEntries || !paths.equals(asList(oldEntries))) { this.classpathEntries = paths.toArray(new IClasspathEntry[paths.size()]); return true; } } catch (CoreException e) { e.printStackTrace(); } return false; }
From source file:com.redhat.ceylon.eclipse.core.classpath.CeylonProjectModulesContainer.java
License:Apache License
private Collection<IClasspathEntry> findModuleArchivePaths(IJavaProject javaProject, IProject project, TypeChecker typeChecker) throws JavaModelException, CoreException { final Map<String, IClasspathEntry> paths = new TreeMap<String, IClasspathEntry>(); Context context = typeChecker.getContext(); RepositoryManager provider = context.getRepositoryManager(); Set<Module> modulesToAdd = context.getModules().getListOfModules(); //modulesToAdd.add(projectModules.getLanguageModule()); for (Module module : modulesToAdd) { JDTModule jdtModule = (JDTModule) module; String name = module.getNameAsString(); if (name.equals(Module.DEFAULT_MODULE_NAME) || JDKUtils.isJDKModule(name) || JDKUtils.isOracleJDKModule(name) || module.equals(module.getLanguageModule()) || isProjectModule(javaProject, module) || !module.isAvailable()) { continue; }/* w ww . j a v a 2s . com*/ IPath modulePath = getModuleArchive(provider, jdtModule); if (modulePath != null) { IPath srcPath = null; for (IProject p : project.getReferencedProjects()) { if (p.isAccessible() && p.getLocation().isPrefixOf(modulePath)) { //the module belongs to a referenced //project, so use the project source srcPath = p.getLocation(); break; } } if (srcPath == null) { for (IClasspathEntry entry : classpathEntries) { if (entry.getPath().equals(modulePath)) { srcPath = entry.getSourceAttachmentPath(); break; } } } if (srcPath == null && !modulesWithSourcesAlreadySearched.contains(module.toString())) { //otherwise, use the src archive srcPath = getSourceArchive(provider, jdtModule); } modulesWithSourcesAlreadySearched.add(module.toString()); IClasspathEntry newEntry = newLibraryEntry(modulePath, srcPath, null); paths.put(newEntry.toString(), newEntry); } else { // FIXME: ideally we should find the module.java file and put the marker there, but // I've no idea how to find it and which import is the cause of the import problem // as it could be transitive IMarker marker = project.createMarker(IJavaModelMarker.BUILDPATH_PROBLEM_MARKER); marker.setAttribute(IMarker.MESSAGE, "no module archive found for classpath container: " + module.getNameAsString() + "/" + module.getVersion()); marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH); marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR); } } if (isExplodeModulesEnabled(project)) { IClasspathEntry newEntry = newLibraryEntry(getCeylonClassesOutputFolder(project).getFullPath(), project.getFullPath(), null, false); paths.put(newEntry.toString(), newEntry); } return asList(paths.values().toArray(new IClasspathEntry[paths.size()])); }
From source file:com.redhat.ceylon.eclipse.core.model.JDTModule.java
License:Open Source License
public synchronized List<IPackageFragmentRoot> getPackageFragmentRoots() { if (packageFragmentRoots.isEmpty() && !moduleManager.isExternalModuleLoadedFromSource(getNameAsString())) { IJavaProject javaProject = moduleManager.getJavaProject(); if (javaProject != null) { if (this.equals(getLanguageModule())) { IClasspathEntry runtimeClasspathEntry = null; try { for (IClasspathEntry entry : javaProject.getRawClasspath()) { if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER && entry.getPath().segment(0) .equals(CeylonLanguageModuleContainer.CONTAINER_ID)) { runtimeClasspathEntry = entry; break; }/*from ww w. j a v a 2 s . co m*/ } if (runtimeClasspathEntry != null) { for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) { if (root.exists() && javaProject.isOnClasspath(root) && root.getRawClasspathEntry().equals(runtimeClasspathEntry)) { packageFragmentRoots.add(root); } } } } catch (JavaModelException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { File jarToSearch = null; try { jarToSearch = returnCarFile(); if (jarToSearch == null) { RepositoryManager repoMgr = CeylonBuilder .getProjectRepositoryManager(javaProject.getProject()); if (repoMgr != null) { jarToSearch = CeylonProjectModulesContainer.getModuleArtifact(repoMgr, this); } } if (jarToSearch != null) { IPackageFragmentRoot root = moduleManager.getJavaProject() .getPackageFragmentRoot(jarToSearch.toString()); if (root instanceof JarPackageFragmentRoot) { JarPackageFragmentRoot jarRoot = (JarPackageFragmentRoot) root; if (jarRoot.getJar().getName().equals(jarToSearch.getPath())) { packageFragmentRoots.add(root); } } } } catch (CoreException e) { if (jarToSearch != null) { System.err.println("Exception trying to get Jar file '" + jarToSearch + "' :"); } e.printStackTrace(); } } } } return packageFragmentRoots; }
From source file:com.redhat.victims.plugin.eclipse.handler.ScanHandler.java
License:Open Source License
/** * Begins the life-cycle of the plugin. Finds the absolute path names of all * dependencies for a project in eclipse and creates the popup menu for * settings.// w ww . j a v a 2s .co m */ public Object execute(ExecutionEvent event) throws ExecutionException { Shell shell = HandlerUtil.getActiveShell(event); ISelection sel = HandlerUtil.getActiveMenuSelection(event); IStructuredSelection selection = (IStructuredSelection) sel; Object firstElement = selection.getFirstElement(); if (firstElement instanceof IJavaProject) { IJavaProject jp = (IJavaProject) firstElement; optionMenu = new OptionMenuRunnable(this); EventQueue.invokeLater(optionMenu); try { IClasspathEntry[] cp = jp.getResolvedClasspath(true); /* * Extracts all .jar files from the classpath and adds them to a * list to be passed to VictimScan during the callback */ for (IClasspathEntry entry : cp) { IPath path = entry.getPath(); String ext = path.getFileExtension(); // This condition seems weird but if it's the other way // around you open yourself up to null pointer exceptions! if (JAR_EXT.equals(ext)) { paths.add(path); } } } catch (JavaModelException e) { log.log(new Status(Status.INFO, Activator.PLUGIN_ID, e.getLocalizedMessage())); } } else { MessageDialog.openInformation(shell, "Info", "Please select a Java Project"); } return null; }
From source file:com.siteview.mde.internal.core.builders.SourceEntryErrorReporter.java
License:Open Source License
private IPath getPath(Object entry) { IPath path = null;// w ww .ja v a2 s.c o m if (entry instanceof IClasspathEntry) { IClasspathEntry cpes = (IClasspathEntry) entry; path = cpes.getPath(); } else if (entry instanceof IPath) { path = (IPath) entry; } if (path.matchingFirstSegments(fProject.getFullPath()) > 0) { path = path.removeFirstSegments(1); } if (path != null) { return path.addTrailingSeparator(); } return null; }
From source file:com.siteview.mde.internal.core.ClasspathComputer.java
License:Open Source License
private static void addSourceAndLibraries(IProject project, IMonitorModelBase model, IBuild build, boolean clear, Map sourceLibraryMap, ArrayList result) throws CoreException { HashSet paths = new HashSet(); // keep existing source folders if (!clear) { IClasspathEntry[] entries = JavaCore.create(project).getRawClasspath(); for (int i = 0; i < entries.length; i++) { IClasspathEntry entry = entries[i]; if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { if (paths.add(entry.getPath())) result.add(entry);// w ww. j a v a 2 s . c om } } } IClasspathAttribute[] attrs = getClasspathAttributes(project, model); IMonitorLibrary[] libraries = model.getMonitorBase().getLibraries(); for (int i = 0; i < libraries.length; i++) { IBuildEntry buildEntry = build == null ? null : build.getEntry("source." + libraries[i].getName()); //$NON-NLS-1$ if (buildEntry != null) { addSourceFolder(buildEntry, project, paths, result); } else { IPath sourceAttachment = sourceLibraryMap != null ? (IPath) sourceLibraryMap.get(libraries[i].getName()) : null; if (libraries[i].getName().equals(".")) //$NON-NLS-1$ addJARdPlugin(project, ClasspathUtilCore.getFilename(model), sourceAttachment, attrs, result); else addLibraryEntry(project, libraries[i], sourceAttachment, attrs, result); } } if (libraries.length == 0) { if (build != null) { IBuildEntry buildEntry = build == null ? null : build.getEntry("source.."); //$NON-NLS-1$ if (buildEntry != null) { addSourceFolder(buildEntry, project, paths, result); } } else if (ClasspathUtilCore.hasBundleStructure(model)) { IPath sourceAttachment = sourceLibraryMap != null ? (IPath) sourceLibraryMap.get(".") : null; //$NON-NLS-1$ addJARdPlugin(project, ClasspathUtilCore.getFilename(model), sourceAttachment, attrs, result); } } }
From source file:com.siteview.mde.internal.core.project.BundleProjectDescription.java
License:Open Source License
/** * Creates and returns a bundle claspath specifications for the given source.<library> build * entry/* w ww .ja v a 2 s . co m*/ * * @param project * @param entry * @param binary whether a binary folder (<code>true</code>) or source folder (<code>false</code>) * @return associated bundle classpath specifications or <code>null</code> if a malformed entry * @throws CoreException if unable to access Java build path */ private IBundleClasspathEntry[] getClasspathEntries(IProject project, IBuildEntry entry, boolean binary) throws CoreException { String[] tokens = entry.getTokens(); IPath lib = null; if (binary) { lib = new Path(entry.getName().substring(IBuildEntry.OUTPUT_PREFIX.length())); } else { lib = new Path(entry.getName().substring(IBuildEntry.JAR_PREFIX.length())); } if (tokens != null && tokens.length > 0) { IBundleClasspathEntry[] bces = new IBundleClasspathEntry[tokens.length]; for (int i = 0; i < tokens.length; i++) { IPath path = new Path(tokens[i]); IBundleClasspathEntry spec = null; if (binary) { spec = getBundleProjectService().newBundleClasspathEntry(null, path, lib); } else { IJavaProject jp = JavaCore.create(project); IPath output = null; if (jp.exists()) { IClasspathEntry[] rawClasspath = jp.getRawClasspath(); for (int j = 0; j < rawClasspath.length; j++) { IClasspathEntry cpe = rawClasspath[j]; if (cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE) { if (cpe.getPath().removeFirstSegments(1).equals(path)) { output = cpe.getOutputLocation(); if (output != null) { output = output.removeFirstSegments(1); } break; } } } } spec = getBundleProjectService().newBundleClasspathEntry(path, output, lib); } bces[i] = spec; } return bces; } return null; }
From source file:com.siteview.mde.internal.core.project.ProjectModifyOperation.java
License:Open Source License
/** * Creates or modifies a project based on the given description. * //from w ww . ja va 2 s . c om * @param monitor progress monitor or <code>null</code> * @param description project description * @throws CoreException if project creation fails */ public void execute(IProgressMonitor monitor, IBundleProjectDescription description) throws CoreException { // retrieve current description of the project to detect differences IProject project = description.getProject(); IBundleProjectService service = (IBundleProjectService) MDECore.getDefault() .acquireService(IBundleProjectService.class.getName()); IBundleProjectDescription before = service.getDescription(project); boolean considerRoot = !project.exists(); String taskName = null; boolean jpExisted = false; if (project.exists()) { taskName = Messages.ProjectModifyOperation_0; jpExisted = before.hasNature(JavaCore.NATURE_ID); } else { taskName = Messages.ProjectModifyOperation_1; // new bundle projects get Java and Plug-in natures if (description.getNatureIds().length == 0) { description .setNatureIds(new String[] { IBundleProjectDescription.PLUGIN_NATURE, JavaCore.NATURE_ID }); } } boolean becomeBundle = !before.hasNature(IBundleProjectDescription.PLUGIN_NATURE) && description.hasNature(IBundleProjectDescription.PLUGIN_NATURE); // set default values when migrating from Java project to bundle project if (jpExisted && becomeBundle) { if (description.getExecutionEnvironments() == null) { // use EE from Java project when unspecified in the description, and a bundle nature is being added IJavaProject jp = JavaCore.create(project); if (jp.exists()) { IClasspathEntry[] classpath = jp.getRawClasspath(); for (int i = 0; i < classpath.length; i++) { IClasspathEntry entry = classpath[i]; if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) { String id = JavaRuntime.getExecutionEnvironmentId(entry.getPath()); if (id != null) { description.setExecutionEnvironments(new String[] { id }); break; } } } } } } // other default values when becoming a bundle if (becomeBundle) { // set default values for where unspecified if (description.getBundleVersion() == null) { description.setBundleVersion(new Version(1, 0, 0, "qualifier")); //$NON-NLS-1$ } } SubMonitor sub = SubMonitor.convert(monitor, taskName, 6); // create and open project createProject(description); // set bundle root for new projects if (considerRoot) { IFolder folder = null; IPath root = description.getBundleRoot(); if (root != null && !root.isEmpty()) { folder = project.getFolder(root); CoreUtility.createFolder(folder); } PDEProject.setBundleRoot(project, folder); } sub.worked(1); configureNatures(description); sub.worked(1); if (project.hasNature(JavaCore.NATURE_ID)) { configureJavaProject(description, before, jpExisted); } sub.worked(1); configureManifest(description, before); sub.worked(1); configureBuildPropertiesFile(description, before); sub.worked(1); // project settings for Equinox, Extension Registry, Automated dependency policy, // manifest editor launch shortcuts and export wizard IEclipsePreferences pref = new ProjectScope(project).getNode(MDECore.PLUGIN_ID); if (pref != null) { // best guess for automated dependency management: Equinox + Extensions = use required bundle if (description.isEquinox() && description.isExtensionRegistry()) { pref.remove(ICoreConstants.RESOLVE_WITH_REQUIRE_BUNDLE); // i.e. use required bundle } else { pref.putBoolean(ICoreConstants.RESOLVE_WITH_REQUIRE_BUNDLE, false); } if (description.isExtensionRegistry()) { pref.remove(ICoreConstants.EXTENSIONS_PROPERTY); // i.e. support extensions } else { pref.putBoolean(ICoreConstants.EXTENSIONS_PROPERTY, false); } if (description.isEquinox()) { pref.remove(ICoreConstants.EQUINOX_PROPERTY); // i.e. using Equinox } else { pref.putBoolean(ICoreConstants.EQUINOX_PROPERTY, false); } String[] shorts = description.getLaunchShortcuts(); if (shorts == null || shorts.length == 0) { pref.remove(ICoreConstants.MANIFEST_LAUNCH_SHORTCUTS); // use defaults } else { StringBuffer value = new StringBuffer(); for (int i = 0; i < shorts.length; i++) { if (i > 0) { value.append(','); } value.append(shorts[i]); } pref.put(ICoreConstants.MANIFEST_LAUNCH_SHORTCUTS, value.toString()); } if (description.getExportWizardId() == null) { pref.remove(ICoreConstants.MANIFEST_EXPORT_WIZARD); } else { pref.put(ICoreConstants.MANIFEST_EXPORT_WIZARD, description.getExportWizardId()); } try { pref.flush(); } catch (BackingStoreException e) { throw new CoreException( new Status(IStatus.ERROR, MDECore.PLUGIN_ID, Messages.ProjectModifyOperation_2, e)); } } if (fModel.isDirty()) { fModel.save(); } sub.worked(1); sub.done(); if (monitor != null) { monitor.done(); } }
From source file:com.siteview.mde.internal.core.project.ProjectModifyOperation.java
License:Open Source License
/** * Configures the build path and output location of the described Java project. * If the Java project existed before this operation, new build path entries are * added for the bundle class path, if required, but we don't change the exiting * build path./*from w w w .j a v a 2 s . c o m*/ * * @param description desired project description * @param before state before the operation * @param existed whether the Java project existed before the operation */ private void configureJavaProject(IBundleProjectDescription description, IBundleProjectDescription before, boolean existed) throws CoreException { IProject project = description.getProject(); IJavaProject javaProject = JavaCore.create(project); // create source folders as required IBundleClasspathEntry[] bces = description.getBundleClasspath(); if (bces != null && bces.length > 0) { for (int i = 0; i < bces.length; i++) { IPath folder = bces[i].getSourcePath(); if (folder != null) { CoreUtility.createFolder(project.getFolder(folder)); } } } // Set default output folder if (description.getDefaultOutputFolder() != null) { IPath path = project.getFullPath().append(description.getDefaultOutputFolder()); javaProject.setOutputLocation(path, null); } // merge the class path if the project existed before IBundleClasspathEntry[] prev = before.getBundleClasspath(); if (!isEqual(bces, prev)) { if (existed) { // add entries not already present IClasspathEntry[] entries = getSourceFolderEntries(javaProject, description); IClasspathEntry[] rawClasspath = javaProject.getRawClasspath(); List add = new ArrayList(); for (int i = 0; i < entries.length; i++) { IClasspathEntry entry = entries[i]; boolean present = false; for (int j = 0; j < rawClasspath.length; j++) { IClasspathEntry existingEntry = rawClasspath[j]; if (existingEntry.getEntryKind() == entry.getEntryKind()) { if (existingEntry.getPath().equals(entry.getPath())) { present = true; break; } } } if (!present) { add.add(entry); } } // check if the 'required plug-ins' container is present boolean addRequired = false; if (description.hasNature(IBundleProjectDescription.PLUGIN_NATURE)) { addRequired = true; for (int i = 0; i < rawClasspath.length; i++) { IClasspathEntry cpe = rawClasspath[i]; if (cpe.getEntryKind() == IClasspathEntry.CPE_CONTAINER) { if (MDECore.REQUIRED_PLUGINS_CONTAINER_PATH.equals(cpe.getPath())) { addRequired = false; break; } } } } if (addRequired) { add.add(ClasspathComputer.createContainerEntry()); } if (!add.isEmpty()) { List all = new ArrayList(); for (int i = 0; i < rawClasspath.length; i++) { all.add(rawClasspath[i]); } all.addAll(add); javaProject.setRawClasspath((IClasspathEntry[]) all.toArray(new IClasspathEntry[all.size()]), null); } } else { IClasspathEntry[] entries = getClassPathEntries(javaProject, description); javaProject.setRawClasspath(entries, null); } } }
From source file:com.siteview.mde.internal.launching.sourcelookup.PDESourceLookupDirector.java
License:Open Source License
private IRuntimeClasspathEntry convertClasspathEntry(IClasspathEntry entry) { if (entry == null) return null; IPath srcPath = entry.getSourceAttachmentPath(); if (srcPath != null && srcPath.segmentCount() > 0) { IRuntimeClasspathEntry rte = JavaRuntime.newArchiveRuntimeClasspathEntry(entry.getPath()); rte.setSourceAttachmentPath(srcPath); rte.setSourceAttachmentRootPath(entry.getSourceAttachmentRootPath()); return rte; }/*from ww w. j av a 2s .c o m*/ return null; }