Java Code Examples for org.eclipse.jdt.core.JavaCore#newSourceEntry()

The following examples show how to use org.eclipse.jdt.core.JavaCore#newSourceEntry() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GWTRuntimeTest.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Tests that we find an {@link com.google.gdt.eclipse.core.sdk.Sdk} on the
 * gwt-user project.
 *
 * @throws Exception
 */
public void testFindSdkFor_GwtUserProject() throws Exception {
  GwtRuntimeTestUtilities.importGwtSourceProjects();
  try {
    IJavaModel javaModel = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
    IJavaProject javaProject = javaModel.getJavaProject("gwt-user");
    GwtSdk sdk = GwtSdk.findSdkFor(javaProject);
    IClasspathEntry gwtUserEntry =
        JavaCore.newSourceEntry(
            javaModel.getJavaProject("gwt-user").getPath().append("core/src"),
            new IPath[] {new Path("**/super/**")});
    /*
     * NOTE: Passing null for the IClasspathAttribute array tickles a bug in
     * eclipse 3.3.
     */
    IClasspathEntry gwtDevEntry =
        JavaCore.newProjectEntry(javaModel.getJavaProject("gwt-dev").getPath(), null, false,
            new IClasspathAttribute[0] /* */, false);
    IClasspathEntry[] expected = new IClasspathEntry[] {gwtUserEntry, gwtDevEntry};
    assertEquals(expected, sdk.getClasspathEntries());
  } finally {
    GwtRuntimeTestUtilities.removeGwtSourceProjects();
  }
}
 
Example 2
Source File: JavaProjectHelper.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Adds a source container to a IJavaProject.
 *
 * @param jproject
 *            The parent project
 * @param containerName
 *            The name of the new source container
 * @param inclusionFilters
 *            Inclusion filters to set
 * @param exclusionFilters
 *            Exclusion filters to set
 * @return The handle to the new source container
 * @throws CoreException
 *             Creation failed
 */
public static IPackageFragmentRoot addSourceContainer(IJavaProject jproject, String containerName, IPath[] inclusionFilters,
        IPath[] exclusionFilters) throws CoreException {
    IProject project = jproject.getProject();
    IContainer container = null;
    if (containerName == null || containerName.length() == 0) {
        container = project;
    } else {
        IFolder folder = project.getFolder(containerName);
        if (!folder.exists()) {
            CoreUtility.createFolder(folder, false, true, null);
        }
        container = folder;
    }
    IPackageFragmentRoot root = jproject.getPackageFragmentRoot(container);

    IClasspathEntry cpe = JavaCore.newSourceEntry(root.getPath(), inclusionFilters, exclusionFilters, null);
    addToClasspath(jproject, cpe);
    return root;
}
 
Example 3
Source File: JDTAwareEclipseResourceFileSystemAccess2.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @param prototype settings will be copied from the prototype and the new entry is inserted after that one.
 */
private void insertClasspathEntry(IContainer folder, IClasspathEntry prototype, IJavaProject project)
		throws JavaModelException {
	IClasspathEntry newEntry = JavaCore.newSourceEntry(
			folder.getFullPath(),
			prototype.getInclusionPatterns(),
			prototype.getExclusionPatterns(),
			prototype.getOutputLocation(),
			prototype.getExtraAttributes());
	IClasspathEntry[] classPath = project.getRawClasspath();
	IClasspathEntry[] newClassPath = new IClasspathEntry[classPath.length + 1];
	int i = 0;
	for(IClasspathEntry entry: classPath) {
		newClassPath[i++] = entry;
		if (entry.equals(prototype)) {
			newClassPath[i++] = newEntry;
		}
	}
	// should not happen, but to be sure
	if (i == newClassPath.length - 1 && newClassPath[i] == null) {
		LOG.warn("Cannot find classpath entry '" + prototype + "'");
		newClassPath[i] = newEntry;
	}
	project.setRawClasspath(newClassPath, getMonitor());
}
 
