Java "this"-keyword marked as static in eclipse content assist

Viewed 467

Does anybody know the reason why the this keyword (Java) is marked as static final in the content assist in Eclipse? final makes sense to me, but why static?

Screenshot Eclipse

The screenshot was made with Eclipse 2020-03, but I'm observing this behavior since many years.

2 Answers

The code doing this is in org.eclipse.jdt.internal.ui.text.java.ParameterGuesser

// add 'this'
if (currentType != null && !(fEnclosingElement instanceof IMethod && Flags.isStatic(((IMethod) fEnclosingElement).getFlags()))) {
  String fullyQualifiedName= currentType.getFullyQualifiedName('.');
  if (fullyQualifiedName.equals(expectedType)) {
    ImageDescriptor desc= new JavaElementImageDescriptor(JavaPluginImages.DESC_FIELD_PUBLIC, JavaElementImageDescriptor.FINAL | JavaElementImageDescriptor.STATIC, JavaElementImageProvider.SMALL_SIZE);
    res.add(new Variable(fullyQualifiedName, "this", Variable.LITERALS, false, res.size(), new char[] {'.'}, desc));  //$NON-NLS-1$
  }
}

The key thing in that code is

JavaElementImageDescriptor.FINAL | JavaElementImageDescriptor.STATIC

as the flags to JavaElementImageDescriptor which is hard coding the display of the static and final overlay images. So these are always displayed for this.

As to why that was chosen the code doesn't give any reason.

Two reasons. 1) this can not be reassigned and 2) you're inside a constructor and not a normal method.

Related