List of usage examples for org.eclipse.jdt.core IClasspathEntry CPE_SOURCE
int CPE_SOURCE
To view the source code for org.eclipse.jdt.core IClasspathEntry CPE_SOURCE.
Click Source Link
From source file:com.javadude.antxr.eclipse.core.AntxrNature.java
License:Open Source License
public void configure() throws CoreException { if (AntxrNature.DEBUG) { System.out.println("configuring ANTXR nature"); }/*w ww . j a v a2s.co m*/ IProject project = getProject(); IProjectDescription projectDescription = project.getDescription(); List<ICommand> commands = new ArrayList<ICommand>(Arrays.asList(projectDescription.getBuildSpec())); ICommand antxrBuilderCommand = projectDescription.newCommand(); antxrBuilderCommand.setBuilderName(AntxrBuilder.BUILDER_ID); ICommand warningCleanerBuilderCommand = projectDescription.newCommand(); warningCleanerBuilderCommand.setBuilderName(WarningCleanerBuilder.BUILDER_ID); ICommand smapBuilderCommand = projectDescription.newCommand(); smapBuilderCommand.setBuilderName(SMapInstallerBuilder.BUILDER_ID); if (!commands.contains(antxrBuilderCommand)) { commands.add(0, antxrBuilderCommand); // add at start } if (!commands.contains(warningCleanerBuilderCommand)) { commands.add(warningCleanerBuilderCommand); // add at end } if (!commands.contains(smapBuilderCommand)) { commands.add(smapBuilderCommand); // add at end } // Commit the spec change into the project projectDescription.setBuildSpec(commands.toArray(new ICommand[commands.size()])); getProject().setDescription(projectDescription, null); // // add the antxr.jar file to a lib dir // IFolder folder = getProject().getFolder("lib"); // if (!folder.exists()) { // folder.create(true, true, null); // } // IPath rawLocation = folder.getRawLocation(); // URL antxrJarUrl = AntxrCorePlugin.getDefault().getBundle().getEntry("lib/antxr.jar"); // // BufferedOutputStream bout = null; // BufferedInputStream bin = null; // int b; // try { // try { // bout = new BufferedOutputStream(new FileOutputStream(new File(rawLocation.toFile(), "antxr.jar"))); // bin = new BufferedInputStream(antxrJarUrl.openStream()); // while ((b = bin.read()) != -1) { // bout.write(b); // } // } finally { // if (bout != null) { // bout.close(); // } // if (bin != null) { // bin.close(); // } // } // } catch (IOException e) { // e.printStackTrace(); // } // // folder.refreshLocal(IResource.DEPTH_ONE, null); // // // add the jar to the classpath if not present // String jarPath = "/" + getProject().getName() + "/lib/antxr.jar"; IFolder antxr3GeneratedFolder = getProject().getFolder("antxr-generated"); if (!antxr3GeneratedFolder.exists()) { antxr3GeneratedFolder.create(true, true, null); } String generatedSourcePath = "/" + getProject().getName() + "/antxr-generated"; final IJavaProject javaProject = JavaCore.create(getProject()); IClasspathEntry[] rawClasspath = javaProject.getRawClasspath(); boolean generatedSourceDirFound = false; for (IClasspathEntry classpathEntry : rawClasspath) { // if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) { // if (classpathEntry.getPath().toString().equals(jarPath)) { // found = true; // } // } if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { if (classpathEntry.getPath().toString().equals(generatedSourcePath)) { generatedSourceDirFound = true; } } } int toAdd = 0; if (!generatedSourceDirFound) { toAdd++; } if (!generatedSourceDirFound) { final IClasspathEntry newEntries[] = new IClasspathEntry[rawClasspath.length + toAdd]; System.arraycopy(rawClasspath, 0, newEntries, 0, rawClasspath.length); // newEntries[rawClasspath.length] = JavaCore.newLibraryEntry(new Path(jarPath), Path.EMPTY, Path.ROOT); if (!generatedSourceDirFound) { newEntries[newEntries.length - 1] = JavaCore.newSourceEntry(new Path(generatedSourcePath)); } JavaCore.run(new IWorkspaceRunnable() { public void run(IProgressMonitor monitor) throws CoreException { javaProject.setRawClasspath(newEntries, null); } }, null); } }
From source file:com.javadude.antxr.eclipse.core.AntxrNature.java
License:Open Source License
public void deconfigure() throws CoreException { if (AntxrNature.DEBUG) { System.out.println("deconfiguring ANTXR nature"); }/*from ww w . j a v a 2s . c o m*/ IProject project = getProject(); IProjectDescription desc = project.getDescription(); List<ICommand> commands = new ArrayList<ICommand>(Arrays.asList(desc.getBuildSpec())); for (Iterator i = commands.iterator(); i.hasNext();) { ICommand command = (ICommand) i.next(); if (command.getBuilderName().equals(AntxrBuilder.BUILDER_ID) || command.getBuilderName().equals(SMapInstallerBuilder.BUILDER_ID) || command.getBuilderName().equals(WarningCleanerBuilder.BUILDER_ID)) { i.remove(); } } // Commit the spec change into the project desc.setBuildSpec(commands.toArray(new ICommand[commands.size()])); project.setDescription(desc, null); // remove the jar from the classpath if present // delete the jar from the lib dir // if the lib dir is empty, delete it // String jarPath = "/" + getProject().getName() + "/lib/antxr.jar"; String generatedSourcePath = "/" + getProject().getName() + "/antxr-generated"; final IJavaProject javaProject = JavaCore.create(getProject()); IClasspathEntry[] rawClasspath = javaProject.getRawClasspath(); final List<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>(); for (IClasspathEntry entry : rawClasspath) { // if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) { // if (entry.getPath().toString().equals(jarPath)) { // found = i; // break; // } if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { if (entry.getPath().toString().equals(generatedSourcePath)) { continue; // skip the antxr-generated source folder } } newEntries.add(entry); } JavaCore.run(new IWorkspaceRunnable() { public void run(IProgressMonitor monitor) throws CoreException { IClasspathEntry entries[] = newEntries.toArray(new IClasspathEntry[newEntries.size()]); javaProject.setRawClasspath(entries, null); IFolder folder = getProject().getFolder("antxr-generated"); if (folder.exists() && !hasNonAntxrGeneratedFile(folder)) { folder.delete(true, null); } else { AntxrCorePlugin.getUtil().warning(4211, "Did not delete antxr-generated source directory; it contains non-generated source. You should move that source to another source dir; it really doesn't belong there..."); } } }, null); }
From source file:com.javadude.antxr.eclipse.core.builder.AntxrBuilder.java
License:Open Source License
/** * Compile a grammar//from ww w .j ava 2s . com * * @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 (AntxrCorePlugin.getUtil().hasNature(aFile.getProject(), AntxrNature.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( AntxrCorePlugin.getFormattedMessage("AntxrBuilder.compiling", aFile.getFullPath().toString()), 4); // Remove all markers from this file try { aFile.deleteMarkers(null, true, IResource.DEPTH_INFINITE); } catch (CoreException e) { AntxrCorePlugin.log(e); } // Prepare arguments for ANTXR compiler // read the settings for this grammar Map<String, Map<String, String>> map = SettingsPersister.readSettings(aFile.getProject()); List<String> args = createArguments(map, aFile); if (AntxrBuilder.DEBUG) { System.out.println("Compiling ANTXR 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 ANTXR grammar file AntxrTool tool = new AntxrTool(); 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) { AntxrCorePlugin.log(e); } } } catch (Throwable e) { if (!(e instanceof SecurityException)) { AntxrCorePlugin.log(e); } } finally { System.setOut(fOriginalOut); System.setErr(fOriginalErr); aMonitor.done(); } }
From source file:com.javadude.antxr.eclipse.core.builder.AntxrBuilder.java
License:Open Source License
/** * Returns list of commandline arguments for ANTXR compiler. * * @param map//from w ww .j a va2s.c om * the saved antxr-eclipse settings for this project * @param file * The grammar file to parse * @return a list of command-line arguments */ private List<String> createArguments(Map<String, Map<String, String>> map, IFile file) { List<String> args = new ArrayList<String>(); AntxrCorePlugin.getDefault().upgradeOldSettings(file, map); fOutput = SettingsPersister.get(map, file, SettingsPersister.OUTPUT_PROPERTY); // Prepare absolute output path (-o) String outputPath = null; if (fOutput != null && fOutput.trim().length() > 0) { outputPath = convertRootRelatedPath(fOutput); } if (outputPath == null) { try { IJavaProject javaProject = JavaCore.create(file.getProject()); IClasspathEntry rawClasspath[] = javaProject.getRawClasspath(); String longestSourceDirPrefix = ""; String grammarFilePath = file.getFullPath().toString(); for (IClasspathEntry entry : rawClasspath) { if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { String sourceDirString = entry.getPath().toString(); if (grammarFilePath.startsWith(sourceDirString) && sourceDirString.length() > longestSourceDirPrefix.length()) { longestSourceDirPrefix = sourceDirString; } } } if ("".equals(longestSourceDirPrefix)) { return null; } String basePath = file.getFullPath().removeLastSegments(1).toString(); String packageName = basePath.substring(longestSourceDirPrefix.length()); IFolder targetDirFolder = file.getProject().getFolder("antxr-generated/" + packageName); if (!targetDirFolder.exists()) { targetDirFolder.getRawLocation().toFile().mkdirs(); file.getProject().refreshLocal(IResource.DEPTH_INFINITE, null); } fOutput = targetDirFolder.getFullPath().toString(); outputPath = targetDirFolder.getLocation().toString(); } catch (Exception e) { throw new RuntimeException("Could not set up antxr-generated as output dir", e); } } args.add("-o"); args.add(outputPath); // Get boolean options from grammar properties addBooleanGrammarProperty(map, file, SettingsPersister.DEBUG_PROPERTY, "-debug", null, args, false); addBooleanGrammarProperty(map, file, SettingsPersister.HTML_PROPERTY, "-html", null, args, false); addBooleanGrammarProperty(map, file, SettingsPersister.DOCBOOK_PROPERTY, "-docbook", null, args, false); addBooleanGrammarProperty(map, file, SettingsPersister.DIAGNOSTIC_PROPERTY, "-diagnostic", null, args, false); addBooleanGrammarProperty(map, file, SettingsPersister.TRACE_PROPERTY, "-trace", null, args, false); addBooleanGrammarProperty(map, file, SettingsPersister.TRACE_PARSER_PROPERTY, "-traceParser", null, args, false); addBooleanGrammarProperty(map, file, SettingsPersister.TRACE_LEXER_PROPERTY, "-traceLexer", null, args, false); addBooleanGrammarProperty(map, file, SettingsPersister.TRACE_TREE_PARSER_PROPERTY, "-traceTreeParser", null, args, false); addBooleanGrammarProperty(map, file, SettingsPersister.SMAP_PROPERTY, "-smap", null, args, true); // Get super grammars from grammar properties String superGrammars = SettingsPersister.get(map, file, SettingsPersister.SUPER_GRAMMARS_PROPERTY); // Prepare optional super grammar(s) (-glib) // Can be defined in two ways : in a property dialog OR // inside the grammar using a comment "// -glib <file in same folder>" if (superGrammars != null && superGrammars.trim().length() > 0) { superGrammars = convertRootRelatedPathList(superGrammars); if (superGrammars != null) { args.add("-glib"); args.add(superGrammars); } } else { // Try to get // -glib parameter from comment in .g file. This // enables sharing in a team project without local reconfiguration String localSuperGrammar = extractGlibComment(fFile); if (localSuperGrammar != null) { localSuperGrammar = convertFolderRelatedPath(localSuperGrammar); if (localSuperGrammar != null) { args.add("-glib"); args.add(localSuperGrammar); } } } // Prepare ANTXR grammar file which needs to be compiled args.add(fFile.getLocation().toOSString()); return args; }
From source file:com.javadude.antxr.eclipse.smapinstaller.SMapInstallerBuilder.java
License:Open Source License
/** * Installs the modified smap into a generated classfile * @param resource/*from w w w . ja va2 s. c om*/ * @throws JavaModelException */ protected void installSmap(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 (IClasspathEntry entry : classpathEntries) { 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(AntxrBuilder.INSTALL_SMAP); if ("true".equals(installSmap)) { SDEInstaller.install(classFile, smapFile); } } catch (CoreException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:com.javapathfinder.vjp.DefaultProperties.java
License:Open Source License
/** * append all relevant paths from the target project settings to the vm.classpath *///from w w w . j a v a2 s. c om private static void appendProjectClassPaths(IJavaProject project, StringBuilder cp) { try { // we need to maintain order LinkedHashSet<IPath> paths = new LinkedHashSet<IPath>(); // append the default output folder IPath defOutputFolder = project.getOutputLocation(); if (defOutputFolder != null) { paths.add(defOutputFolder); } // look for libraries and source root specific output folders for (IClasspathEntry e : project.getResolvedClasspath(true)) { IPath ePath = null; switch (e.getContentKind()) { case IClasspathEntry.CPE_LIBRARY: ePath = e.getPath(); break; case IClasspathEntry.CPE_SOURCE: ePath = e.getOutputLocation(); break; } if (ePath != null && !paths.contains(ePath)) { paths.add(ePath); } } for (IPath path : paths) { String absPath = getAbsolutePath(project, path).toOSString(); // if (cp.length() > 0) { // cp.append(Config.LIST_SEPARATOR); // } // only add classes that are found in bin if (absPath.contains("/bin")) cp.append(absPath); } System.out.println("cp is" + cp); } catch (JavaModelException jme) { VJP.logError("Could not append Project classpath", jme); } }
From source file:com.javapathfinder.vjp.DefaultProperties.java
License:Open Source License
private static String getSourcepathEntry(IJavaProject project) { StringBuilder sourcepath = new StringBuilder(); IClasspathEntry[] paths;//from w ww . j av a2 s. com try { paths = project.getResolvedClasspath(true); } catch (JavaModelException e) { VJP.logError("Could not retrieve project classpaths.", e); return ""; } for (IClasspathEntry entry : paths) { if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { sourcepath.append(getAbsolutePath(project, entry.getPath())); sourcepath.append(Config.LIST_SEPARATOR); } else if (entry.getSourceAttachmentPath() != null) { IPath path = entry.getSourceAttachmentPath(); if (path.getFileExtension() == null) { //null for a directory sourcepath.append(path); sourcepath.append(Config.LIST_SEPARATOR); } } } if (sourcepath.length() > 0) sourcepath.setLength(sourcepath.length() - 1); //remove that trailing separator // VJP.logInfo(sourcepath.toString()); return sourcepath.toString(); }
From source file:com.jstar.eclipse.objects.JavaFile.java
License:BSD License
public String getProjectClasspath() { StringBuilder projectClassPath = new StringBuilder(); try {/*from w ww.j av a 2 s .com*/ for (IClasspathEntry entry : getJavaProject().getProject().getResolvedClasspath(true)) { final int entryKind = entry.getEntryKind(); if (entryKind == IClasspathEntry.CPE_SOURCE) { projectClassPath .append(getJavaProject().getWorkspaceLocation().append(entry.getPath()).toOSString()); projectClassPath.append(File.pathSeparator); } if (entryKind == IClasspathEntry.CPE_LIBRARY) { projectClassPath.append(getAbsolutePath(entry)); projectClassPath.append(File.pathSeparator); } //TODO: IClasspathEntry.CPE_PROJECT } return projectClassPath.toString(); } catch (CoreException ce) { ce.printStackTrace(ConsoleService.getInstance().getConsoleStream()); return ""; } }
From source file:com.laex.j2objc.preferences.ClasspathPropertyPage.java
License:Open Source License
/** * Load project referenced classpaths.//from ww w . ja v a 2 s . co m */ private void loadProjectReferencedClasspaths() { try { IClasspathEntry[] refClasspath = ProjectUtil.getJavaProject(getElement()).getResolvedClasspath(true); for (IClasspathEntry o : refClasspath) { IClasspathEntry entry = JavaCore.getResolvedClasspathEntry((IClasspathEntry) o); String path = null; // We need to figure out the path for different types of // classpath entries. // the types of entries are: CPE_LIBRARY, CPE_PROJECT, // CPE_SOURCE, CPE_VARIABLE, CPE_CONTAINER switch (entry.getEntryKind()) { case IClasspathEntry.CPE_LIBRARY: // With CPE Library: some jar files could reside in system, // like JDK jar files // some jar files may reside in workspace, within other // project. // Check if the library resides in workspace project IResource file = ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath()); // null means, this library does not reside in workspace but // instead in the system if (file == null) { // get the apropriate path path = entry.getPath().makeAbsolute().toOSString(); } else { // if resdies in workspace, get the appropriate path if (file.exists()) { path = file.getLocation().makeAbsolute().toOSString(); } } break; case IClasspathEntry.CPE_PROJECT: case IClasspathEntry.CPE_SOURCE: // project and source IResource res = ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath()); if (res.exists()) { path = res.getFullPath().makeAbsolute().toOSString(); } break; } // some path may be folders. In that case, we have to make sure // we store the full absolute path to the folders boolean isArchive = path.endsWith("jar") || path.endsWith("zip"); if (!isArchive) { IPath ipath = new Path(path); // We can only get a folder if the segment count is greater // 2 i.e. if the path has at least two segments if (ipath.segmentCount() >= 2) { IFolder folder = ResourcesPlugin.getWorkspace().getRoot().getFolder(ipath); if (folder.exists()) { classpathRef.add(folder.getLocation().makeAbsolute().toOSString()); } } // If the segment count is 1, then it is probably a project if (ipath.segmentCount() == 1) { IProject refPrj = ResourcesPlugin.getWorkspace().getRoot().getProject(path); // if this is a project, then we assume it has a SRC // folder; and therefore append SRC at the end // this is required because j2objc compiler needs the // path till the src folder to compile properly classpathRef.add(refPrj.getLocation().append("src").makeAbsolute().toOSString()); } } else { // add whatever archive path we get from the results classpathRef.add(path); } } checkboxTableViewer.setInput(classpathRef); checkboxTableViewer.refresh(); } catch (JavaModelException e) { LogUtil.logException(e); } catch (CoreException e) { LogUtil.logException(e); } }
From source file:com.legstar.eclipse.plugin.cixscom.wizards.AbstractCixsGeneratorWizardPage.java
License:Open Source License
/** * From a Java nature Eclipse project, this utility method retrieves either * a java source folder or an associated java output folder for binaries. * <p/>/*from ww w . j a v a2 s. co m*/ * The algorithm stops at the first source directory encountered. * * @param project the current Eclipse project * @param source true if we are looking for a source folder, false for an * output folder * @return a java folder (either source or output) */ protected File getJavaDir(final IProject project, final boolean source) { IJavaProject javaProject = JavaCore.create(project); if (javaProject != null) { IPath rootPath = javaProject.getProject().getLocation().removeLastSegments(1); /* * If this is a java project, get first Java source and output * folders. */ try { IClasspathEntry[] cpe = javaProject.getRawClasspath(); for (int i = 0; i < cpe.length; i++) { if (cpe[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) { if (source) { return rootPath.append(cpe[i].getPath()).toFile(); } else { if (cpe[i].getOutputLocation() == null) { return rootPath.append(javaProject.getOutputLocation()).toFile(); } else { return rootPath.append(cpe[i].getOutputLocation()).toFile(); } } } } } catch (JavaModelException e) { AbstractWizard.errorDialog(getShell(), Messages.generate_error_dialog_title, getPluginId(), Messages.java_location_lookup_failure_msg, NLS.bind(Messages.invalid_java_project_msg, project.getName(), e.getMessage())); AbstractWizard.logCoreException(e, getPluginId()); } } /* * If everything else failed, assume generated artifacts will go to the * project root. */ return project.getLocation().toFile(); }