Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 322645 - NPE in workflow during validation if location information is missing
Summary: NPE in workflow during validation if location information is missing
Status: CLOSED FIXED
Alias: None
Product: TMF
Classification: Modeling
Component: Xtext (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows XP
: P3 major (vote)
Target Milestone: M5   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-08-13 08:18 EDT by Daniel Weber CLA
Modified: 2017-09-19 17:38 EDT (History)
3 users (show)

See Also:
sebastian.zarnekow: indigo+


Attachments
proposed patch (1.69 KB, patch)
2010-08-15 16:49 EDT, Michael Clay CLA
no flags Details | Diff
revised patch (not tested) (2.70 KB, patch)
2010-08-23 14:21 EDT, Michael Clay CLA
no flags Details | Diff
patch + test (8.91 KB, patch)
2010-12-16 15:11 EST, Michael Clay CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Daniel Weber CLA 2010-08-13 08:18:45 EDT
Build Identifier: I20100608-0911

Validation runs into a NPE when there is no location information for the "erroneous object".

Reproducible: Always

Steps to Reproduce:
1. Create a new Xtext project using the default values, including a generator project
2. Add the following to the generated Validator
	@Check
	public void checkGreetingStartsWithCapital(Greeting greeting) {
	   Greeting objectWithoutLocation = MyDslFactory.eINSTANCE.createGreeting();
	   error("Name should start with a capital", objectWithoutLocation, MyDslPackage.GREETING__NAME);
	}
3. Run the grammar generator
4. Run the generator workflow from the generator project

==> 
Caused by: java.lang.NullPointerException
	at org.eclipse.xtext.mwe.Validator$1$1.compare(Validator.java:148)
	at org.eclipse.xtext.mwe.Validator$1$1.compare(Validator.java:1)
	at java.util.TreeMap.compare(TreeMap.java:1093)
	at java.util.TreeMap.put(TreeMap.java:465)
	at java.util.TreeSet.add(TreeSet.java:210)

This is because linenumber is null for this issue:
   if (issue1.getLineNumber() < issue2.getLineNumber())
And probably the same for the offset (a few lines below in the Validator class).

The org.eclipse.xtext.validation.DiagnosticConverterImpl seems to handle missing location information just fine:

   Triple<Integer, Integer, Integer> locationData = getLocationData(diagnostic);
   if (locationData != null) {
      issue.setLineNumber(locationData.getFirst());
      issue.setOffset(locationData.getSecond());
      issue.setLength(locationData.getThird() - issue.getOffset());
   }

So org.eclipse.xtext.mwe.Validator should probably handle this as well.

We actually ran into this because we added issues for imported non-Xtext resources (XMI), for which getLocationData does not work.
Comment 1 Michael Clay CLA 2010-08-15 16:49:51 EDT
Created attachment 176644 [details]
proposed patch
Comment 2 Sebastian Zarnekow CLA 2010-08-16 01:30:32 EDT
I refuse the patch as nullsafeCompare seems to complicated to me and Integer.MAX and Integer.MIN suggest something that's not in sync with the contract of comparable. I'ld rather like a simple implementation like:

int compare(Integer i, Integer j) {
  i = ensureNotNull(i);
  j = ensureNotNull(j);
  return i.compareTo(j);
}

Integer ensureNotNull(Integer i) {
  return i != null ? i : Integer.valueOf(-1);
}
Comment 3 Sebastian Zarnekow CLA 2010-08-23 05:21:35 EDT
Michael, will you provide an updated patch?
Comment 4 Michael Clay CLA 2010-08-23 08:01:22 EDT
yes, later today
(In reply to comment #3)
> Michael, will you provide an updated patch?
Comment 5 Michael Clay CLA 2010-08-23 14:21:24 EDT
Created attachment 177247 [details]
revised patch (not tested)
Comment 6 Sebastian Zarnekow CLA 2010-10-05 04:15:37 EDT
Scheduled to ensure a review of the attached patch.
Comment 7 Federico Tomassetti CLA 2010-11-19 07:02:22 EST
There is workaround to use until the parch is not inserted in the released version?
Comment 8 Sebastian Zarnekow CLA 2010-12-06 03:50:12 EST
Michael, are you still working on this one? The patch is still marked as "not tested"?
Comment 9 Sebastian Zarnekow CLA 2010-12-15 11:45:05 EST
Michael, any comment on comment #8?
Comment 10 Michael Clay CLA 2010-12-15 15:02:12 EST
(In reply to comment #9)
> Michael, any comment on comment #8?

Sebastian, i will come up with some tests for this patch in the next days if it's ok for you.
Comment 11 Sebastian Zarnekow CLA 2010-12-15 15:03:21 EST
That would be great.
Comment 12 Michael Clay CLA 2010-12-16 15:11:32 EST
Created attachment 185362 [details]
patch + test
Comment 13 Sebastian Zarnekow CLA 2010-12-21 09:16:24 EST
Hi Michael,

thanks for the update. The implementation looks good to me. 
Some minor remarks:
- getDiagnosticComparator should return a Comparator<MWEDiagnostic>
- MWEDiagnosticComparator should be public

Making MWEDiagnosticComparator public has the advantage that it can be tested independently thus poluting the IndexTestLanguage-module would not be necessary. Simply comparing inconsistent diagnostics would be sufficient to proof the fix to be valid.

However, from my point of view the patch is ok besides the aforementioned two minor issues.
Comment 14 Michael Clay CLA 2011-01-12 15:44:56 EST
pushed to master
Comment 15 Karsten Thoms CLA 2017-09-19 17:27:44 EDT
Closing all bugs that were set to RESOLVED before Neon.0
Comment 16 Karsten Thoms CLA 2017-09-19 17:38:53 EDT
Closing all bugs that were set to RESOLVED before Neon.0