List of usage examples for org.eclipse.jdt.core IClasspathEntry getEntryKind
int getEntryKind();
From source file:nl.mwensveen.m2e.extras.cxf.tests.CXFGenerationTest.java
License:Open Source License
public void test_p001_simple() throws Exception { ResolverConfiguration configuration = new ResolverConfiguration(); IProject project1 = importProject("projects/cxf/pom.xml", configuration); waitForJobsToComplete();// w ww. ja v a 2s.c o m project1.build(IncrementalProjectBuilder.FULL_BUILD, monitor); project1.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, monitor); waitForJobsToComplete(); assertNoErrors(project1); IJavaProject javaProject1 = JavaCore.create(project1); IClasspathEntry[] cp1 = javaProject1.getRawClasspath(); boolean found = false; for (IClasspathEntry iClasspathEntry : cp1) { if (iClasspathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { if (iClasspathEntry.getPath().equals(new Path("/cxf-test-project/target/generated-sources/cxf"))) { found = true; } } } assertTrue(found); assertTrue(project1.getFile("target/generated-sources/cxf/com/example/xsd/ComplexType.java") .isSynchronized(IResource.DEPTH_ZERO)); assertTrue( project1.getFile("target/generated-sources/cxf/com/example/xsd/ComplexType.java").isAccessible()); }
From source file:nz.ac.auckland.ptjava.newclasswizard.WizardNewPTJavaFileCreationPage.java
License:Open Source License
/** * Creates the initial contents to be placed inside the newly created PTJava class file. * Current version will generate package declaration and basic class declaration if * those options are checked on the wizard page. *//*from ww w .j av a 2 s . co m*/ @Override protected InputStream getInitialContents() { // String buffer for appending to file StringBuffer sb = new StringBuffer(); if (fCodeCreationGroup.getGeneratePackage()) { IPath containerFullPath = getContainerFullPath(); // the original string to the container path String pathString = containerFullPath.toString(); //System.out.println(pathString); int slashIndex = pathString.indexOf('/', 1); String truncatedPath = null; if (slashIndex > 0) truncatedPath = pathString.substring(0, slashIndex); else truncatedPath = pathString; // find the project src folder path IProject project = JavaPlugin.getWorkspace().getRoot().getProject(truncatedPath); IJavaProject javaProject = JavaCore.create(project); if (javaProject != null) { try { String srcPath = null; IClasspathEntry[] cp = javaProject.getRawClasspath(); for (IClasspathEntry i : cp) { //System.out.println("i:"+i); if (i.getEntryKind() == IClasspathEntry.CPE_SOURCE) { // src entry found! srcPath = i.getPath().toString(); //System.out.println("srcPath: " + srcPath); break; } } if (srcPath != null) { if (pathString.equals(srcPath)) { // do nothing } else { String s = null; // omit src path if part of path string if (pathString.startsWith(srcPath)) s = pathString.substring(srcPath.length() + 1); // the +1 is to remove the first "/" after the src path else s = pathString; // currently looks like "some/path/to/file", we want "some.path.to.file" s = s.replace('/', '.'); // append it // should be something like "package some.path;" sb.append("package "); sb.append(s); sb.append(";"); sb.append("\n\n"); //System.out.println("s: " + s); } } } catch (JavaModelException e) { e.printStackTrace(); } } } if (fCodeCreationGroup.getGenerateClass()) { sb.append("public class "); String filename = getFileName(); if (filename.contains(".")) filename = filename.substring(0, filename.indexOf('.')); sb.append(filename); sb.append(" {\n\t\n}"); } if (sb.length() == 0) return null; return new ByteArrayInputStream(sb.toString().getBytes()); }
From source file:org.antlr.eclipse.core.AntlrNature.java
License:Open Source License
/** * Add the ANTLR nature to a project/*w w w.j a va 2s. com*/ * @param aProject The project to add it to * @param aMonitor A progess monitor */ public static void addNature(final IProject aProject, final IProgressMonitor aMonitor) { if (aProject != null) { if (DEBUG) { System.out.println("adding ANTLR nature to project '" + aProject.getName() + "'"); } try { if (!aProject.hasNature(ID)) { IProjectDescription desc = aProject.getDescription(); ArrayList<String> natures = new ArrayList<String>(Arrays.asList(desc.getNatureIds())); natures.add(0, ID); desc.setNatureIds(natures.toArray(new String[natures.size()])); aProject.setDescription(desc, aMonitor); // if it's a Java project, set up classpath variable for: // ANTLR_HOME // these will point to the ANTLR plugins' // jars, respectively if (aProject.hasNature(JavaCore.NATURE_ID)) { IJavaProject javaProject = JavaCore.create(aProject); ArrayList<IClasspathEntry> rawClasspath = new ArrayList<IClasspathEntry>( Arrays.asList(javaProject.getRawClasspath())); boolean hasAntlrJar = false; for (Iterator<IClasspathEntry> i = rawClasspath.iterator(); i.hasNext();) { IClasspathEntry entry = i.next(); if (entry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) { String segment0 = entry.getPath().segment(0); if (AntlrCorePlugin.ANTLR_HOME.equals(segment0)) { hasAntlrJar = true; break; } } } if (!hasAntlrJar) rawClasspath.add( JavaCore.newVariableEntry(new Path(AntlrCorePlugin.ANTLR_HOME + "/antlr.jar"), new Path(AntlrCorePlugin.ANTLR_HOME + "/antlrsrc.zip"), null)); if (!hasAntlrJar) javaProject.setRawClasspath( rawClasspath.toArray(new IClasspathEntry[rawClasspath.size()]), null); } } } catch (CoreException e) { AntlrCorePlugin.log(e); } } }
From source file:org.antlr.eclipse.core.AntlrNature.java
License:Open Source License
/** * Remove the ANTLR nature from a project * @param aProject The project to remove the nature from * @param aMonitor A progress monitor/*from ww w .j a v a2 s . c o m*/ */ public static void removeNature(final IProject aProject, final IProgressMonitor aMonitor) { if (aProject != null) { if (DEBUG) { System.out.println("removing ANTLR nature from project '" + aProject.getName() + "'"); } try { if (aProject.hasNature(ID)) { IProjectDescription desc = aProject.getDescription(); ArrayList<String> natures = new ArrayList<String>(Arrays.asList(desc.getNatureIds())); natures.remove(ID); desc.setNatureIds(natures.toArray(new String[natures.size()])); aProject.setDescription(desc, aMonitor); // if it's a Java Project, remove the ANTLR_HOME class path variable if (aProject.hasNature(JavaCore.NATURE_ID)) { IJavaProject javaProject = JavaCore.create(aProject); ArrayList<IClasspathEntry> rawClasspath = new ArrayList<IClasspathEntry>( Arrays.asList(javaProject.getRawClasspath())); boolean hadAntlrJar = false; for (Iterator<IClasspathEntry> i = rawClasspath.iterator(); i.hasNext();) { IClasspathEntry entry = i.next(); if (entry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) { String segment0 = entry.getPath().segment(0); if (AntlrCorePlugin.ANTLR_HOME.equals(segment0)) { i.remove(); hadAntlrJar = true; } if ("PARSEVIEW_HOME".equals(segment0)) { i.remove(); hadAntlrJar = true; } } } if (hadAntlrJar) { javaProject.setRawClasspath( rawClasspath.toArray(new IClasspathEntry[rawClasspath.size()]), null); } } } } catch (CoreException e) { AntlrCorePlugin.log(e); } } }
From source file:org.antlr.eclipse.core.builder.AntlrBuilder.java
License:Open Source License
/** * Compile a grammar//from w w w.j ava 2 s .co 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//from ww w . j a va 2s .c om * @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(); } } }
From source file:org.apache.cactus.eclipse.webapp.internal.ui.WebAppConfigurationBlock.java
License:Apache License
/** * Returns a list of jar entries contained in an array of entries. * @param theClasspathEntries array of classpath entries * @return ArrayList list containing the jar entries *//*from w w w. j ava 2 s . co m*/ private ArrayList getExistingEntries(final IClasspathEntry[] theClasspathEntries) { ArrayList newClassPath = new ArrayList(); for (int i = 0; i < theClasspathEntries.length; i++) { IClasspathEntry curr = theClasspathEntries[i]; if (curr.getEntryKind() == IClasspathEntry.CPE_LIBRARY) { try { newClassPath.add(CPListElement.createFromExisting(curr, javaProject)); } catch (NullPointerException e) { // an error occured when parsing the entry // (possibly invalid entry) // We don't add it } } } return newClassPath; }
From source file:org.apache.cactus.eclipse.webapp.internal.WarBuilder.java
License:Apache License
/** * For each IClasspathEntry transform the path in an absolute path. * @param theEntries array of IClasspathEntry to render asbolute * @return an array of absolute IClasspathEntries */// w w w . ja va2 s . c o m private static IClasspathEntry[] getAbsoluteEntries(final IClasspathEntry[] theEntries) { if (theEntries == null) { return new IClasspathEntry[0]; } Vector result = new Vector(); for (int i = 0; i < theEntries.length; i++) { IClasspathEntry currentEntry = theEntries[i]; if (currentEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) { IPath path = currentEntry.getPath(); Object target = JavaModel.getTarget(ResourcesPlugin.getWorkspace().getRoot(), path, true); if (target instanceof IFile) { IFile file = (IFile) target; IPath absolutePath = file.getLocation(); result.add(JavaCore.newLibraryEntry(absolutePath, null, null)); } else if (target instanceof File) { File file = (File) target; result.add(JavaCore.newLibraryEntry(new Path(file.getAbsolutePath()), null, null)); } } } return (IClasspathEntry[]) result.toArray(new IClasspathEntry[result.size()]); }
From source file:org.apache.cactus.eclipse.webapp.internal.WarBuilder.java
License:Apache License
/** * @param theJarEntries the jars to build ZipFileSets from * @return an array of ZipFileSet corresponding to the given jars *//* ww w . j a va2 s . c o m*/ private static ZipFileSet[] getZipFileSets(final IClasspathEntry[] theJarEntries) { Vector result = new Vector(); for (int i = 0; i < theJarEntries.length; i++) { IClasspathEntry currentEntry = theJarEntries[i]; if (currentEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) { File currentJar = currentEntry.getPath().toFile(); ZipFileSet zipFS = new ZipFileSet(); zipFS.setFile(currentJar); result.add(zipFS); } } return (ZipFileSet[]) result.toArray(new ZipFileSet[result.size()]); }
From source file:org.apache.derby.ui.popup.actions.RemoveDerbyNature.java
License:Apache License
public void run(IAction action) { IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); try {/*from w w w.j av a2 s .co m*/ ((ApplicationWindow) window).setStatus(Messages.REMOVING_NATURE); if (currentJavaProject == null) { currentJavaProject = JavaCore.create(currentProject); } //Shutdown server if running for the current project if (DerbyServerUtils.getDefault().getRunning(currentJavaProject.getProject())) { DerbyServerUtils.getDefault().stopDerbyServer(currentJavaProject.getProject()); } IClasspathEntry[] rawClasspath = currentJavaProject.getRawClasspath(); List<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>(); for (IClasspathEntry e : rawClasspath) { if (e.getEntryKind() != IClasspathEntry.CPE_CONTAINER) { newEntries.add(e); } else if (!e.getPath().equals(DerbyClasspathContainer.CONTAINER_ID)) { newEntries.add(e); } } IClasspathEntry[] newEntriesArray = new IClasspathEntry[newEntries.size()]; newEntriesArray = (IClasspathEntry[]) newEntries.toArray(newEntriesArray); currentJavaProject.setRawClasspath(newEntriesArray, null); IProjectDescription description = currentJavaProject.getProject().getDescription(); String[] natures = description.getNatureIds(); description.setNatureIds(removeDerbyNature(natures)); currentJavaProject.getProject().setDescription(description, null); // refresh project so user sees changes currentJavaProject.getProject().refreshLocal(IResource.DEPTH_INFINITE, null); ((ApplicationWindow) window).setStatus(Messages.DERBY_NATURE_REMOVED); } catch (Exception e) { Logger.log( Messages.ERROR_REMOVING_NATURE + " '" + currentJavaProject.getProject().getName() + "': " + e, IStatus.ERROR); Shell shell = new Shell(); MessageDialog.openInformation(shell, CommonNames.PLUGIN_NAME, Messages.ERROR_REMOVING_NATURE + ":\n" + SelectionUtil.getStatusMessages(e)); } }