Example 4
Source File: FixProjectsUtils.java    From hybris-commerce-eclipse-plugin with Apache License 2.0 6 votes vote down vote up
public static void addToClassPath(IResource res, int type, IJavaProject javaProject, IProgressMonitor monitor) throws JavaModelException
{
	Set<IClasspathEntry> entries = new HashSet<IClasspathEntry>(Arrays.asList(javaProject.getRawClasspath()));
	IClasspathEntry entry = null;
	switch (type) {
	case IClasspathEntry.CPE_SOURCE: 
		entry = JavaCore.newSourceEntry(res.getFullPath());
		break;
	case IClasspathEntry.CPE_LIBRARY: 
		entry = JavaCore.newLibraryEntry(res.getFullPath(), null, null, true);
		break;
	case IClasspathEntry.CPE_PROJECT: 
		entry = JavaCore.newProjectEntry(res.getFullPath(), true);
		break;
	}
		
	entries.add(entry);
	setClasspath(entries.toArray(new IClasspathEntry[entries.size()]), javaProject, monitor);
}
 
Example 5
Source File: NewReportProjectWizard.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
protected IClasspathEntry[] getInternalClassPathEntries( IProject project )
{
	if ( sourceText.getText( ) == null
			|| sourceText.getText( ).trim( ).equals( "" ) ) //$NON-NLS-1$
	{
		return new IClasspathEntry[0];
	}
	IClasspathEntry[] entries = new IClasspathEntry[1];
	IPath path = project.getFullPath( ).append( sourceText.getText( ) );
	entries[0] = JavaCore.newSourceEntry( path );
	return entries;
}
 
Example 6
Source File: SarlClassPathDetector.java    From sarl with Apache License 2.0 5 votes vote down vote up
private IClasspathEntry createSourceFolderEntry(IPath path) {
	final List<IPath> excluded = new ArrayList<>();
	for (final IPath other : this.sourceFolders.keySet()) {
		if (!path.equals(other) && path.isPrefixOf(other)) {
			final IPath pathToExclude = other.removeFirstSegments(path.segmentCount()).addTrailingSeparator();
			excluded.add(pathToExclude);
		}
	}
	final IPath[] excludedPaths = excluded.toArray(new IPath[excluded.size()]);
	return JavaCore.newSourceEntry(path, excludedPaths);
}
 
Example 7
Source File: NewJavaProjectWizardPageOne.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the source class path entries to be added on new projects.
 * The underlying resources may not exist. All entries that are returned must be of kind
 * {@link IClasspathEntry#CPE_SOURCE}.
 *
 * @return returns the source class path entries for the new project
 */
public IClasspathEntry[] getSourceClasspathEntries() {
	IPath sourceFolderPath= new Path(getProjectName()).makeAbsolute();

	if (fLayoutGroup.isSrcBin()) {
		IPath srcPath= new Path(PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.SRCBIN_SRCNAME));
		if (srcPath.segmentCount() > 0) {
			sourceFolderPath= sourceFolderPath.append(srcPath);
		}
	}
	return new IClasspathEntry[] {  JavaCore.newSourceEntry(sourceFolderPath) };
}
 
Example 8
Source File: ProjectTestUtil.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates a Java project with the given name and a "src" folder and the default JRE library on
 * its classpath.
 */
public static IJavaProject createProject(String projectName) throws CoreException {
  IClasspathEntry[] classpath = new IClasspathEntry[] {
      JavaCore.newSourceEntry(Path.fromOSString("/" + projectName + "/" + SOURCE_FOLDER)),
      JavaCore.newContainerEntry(Path.fromOSString("org.eclipse.jdt.launching.JRE_CONTAINER"))};
  return ProjectTestUtil.createProject(projectName, classpath);
}
 
Example 9
Source File: IDEOpenSampleReportAction.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
protected IClasspathEntry[] getInternalClassPathEntries( IProject project )
{
	IClasspathEntry[] entries = new IClasspathEntry[1];
	IPath path = project.getFullPath( ).append( "src" ); //$NON-NLS-1$
	entries[0] = JavaCore.newSourceEntry( path );
	return entries;
}
 
