IPackageFragment.copy doesn't always update inner compilationunits? [message #229271] |
Fri, 05 May 2006 05:55 |
Eclipse User |
|
|
|
Originally posted by: sankwak.instantstuff.net
Hi all,
I'm trying to create a custom project builder which copies source files
from a
source folder of an outside project to the builder's project, and makes
some changes to the copy. To do this, I'm using the copy method of
IPackageFragment and alter the AST of a compilation unit in one of the
copied packagefragments. The source code for these operations is supplied
below.
The first time this code is executed (after a build has been triggered by
the IDE), the source code is copied and altered as it should be: in the
changed source file, the variable TESTVAR is set to 1001, whereas the
original source file had the value 100. So no problems there.
But when the build is triggered a second time, the resulting source code
is reverted back to its original. The resulting source code's TESTVAR is
set back to 100, instead of it's intended value 1001.
When running the code shown below, the debugoutput marked by '** ORIG
SOURCE **' displays the source code copied in newSrc, right after
the copy process has been completed. This source should therefore always
be the same as the original source code that is copied. But when the build
is executed a second time, the output shows the contents of the previously
altered source code (with the TESTVAR value set to 1001). And after the
source altering has been done, the result is somehow reverted back to the
real original source code value (100).
If the builder is executed a third time, the same scenario as the first
execution occurs. So this problem occurs every two builds.
At first glance it seems that the compilation unit who's source is
altered, doesn't update its new state properly after the packagecopy in
the first build. But if this is the case, how can it be updated properly
after the second build? As you can see in the code below, the working copy
of the compilationunit's buffer is committed after each alteration, so
this shouldn't be a problem. Or am I forgetting something?
Any suggestions?
Regards,
Sannie Kwakman
protected IProject[] build(int kind, Map args, IProgressMonitor monitor)
throws CoreException
{
IProject project = getProject();
JavaProject javaProject = (JavaProject) JavaCore.create(project);
// find newsrc folder (source folder in which the copy will be placed)
IPackageFragmentRoot newSrcFolder =
getPackageRootFromProject(javaProject, NEW_SRC_FOLDER);
// find the reference project (the project which holds the original
source)
IProject[] refProjects = project.getReferencedProjects();
IProject refProject = refProjects[0];
JavaProject refJavaProject = (JavaProject)
JavaCore.create(refProject);
// find refsrc folder
IPackageFragmentRoot refSrcFolder =
getPackageRootFromProject(refJavaProject, REF_SRC_FOLDER);
// start copying every found packagefragment from refsrcfolder to
newsrcfolder
IJavaElement[] elements = refSrcFolder.getChildren();
for (int i = 0; i < elements.length; i++)
{
System.out.println(elements[i].getElementName());
IPackageFragment currPack =
refSrcFolder.getPackageFragment(elements[i].getElementName() );
currPack.copy(newSrcFolder, null, null, true, monitor);
}
// now change the source of one of the source-copy's compilationunits
try
{
IPackageFragment targetPackage =
newSrcFolder.getPackageFragment(MAIN_PACKAGE);
ICompilationUnit compUnit =
targetPackage.getCompilationUnit("Test.java");
compUnit.becomeWorkingCopy(null, monitor);
String source = compUnit.getBuffer().getContents();
Document document= new Document(source);
System.out.println("******** ORIG SOURCE **********");
System.out.println(source);
System.out.println("*******************************");
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(compUnit);
CompilationUnit astRoot = (CompilationUnit) parser.createAST(null);
ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST());
List astRootTypes = astRoot.types();
TypeDeclaration currType = (TypeDeclaration) astRootTypes.get(0);
FieldDeclaration[] fields = currType.getFields();
for (int i = 0; i < fields.length; i++)
{
FieldDeclaration currField = fields[i];
List vars = currField.fragments();
for (int v = 0; v < vars.size(); v++)
{
VariableDeclarationFragment currVar =
(VariableDeclarationFragment)vars.get(v);
String currVarName = currVar.getName().getFullyQualifiedName();
System.out.println(currVar.getName()+" = "+currVar.getInitializer());
if (currVarName.equals("TESTVAR"))
{
Expression oldExpr = currVar.getInitializer();
Expression newExpr = currVar.getAST().newNumberLiteral("1001");
rewrite.replace(oldExpr, newExpr, null);
}
}
}
// apply changes
TextEdit edits = rewrite.rewriteAST(document,
javaProject.getOptions(true));
edits.apply(document);
String newSource = document.get();
compUnit.getBuffer().setContents(newSource);
compUnit.commitWorkingCopy(true, monitor);
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
|
|
|
Powered by
FUDForum. Page generated in 0.02751 seconds