[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[news.eclipse.tools.jdt] Re: Question on Marker use (APT vs. JavaModel)

Mike Yawn wrote:
Walter Harley wrote:
"Mike Yawn" <myawn@xxxxxxxx> wrote in message news:fbpcnd$4qj$1@xxxxxxxxxxxxxxxxxxxx
Question 1: Is there a way to retrieve the markers created by EclipseMessager.printFixableError starting from an ICompilationUnit?

Hmm. It must be possible to pull them off the resource in some way; after all, the JDT Problems view manages to do so. In my experience the challenge is to tell them apart from the other JDT compilation problem markers.


Take a look at AbstractImageBuilder.storeProblemsFor(), and IncrementalImageBuilder.updateProblemsFor(), in org.eclipse.jdt.internal.core.builder; and at APTProblem.getMarkerType(), in org.eclipse.jdt.apt.core.internal.env.

You're right; I was thinking they must be visible since the problems view shows them. I think I was trying to see them as I typed; they don't actually become visible to the Problems view (or in my action code until I save the file after making the edit that results in the error.

And then, as you indicate, getting at the content to distinguish the error is an issue.

I looked the at code that creates the markers (EclipseMessager implementation), and it appears that what I'm creating _ought_ to be an APTProbem -- which implements IProblem, so I should be able to cast it to an IProblem, and then use getArguments to retrieve the plugin id & error id. However, what I'm seeing from .findMarkers is just an org.eclipse.core.internal.resources.Marker, and if I try to cast it to an IProblem I get a ClassCastException. The Marker errorids seem to be random (they are sequential and unrelated to the errorId I passed in at creation time).

Any ideas of how I can retrieve these markers in a way that allows them to be cast to IProblems (or some other clever way to get at the content -- I'm not sure what lurks behind marker.getAttribute for those who have the attribute name decoder ring)

Mike

Well, I decided to dump out the contents of the 'getAttributes()' map to see if that was of any use, and it turns out to have the arguments I was trying to get from IProblem.


So just to document this for anyone else that might run into the same question, here's what works for me:

public IMarker[] findJavaProblemMarkers(ICompilationUnit cu) {
try {
IResource javaSourceFile = cu.getUnderlyingResource();
String aptProblemMarker = "org.eclipse.jdt.apt.core.compile.problem";
IMarker[] markers =
//IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER does't see APT error markers
javaSourceFile.findMarkers(aptProblemMarker,
true, IResource.DEPTH_INFINITE);
for (IMarker marker : markers) {
Map attrs = marker.getAttributes();
String arguments = (String) attrs.get("arguments");
String arg2 = arguments.split("#")[1];
if (arg2.equals(CodegenAnnotationProcessor.DAO_REGEN_NEEDED))
System.err.println("DAO regen required");
else if (arg2.equals(CodegenAnnotationProcessor.CGDOI_REGEN_NEEDED))
System.err.println("CGDOI regen required");
}
return markers;
} catch (JavaModelException e) {
e.printStackTrace();
} catch (CoreException e) {
e.printStackTrace();
}
return null;
}