Example 10
Source File: JDTAwareEclipseResourceFileSystemAccess2.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private void addClasspathEntry(IContainer folder, IJavaProject project) throws JavaModelException {
	IClasspathEntry srcFolderClasspathEntry = JavaCore.newSourceEntry(folder.getFullPath());
	IClasspathEntry[] classPath = project.getRawClasspath();
	IClasspathEntry[] newClassPath = new IClasspathEntry[classPath.length + 1];
	System.arraycopy(classPath, 0, newClassPath, 0, classPath.length);
	newClassPath[newClassPath.length - 1] = srcFolderClasspathEntry;
	project.setRawClasspath(newClassPath, getMonitor());
}
 
Example 11
Source File: JavaProjectSetupUtil.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public static IFolder addSourceFolder(IJavaProject javaProject, String folderName, String[] inclusionPatterns, String[] exclusionPatterns) throws CoreException,
		JavaModelException {
	
	IProject project = javaProject.getProject();
	IPath projectPath = project.getFullPath();

	deleteClasspathEntry(javaProject, projectPath);

	IFolder srcFolder = createSubFolder(project, folderName); //$NON-NLS-1$
	IClasspathEntry srcFolderClasspathEntry = JavaCore.newSourceEntry(srcFolder.getFullPath(), getInclusionPatterns(inclusionPatterns), getExclusionPatterns(exclusionPatterns), null);
	addToClasspath(javaProject, srcFolderClasspathEntry);
	return srcFolder;
}
 
Example 12
Source File: SARLProjectConfigurator.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies the default source entries for a SARL project.
 *
 * @param projectFolder the folder of the project.
 * @return the classpath entries.
 */
public static List<IClasspathEntry> getDefaultSourceClassPathEntries(IPath projectFolder) {
	final IPath srcJava = projectFolder.append(
			Path.fromPortableString(SARLConfig.FOLDER_SOURCE_JAVA));
	final IClasspathEntry srcJavaEntry = JavaCore.newSourceEntry(srcJava.makeAbsolute());

	final IPath srcSarl = projectFolder.append(
			Path.fromPortableString(SARLConfig.FOLDER_SOURCE_SARL));
	final IClasspathEntry srcSarlEntry = JavaCore.newSourceEntry(srcSarl.makeAbsolute());

	final IPath srcGeneratedSources = projectFolder.append(
			Path.fromPortableString(SARLConfig.FOLDER_SOURCE_GENERATED));
	final IClasspathAttribute attr = JavaCore.newClasspathAttribute(
			IClasspathAttribute.IGNORE_OPTIONAL_PROBLEMS,
			Boolean.TRUE.toString());
	final IClasspathEntry srcGeneratedSourcesEntry = JavaCore.newSourceEntry(
			srcGeneratedSources.makeAbsolute(),
			ClasspathEntry.INCLUDE_ALL,
			ClasspathEntry.EXCLUDE_NONE,
			null /*output location*/,
			new IClasspathAttribute[] {attr});

	final IPath srcResources = projectFolder.append(
			Path.fromPortableString(SARLConfig.FOLDER_RESOURCES));
	final IClasspathEntry srcResourcesEntry = JavaCore.newSourceEntry(srcResources.makeAbsolute());

	return Arrays.asList(
			srcSarlEntry,
			srcJavaEntry,
			srcResourcesEntry,
			srcGeneratedSourcesEntry);
}
 
Example 13
Source File: ClasspathUtilTest.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddClasspathEntry() throws JavaModelException {
  IClasspathEntry entry = JavaCore.newSourceEntry(new Path("/my/src"));
  assertFalse(classpathExists(entry));
  ClasspathUtil.addClasspathEntry(project, entry, monitor);
  assertTrue(classpathExists(entry));
}
 
