Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build-oudated-file feature is broken #1685

Closed
Egor18 opened this issue Nov 1, 2017 · 2 comments
Closed

build-oudated-file feature is broken #1685

Egor18 opened this issue Nov 1, 2017 · 2 comments
Labels

Comments

@Egor18
Copy link
Contributor

Egor18 commented Nov 1, 2017

Hi. I was trying to speed up model building process by building only outdated files, and probably I found some bug.
Here is the code, that I use to build the model:

public static CtModel buildModel(String resourcePath)
{
	Launcher launcher = new Launcher();
	launcher.addInputResource(resourcePath);
	launcher.getEnvironment().setNoClasspath(true);
	launcher.getEnvironment().setCommentEnabled(true);
	launcher.createCompiler();
	launcher.setSourceOutputDirectory("D:/outpath");
	launcher.getModelBuilder().setBuildOnlyOutdatedFiles(true);
	launcher.buildModel();
	launcher.getModelBuilder().generateProcessedSourceFiles(OutputType.COMPILATION_UNITS);
	return launcher.getModel();
}

So, as far as I understand, in order to build only outdated files we need to save spoon-generated source files, and then spoon will compare their modification time with actual source files.
When I run this code at the first time, it works fine (when it builds model from scratch).
But then, if I run this code again, I will get:

Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.Collections$UnmodifiableCollection.remove(Collections.java:1058)
	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.keepOutdatedFiles(JDTBasedSpoonCompiler.java:546)
	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildUnits(JDTBasedSpoonCompiler.java:412)
	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildUnitsAndModel(JDTBasedSpoonCompiler.java:377)
	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildSources(JDTBasedSpoonCompiler.java:353)
	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.build(JDTBasedSpoonCompiler.java:124)
	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.build(JDTBasedSpoonCompiler.java:107)
	at spoon.Launcher.buildModel(Launcher.java:705)
	at com.pvsstudio.pvsjava.analyzer.Main.buildModel(Main.java:80)
	at com.pvsstudio.pvsjava.analyzer.Main.main(Main.java:138)

I looked through the spoon code, and found this:

protected CompilationUnitDeclaration[] buildUnits(JDTBuilder jdtBuilder, SpoonFolder sourcesFolder, String[] classpath, String debugMessagePrefix, boolean buildOnlyOutdatedFiles) {
	List<SpoonFile> sourceFiles = Collections.unmodifiableList(sourcesFolder.getAllJavaFiles());
	....
	if (buildOnlyOutdatedFiles && outputDirectory.exists()) {
		@SuppressWarnings("unchecked") Collection<File> outputFiles = FileUtils.listFiles(outputDirectory, new String[] { "java" }, true);
		keepOutdatedFiles(sourceFiles, outputFiles);
	}
	....
}
protected void keepOutdatedFiles(List<SpoonFile> files, Collection<File> outputFiles) {
	....
	File f = sf.toFile();
	for (String s : relativeOutputPaths) {
		if (f.getAbsolutePath().endsWith(s)) {
			if (f.lastModified() <= new File(outputDirectory, s).lastModified()) {
				files.remove(sf); // <== But files is unmodifiable list!
			}
		}
	}
}

So, as you can see, exception is thrown because of .remove(sf) call on unmodifiable list.
I'm pretty sure, that sourceFiles should not be an unmodifiableList.
I really want some fix, because without it I can not use incremental model build.

In addition, I've got one more question about model building. Is it possible to use multiple CPU-cores to speed up the build?
Thank you.

@monperrus
Copy link
Collaborator

I'm pretty sure, that sourceFiles should not be an unmodifiableList.

you're right. would you propose a pull-request to fix this bug?

@surli surli added the bug label Nov 6, 2017
@monperrus monperrus changed the title Possible bug in buildUnits build-oudated-file feature is broken Dec 4, 2017
@monperrus
Copy link
Collaborator

closed per #1913

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants