Skip to content

Commit 8ffbb09

Browse files
[BAEL-6955] finalization deprcation (#14932)
Co-authored-by: Bhaskar <bhaskar.dastidar@freshworks.com>
1 parent 0faf3ee commit 8ffbb09

File tree

8 files changed

+216
-0
lines changed

8 files changed

+216
-0
lines changed

core-java-modules/core-java-18/README.md

Whitespace-only changes.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>core-java-18</artifactId>
7+
<name>core-java-18</name>
8+
<packaging>jar</packaging>
9+
10+
<parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>parent-modules</artifactId>
13+
<version>1.0.0-SNAPSHOT</version>
14+
<relativePath>../../</relativePath>
15+
</parent>
16+
17+
<build>
18+
<plugins>
19+
<plugin>
20+
<groupId>org.apache.maven.plugins</groupId>
21+
<artifactId>maven-compiler-plugin</artifactId>
22+
<version>${maven-compiler-plugin.version}</version>
23+
<configuration>
24+
<release>${maven.compiler.release}</release>
25+
<compilerArgs>--enable-preview</compilerArgs>
26+
<source>${maven.compiler.source.version}</source>
27+
<target>${maven.compiler.target.version}</target>
28+
</configuration>
29+
</plugin>
30+
<plugin>
31+
<groupId>org.apache.maven.plugins</groupId>
32+
<artifactId>maven-surefire-plugin</artifactId>
33+
<version>${surefire.plugin.version}</version>
34+
<configuration>
35+
<forkCount>1</forkCount>
36+
</configuration>
37+
<dependencies>
38+
<dependency>
39+
<groupId>org.apache.maven.surefire</groupId>
40+
<artifactId>surefire-api</artifactId>
41+
<version>${surefire.plugin.version}</version>
42+
</dependency>
43+
</dependencies>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
48+
<properties>
49+
<maven.compiler.source.version>18</maven.compiler.source.version>
50+
<maven.compiler.target.version>18</maven.compiler.target.version>
51+
<maven.compiler.release>18</maven.compiler.release>
52+
<surefire.plugin.version>3.0.0-M5</surefire.plugin.version>
53+
</properties>
54+
55+
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.finalization_closeable_cleaner;
2+
3+
import java.io.FileInputStream;
4+
import java.io.IOException;
5+
6+
public class FinalizationExamples {
7+
FileInputStream fis = null;
8+
9+
public void readFileOperationWithFinalization() throws IOException {
10+
try {
11+
fis = new FileInputStream("input.txt");
12+
// perform operation on the file
13+
System.out.println(fis.readAllBytes().length);
14+
15+
} finally {
16+
if (fis != null)
17+
fis.close();
18+
}
19+
}
20+
21+
public void readFileOperationWithTryWith() throws IOException {
22+
try (FileInputStream fis = new FileInputStream("input.txt")) {
23+
// perform operations
24+
System.out.println(fis.readAllBytes().length);
25+
}
26+
}
27+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.finalization_closeable_cleaner;
2+
3+
import java.lang.ref.Cleaner;
4+
5+
public class MyCleanerResourceClass implements AutoCloseable {
6+
private static Resource resource;
7+
8+
private static final Cleaner cleaner = Cleaner.create();
9+
private final Cleaner.Cleanable cleanable;
10+
11+
public MyCleanerResourceClass() {
12+
resource = new Resource();
13+
this.cleanable = cleaner.register(this, new CleaningState());
14+
}
15+
16+
public void useResource() {
17+
// using the resource here
18+
resource.use();
19+
}
20+
21+
@Override
22+
public void close() {
23+
// perform actions to close all underlying resources
24+
this.cleanable.clean();
25+
}
26+
27+
static class CleaningState implements Runnable {
28+
CleaningState() {
29+
// constructor
30+
}
31+
32+
@Override
33+
public void run() {
34+
// some cleanup action
35+
System.out.println("Cleanup done");
36+
}
37+
}
38+
39+
static class Resource {
40+
void use() {
41+
System.out.println("Using the resource");
42+
}
43+
44+
void close() {
45+
System.out.println("Cleanup done");
46+
}
47+
}
48+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.finalization_closeable_cleaner;
2+
3+
import java.io.FileInputStream;
4+
import java.io.FileNotFoundException;
5+
import java.io.IOException;
6+
7+
public class MyCloseableResourceClass implements AutoCloseable {
8+
9+
private final FileInputStream fis;
10+
11+
public MyCloseableResourceClass() throws FileNotFoundException {
12+
this.fis = new FileInputStream("src/main/resources/file.txt");
13+
14+
}
15+
16+
public int getByteLength() throws IOException {
17+
System.out.println("Some operation");
18+
return this.fis.readAllBytes().length;
19+
}
20+
@Override
21+
public void close() throws IOException {
22+
System.out.println("Finalized object");
23+
this.fis.close();
24+
}
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.finalization_closeable_cleaner;
2+
3+
import java.io.FileInputStream;
4+
import java.io.FileNotFoundException;
5+
import java.io.IOException;
6+
7+
public class MyFinalizableResourceClass {
8+
private FileInputStream fis;
9+
10+
public MyFinalizableResourceClass() throws FileNotFoundException {
11+
this.fis = new FileInputStream("src/main/resources/file.txt");
12+
}
13+
14+
public int getByteLength() throws IOException {
15+
System.out.println("Some operation");
16+
return this.fis.readAllBytes().length;
17+
}
18+
19+
@Override
20+
protected void finalize() throws Throwable {
21+
System.out.println("Finalized object");
22+
this.fis.close();
23+
}
24+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a test file.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.finalization_closeable_cleaner;
2+
3+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
4+
5+
import java.io.IOException;
6+
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
public class FinalizationCloseableCleanerUnitTest {
11+
12+
@Test
13+
public void givenMyFinalizationResource_whenUsingFinalize_thenShouldClean() {
14+
assertDoesNotThrow(() -> {
15+
MyFinalizableResourceClass mfr = new MyFinalizableResourceClass();
16+
mfr.getByteLength();
17+
});
18+
}
19+
@Test
20+
public void givenMyCleanerResource_whenUsingCleanerAPI_thenShouldClean() {
21+
assertDoesNotThrow(() -> {
22+
try (MyCleanerResourceClass myCleanerResourceClass = new MyCleanerResourceClass()) {
23+
myCleanerResourceClass.useResource();
24+
}
25+
});
26+
}
27+
28+
@Test
29+
public void givenCloseableResource_whenUsingTryWith_thenShouldClose() throws IOException {
30+
int length = 0;
31+
try (MyCloseableResourceClass mcr = new MyCloseableResourceClass()) {
32+
length = mcr.getByteLength();
33+
}
34+
Assert.assertEquals(20, length);
35+
}
36+
}

0 commit comments

Comments
 (0)