Example 14
Source File: CreateAppEngineWtpProject.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
private void fixTestSourceDirectorySettings(IProject newProject, IProgressMonitor monitor)
    throws CoreException {
  // 1. Fix the output folder of "src/test/java".
  IPath testSourcePath = newProject.getFolder("src/test/java").getFullPath();

  IJavaProject javaProject = JavaCore.create(newProject);
  IClasspathEntry[] entries = javaProject.getRawClasspath();
  for (int i = 0; i < entries.length; i++) {
    IClasspathEntry entry = entries[i];
    if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE
        && entry.getPath().equals(testSourcePath)
        && entry.getOutputLocation() == null) {  // Default output location?
      IPath oldOutputPath = javaProject.getOutputLocation();
      IPath newOutputPath = oldOutputPath.removeLastSegments(1).append("test-classes");

      entries[i] = JavaCore.newSourceEntry(testSourcePath, ClasspathEntry.INCLUDE_ALL,
          ClasspathEntry.EXCLUDE_NONE, newOutputPath);
      javaProject.setRawClasspath(entries, monitor);
      break;
    }
  }

  // 2. Remove "src/test/java" from the Web Deployment Assembly sources.
  deployAssemblyEntryRemoveJob =
      new DeployAssemblyEntryRemoveJob(newProject, new Path("src/test/java"));
  deployAssemblyEntryRemoveJob.setSystem(true);
  deployAssemblyEntryRemoveJob.schedule();
}
 
Example 15
Source File: IDECPListElement.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
private IClasspathEntry newClasspathEntry( )
{

	IClasspathAttribute[] extraAttributes = new IClasspathAttribute[0];
	switch ( fEntryKind )
	{
		case IClasspathEntry.CPE_SOURCE :
			return JavaCore.newSourceEntry( fPath,
					null,
					null,
					null,
					extraAttributes );
		case IClasspathEntry.CPE_LIBRARY :
		{
			return JavaCore.newLibraryEntry( fPath,
					null,
					null,
					null,
					extraAttributes,
					isExported( ) );
		}
		case IClasspathEntry.CPE_PROJECT :
		{
			return JavaCore.newProjectEntry( fPath,
					null,
					false,
					extraAttributes,
					isExported( ) );
		}
		case IClasspathEntry.CPE_CONTAINER :
		{
			return JavaCore.newContainerEntry( fPath,
					null,
					extraAttributes,
					isExported( ) );
		}
		case IClasspathEntry.CPE_VARIABLE :
		{
			return JavaCore.newVariableEntry( fPath,
					null,
					null,
					null,
					extraAttributes,
					isExported( ) );
		}
		default :
			return null;
	}
}
 
Example 16
Source File: ProjectsManager.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public static IProject createJavaProject(IProject project, IPath projectLocation, String src, String bin, IProgressMonitor monitor) throws CoreException, OperationCanceledException {
	if (project.exists()) {
		return project;
	}
	JavaLanguageServerPlugin.logInfo("Creating the Java project " + project.getName());
	//Create project
	IProjectDescription description = ResourcesPlugin.getWorkspace().newProjectDescription(project.getName());
	if (projectLocation != null) {
		description.setLocation(projectLocation);
	}
	project.create(description, monitor);
	project.open(monitor);

	//Turn into Java project
	description = project.getDescription();
	description.setNatureIds(new String[] { JavaCore.NATURE_ID });
	project.setDescription(description, monitor);

	IJavaProject javaProject = JavaCore.create(project);
	configureJVMSettings(javaProject);

	//Add build output folder
	if (StringUtils.isNotBlank(bin)) {
		IFolder output = project.getFolder(bin);
		if (!output.exists()) {
			output.create(true, true, monitor);
		}
		javaProject.setOutputLocation(output.getFullPath(), monitor);
	}

	List<IClasspathEntry> classpaths = new ArrayList<>();
	//Add source folder
	if (StringUtils.isNotBlank(src)) {
		IFolder source = project.getFolder(src);
		if (!source.exists()) {
			source.create(true, true, monitor);
		}
		IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(source);
		IClasspathEntry srcClasspath = JavaCore.newSourceEntry(root.getPath());
		classpaths.add(srcClasspath);
	}

	//Find default JVM
	IClasspathEntry jre = JavaRuntime.getDefaultJREContainerEntry();
	classpaths.add(jre);

	//Add JVM to project class path
	javaProject.setRawClasspath(classpaths.toArray(new IClasspathEntry[0]), monitor);

	JavaLanguageServerPlugin.logInfo("Finished creating the Java project " + project.getName());
	return project;
}
 
