Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

66%
+2 −0
Q&A What is the purpose of having a wrapper class for JarFile in Spring-boot-loader?

My question relates to Spring-boot-loader v2.6.15. For the purposes of this question, I will refer to Spring's custom JarFile class as CustomJarFile to avoid confusion. Context I am reusing so...

1 answer  ·  posted 4mo ago by Miss Skooter‭  ·  last activity 3mo ago by Miss Skooter‭

#2: Nominated for promotion by user avatar Alexei‭ · 2024-01-28T12:52:00Z (3 months ago)
#1: Initial revision by user avatar Miss Skooter‭ · 2024-01-24T13:14:47Z (4 months ago)
What is the purpose of having a wrapper class for JarFile in Spring-boot-loader?
My question relates to [Spring-boot-loader v2.6.15](https://github.com/spring-projects/spring-boot/tree/v2.6.15/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar).

For the purposes of this question, I will refer to Spring's custom `JarFile` class as `CustomJarFile` to avoid confusion.

----

## Context

I am reusing some of the jar package code in spring-boot-loader. I noticed that they define their own [`CustomJarFile` implementation](https://github.com/spring-projects/spring-boot/blob/v2.6.15/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java) that (most relevant to this question) overrides the `close()` method like so:

```
@Override
public void close() throws IOException {
	if (this.closed) {
		return;
	}
	super.close();
	if (this.type == JarFileType.DIRECT) {
		this.rootFile.close();
	}
	this.closed = true;
}
```

They also define a [`JarFileWrapper`](https://github.com/spring-projects/spring-boot/blob/v2.6.15/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFileWrapper.java) class that **does not** override the close method.

The custom [`JarUrlConnection#get()`](https://github.com/spring-projects/spring-boot/blob/v2.6.15/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarURLConnection.java#L266) method notably returns an instance with the wrapper rather than the jar itself like so:

```
static JarURLConnection get(URL url, JarFile jarFile) throws IOException {
    // some stuff here...
    return new JarURLConnection(url, jarFile.getWrapper(), jarEntryName); // JarFile here is the custom class
}

```

Testing the code without the wrapper class by returning a `JarUrlConnection` with the `CustomJarFile` itself rather than its wrapper results in a "zip file closed" exception.

This is reasonable given that when you call `close()` on the wrapper, it will call the `close()` method of the `ZipFile` class. However, if the `JarFile` itself is sent, it will call the `close()` method of the `JarFile` since it is overriden.

Notably, The JavaDoc of the wrapper class states:

> A wrapper used to create a copy of a {@link CustomJarFile} so that it can be safely closed without closing the original.

----

## Question

I do not quite understand the purpose of using a wrapper here. If the goal is to just avoid closing the `CustomJarFile`, wouldn't simply not overriding the `close()` method in `CustomJarFile` (allowing it to propogate to `ZipFile`) have the same effect as using a wrapper? It feels like we just added a redundant layer.

While debugging, I've noticed that the `close()` method of `CustomJarFile` is never called between startup and shutdown of the service which makes me further believe that the wrapper is pointless if you simply remove the overriden close method in `CustomJarFile`.