[BAEL-6955] finalization deprcation (#14932)

Co-authored-by: Bhaskar <bhaskar.dastidar@freshworks.com>
This commit is contained in:
Bhaskar Ghosh Dastidar 2023-10-10 01:29:52 +05:30 committed by GitHub
parent 0faf3ee42c
commit 8ffbb095da
8 changed files with 216 additions and 0 deletions

View File

View File

@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-18</artifactId>
<name>core-java-18</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../</relativePath>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<release>${maven.compiler.release}</release>
<compilerArgs>--enable-preview</compilerArgs>
<source>${maven.compiler.source.version}</source>
<target>${maven.compiler.target.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.plugin.version}</version>
<configuration>
<forkCount>1</forkCount>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-api</artifactId>
<version>${surefire.plugin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source.version>18</maven.compiler.source.version>
<maven.compiler.target.version>18</maven.compiler.target.version>
<maven.compiler.release>18</maven.compiler.release>
<surefire.plugin.version>3.0.0-M5</surefire.plugin.version>
</properties>
</project>

View File

@ -0,0 +1,27 @@
package com.baeldung.finalization_closeable_cleaner;
import java.io.FileInputStream;
import java.io.IOException;
public class FinalizationExamples {
FileInputStream fis = null;
public void readFileOperationWithFinalization() throws IOException {
try {
fis = new FileInputStream("input.txt");
// perform operation on the file
System.out.println(fis.readAllBytes().length);
} finally {
if (fis != null)
fis.close();
}
}
public void readFileOperationWithTryWith() throws IOException {
try (FileInputStream fis = new FileInputStream("input.txt")) {
// perform operations
System.out.println(fis.readAllBytes().length);
}
}
}

View File

@ -0,0 +1,48 @@
package com.baeldung.finalization_closeable_cleaner;
import java.lang.ref.Cleaner;
public class MyCleanerResourceClass implements AutoCloseable {
private static Resource resource;
private static final Cleaner cleaner = Cleaner.create();
private final Cleaner.Cleanable cleanable;
public MyCleanerResourceClass() {
resource = new Resource();
this.cleanable = cleaner.register(this, new CleaningState());
}
public void useResource() {
// using the resource here
resource.use();
}
@Override
public void close() {
// perform actions to close all underlying resources
this.cleanable.clean();
}
static class CleaningState implements Runnable {
CleaningState() {
// constructor
}
@Override
public void run() {
// some cleanup action
System.out.println("Cleanup done");
}
}
static class Resource {
void use() {
System.out.println("Using the resource");
}
void close() {
System.out.println("Cleanup done");
}
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.finalization_closeable_cleaner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class MyCloseableResourceClass implements AutoCloseable {
private final FileInputStream fis;
public MyCloseableResourceClass() throws FileNotFoundException {
this.fis = new FileInputStream("src/main/resources/file.txt");
}
public int getByteLength() throws IOException {
System.out.println("Some operation");
return this.fis.readAllBytes().length;
}
@Override
public void close() throws IOException {
System.out.println("Finalized object");
this.fis.close();
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.finalization_closeable_cleaner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class MyFinalizableResourceClass {
private FileInputStream fis;
public MyFinalizableResourceClass() throws FileNotFoundException {
this.fis = new FileInputStream("src/main/resources/file.txt");
}
public int getByteLength() throws IOException {
System.out.println("Some operation");
return this.fis.readAllBytes().length;
}
@Override
protected void finalize() throws Throwable {
System.out.println("Finalized object");
this.fis.close();
}
}

View File

@ -0,0 +1 @@
This is a test file.

View File

@ -0,0 +1,36 @@
package com.baeldung.finalization_closeable_cleaner;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Test;
public class FinalizationCloseableCleanerUnitTest {
@Test
public void givenMyFinalizationResource_whenUsingFinalize_thenShouldClean() {
assertDoesNotThrow(() -> {
MyFinalizableResourceClass mfr = new MyFinalizableResourceClass();
mfr.getByteLength();
});
}
@Test
public void givenMyCleanerResource_whenUsingCleanerAPI_thenShouldClean() {
assertDoesNotThrow(() -> {
try (MyCleanerResourceClass myCleanerResourceClass = new MyCleanerResourceClass()) {
myCleanerResourceClass.useResource();
}
});
}
@Test
public void givenCloseableResource_whenUsingTryWith_thenShouldClose() throws IOException {
int length = 0;
try (MyCloseableResourceClass mcr = new MyCloseableResourceClass()) {
length = mcr.getByteLength();
}
Assert.assertEquals(20, length);
}
}