Forcing a file to be rebuilt on next Make
Hi,
I use a JavaSourceTranslatingCompiler to instrument Java sources just before they are compiled.
What is actually done depends on plugin per-file settings, so when they are changed I want to mark the file for rebuild on next Make (currently I just request a full project rebuild which is sub-optimal)
The approach I tried to use was to add a dummy SourceInstrumentingCompiler whose only purpose is to provide settings-based ValidityState.
Dummy means the process() method just returns the items[] array it is invoked with (is this what I should do to say all files have been processed?)
This almost works - my dummy compiler gets invoked when the settings for given file change, but the file does not get recompiled by actual JavaCompiler unless I touch the file itself (setTimeStamp() on NewVirtualFile instance I get in the ProcessingItem).
To help that I just delete all the corresponding .class files in module's output directory.
Two questions:
1. Can I do it any smarter? Like, somehow mark the source file as requiring rebuild? Inject some additional dependency?
2. Is there any easier way to delete the output .class than this:
for (ProcessingItem item : items) {
VirtualFile vf = item.getFile();
final PsiFile file = PsiManager.getInstance(context.getProject()).findFile(vf);
if (file instanceof PsiJavaFile) {
PsiJavaFile pjf = (PsiJavaFile) file;
Module m = context.getModuleByFile(vf);
final VirtualFile outputDirectory = context.getModuleOutputDirectory(m);
final VirtualFile outputTestDirectory = context.getModuleOutputDirectoryForTests(m);
for (PsiClass psiClass : pjf.getClasses()) {
final String name = psiClass.getQualifiedName();
if (name == null) {
continue;
}
final String path = name.replace('.', '/') + ".class";
VirtualFile classFile = outputDirectory != null ? outputDirectory.findFileByRelativePath(path) : null;
if (classFile == null && outputTestDirectory != null) {
classFile = outputTestDirectory.findFileByRelativePath(path);
}
if (classFile != null) {
try {
classFile.delete(this);
} catch (IOException e) {
}
}
}
}
}
Please sign in to leave a comment.