Community
Participate
Working Groups
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(),
Eclipse Platform Version: 3.0.0 Build id: 200307230800
Quick fix is a JDT/UI feature. Move to JDT/UI.
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
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.
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); }
Fixed and released in HEAD. Regression test added in jdt.ui tests.
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 ?
Also think match locator should take advantage of these extra positions.
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.
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.
Closing as fixed, opened bug 42287 for generalization.
Verified