Merge remote-tracking branch 'upstream/master' into BAEL_4302

This commit is contained in:
root
2020-07-14 19:03:16 +00:00
326 changed files with 21314 additions and 475 deletions

View File

@@ -0,0 +1,25 @@
package com.baeldung.boolsize;
import org.junit.Test;
import org.openjdk.jol.info.ClassLayout;
import org.openjdk.jol.vm.VM;
public class BooleanSizeUnitTest {
@Test
public void printingTheVMDetails() {
System.out.println(VM.current().details());
}
@Test
public void printingTheBoolWrapper() {
System.out.println(ClassLayout.parseClass(BooleanWrapper.class).toPrintable());
}
@Test
public void printingTheBoolArray() {
boolean[] value = new boolean[3];
System.out.println(ClassLayout.parseInstance(value).toPrintable());
}
}

View File

@@ -0,0 +1,5 @@
package com.baeldung.boolsize;
class BooleanWrapper {
private boolean value;
}

View File

@@ -0,0 +1,24 @@
package com.baeldung.memaddress;
import org.junit.Test;
import org.openjdk.jol.vm.VM;
public class MemoryAddressUnitTest {
@Test
public void printTheMemoryAddress() {
String answer = "42";
System.out.println("The memory address is " + VM.current().addressOf(answer));
}
@Test
public void identityHashCodeAndMemoryAddress() {
Object obj = new Object();
System.out.println("Memory address: " + VM.current().addressOf(obj));
System.out.println("hashCode: " + obj.hashCode());
System.out.println("hashCode: " + System.identityHashCode(obj));
System.out.println("toString: " + obj);
}
}

View File

@@ -0,0 +1,10 @@
package com.baeldung.objectsize;
public class Course {
private String name;
public Course(String name) {
this.name = name;
}
}

View File

@@ -0,0 +1,11 @@
package com.baeldung.objectsize;
public class InstrumentedSize {
public static void main(String[] args) {
String ds = "Data Structures";
Course course = new Course(ds);
System.out.println(ObjectSizeCalculator.sizeOf(course));
}
}

View File

@@ -0,0 +1 @@
Premain-Class: com.baeldung.objectsize.ObjectSizeCalculator

View File

@@ -0,0 +1,16 @@
package com.baeldung.objectsize;
import java.lang.instrument.Instrumentation;
public class ObjectSizeCalculator {
private static Instrumentation instrumentation;
public static void premain(String args, Instrumentation inst) {
instrumentation = inst;
}
public static long sizeOf(Object o) {
return instrumentation.getObjectSize(o);
}
}

View File

@@ -0,0 +1,40 @@
package com.baeldung.objectsize;
import org.junit.Test;
import org.openjdk.jol.info.ClassLayout;
import org.openjdk.jol.info.GraphLayout;
import org.openjdk.jol.vm.VM;
public class ObjectSizeUnitTest {
@Test
public void printingTheVMDetails() {
System.out.println(VM.current().details());
}
@Test
public void printingTheProfClassLayout() {
System.out.println(ClassLayout.parseClass(Professor.class).toPrintable());
}
@Test
public void printingTheCourseClassLayout() {
System.out.println(ClassLayout.parseClass(Course.class).toPrintable());
}
@Test
public void printingACourseInstanceLayout() {
String ds = "Data Structures";
Course course = new Course(ds);
System.out.println("The shallow size is :" + VM.current().sizeOf(course));
System.out.println(ClassLayout.parseInstance(course).toPrintable());
System.out.println(ClassLayout.parseInstance(ds).toPrintable());
System.out.println(ClassLayout.parseInstance(ds.toCharArray()).toPrintable());
System.out.println(GraphLayout.parseInstance(course).totalSize());
System.out.println(GraphLayout.parseInstance(course).toFootprint());
System.out.println(GraphLayout.parseInstance(course).toPrintable());
}
}

View File

@@ -0,0 +1,24 @@
package com.baeldung.objectsize;
import java.time.LocalDate;
import java.util.List;
public class Professor {
private String name;
private boolean tenured;
private List<Course> courses;
private int level;
private LocalDate birthDay;
private double lastEvaluation;
public Professor(String name, boolean tenured, List<Course> courses,
int level, LocalDate birthDay, double lastEvaluation) {
this.name = name;
this.tenured = tenured;
this.courses = courses;
this.level = level;
this.birthDay = birthDay;
this.lastEvaluation = lastEvaluation;
}
}

View File

@@ -0,0 +1,47 @@
package com.baeldung.shutdownhook;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class ShutdownHookUnitTest {
@Test
public void givenAHook_WhenShutsDown_ThenHookShouldBeExecuted() {
Thread printingHook = new Thread(() -> System.out.println("In the middle of a shutdown"));
Runtime.getRuntime().addShutdownHook(printingHook);
}
@Test
public void addingAHook_WhenThreadAlreadyStarted_ThenThrowsAnException() {
Thread longRunningHook = new Thread(() -> {
try {
Thread.sleep(300);
} catch (InterruptedException ignored) {}
});
longRunningHook.start();
assertThatThrownBy(() -> Runtime.getRuntime().addShutdownHook(longRunningHook))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Hook already running");
}
@Test
public void addingAHook_WhenAlreadyExists_ThenAnExceptionWouldBeThrown() {
Thread unfortunateHook = new Thread(() -> {});
Runtime.getRuntime().addShutdownHook(unfortunateHook);
assertThatThrownBy(() -> Runtime.getRuntime().addShutdownHook(unfortunateHook))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Hook previously registered");
}
@Test
public void removeAHook_WhenItIsAlreadyRegistered_ThenWouldDeRegisterTheHook() {
Thread willNotRun = new Thread(() -> System.out.println("Won't run!"));
Runtime.getRuntime().addShutdownHook(willNotRun);
assertThat(Runtime.getRuntime().removeShutdownHook(willNotRun)).isTrue();
}
}