List of usage examples for org.eclipse.jdt.core IJavaProject getResolvedClasspath
IClasspathEntry[] getResolvedClasspath(boolean ignoreUnresolvedEntry) throws JavaModelException;
From source file:net.rim.ejde.internal.util.ProjectUtils.java
License:Open Source License
/** * Gets a file that exists in an Eclipse project. * <p>/*from ww w.j a va 2 s . c om*/ * TODO: Someone can probably optimize this method better. Like using some of the IWorkspaceRoot.find*() methods... * * @param project * the Eclipse project the file belongs to * @param file * the File which is in the Eclipse project * @return the Eclipse resource file associated with the file */ public static IResource getResource(IProject project, File file) { IJavaProject javaProject = JavaCore.create(project); IPath filePath = new Path(file.getAbsolutePath()); try { IClasspathEntry[] classpathEntries = javaProject.getResolvedClasspath(true); IFile input = null; // Look for a source folder for (IClasspathEntry classpathEntry : classpathEntries) { if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { // Try to resolve the source container IWorkspaceRoot workspaceRoot = project.getWorkspace().getRoot(); IResource resource = workspaceRoot.findMember(classpathEntry.getPath()); if (resource instanceof IContainer) { IContainer sourceContainer = (IContainer) resource; File sourceContainerFile = resource.getLocation().toFile(); IPath sourceFolderPath = new Path(sourceContainerFile.getAbsolutePath()); // See if the file path is within this source folder // path if (sourceFolderPath.isPrefixOf(filePath)) { int segmentCount = sourceFolderPath.segmentCount(); IPath relativePath = filePath.removeFirstSegments(segmentCount); input = sourceContainer.getFile(relativePath); break; } } } } return input; } catch (JavaModelException e) { e.printStackTrace(); return null; } }
From source file:net.sf.eclipsecs.core.builder.ProjectClassLoader.java
License:Open Source License
/** * Adds the contents of a project to list of URLs. * /*from w w w . ja va 2s. c o m*/ * @param project the project * @param cpURLs the resulting list * @param isReferenced true if a referenced project is processed */ private static void addToClassPath(IProject project, List<URL> cpURLs, boolean isReferenced, Collection<IProject> processedProjects) { try { // this project has already been added if (processedProjects.contains(project)) { return; } else { processedProjects.add(project); } // get the java project IJavaProject javaProject = JavaCore.create(project); // get the resolved classpath of the project IClasspathEntry[] cpEntries = javaProject.getResolvedClasspath(true); // iterate over classpath to create classpath urls int size = cpEntries.length; for (int i = 0; i < size; i++) { int entryKind = cpEntries[i].getEntryKind(); // handle a source path if (IClasspathEntry.CPE_SOURCE == entryKind) { handleSourcePath(project, cpURLs, cpEntries[i], javaProject); } // handle a project reference else if (IClasspathEntry.CPE_PROJECT == entryKind) { handleRefProject(cpURLs, cpEntries[i], processedProjects); } // handle a library entry else if (IClasspathEntry.CPE_LIBRARY == entryKind) { handleLibrary(project, cpURLs, cpEntries[i]); } // cannot happen since we use a resolved classpath else { // log as exception CheckstylePluginException ex = new CheckstylePluginException( NLS.bind(Messages.errorUnknownClasspathEntry, cpEntries[i].getPath())); CheckstyleLog.log(ex); } } } catch (JavaModelException jme) { CheckstyleLog.log(jme); } }
From source file:net.sf.eclipsecs.core.projectconfig.filters.NonSrcDirsFilter.java
License:Open Source License
/** * Gets all source paths of a project./* w ww . j a v a 2 s .c o m*/ * * @param project the project * @return the list of source paths */ private List<IPath> getSourceDirPaths(IProject project) { List<IPath> sourceDirs = new ArrayList<IPath>(); try { if (project.hasNature(JavaCore.NATURE_ID)) { IJavaProject javaProject = JavaCore.create(project); IClasspathEntry[] cp = javaProject.getResolvedClasspath(true); for (int i = 0; i < cp.length; i++) { if (cp[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) { sourceDirs.add(cp[i].getPath()); } } } } catch (JavaModelException e) { CheckstyleLog.log(e); } catch (CoreException e) { CheckstyleLog.log(e); } return sourceDirs; }
From source file:net.sf.refactorit.eclipse.vfs.EclipseClassPath.java
License:Open Source License
public ClassPathElement[] createElements() { if (project == null || !project.isOpen()) { return new ClassPathElement[0]; }//from w w w . j a va 2s.c om ArrayList result; if (options.isAutoDetect()) { IJavaProject jProject = JavaCore.create(project); try { IClasspathEntry[] pathEntries = jProject.getResolvedClasspath(true); result = new ArrayList(pathEntries.length); for (int i = 0; i < pathEntries.length; i++) { IClasspathEntry entry = pathEntries[i]; if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) { IPath path = entry.getPath(); if (debug) { log.debug("creating classpath entry for path " + path); } File file = path.toFile(); if (!file.exists()) { // FIXME: hack, improve // lib under workspace IFile f = project.getWorkspace().getRoot().getFile(path); file = f.getRawLocation().toFile(); } addClasspathElement(result, file); } } } catch (JavaModelException e) { throw new SystemException(ErrorCodes.ECLIPSE_INTERNAL_ERROR, "resolving classpath failed", e); } } else { log.debug("creating customized classpath elements"); PathItem[] items = options.getClassPath().getItems(); result = new ArrayList(items.length); for (int i = 0; i < items.length; i++) { File file = new File(items[i].getAbsolutePath()); addClasspathElement(result, file); } } return (ClassPathElement[]) result.toArray(new ClassPathElement[result.size()]); }
From source file:net.sf.refactorit.eclipse.vfs.EclipseSourcePath.java
License:Open Source License
private IClasspathEntry[] getSourcePathEntries() throws JavaModelException { IJavaProject jProject = getJavaProject(); List result = new ArrayList(); IClasspathEntry[] pathEntries = jProject.getResolvedClasspath(true); for (int i = 0; i < pathEntries.length; i++) { IClasspathEntry entry = pathEntries[i]; if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE /*|| entry.getEntryKind() == IClasspathEntry.CPE_PROJECT*/) { result.add(entry);//w ww. j av a 2s. co m if (debug) { log.debug("exclusion patterns = " + Arrays.asList(entry.getExclusionPatterns())); } } } return (IClasspathEntry[]) result.toArray(new IClasspathEntry[result.size()]); }
From source file:net.sourceforge.floggy.eclipse.builder.DefaultBuilder.java
License:Open Source License
/** * DOCUMENT ME!//www . j ava 2s. c o m * * @param project DOCUMENT ME! * @param monitor DOCUMENT ME! * * @return DOCUMENT ME! * * @throws Exception DOCUMENT ME! */ public IProject[] build(IProject project, IProgressMonitor monitor) throws Exception { IJavaProject javaProject = JavaCore.create(project); IClasspathEntry[] entries = javaProject.getResolvedClasspath(true); List classpathList = new ArrayList(); ClassPool classPool = new ClassPool(); IClasspathEntry classpathEntry; String pathName; for (int i = 0; i < entries.length; i++) { classpathEntry = JavaCore.getResolvedClasspathEntry(entries[i]); pathName = classpathEntry.getPath().toFile().toString(); if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) { if (!classpathEntry.getPath().toFile().exists()) { IFile pathIFile = project.getWorkspace().getRoot().getFile(classpathEntry.getPath()); pathName = pathIFile.getLocationURI().getPath(); } } classpathList.add(pathName); classPool.appendClassPath(pathName); } if (validateClasspath(project, classPool)) { Weaver weaver = new Weaver(classPool); IPath root = project.getLocation(); File input = root.removeLastSegments(1).append(javaProject.getOutputLocation()).toFile(); IFolder floggyTemp = project.getFolder(".floggy.tmp"); if (!floggyTemp.exists()) { floggyTemp.create(IResource.DERIVED, true, monitor); } IFile configurationFile = project .getFile(project.getPersistentProperty(ConfigurationFileAction.PROPERTY_NAME)); weaver.setOutputFile(floggyTemp.getLocation().toFile()); weaver.setInputFile(input); weaver.setClasspath((String[]) classpathList.toArray(new String[classpathList.size()])); if (!configurationFile.exists()) { weaver.setConfiguration(createWeaverConfiguration(project)); } else { weaver.setConfigurationFile(configurationFile.getLocation().toFile()); } weaver.execute(); IPath path = javaProject.getOutputLocation(); if (path.segmentCount() > 1) { path = path.removeFirstSegments(1); } IFolder outputLocation = project.getFolder(path); floggyTemp.refreshLocal(IResource.DEPTH_INFINITE, monitor); copyFiles(floggyTemp, outputLocation, monitor); outputLocation.refreshLocal(IResource.DEPTH_INFINITE, monitor); cleanFolder(floggyTemp, monitor); } return new IProject[0]; }
From source file:net.sourceforge.floggy.eclipse.builder.MTJBuilder.java
License:Open Source License
/** * Entry point into the configureClassPool, recursively called to deal with dependencies * @param classPool/* ww w . j a v a 2 s . co m*/ * @param classPoolList * @param javaProject * @param isRootProject true when this is the project that the builder was requested to build, false for dependencies * @throws NotFoundException * @throws CoreException */ private void configureClassPool(ClassPool classPool, List classPoolList, IJavaProject javaProject, boolean isRootProject) throws NotFoundException, CoreException { LOG.debug("Configuring ClassPool for " + javaProject.getProject().getName() + (isRootProject ? " (root project)" : " (project dependency")); IClasspathEntry[] entries = javaProject.getResolvedClasspath(true); configureClassPool(classPool, classPoolList, entries, javaProject.getProject(), isRootProject); // now do dependencies (recursively) IProject[] dependencies = javaProject.getProject().getReferencedProjects(); for (int i = 0; i < dependencies.length; i++) { configureClassPool(classPool, classPoolList, JavaCore.create(dependencies[i]), false); } }
From source file:no.javatime.inplace.bundleproject.ProjectProperties.java
License:Open Source License
/** * Finds all source class path entries and return the relative path of source folders * /* www.j a va2 s .c o m*/ * @param project with source folders * @return source folders or an empty collection * @throws JavaModelException when accessing the project resource or the class path element does not exist * @throws InPlaceException if the project could not be accessed */ public static Collection<IPath> getJavaProjectSourceFolders(IProject project) throws JavaModelException, InPlaceException { ArrayList<IPath> paths = new ArrayList<IPath>(); IJavaProject javaProject = getJavaProject(project); IClasspathEntry[] classpathEntries = javaProject.getResolvedClasspath(true); for (int i = 0; i < classpathEntries.length; i++) { IClasspathEntry entry = classpathEntries[i]; if (entry.getContentKind() == IPackageFragmentRoot.K_SOURCE) { IPath path = entry.getPath(); String segment = path.segment(path.segmentCount() - 1); if (null != segment) { paths.add(new Path(segment)); } } } return paths; }
From source file:org.antlr.eclipse.core.builder.AntlrBuilder.java
License:Open Source License
/** * Compile a grammar/* w w w . ja v a 2 s . c o m*/ * @param aFile The grammar file to compile * @param aMonitor A progress monitor */ public void compileFile(IFile aFile, IProgressMonitor aMonitor) { String grammarFileName = aFile.getProjectRelativePath().toString(); try { // delete the old generated files for this grammar aFile.getProject().accept(new CleaningVisitor(aMonitor, grammarFileName)); // if it's in a java project, only build it if it's in a source dir if (aFile.getProject().hasNature(JavaCore.NATURE_ID)) { IProject project = aFile.getProject(); IJavaProject javaProject = JavaCore.create(project); IPath path = aFile.getFullPath(); boolean ok = false; IClasspathEntry[] resolvedClasspath = javaProject.getResolvedClasspath(true); for (int i = 0; i < resolvedClasspath.length; i++) { IClasspathEntry entry = resolvedClasspath[i]; if (entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) continue; IPath entryPath = entry.getPath(); if (entryPath.isPrefixOf(path)) { ok = true; break; } } if (!ok) { return; } } } catch (CoreException e1) { e1.printStackTrace(); } fFile = aFile; aMonitor.beginTask( AntlrCorePlugin.getFormattedMessage("AntlrBuilder.compiling", aFile.getFullPath().toString()), 4); // Remove all markers from this file try { aFile.deleteMarkers(null, true, IResource.DEPTH_INFINITE); } catch (CoreException e) { AntlrCorePlugin.log(e); } // Prepare arguments for ANTLR compiler // read the settings for this grammar HashMap<String, HashMap<String, String>> map = SettingsPersister.readSettings(aFile.getProject()); ArrayList<String> args = createArguments(map, aFile); if (DEBUG) { System.out.println("Compiling ANTLR grammar '" + aFile.getName() + "': arguments=" + args); } // Monitor system out and err fOriginalOut = System.out; fOriginalErr = System.err; System.setOut(new PrintStream(new MonitoredOutputStream(this))); System.setErr(new PrintStream(new MonitoredOutputStream(this))); try { // Compile ANTLR grammar file AntlrTool tool = new AntlrTool(); if (!tool.preprocess(args.toArray(new String[args.size()]))) { try { aMonitor.worked(1); if (!tool.parse()) { aMonitor.worked(1); if (!tool.generate()) { aMonitor.worked(1); refreshFolder(map, tool, args, aFile, grammarFileName, ResourcesPlugin.getWorkspace().getRoot().findMember(fOutput), aMonitor, tool.getSourceMaps()); } else { // If errors during generate then delete all // generated files deleteFiles(tool, aFile.getParent(), aMonitor); } aMonitor.worked(1); } } catch (CoreException e) { AntlrCorePlugin.log(e); } } } catch (Throwable e) { if (!(e instanceof SecurityException)) { AntlrCorePlugin.log(e); } } finally { System.setOut(fOriginalOut); System.setErr(fOriginalErr); aMonitor.done(); } }
From source file:org.antlr.eclipse.smapinstaller.SMapInstallerBuilder.java
License:Open Source License
/** * Installs the modified smap into a generated classfile * @param resource// w w w . jav a 2 s. c o m * @throws JavaModelException */ protected void installSmap(final IResource resource) throws JavaModelException { // We only work on smap files -- skip everything else if (!(resource instanceof IFile)) return; IFile smapIFile = (IFile) resource; if (!"smap".equalsIgnoreCase(smapIFile.getFileExtension())) return; IJavaProject javaProject = JavaCore.create(smapIFile.getProject()); // get the name of the corresponding java source file IPath smapPath = smapIFile.getFullPath(); IClasspathEntry[] classpathEntries = javaProject.getResolvedClasspath(true); for (int i = 0, l = classpathEntries.length; i < l; i++) { IClasspathEntry entry = classpathEntries[i]; if (entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) continue; if (!entry.getPath().isPrefixOf(smapPath)) continue; // found the right source container IPath outputLocation = entry.getOutputLocation(); if (outputLocation == null) outputLocation = javaProject.getOutputLocation(); // strip the source dir and .smap suffix String sourceDir = entry.getPath().toString(); String smapName = smapPath.toString(); String javaSourceName = smapName.substring(0, smapName.length() - 5) + ".java"; String className = smapName.substring(sourceDir.length(), smapName.length() - 5) + ".class"; IPath path = outputLocation.append(className); IPath workspaceLoc = ResourcesPlugin.getWorkspace().getRoot().getLocation(); IPath classFileLocation = workspaceLoc.append(path); IResource classResource = ResourcesPlugin.getWorkspace().getRoot().findMember(javaSourceName); File classFile = classFileLocation.toFile(); File smapFile = smapIFile.getLocation().toFile(); try { String installSmap = classResource.getPersistentProperty(AntlrBuilder.INSTALL_SMAP); if ("true".equals(installSmap)) SDEInstaller.install(classFile, smapFile); } catch (CoreException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }