Java Code Examples for org.eclipse.core.resources.IFile#touch()

The following examples show how to use org.eclipse.core.resources.IFile#touch() . These examples are extracted from open source projects. 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
private void updateNpmIndex(IProgressMonitor monitor) throws CoreException {
	OutdatedPackageJsonQueue.Task task = packageJsonQueue.exhaust();
	if (task.isEmpty()) {
		return;
	}
	try {
		indexSynchronizer.checkAndClearIndex(monitor);
		Set<URI> toBeUpdated = task.getToBeBuilt().getToBeUpdated();
		IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
		for (URI touchMe : toBeUpdated) {
			if (touchMe.isPlatformResource()) {
				IFile file = workspaceRoot.getFile(new Path(touchMe.toPlatformString(true)));
				// could have been deleted in the meantime
				if (file.exists()) {
					file.touch(monitor);
				}
			}
		}
	} catch (Error | RuntimeException | CoreException e) {
		task.reschedule();
		throw e;
	} finally {
		monitor.done();
	}
}
 
Example 2
@Test
public void testCleanUpDerivedResources() throws Exception {
	IJavaProject project = createJavaProject("foo");
	addNature(project.getProject(), XtextProjectHelper.NATURE_ID);
	IFolder folder = project.getProject().getFolder("src");
	IFile file = folder.getFile("Foo" + F_EXT);
	file.create(new StringInputStream("object Foo"), true, monitor());
	build();
	IFile generatedFile = project.getProject().getFile("./src-gen/Foo.txt");
	assertTrue(generatedFile.exists());
	preferenceStoreAccess.getWritablePreferenceStore(project.getProject()).setValue(getDefaultOutputDirectoryKey(),
			"./src2-gen");

	DerivedResourceCleanerJob derivedResourceCleanerJob = derivedResourceCleanerJobProvider.get();
	derivedResourceCleanerJob.setUser(true);
	derivedResourceCleanerJob.initialize(project.getProject(), "src-gen");
	derivedResourceCleanerJob.schedule();
	derivedResourceCleanerJob.join();
	generatedFile = project.getProject().getFile("./src-gen/Foo.txt");
	assertFalse(generatedFile.exists());
	file.touch(monitor());
	build();
	generatedFile = project.getProject().getFile("./src2-gen/Foo.txt");
	assertTrue(generatedFile.exists());
}
 
Example 3
@Test
public void testDisabled() throws Exception {
	IJavaProject project = createJavaProject("foo");
	participant.getBuilderPreferenceAccess().setAutoBuildEnabled(project, false);
	addNature(project.getProject(), XtextProjectHelper.NATURE_ID);
	IFolder folder = project.getProject().getFolder("src");
	IFile file = folder.getFile("Foo" + F_EXT);
	file.create(new StringInputStream("object Foo"), true, monitor());
	build();
	IFile generatedFile = project.getProject().getFile("./src-gen/Foo.txt");
	assertFalse(generatedFile.exists());
	participant.getBuilderPreferenceAccess().setAutoBuildEnabled(project, true);
	file.touch(monitor());
	build();
	assertTrue(generatedFile.exists());
}
 
Example 4
@Test
public void testFullBuildWhenClasspathChanged_2() throws CoreException, InterruptedException {
	IJavaProject project = setupProject();
	IFile libaryFile = copyAndGetXtendExampleJar(project);
	IClasspathEntry libraryEntry = JavaCore.newLibraryEntry(libaryFile.getFullPath(), null, null);
	addToClasspath(project, libraryEntry);
	build();
	assertFalse(getEvents().isEmpty());
	getEvents().clear();

	libaryFile.touch(null);
	libaryFile.refreshLocal(IResource.DEPTH_INFINITE, null);
	build();
	assertEquals(1, getEvents().size());
	Event singleEvent = getEvents().get(0);
	ImmutableList<Delta> deltas = singleEvent.getDeltas();
	assertEquals(1, deltas.size());
}
 
Example 5
protected void testCreateFile(IContainer output) {
	try {
		IFile file = createFile(project.getFile(("src/Foo" + F_EXT)).getFullPath(), "object Foo");
		build();
		Assert.assertEquals("object Foo", readFile(output.getFile(new Path("Foo.txt"))));
		File javaIoFile = output.getFile(new Path("Foo.txt")).getLocation().toFile();
		javaIoFile.delete();
		javaIoFile.getParentFile().delete();
		file.touch(monitor());
		build();
		Assert.assertEquals("object Foo", readFile(output.getFile(new Path("Foo.txt"))));
	} catch (CoreException e) {
		throw Exceptions.sneakyThrow(e);
	}
}
 
Example 6
@Override
public void buildFinished(IJavaProject project) {
	StoppedTask task = Stopwatches.forTask("DebugSourceInstallingCompilationParticipant.install");
	try {
		task.start();
		super.buildFinished(project);
		if (files == null)
			return;
		for (BuildContext ctx : files) {
			try {
				IFile generatedJavaFile = ctx.getFile();

				// This may fail if there is no trace file.
				IEclipseTrace traceToSource = traceInformation.getTraceToSource(generatedJavaFile);
				if (traceToSource == null) {
					continue;
				}
				AbstractTraceRegion rootTraceRegion = findRootTraceRegion(traceToSource);
				if (rootTraceRegion == null)
					continue;

				SourceRelativeURI dslSourceFile = rootTraceRegion.getAssociatedSrcRelativePath();

				// OutputConfigurations are only available for folders targeted by Xtext's code generation.
				OutputConfiguration outputConfiguration = findOutputConfiguration(dslSourceFile, generatedJavaFile);
				if (outputConfiguration == null)
					continue;

				IJavaElement element = JavaCore.create(generatedJavaFile);
				if (element == null)
					continue;

				deleteTaskMarkers(generatedJavaFile);
				markerReflector.reflectErrorMarkerInSource(generatedJavaFile, traceToSource);

				ITraceToBytecodeInstaller installer = getInstaller(outputConfiguration);
				installer.setTrace(generatedJavaFile.getName(), rootTraceRegion);
				for (IFile javaClassFile : findGeneratedJavaClassFiles(element)) {
					InputStream contents = javaClassFile.getContents();
					try {
						byte[] byteCode = installer.installTrace(ByteStreams.toByteArray(contents));
						if (byteCode != null) {
							javaClassFile.setContents(new ByteArrayInputStream(byteCode), 0, null);
						} else {
							// we need to touch the class file to do a respin of the build
							// otherwise a needsRebuild request is ignored since no IResourceDelta is available
							javaClassFile.touch(null);
						}
					} finally {
						contents.close();
					}
				}
			} catch (Exception e) {
				String msg = "Could not process %s to install source information: %s";
				log.error(String.format(msg, ctx.getFile().getFullPath().toString(), e.getMessage()), e);
			}
		}
	} finally {
		files = null;
		task.stop();
	}
}
 
Example 7
public void openModelPassedAsArgument(final String modelPath) {
	// printAllGuaranteedProperties();

	String filePath = modelPath;
	String expName = null;
	if ( filePath.contains("#") ) {
		final String[] segments = filePath.split("#");
		if ( segments.length != 2 ) {
			DEBUG.OUT("Wrong definition of model and experiment in argument '" + filePath + "'");
			return;
		}
		filePath = segments[0];
		expName = segments[1];
	}
	if ( filePath.endsWith(".experiment") && expName == null ) {
		expName = "0";
		// Verify that it works even if the included model defines experiments itself...

	}
	final IFile file = findAndLoadIFile(filePath);
	if ( file != null ) {
		final String en = expName;
		// final Runnable run = () -> {
		try {
			// DEBUG.OUT(Thread.currentThread().getName() + ": Rebuilding the model " + fp);
			// Force the project to rebuild itself in order to load the various XText plugins.
			file.touch(null);
			file.getProject().build(IncrementalProjectBuilder.FULL_BUILD, null);
		} catch (final CoreException e1) {
			DEBUG.OUT(Thread.currentThread().getName() + ": File " + file.getFullPath() + " cannot be built");
			return;
		}
		while (GAMA.getRegularGui() == null) {
			try {
				Thread.sleep(100);
				System.out.println(Thread.currentThread().getName() + ": waiting for the GUI to become available");
			} catch (final InterruptedException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
		}
		if ( en == null ) {
			// System.out
			// .println(Thread.currentThread().getName() + ": Opening the model " + fp + " in the editor");
			GAMA.getGui().editModel(null, file);
		} else {
			// DEBUG.OUT(Thread.currentThread().getName() + ": Trying to run experiment " + en);
			GAMA.getGui().runModel(file, en);
		}

		// };
		// new Thread(run, "Automatic opening of " + filePath).start();

	}
}