Bug 40880 - Wrong error range for 'indirect static access'
Summary: Wrong error range for 'indirect static access'
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.0   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.0 M3   Edit
Assignee: Kent Johnson CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-07-29 02:09 EDT by Gary Gregory CLA
Modified: 2003-08-30 04:12 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Gary Gregory CLA 2003-07-29 02:09:01 EDT
 
Comment 1 Gary Gregory CLA 2003-07-29 02:14:34 EDT
I have the following line of code:

ToolUserSettingsConstants.FILE_TYPE.extension.toLowerCase(),

in the expression:

		List extensionsToCopy = Arrays.asList( new String[] {
			FileType.extension.toLowerCase(),
			//OBSOLETE: "properties",
			StandardFileTypes.FILE_TYPE_TEXT.extension.toLowerCase(),
			StandardFileTypes.FILE_TYPE_HTML.extension.toLowerCase(),
			StandardFileTypes.FILE_TYPE_GIF.extension.toLowerCase(),
			StandardFileTypes.FILE_TYPE_JPG.extension.toLowerCase()
			} );

With the warning:

Severity	Description	Resource	In Folder	Location	Creation Time
	The static field ToolConfigurationSettingsConstants.FILE_TYPE should be
accessed directly	ToolDeveloperSettings.java
Transidiom-DevE/Java/Source/com/seagullsw/appinterface/tools/framework	line 90
July 28, 2003 11:06:35 PM

If I apply the quick fix I get:

FileType.extension.toLowerCase(),

Which yields the error:

Severity	Description	Resource	In Folder	Location	Creation Time
	Cannot make a static reference to the non-static field FileType.extension
ToolDeveloperSettings.java
Transidiom-DevE/Java/Source/com/seagullsw/appinterface/tools/framework	line 91
July 28, 2003 11:12:11 PM

ToolUserSettingsConstants is defined as:

public interface ToolUserSettingsConstants extends
ToolConfigurationSettingsConstants ...

And:

public interface ToolConfigurationSettingsConstants {

	FileType FILE_TYPE = AppInterfaceSettingsConstants.FILE_TYPE;

} //ToolConfigurationSettingsConstants

And:

import com.seagullsw.javax.io.FileType;
import com.seagullsw.toolbox.settings.SettingsConstants;

public interface AppInterfaceSettingsConstants extends SettingsConstants {

	FileType FILE_TYPE = FileType.defineConstant( "settings", "cfg", "settings file" );

} //AppInterfaceSettingsConstants

FileType is our own doo-dad.

The expected Quick Fix result should be:

 ToolConfigurationSettingsConstants.FILE_TYPE.extension.toLowerCase(),
Comment 2 Gary Gregory CLA 2003-07-29 02:14:54 EDT
Eclipse Platform

Version: 3.0.0
Build id: 200307230800
Comment 3 Olivier Thomann CLA 2003-07-29 09:13:59 EDT
Quick fix is a JDT/UI feature.
Move to JDT/UI.
Comment 4 Martin Aeschlimann CLA 2003-08-06 08:28:07 EDT
I think this is a bug on how the compiler marks this error. 
The expression is
   ToolUserSettingsConstants.FILE_TYPE.extension.toLowerCase()
and the error range is 'ToolUserSettingsConstants.FILE_TYPE.extension' with the
description:
'The static field ToolConfigurationSettingsConstants.FILE_TYPE should be
accessed directly'
The quick fix assumes that the error range corresponds to the problematic access.
(-> 'ToolUserSettingsConstants.FILE_TYPE')

This is the code I used to reproduce:
----
public class E {
	public void foo() {
		ToolUserSettingsConstants.FILE_TYPE.extension.toLowerCase();
	}
}

class FileType {
	public String extension;
}
	
interface ToolConfigurationSettingsConstants {
	FileType FILE_TYPE = null;
}

interface ToolUserSettingsConstants extends ToolConfigurationSettingsConstants {
}
----

Moving to JCore
Added Test case
   LocalCorrectionsQuickFixTest.testIndirectStaticAccess_bug40880
Comment 5 Olivier Thomann CLA 2003-08-06 09:41:17 EDT
The only way to fix this is to provide source positions for each token of a 
qualified name reference. Right now we only have the start and the end 
including the whole qualified name reference.
In this case we should report the error only on the first two tokens and not 
the whole qualified name reference. Fixing the positions fixes the problem.
Comment 6 Olivier Thomann CLA 2003-08-06 10:41:34 EDT
Add CC'.
Philippe, do you think it is ok to add positions as part of a qualified name
reference? Subclasses of the QualifiedNameReference are already defining such
field. I would simply move it up to the QualifiedNameReference. Initially I
wanted to get the positions only when the problem is reported, but at this point
we don't have the source anymore.
This is fixing the problem.
Proposal:
public void indirectAccessToStaticField(QualifiedNameReference
qualifiedNameReference, FieldBinding field){
	int sourceStart = qualifiedNameReference.sourceStart;
	int sourceEnd = qualifiedNameReference.sourceEnd;
	final long[] positions = qualifiedNameReference.positions;	
	final int indexOfFirstFieldBinding =
qualifiedNameReference.indexOfFirstFieldBinding;
	if (indexOfFirstFieldBinding >= 1) {
		sourceEnd = (int) positions[indexOfFirstFieldBinding - 1];
	}
	this.handle(
		IProblem.IndirectAccessToStaticField,
		new String[] {new String(field.declaringClass.readableName()), new
String(field.name)},
		new String[] {new String(field.declaringClass.shortReadableName()), new
String(field.name)},
		sourceStart,
		sourceEnd);
}
Comment 7 Olivier Thomann CLA 2003-08-13 17:16:24 EDT
Fixed and released in HEAD.
Regression test added in jdt.ui tests.
Comment 8 Philipe Mulet CLA 2003-08-27 04:46:52 EDT
I am not a big fan of recording intermediate positions for error reporting only.
I think quite numerous error messages would need to be revisited using these 
extra positions.

Kent, can you pls investigate ?
Comment 9 Philipe Mulet CLA 2003-08-27 04:48:08 EDT
Also think match locator should take advantage of these extra positions.
Comment 10 Olivier Thomann CLA 2003-08-27 08:26:19 EDT
In this case, most of the subclasses got the positions. So I simply moved up 
the positions. Having them was the only way to fix this PR, because the source 
was not available inside the problem reporter.
Comment 11 Philipe Mulet CLA 2003-08-27 09:28:59 EDT
Historically, we did not want to record these positions for qualified names, 
for memory usage... subclasses are not that critically allocated.

After all, I think I like this move, and we simply need to take it into account 
everywhere, not just for this error message.
Comment 12 Philipe Mulet CLA 2003-08-30 04:12:21 EDT
Closing as fixed, opened bug 42287 for generalization.
Comment 13 Philipe Mulet CLA 2003-08-30 04:12:39 EDT
Verified