[BAEL-6955] finalization deprcation (#14932)
Co-authored-by: Bhaskar <bhaskar.dastidar@freshworks.com>
This commit is contained in:
committed by
GitHub
parent
0faf3ee42c
commit
8ffbb095da
+27
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
+48
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
+25
@@ -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();
|
||||
}
|
||||
}
|
||||
+24
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
This is a test file.
|
||||
+36
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user