From be292ec3f22c4ca82e7154718d89595c9cd1c9c6 Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Tue, 2 Jun 2020 21:32:45 +0430 Subject: [PATCH 1/6] Avoid Using newInstance on Class --- .../reflection/java/reflection/ReflectionUnitTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/java/reflection/ReflectionUnitTest.java b/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/java/reflection/ReflectionUnitTest.java index 0c090901e7..a791d64874 100644 --- a/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/java/reflection/ReflectionUnitTest.java +++ b/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/java/reflection/ReflectionUnitTest.java @@ -200,7 +200,7 @@ public class ReflectionUnitTest { @Test public void givenClassField_whenSetsAndGetsValue_thenCorrect() throws Exception { final Class birdClass = Class.forName("com.baeldung.java.reflection.Bird"); - final Bird bird = (Bird) birdClass.newInstance(); + final Bird bird = (Bird) birdClass.getConstructor().newInstance(); final Field field = birdClass.getDeclaredField("walks"); field.setAccessible(true); @@ -266,7 +266,7 @@ public class ReflectionUnitTest { @Test public void givenMethod_whenInvokes_thenCorrect() throws Exception { final Class birdClass = Class.forName("com.baeldung.java.reflection.Bird"); - final Bird bird = (Bird) birdClass.newInstance(); + final Bird bird = (Bird) birdClass.getConstructor().newInstance(); final Method setWalksMethod = birdClass.getDeclaredMethod("setWalks", boolean.class); final Method walksMethod = birdClass.getDeclaredMethod("walks"); final boolean walks = (boolean) walksMethod.invoke(bird); From b07bcce7efcdf939dd336b2dceabbb88449b89b9 Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Tue, 2 Jun 2020 22:03:54 +0430 Subject: [PATCH 2/6] Introducing Integer Cache --- .../stringtoint/StringToIntOrIntegerUnitTest.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/core-java-modules/core-java-string-conversions/src/test/java/com/baeldung/stringtoint/StringToIntOrIntegerUnitTest.java b/core-java-modules/core-java-string-conversions/src/test/java/com/baeldung/stringtoint/StringToIntOrIntegerUnitTest.java index 106f1fc974..336b2ac324 100644 --- a/core-java-modules/core-java-string-conversions/src/test/java/com/baeldung/stringtoint/StringToIntOrIntegerUnitTest.java +++ b/core-java-modules/core-java-string-conversions/src/test/java/com/baeldung/stringtoint/StringToIntOrIntegerUnitTest.java @@ -26,6 +26,17 @@ public class StringToIntOrIntegerUnitTest { assertThat(result).isEqualTo(new Integer(42)); } + @Test + public void givenString_whenCallingValueOf_shouldCacheSomeValues() { + for (int i = -128; i <= 127; i++) { + String value = i + ""; + Integer first = Integer.valueOf(value); + Integer second = Integer.valueOf(value); + + assertThat(first).isSameAs(second); + } + } + @Test public void givenString_whenCallingIntegerConstructor_shouldConvertToInt() { String givenString = "42"; From 829140d488d33795b1c5b674cc4865f116391ba4 Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Wed, 3 Jun 2020 14:10:58 +0430 Subject: [PATCH 3/6] Polluting the Heap --- .../varargs/HeapPollutionUnitTest.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 core-java-modules/core-java-lang-syntax/src/test/java/com/baeldung/varargs/HeapPollutionUnitTest.java diff --git a/core-java-modules/core-java-lang-syntax/src/test/java/com/baeldung/varargs/HeapPollutionUnitTest.java b/core-java-modules/core-java-lang-syntax/src/test/java/com/baeldung/varargs/HeapPollutionUnitTest.java new file mode 100644 index 0000000000..ced2c00bea --- /dev/null +++ b/core-java-modules/core-java-lang-syntax/src/test/java/com/baeldung/varargs/HeapPollutionUnitTest.java @@ -0,0 +1,40 @@ +package com.baeldung.varargs; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +public class HeapPollutionUnitTest { + + @Test(expected = ClassCastException.class) + public void givenGenericVararg_whenUsedUnsafe_shouldThrowClassCastException() { + String one = firstOfFirst(Arrays.asList("one", "two"), Collections.emptyList()); + + assertEquals("one", one); + } + + @Test(expected = ClassCastException.class) + public void givenGenericVararg_whenRefEscapes_mayCauseSubtleBugs() { + String[] args = returnAsIs("One", "Two"); + } + + private static String firstOfFirst(List... strings) { + List ints = Collections.singletonList(42); + Object[] objects = strings; + objects[0] = ints; + + return strings[0].get(0); + } + + private static T[] toArray(T... arguments) { + return arguments; + } + + private static T[] returnAsIs(T a, T b) { + return toArray(a, b); + } +} From ea59a08f4fa1c0df1c527d0d29bdc4d28ff78c10 Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Sun, 7 Jun 2020 17:53:06 +0200 Subject: [PATCH 4/6] JAVA-1782: Remove byte-buddy dep from the main pom.xml --- parent-boot-2/pom.xml | 5 ----- pom.xml | 6 ------ 2 files changed, 11 deletions(-) diff --git a/parent-boot-2/pom.xml b/parent-boot-2/pom.xml index 631d8a0581..c7bb11b1d5 100644 --- a/parent-boot-2/pom.xml +++ b/parent-boot-2/pom.xml @@ -32,11 +32,6 @@ io.rest-assured rest-assured - - net.bytebuddy - byte-buddy - ${byte-buddy.version} - org.springframework.boot spring-boot-starter-test diff --git a/pom.xml b/pom.xml index f797f1bbce..20adc4bfef 100644 --- a/pom.xml +++ b/pom.xml @@ -69,12 +69,6 @@ ${hamcrest-all.version} test - - net.bytebuddy - byte-buddy - ${byte-buddy.version} - test - org.mockito mockito-core From 0ad021f13402a31a27d2204dad7d4401e6bfa3ba Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Sun, 7 Jun 2020 18:17:05 +0200 Subject: [PATCH 5/6] JAVA-1782: Add byte-buddy explicitly to avoid versions confilict --- jhipster-5/bookstore-monolith/pom.xml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/jhipster-5/bookstore-monolith/pom.xml b/jhipster-5/bookstore-monolith/pom.xml index 233765e0f3..4e4c82f327 100644 --- a/jhipster-5/bookstore-monolith/pom.xml +++ b/jhipster-5/bookstore-monolith/pom.xml @@ -225,6 +225,12 @@ io.dropwizard.metrics metrics-core + + net.bytebuddy + byte-buddy + ${byte-buddy.version} + test + From a1742fc8561afa455d1de4890ecf9db4719e53c1 Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Sun, 7 Jun 2020 22:48:02 +0200 Subject: [PATCH 6/6] JAVA-1782: Add byte-buddy explicitly to avoid versions confilict --- libraries-data-2/pom.xml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libraries-data-2/pom.xml b/libraries-data-2/pom.xml index bdfb2c5ed6..bc2f2c77a1 100644 --- a/libraries-data-2/pom.xml +++ b/libraries-data-2/pom.xml @@ -153,6 +153,12 @@ renjin-script-engine ${renjin.version} + + net.bytebuddy + byte-buddy + ${byte-buddy.version} + test +