Bug 361263 - JDT Core overwrites Status.matches(...) and isOK() in a breaking way
Summary: JDT Core overwrites Status.matches(...) and isOK() in a breaking way
Status: CLOSED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.6.2   Edit
Hardware: All All
: P3 major (vote)
Target Milestone: ---   Edit
Assignee: Jay Arthanareeswaran CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2011-10-18 10:34 EDT by Dani Megert CLA
Modified: 2018-10-20 15:31 EDT (History)
4 users (show)

See Also:


Attachments
Test for IJavaModelStatus.matches() (2.19 KB, patch)
2011-11-10 04:46 EST, Jay Arthanareeswaran CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Dani Megert CLA 2011-10-18 10:34:57 EDT
JDT Core overwrites Status.matches(int) in a breaking way.

The JavaModelStatus.matches(int) ignores the severity that has been set on the status.
Comment 1 Dani Megert CLA 2011-10-18 10:45:24 EDT
It also violates org.eclipse.core.runtime.IStatus.isOK().
Comment 2 Srikanth Sankaran CLA 2011-11-07 00:39:49 EST
Dani, please spell out/clarify what exactly you are talking about so
we are on the same page - TIA.
Comment 3 Dani Megert CLA 2011-11-07 02:25:11 EST
(In reply to comment #2)
> Dani, please spell out/clarify what exactly you are talking about so
> we are on the same page - TIA.

Srikanth, are you asking to clarify what/why it is broken, or rather how it should be fixed?
Comment 4 Srikanth Sankaran CLA 2011-11-07 03:09:29 EST
(In reply to comment #3)
> (In reply to comment #2)
> > Dani, please spell out/clarify what exactly you are talking about so
> > we are on the same page - TIA.
> 
> Srikanth, are you asking to clarify what/why it is broken, or rather how it
> should be fixed?

The former part Dani.
Comment 5 Dani Megert CLA 2011-11-09 05:49:46 EST
(In reply to comment #2)
> Dani, please spell out/clarify what exactly you are talking about so
> we are on the same page - TIA.

The API specification of IStatus.matches(int) is:

	 * Returns whether the severity of this status matches the given
	 * severity mask. Note that a status with severity <code>OK</code>
	 * will never match; use <code>isOK</code> instead to detect
	 * a status with a severity of <code>OK</code>.
	 *
	 * @param severityMask a mask formed by bitwise or'ing severity mask
	 *    constants (<code>ERROR</code>, <code>WARNING</code>,
	 *    <code>INFO</code>, <code>CANCEL</code>)
	 * @return <code>true</code> if there is at least one match, 
	 *    <code>false</code> if there are no matches

but JavaModelStatus.matches(int) completely ignores the severity. It does some bit juggling on the code instead.


This is what the API of IStatus.isOK() defines:

	 * Returns whether this status indicates everything is okay
	 * (neither info, warning, nor error).
	 *
	 * @return <code>true</code> if this status has severity
	 *    <code>OK</code>, and <code>false</code> otherwise

and JavaModelStatus.isOK() does not respect that: it looks at the code instead of at the severity.
Comment 6 Srikanth Sankaran CLA 2011-11-10 00:55:47 EST
(In reply to comment #5)
> (In reply to comment #2)

> This is what the API of IStatus.isOK() defines:
> 
>      * Returns whether this status indicates everything is okay
>      * (neither info, warning, nor error).
>      *
>      * @return <code>true</code> if this status has severity
>      *    <code>OK</code>, and <code>false</code> otherwise
> 
> and JavaModelStatus.isOK() does not respect that: it looks at the code instead
> of at the severity.

Looking at how the singleton object org.eclipse.jdt.internal.core.JavaModelStatus.VERIFIED_OK is defined,
it appears that org.eclipse.core.runtime.IStatus.OK is overloaded and
reused (integers codes making it easier to do this as opposed to type safe
enumerations) to indicate a status of all being alright. I think it is worth
investigating, but I suspect being the only the producer of java model status
objects, we are internally consistent despite this type looseness.

matches: I didn't check this one, but perhaps the case is similar. We seem
to assume a code of OK implies a severity of OK and I don't know if there
are counter examples that exist currently.
Comment 7 Srikanth Sankaran CLA 2011-11-10 01:09:12 EST
(In reply to comment #5)

[...]

> but JavaModelStatus.matches(int) completely ignores the severity. It does some
> bit juggling on the code instead.

[...]

> and JavaModelStatus.isOK() does not respect that: it looks at the code instead
> of at the severity.

Jay/Dani, Can we construct a case where we can actually see any broken
behavior purely from an API standpoint: i.e without having looked at the
implementation, could we have concluded something is amiss here purely by
observing behavior ?
Comment 8 Dani Megert CLA 2011-11-10 01:41:50 EST
> Jay/Dani, Can we construct a case where we can actually see any broken
> behavior purely from an API standpoint: i.e without having looked at the
> implementation, could we have concluded something is amiss here purely by
> observing behavior ?

We ran into this with the fix for bug 287164 where JDT Core currently patches the API by creating a subclass of JavaModelStatus.

Also, every JDT Core developer who constructs a JavaModelStatus with JavaModelStatus.JavaModelStatus(int, int, IJavaElement, IPath, String) will run into this as soon as he uses a severity that doesn't match with the code, e.g. if he uses a (error) code to signal a warning.

In addition I could not find any documentation that would indicate that the code is somehow linked to the severity or that could explain the implementation in #getBits() and #matches(JavaModelStatus, int).
Comment 9 Jay Arthanareeswaran CLA 2011-11-10 02:27:25 EST
(In reply to comment #8)
> In addition I could not find any documentation that would indicate that the
> code is somehow linked to the severity or that could explain the implementation
> in #getBits() and #matches(JavaModelStatus, int).

True, I had trouble understanding the code and the relevance too. I will try to come up with a failing test.
Comment 10 Jay Arthanareeswaran CLA 2011-11-10 04:46:11 EST
Created attachment 206771 [details]
Test for IJavaModelStatus.matches()

The attached test case checks a particular status against different severity.
The following is the status that is being tested and results of matches() calls against IStatus.OK, ERROR, WARNING and INFO.

Java Model Status [Unbound classpath variable: 'JCL_LIB' in project 'P1']
isError:false
isWarning:true
isInfo :false
isOK:true

The status is an ERROR and only isError should return true here. I don't yet know what's happening.
Comment 11 Jay Arthanareeswaran CLA 2011-11-11 04:36:21 EST
The following change to the matches() makes the previous test produce the expected results. And all the existing model tests pass.

	protected boolean matches(JavaModelStatus status, int mask) {
		return (status.getSeverity() == mask);
		//int severityMask = mask & 0x7;
		//int categoryMask = mask & ~0x7;
		//int bits = status.getBits();
		//return ((severityMask == 0) || (bits & severityMask) != 0) && ((categoryMask == 0) || (bits & categoryMask) != 0);
	}

I wonder what all the bitwise operations in matches() and getBits() are for.
Comment 12 Dani Megert CLA 2011-11-14 03:08:19 EST
(In reply to comment #11)
> The following change to the matches() makes the previous test produce the
> expected results. And all the existing model tests pass.

Then you need more test cases ;-).

The following is a good one:

s= new JavaModelStatus(IStatus.OK, some code, "shows bug");
assert: s.isOK().
Comment 13 Srikanth Sankaran CLA 2011-11-14 04:21:28 EST
(In reply to comment #12)
> (In reply to comment #11)
> > The following change to the matches() makes the previous test produce the
> > expected results. And all the existing model tests pass.
> 
> Then you need more test cases ;-).
> 
> The following is a good one:
> 
> s= new JavaModelStatus(IStatus.OK, some code, "shows bug");
> assert: s.isOK().

Right. But being the sole producer of these status objects, I don't think
we ever create such objects i.e we are internally consistent.
Comment 14 Dani Megert CLA 2011-11-14 04:34:53 EST
(In reply to comment #13)
> (In reply to comment #12)
> > (In reply to comment #11)
> > > The following change to the matches() makes the previous test produce the
> > > expected results. And all the existing model tests pass.
> > 
> > Then you need more test cases ;-).
> > 
> > The following is a good one:
> > 
> > s= new JavaModelStatus(IStatus.OK, some code, "shows bug");
> > assert: s.isOK().
> 
> Right. But being the sole producer of these status objects, I don't think
> we ever create such objects i.e we are internally consistent.

That's good. But given the lack of documentation a developer can easily produce such code and then we're no longer good. The least thing we need to do for this bug is to improve the doc and also explain the juggling with the code value.
Comment 15 Srikanth Sankaran CLA 2011-11-30 00:17:30 EST
Since Jay had to go on unavoidable unplanned time off for several days,
this is not ready: retargetting to 3.8 M5.
Comment 16 Srikanth Sankaran CLA 2012-01-11 23:46:33 EST
This is not a recent regression and is a long standing issue.
As Jay is busy with Orion for now, moving this out of M5.
Will include in plan a later time as cycles become available.
Comment 17 Dani Megert CLA 2012-01-12 02:01:16 EST
(In reply to comment #16)
> This is not a recent regression and is a long standing issue.
> As Jay is busy with Orion for now, moving this out of M5.
> Will include in plan a later time as cycles become available.

That's fine with me.
Comment 18 Eclipse Genie CLA 2018-10-20 15:31:44 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.