String prefix = null;
// Identify the closest resource to the ASTNode,
// otherwise fall back to using the high-level workspace root.
if (root != null && root.getNodeType() == ASTNode.COMPILATION_UNIT) {
CompilationUnit cu = (CompilationUnit) root;
IJavaElement je = cu.getJavaElement();
resource = je.getResource();
// print type root name into message if no resource
prefix = resource == null ?
"[" + analysisName + " in " + je.getElementName() + "]" :
"[" + analysisName + "]";
}
if (resource == null)
// Fall back to the high-level Workspace
resource = ResourcesPlugin.getWorkspace().getRoot();
if (prefix == null)
prefix = "[" + analysisName + " in ???]";
int sevMarker;
if (severity == SEVERITY.ERROR)
sevMarker = IMarker.SEVERITY_ERROR;
else if (severity == SEVERITY.WARNING)
sevMarker = IMarker.SEVERITY_WARNING;
else
sevMarker = IMarker.SEVERITY_INFO;
// Create the marker
// TODO: create markers according to the type of the analysis
try {
IMarker marker = resource.createMarker(Crystal.MARKER_DEFAULT);
marker.setAttribute(IMarker.CHAR_START, node.getStartPosition());
marker.setAttribute(IMarker.CHAR_END, node.getStartPosition() + node.getLength());
marker.setAttribute(IMarker.MESSAGE, prefix + ": " + problemDescription);
marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_NORMAL);
marker.setAttribute(IMarker.SEVERITY, sevMarker);
marker.setAttribute(Crystal.MARKER_ATTR_ANALYSIS, analysisName);
CompilationUnit cu = (CompilationUnit) node.getRoot();
int line = cu.getLineNumber(node.getStartPosition());
if (line >= 0) // -1 and -2 indicate error conditions
marker.setAttribute(IMarker.LINE_NUMBER, line);
}
catch (CoreException ce) {
logger.log(Level.SEVERE, "CoreException when creating marker", ce);