Example 17
Source File: ProjectClasspathFactory.java    From bonita-studio with GNU General Public License v2.0 4 votes vote down vote up
protected IClasspathEntry newSourceEntry(final IPath path) {
    return JavaCore.newSourceEntry(path);
}
 
Example 18
Source File: BinFolderConfigurator.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Copy a classpath entry and sets the given output path instead of the original path.
 */
private IClasspathEntry copyWithOutput(IClasspathEntry entry, IPath output) {
	return JavaCore.newSourceEntry(entry.getPath(), entry.getInclusionPatterns(), entry.getExclusionPatterns(), output, entry.getExtraAttributes());
}
 
Example 19
Source File: JavaProjectFactory.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected void enhanceProject(IProject project, SubMonitor monitor, Shell shell) throws CoreException {
	super.enhanceProject(project, monitor, shell);
	if (builderIds.contains(JavaCore.BUILDER_ID)) {
		SubMonitor subMonitor = SubMonitor.convert(monitor, 10);
		try {
			subMonitor.subTask(Messages.JavaProjectFactory_ConfigureJavaProject + projectName);
			IJavaProject javaProject = JavaCore.create(project);
			List<IClasspathEntry> classpathEntries = new ArrayList<IClasspathEntry>();
			for (final IProject referencedProject : project.getReferencedProjects()) {
				final IClasspathEntry referencedProjectClasspathEntry = JavaCore.newProjectEntry(referencedProject
						.getFullPath());
				classpathEntries.add(referencedProjectClasspathEntry);
			}
			for (final String folderName : getFolders()) {
				final IFolder sourceFolder = project.getFolder(folderName);
				String outputFolderName = sourceFolderOutputs.get(folderName);
				final IClasspathEntry srcClasspathEntry = JavaCore.newSourceEntry(sourceFolder.getFullPath(),
						ClasspathEntry.INCLUDE_ALL, ClasspathEntry.EXCLUDE_NONE,
						outputFolderName == null ? null : project.getFolder(outputFolderName).getFullPath(),
						testSourceFolders.contains(folderName)
								? new IClasspathAttribute[] { JavaCore.newClasspathAttribute("test", "true") }
								: new IClasspathAttribute[0]);
				classpathEntries.add(srcClasspathEntry);
			}
			classpathEntries.addAll(extraClasspathEntries);

			IClasspathEntry defaultJREContainerEntry = getJreContainerEntry();
			classpathEntries.add(defaultJREContainerEntry);
			addMoreClasspathEntriesTo(classpathEntries);
			
			javaProject.setRawClasspath(classpathEntries.toArray(new IClasspathEntry[classpathEntries.size()]),
					subMonitor.newChild(1));
			javaProject.setOutputLocation(new Path("/" + project.getName() + "/" + defaultOutput), subMonitor.newChild(1));
			
			String executionEnvironmentId = JavaRuntime.getExecutionEnvironmentId(defaultJREContainerEntry.getPath());
			if (executionEnvironmentId != null) {
				BuildPathSupport.setEEComplianceOptions(javaProject, executionEnvironmentId, null);
			}
		} catch (JavaModelException e) {
			logger.error(e.getMessage(), e);
		}
	}
}
 
Example 20
Source File: JavaProject.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Returns a default class path.
 * This is the root of the project
 */
protected IClasspathEntry[] defaultClasspath() {

	return new IClasspathEntry[] {
		 JavaCore.newSourceEntry(this.project.getFullPath())};
}