eclipse-jdt / eclipse-jdt/eclipse.jdt.core

avoiding unnecessary second Cast in instanceof patterns

Open
#2,992 6 comments 0 reactions 1 assignee Claimed by @srikanth-sankaran View on GitHub
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 12h
Merged PRs (30d)
47

Description

refer @stephan-herrmann 's comment at:
https://github.com/eclipse-platform/eclipse.platform.releng.aggregator/issues/2358#issuecomment-2359108740
"
oking at AnnotationCodeMiningProvider we are talking about this code:

if (getAdapter(IAnnotationAccess.class) instanceof IAnnotationAccessExtension annotationAccessExtension) {
this.annotationAccess= annotationAccessExtension;
} else {
throw new IllegalStateException("annotationAccess must implement IAnnotationAccessExtension"); //$NON-NLS-1$
}
Here getAdapter(Class) is declared to return . With T=IAnnotationAccess a checkcast to the statically known type is correct. I only wonder, if previously implementation of instanceof patterns took care to avoid unnecessary casts, when the only use of that value would undergo a second checkcast immediately after. -.. was any such optimization intended at some point?
"

Also
ref the comment below as well:
public T getAdapter(Class adapter) {
if (adapter == X509Certificate.class && !certificates.isEmpty()) {
if (certificates.get(0) instanceof X509Certificate certificate) {
return adapter.cast(certificate);
}
}
The result of certificates.get(0) gets casted twice.

To check if such an optimization can be implemented

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.