11package com .datadog .api .compatibility ;
22
3+ import static org .junit .Assert .*;
4+
35import com .fasterxml .jackson .core .type .TypeReference ;
46import com .fasterxml .jackson .databind .JsonNode ;
57import com .fasterxml .jackson .databind .ObjectMapper ;
6- import org .junit .Test ;
7-
88import java .io .IOException ;
99import java .util .List ;
10-
11- import static org .junit .Assert .*;
10+ import org .junit .Test ;
1211
1312/**
14- * CRITICAL TEST: This demonstrates the exact Java 8 compilation issue that PR #4191 would have caused.
13+ * CRITICAL TEST: This demonstrates the exact Java 8 compilation issue that PR #4191 would have
14+ * caused.
1515 *
16- * This test contains BOTH the problematic pattern AND the fixed pattern to prove our solution works.
16+ * <p>This test contains BOTH the problematic pattern AND the fixed pattern to prove our solution
17+ * works.
1718 */
1819public class DiamondOperatorCompilationTest {
1920
20- private final ObjectMapper objectMapper = new ObjectMapper ();
21+ private final ObjectMapper objectMapper = new ObjectMapper ();
22+
23+ @ Test
24+ public void testJava8DiamondOperatorIssueReproduction () throws IOException {
25+ // This test reproduces the EXACT issue from PR #4191
26+
27+ String jsonArray = "[{\" email\" : \" test@example.com\" }, {\" role\" : \" viewer\" }]" ;
28+ JsonNode tree = objectMapper .readTree (jsonArray );
2129
22- @ Test
23- public void testJava8DiamondOperatorIssueReproduction () throws IOException {
24- // This test reproduces the EXACT issue from PR #4191
30+ // ❌ PROBLEMATIC PATTERN (would fail compilation in Java 8):
31+ // The following line would cause this compilation error:
32+ // "cannot infer type arguments for com.fasterxml.jackson.core.type.TypeReference<T>
33+ // reason: '<>' with anonymous inner classes is not supported in -source 8"
34+ //
35+ // Object tmp = tree.traverse(objectMapper).readValueAs(new TypeReference<>() {});
36+ // ^^
37+ // Diamond operator fails!
2538
26- String jsonArray = "[{ \" email \" : \" test@example.com \" }, { \" role \" : \" viewer \" }]" ;
27- JsonNode tree = objectMapper . readTree ( jsonArray );
39+ // ✅ FIXED PATTERN (works in Java 8):
40+ Object tmp = tree . traverse ( objectMapper ). readValueAs ( new TypeReference < List < Object >>() {} );
2841
29- // ❌ PROBLEMATIC PATTERN (would fail compilation in Java 8):
30- // The following line would cause this compilation error:
31- // "cannot infer type arguments for com.fasterxml.jackson.core.type.TypeReference<T>
32- // reason: '<>' with anonymous inner classes is not supported in -source 8"
33- //
34- // Object tmp = tree.traverse(objectMapper).readValueAs(new TypeReference<>() {});
35- // ^^
36- // Diamond operator fails!
42+ assertNotNull (tmp );
43+ assertTrue ("Should deserialize successfully with explicit type" , tmp instanceof List );
3744
38- // ✅ FIXED PATTERN (works in Java 8):
39- Object tmp = tree .traverse (objectMapper ).readValueAs (new TypeReference <List <Object >>() {});
45+ @ SuppressWarnings ("unchecked" )
46+ List <Object > result = (List <Object >) tmp ;
47+ assertEquals ("Should have 2 elements" , 2 , result .size ());
48+ }
4049
41- assertNotNull (tmp );
42- assertTrue ("Should deserialize successfully with explicit type" , tmp instanceof List );
50+ @ Test
51+ public void testTemplateFixSimulation () throws IOException {
52+ // This simulates what our template fix does:
53+ // Detects parameterized types and uses explicit TypeReference
4354
44- @ SuppressWarnings ("unchecked" )
45- List <Object > result = (List <Object >) tmp ;
46- assertEquals ("Should have 2 elements" , 2 , result .size ());
47- }
55+ String jsonData = "[1.5, 2.0, 3.7]" ; // Numbers (like DistributionPointItem)
56+ JsonNode tree = objectMapper .readTree (jsonData );
4857
49- @ Test
50- public void testTemplateFixSimulation () throws IOException {
51- // This simulates what our template fix does:
52- // Detects parameterized types and uses explicit TypeReference
58+ // Template logic simulation:
59+ String dataType = "List<Double>" ;
60+ String unParameterizedDataType = "Double" ; // This would be different, so use TypeReference
5361
54- String jsonData = "[1.5, 2.0, 3.7]" ; // Numbers (like DistributionPointItem)
55- JsonNode tree = objectMapper . readTree ( jsonData );
62+ boolean isParameterized = ! dataType . equals ( unParameterizedDataType );
63+ assertTrue ( "Template should detect this as parameterized" , isParameterized );
5664
57- // Template logic simulation:
58- String dataType = "List<Double>" ;
59- String unParameterizedDataType = "Double" ; // This would be different, so use TypeReference
65+ // Because it's parameterized, template generates this (FIXED pattern):
66+ Object tmp = tree .traverse (objectMapper ).readValueAs (new TypeReference <List <Double >>() {});
67+ // Instead of this (BROKEN pattern that would fail):
68+ // Object tmp = tree.traverse(objectMapper).readValueAs(new TypeReference<>() {});
6069
61- boolean isParameterized = ! dataType . equals ( unParameterizedDataType );
62- assertTrue ("Template should detect this as parameterized" , isParameterized );
70+ assertNotNull ( tmp );
71+ assertTrue (tmp instanceof List );
6372
64- // Because it's parameterized, template generates this (FIXED pattern):
65- Object tmp = tree .traverse (objectMapper ).readValueAs (new TypeReference <List <Double >>() {});
66- // Instead of this (BROKEN pattern that would fail):
67- // Object tmp = tree.traverse(objectMapper).readValueAs(new TypeReference<>() {});
73+ @ SuppressWarnings ("unchecked" )
74+ List <Double > result = (List <Double >) tmp ;
75+ assertEquals (3 , result .size ());
76+ assertEquals (1.5 , result .get (0 ), 0.001 );
77+ }
6878
69- assertNotNull (tmp );
70- assertTrue (tmp instanceof List );
79+ @ Test
80+ public void testOriginalSharedDashboardInvitesDataScenario () throws IOException {
81+ // This recreates the exact failing scenario from SharedDashboardInvitesData.java:138
7182
72- @ SuppressWarnings ("unchecked" )
73- List <Double > result = (List <Double >) tmp ;
74- assertEquals (3 , result .size ());
75- assertEquals (1.5 , result .get (0 ), 0.001 );
76- }
83+ String jsonInvites = "[{\" email\" :\" user@example.com\" ,\" role\" :\" viewer\" }]" ;
84+ JsonNode tree = objectMapper .readTree (jsonInvites );
7785
78- @ Test
79- public void testOriginalSharedDashboardInvitesDataScenario () throws IOException {
80- // This recreates the exact failing scenario from SharedDashboardInvitesData.java:138
86+ // BEFORE our fix (would cause Java 8 compilation failure):
87+ // tmp = tree.traverse(jp.getCodec()).readValueAs(new TypeReference<>() {});
8188
82- String jsonInvites = "[{ \" email \" : \" user@example.com \" , \" role \" : \" viewer \" }]" ;
83- JsonNode tree = objectMapper . readTree ( jsonInvites );
89+ // AFTER our fix (Java 8 compatible):
90+ Object tmp = tree . traverse ( objectMapper ). readValueAs ( new TypeReference < List < Object >>() {} );
8491
85- // BEFORE our fix (would cause Java 8 compilation failure):
86- // tmp = tree.traverse(jp.getCodec()).readValueAs(new TypeReference<>() {} );
92+ assertNotNull ( "Should deserialize invite data" , tmp );
93+ assertTrue ( "Should be a List" , tmp instanceof List );
8794
88- // AFTER our fix (Java 8 compatible):
89- Object tmp = tree .traverse (objectMapper )
90- .readValueAs (new TypeReference <List <Object >>() {});
95+ @ SuppressWarnings ("unchecked" )
96+ List <Object > invites = (List <Object >) tmp ;
97+ assertEquals ("Should have one invite" , 1 , invites .size ());
98+ }
9199
92- assertNotNull ("Should deserialize invite data" , tmp );
93- assertTrue ("Should be a List" , tmp instanceof List );
100+ @ Test
101+ public void testOriginalDistributionPointItemScenario () throws IOException {
102+ // This recreates the exact failing scenario from DistributionPointItem.java:132
94103
95- @ SuppressWarnings ("unchecked" )
96- List <Object > invites = (List <Object >) tmp ;
97- assertEquals ("Should have one invite" , 1 , invites .size ());
98- }
104+ String jsonPoints = "[1.0, 2.5, 3.14]" ; // Distribution points as numbers
105+ JsonNode tree = objectMapper .readTree (jsonPoints );
99106
100- @ Test
101- public void testOriginalDistributionPointItemScenario () throws IOException {
102- // This recreates the exact failing scenario from DistributionPointItem.java:132
107+ // BEFORE our fix (would cause Java 8 compilation failure):
108+ // tmp = tree.traverse(jp.getCodec()).readValueAs(new TypeReference<>() {});
103109
104- String jsonPoints = "[1.0, 2.5, 3.14]" ; // Distribution points as numbers
105- JsonNode tree = objectMapper . readTree ( jsonPoints );
110+ // AFTER our fix (Java 8 compatible):
111+ Object tmp = tree . traverse ( objectMapper ). readValueAs ( new TypeReference < List < Double >>() {} );
106112
107- // BEFORE our fix (would cause Java 8 compilation failure):
108- // tmp = tree.traverse(jp.getCodec()).readValueAs(new TypeReference<>() {} );
113+ assertNotNull ( "Should deserialize distribution points" , tmp );
114+ assertTrue ( "Should be a List" , tmp instanceof List );
109115
110- // AFTER our fix (Java 8 compatible):
111- Object tmp = tree .traverse (objectMapper )
112- .readValueAs (new TypeReference <List <Double >>() {});
116+ @ SuppressWarnings ("unchecked" )
117+ List <Double > points = (List <Double >) tmp ;
118+ assertEquals ("Should have 3 points" , 3 , points .size ());
119+ assertEquals ("First point should be 1.0" , 1.0 , points .get (0 ), 0.001 );
120+ }
113121
114- assertNotNull ("Should deserialize distribution points" , tmp );
115- assertTrue ("Should be a List" , tmp instanceof List );
122+ @ Test
123+ public void testCustomAttributeValuesUnionScenario () throws IOException {
124+ // This recreates what CustomAttributeValuesUnion.java:133 would have been
116125
117- @ SuppressWarnings ("unchecked" )
118- List <Double > points = (List <Double >) tmp ;
119- assertEquals ("Should have 3 points" , 3 , points .size ());
120- assertEquals ("First point should be 1.0" , 1.0 , points .get (0 ), 0.001 );
121- }
122-
123- @ Test
124- public void testCustomAttributeValuesUnionScenario () throws IOException {
125- // This recreates what CustomAttributeValuesUnion.java:133 would have been
126-
127- String jsonValues = "[\" string_value\" , 42, {\" object\" : \" value\" }]" ; // Mixed values
128- JsonNode tree = objectMapper .readTree (jsonValues );
129-
130- // BEFORE our fix (would cause Java 8 compilation failure):
131- // tmp = tree.traverse(jp.getCodec()).readValueAs(new TypeReference<>() {});
126+ String jsonValues = "[\" string_value\" , 42, {\" object\" : \" value\" }]" ; // Mixed values
127+ JsonNode tree = objectMapper .readTree (jsonValues );
132128
133- // AFTER our fix (Java 8 compatible):
134- Object tmp = tree .traverse (objectMapper )
135- .readValueAs (new TypeReference <List <Object >>() {});
129+ // BEFORE our fix (would cause Java 8 compilation failure):
130+ // tmp = tree.traverse(jp.getCodec()).readValueAs(new TypeReference<>() {});
131+
132+ // AFTER our fix (Java 8 compatible):
133+ Object tmp = tree .traverse (objectMapper ).readValueAs (new TypeReference <List <Object >>() {});
136134
137- assertNotNull ("Should deserialize custom attribute values" , tmp );
138- assertTrue ("Should be a List" , tmp instanceof List );
135+ assertNotNull ("Should deserialize custom attribute values" , tmp );
136+ assertTrue ("Should be a List" , tmp instanceof List );
139137
140- @ SuppressWarnings ("unchecked" )
141- List <Object > values = (List <Object >) tmp ;
142- assertEquals ("Should have 3 values" , 3 , values .size ());
143- }
138+ @ SuppressWarnings ("unchecked" )
139+ List <Object > values = (List <Object >) tmp ;
140+ assertEquals ("Should have 3 values" , 3 , values .size ());
141+ }
144142
145- @ Test
146- public void testCompilationDifferenceDocumentation () {
147- // This test documents the exact difference between failing and working code
143+ @ Test
144+ public void testCompilationDifferenceDocumentation () {
145+ // This test documents the exact difference between failing and working code
148146
149- // ❌ FAILS in Java 8 (diamond operator with anonymous inner class):
150- String failingPattern = "new TypeReference<>() {}" ;
147+ // ❌ FAILS in Java 8 (diamond operator with anonymous inner class):
148+ String failingPattern = "new TypeReference<>() {}" ;
151149
152- // ✅ WORKS in Java 8 (explicit type parameters):
153- String workingPattern1 = "new TypeReference<List<String>>() {}" ;
154- String workingPattern2 = "new TypeReference<List<Object>>() {}" ;
155- String workingPattern3 = "new TypeReference<List<Double>>() {}" ;
150+ // ✅ WORKS in Java 8 (explicit type parameters):
151+ String workingPattern1 = "new TypeReference<List<String>>() {}" ;
152+ String workingPattern2 = "new TypeReference<List<Object>>() {}" ;
153+ String workingPattern3 = "new TypeReference<List<Double>>() {}" ;
156154
157- // Verify the patterns
158- assertTrue ("Failing pattern uses diamond operator" , failingPattern .contains ("<>" ));
155+ // Verify the patterns
156+ assertTrue ("Failing pattern uses diamond operator" , failingPattern .contains ("<>" ));
159157
160- assertFalse ("Working pattern 1 should not use diamond operator" , workingPattern1 .contains ("<>" ));
161- assertFalse ("Working pattern 2 should not use diamond operator" , workingPattern2 .contains ("<>" ));
162- assertFalse ("Working pattern 3 should not use diamond operator" , workingPattern3 .contains ("<>" ));
158+ assertFalse (
159+ "Working pattern 1 should not use diamond operator" , workingPattern1 .contains ("<>" ));
160+ assertFalse (
161+ "Working pattern 2 should not use diamond operator" , workingPattern2 .contains ("<>" ));
162+ assertFalse (
163+ "Working pattern 3 should not use diamond operator" , workingPattern3 .contains ("<>" ));
163164
164- // All working patterns have explicit types
165- assertTrue ("Pattern 1 has explicit type" , workingPattern1 .contains ("List<String>" ));
166- assertTrue ("Pattern 2 has explicit type" , workingPattern2 .contains ("List<Object>" ));
167- assertTrue ("Pattern 3 has explicit type" , workingPattern3 .contains ("List<Double>" ));
168- }
169- }
165+ // All working patterns have explicit types
166+ assertTrue ("Pattern 1 has explicit type" , workingPattern1 .contains ("List<String>" ));
167+ assertTrue ("Pattern 2 has explicit type" , workingPattern2 .contains ("List<Object>" ));
168+ assertTrue ("Pattern 3 has explicit type" , workingPattern3 .contains ("List<Double>" ));
169+ }
170+ }
0 commit comments