Skip to content

Introduce default method checked() for throwable functional interface #33825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

quaff
Copy link
Contributor

@quaff quaff commented Oct 31, 2024

Allow throwing original checked exception instead of wrapping unchecked exception.

Use case:
https://github.com/spring-projects/spring-boot/blob/23fe3977d2d319f9f1bd0101f40dbb639ade7082/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/type/classreading/ConcurrentReferenceCachingMetadataReaderFactory.java#L71-L89

	@Override
	public MetadataReader getMetadataReader(String className) throws IOException {
		MetadataReader metadataReader = this.classNameCache.get(className);
		if (metadataReader == null) {
			metadataReader = super.getMetadataReader(className);
			this.classNameCache.put(className, metadataReader);
		}
		return metadataReader;
	}

	@Override
	public MetadataReader getMetadataReader(Resource resource) throws IOException {
		MetadataReader metadataReader = this.resourceCache.get(resource);
		if (metadataReader == null) {
			metadataReader = createMetadataReader(resource);
			this.resourceCache.put(resource, metadataReader);
		}
		return metadataReader;
	}

can be rewritten to:

	@Override
	public MetadataReader getMetadataReader(String className) throws IOException {
		return this.classNameCache.computeIfAbsent(className,ThrowingFunction.of((ThrowingFunction<String, MetadataReader>)super::getMetadataReader).checked());
	}

	@Override
	public MetadataReader getMetadataReader(Resource resource) throws IOException {
		return this.resourceCache.computeIfAbsent(resource,ThrowingFunction.of(this::createMetadataReader).checked());
	}

WDYT @philwebb

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 31, 2024
@quaff quaff marked this pull request as draft October 31, 2024 10:32
@philwebb
Copy link
Member

philwebb commented Oct 31, 2024

Personally, I'm not so keen on the (somewhat) hack that's being used to throw the checked exception from a signature that doesn't expose one.

@SuppressWarnings ("unchecked")
static <E extends Throwable> void throwAsUnchecked(Exception exception) throws E {
	throw (E) exception;
}

I think it somewhat goes against the spirit of the design.

Allow throwing original checked exception instead of wrapping unchecked exception
@snicoll
Copy link
Member

snicoll commented Nov 2, 2024

Yeah I agree.

@quaff thanks for the suggestion, in any case.

@snicoll snicoll closed this Nov 2, 2024
@snicoll snicoll added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants