|
|
|
|
Adopting 3.3 mechanisms and APIs
This section describes changes that are required if you are trying to change
your 3.2 plug-in to adopt the 3.3 mechanisms and APIs.
-
How can I remove deprecation warning I get on
*WorkingCopy(...IProblemRequestor...) methods and keep same behavior?
While implementing bug
175243, following methods
have been deprecated:
-
ICompilationUnit#becomeWorkingCopy(IProblemRequestor, IProgressMonitor)
-
ICompilationUnit#getWorkingCopy(WorkingCopyOwner, IProblemRequestor, IProgressMonitor)
-
IClassFile#becomeWorkingCopy(IProblemRequestor, WorkingCopyOwner, IProgressMonitor)
-
WorkingCopyOwner#newWorkingCopy(String, IClasspathEntry[], IProblemRequestor, IProgressMonitor)
This answer gives some advice to use new API corresponding methods without changing
the behavior of the existing code.
How to re-write the code depends on the fact that given problem requestor was null or not
(i.e. if the client code was interested in reporting problems of the working copy):
-
Problem requestor was null
There's no specific migration issue when the given requestor was null.
So, following snippet warned for calls to deprecated methods...:
IFile file = getWorkspaceRoot().getFile(new Path("FAQ/pack/X.java"));
ICompilationUnit compilationUnit = (ICompilationUnit)JavaCore.create(file);
IFile file = getWorkspaceRoot().getFile(new Path("FAQ/bin/pack/Y.class"));
IClassFile classFile = JavaCore.createClassFileFrom(file);
WorkingCopyOwner owner = new WorkingCopyOwner() {};
IClasspathEntry[] entries = classFile.getJavaProject().getResolvedClasspath(true);
ICompilationUnit workingCopy = compilationUnit.getWorkingCopy(new WorkingCopyOwner() {}, null, null);
ICompilationUnit newWorkingCopy = compilationUnit.becomeWorkingCopy(null, null);
ICompilationUnit newWorkingCopy2 = owner.newWorkingCopy(new Path("FAQ/test/Z.java"), entries, null, null);
ICompilationUnit classWorkingCopy = classFile.becomeWorkingCopy(null, owner, null);
... can now be easily rewritten as follow due to the fact that default owner's problem requestor is null:
IFile file = getWorkspaceRoot().getFile(new Path("FAQ/pack/X.java"));
ICompilationUnit compilationUnit = (ICompilationUnit)JavaCore.create(file);
IFile file = getWorkspaceRoot().getFile(new Path("FAQ/bin/pack/Y.class"));
IClassFile classFile = JavaCore.createClassFileFrom(file);
WorkingCopyOwner owner = new WorkingCopyOwner() {};
IClasspathEntry[] entries = classFile.getJavaProject().getResolvedClasspath(true);
// just remove the IProblemRequestor parameter
ICompilationUnit workingCopy = compilationUnit.getWorkingCopy(new WorkingCopyOwner() {}, null);
ICompilationUnit newWorkingCopy = compilationUnit.becomeWorkingCopy(null);
ICompilationUnit newWorkingCopy2 = owner.newWorkingCopy(new Path("FAQ/test/Z.java"), entries, null);
ICompilationUnit classWorkingCopy = classFile.becomeWorkingCopy(null, owner, null);
-
Problem requestor was not null
In this case, client must ensure that the working copy owner requestor is the same than
the one given as parameter to the deprecated method. The simplest way to to this is to make
the working copy owner returning this requestor.
So, following snippet warned for calls to deprecated methods...:
IFile file = getWorkspaceRoot().getFile(new Path("FAQ/pack/X.java"));
ICompilationUnit compilationUnit = (ICompilationUnit)JavaCore.create(file);
IFile file = getWorkspaceRoot().getFile(new Path("FAQ/bin/pack/Y.class"));
IClassFile classFile = JavaCore.createClassFileFrom(file);
WorkingCopyOwner owner = new WorkingCopyOwner() {};
IClasspathEntry[] entries = classFile.getJavaProject().getResolvedClasspath(true);
IProblemRequestor requestor = new IProblemRequestor {
public void acceptProblem(IProblem problem) {}
public void beginReporting() {}
public void endReporting() {}
public boolean isActive() {
return true;
}
};
ICompilationUnit workingCopy = compilationUnit.getWorkingCopy(new WorkingCopyOwner() {}, requestor, null);
ICompilationUnit newWorkingCopy = compilationUnit.becomeWorkingCopy(requestor, null);
ICompilationUnit newWorkingCopy2 = owner.newWorkingCopy(new Path("FAQ/test/Z.java"), entries, requestor, null);
ICompilationUnit classWorkingCopy = classFile.becomeWorkingCopy(requestor, owner, null);
... needs to be rewritten as follow to keep the same behavior:
IFile file = getWorkspaceRoot().getFile(new Path("FAQ/pack/X.java"));
ICompilationUnit compilationUnit = (ICompilationUnit)JavaCore.create(file);
IFile file = getWorkspaceRoot().getFile(new Path("FAQ/bin/pack/Y.class"));
IClassFile classFile = JavaCore.createClassFileFrom(file);
IClasspathEntry[] entries = classFile.getJavaProject().getResolvedClasspath(true);
// Make the requestor final...
final IProblemRequestor requestor = new IProblemRequestor {
public void acceptProblem(IProblem problem) {}
public void beginReporting() {}
public void endReporting() {}
public boolean isActive() {
return true;
}
};
// ...and let the created working copy owner returning it
WorkingCopyOwner owner = new WorkingCopyOwner() {
public IProblemRequestor getProblemRequestor(ICompilationUnit unit) {
return requestor;
}
};
// Calls can now be done to the new API methods using this working copy owner
ICompilationUnit workingCopy = compilationUnit.getWorkingCopy(owner, null);
ICompilationUnit newWorkingCopy = compilationUnit.becomeWorkingCopy(null);
ICompilationUnit newWorkingCopy2 = owner.newWorkingCopy(new Path("FAQ/test/Z.java"), entries, null);
ICompilationUnit classWorkingCopy = classFile.becomeWorkingCopy(owner, null);
|
|
|