Repository navigation
Expand file tree
/
Copy pathtest_java_advanced_oop.py
More file actions
2043 lines (1669 loc) · 54.7 KB
/
Copy pathtest_java_advanced_oop.py
File metadata and controls
2043 lines (1669 loc) · 54.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Advanced Java OOP feature parsing and relationship testing.
Tests multiple inheritance via interfaces, complex generics with wildcards and bounds,
abstract classes with partial implementations, method overloading and overriding edge cases,
covariant return types, and diamond problem resolution.
"""
from pathlib import Path
from unittest.mock import MagicMock
import pytest
from codebase_rag.graph_updater import GraphUpdater
from codebase_rag.parser_loader import load_parsers
@pytest.fixture
def java_advanced_oop_project(temp_repo: Path) -> Path:
"""Create a Java project structure for advanced OOP testing."""
project_path = temp_repo / "java_advanced_oop"
project_path.mkdir()
# Create standard Java project structure
(project_path / "src").mkdir()
(project_path / "src" / "main").mkdir()
(project_path / "src" / "main" / "java").mkdir()
(project_path / "src" / "main" / "java" / "com").mkdir()
(project_path / "src" / "main" / "java" / "com" / "example").mkdir()
return project_path
def test_multiple_interface_inheritance(
java_advanced_oop_project: Path,
mock_ingestor: MagicMock,
) -> None:
"""Test parsing of classes implementing multiple interfaces with diamond problem."""
test_file = (
java_advanced_oop_project
/ "src"
/ "main"
/ "java"
/ "com"
/ "example"
/ "MultipleInheritance.java"
)
test_file.write_text(
"""
package com.example;
interface Drawable {
void draw();
default void render() {
System.out.println("Rendering drawable");
}
}
interface Colorable {
void setColor(String color);
default void render() {
System.out.println("Rendering colorable");
}
}
interface Movable {
void move(int x, int y);
}
interface Shape extends Drawable, Colorable {
double getArea();
@Override
default void render() {
System.out.println("Rendering shape");
}
}
public class Circle implements Shape, Movable {
private double radius;
private String color;
private int x, y;
public Circle(double radius) {
this.radius = radius;
}
@Override
public void draw() {
System.out.println("Drawing circle");
}
@Override
public void setColor(String color) {
this.color = color;
}
@Override
public void move(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}
"""
)
parsers, queries = load_parsers()
if "java" not in parsers:
pytest.skip("Java parser not available")
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=java_advanced_oop_project,
parsers=parsers,
queries=queries,
)
updater.run()
# Verify interfaces and classes were created
all_calls = mock_ingestor.ensure_node_batch.call_args_list
interface_calls = [call for call in all_calls if call[0][0] == "Interface"]
class_calls = [call for call in all_calls if call[0][0] == "Class"]
created_interfaces = {call[0][1]["qualified_name"] for call in interface_calls}
created_classes = {call[0][1]["qualified_name"] for call in class_calls}
project_name = java_advanced_oop_project.name
expected_interfaces = {
f"{project_name}.src.main.java.com.example.MultipleInheritance.Drawable",
f"{project_name}.src.main.java.com.example.MultipleInheritance.Colorable",
f"{project_name}.src.main.java.com.example.MultipleInheritance.Movable",
f"{project_name}.src.main.java.com.example.MultipleInheritance.Shape",
}
expected_classes = {
f"{project_name}.src.main.java.com.example.MultipleInheritance.Circle",
}
assert expected_interfaces <= created_interfaces
assert expected_classes <= created_classes
def test_complex_generics_with_wildcards(
java_advanced_oop_project: Path,
mock_ingestor: MagicMock,
) -> None:
"""Test parsing of complex generic types with wildcards and bounds."""
test_file = (
java_advanced_oop_project
/ "src"
/ "main"
/ "java"
/ "com"
/ "example"
/ "ComplexGenerics.java"
)
test_file.write_text(
"""
package com.example;
import java.util.*;
public class ComplexGenerics<T extends Comparable<T> & Cloneable> {
// Nested generic types
private Map<String, List<Set<T>>> complexStructure;
// Generic method with multiple bounds
public <U extends Number & Comparable<U>> U processNumber(U number, List<? extends U> numbers) {
return numbers.stream().max(U::compareTo).orElse(number);
}
// Wildcard with super bound
public void addNumbers(List<? super Integer> list) {
list.add(42);
list.add(100);
}
// Complex return type with nested generics
public Map<? extends String, ? extends List<? extends T>> getComplexMap() {
return new HashMap<>();
}
// Generic array handling
@SafeVarargs
public final <E> List<E> createList(E... elements) {
return Arrays.asList(elements);
}
// Recursive generic bound
public static <T extends Enum<T>> T parseEnum(Class<T> enumClass, String value) {
return Enum.valueOf(enumClass, value);
}
}
// Generic interface with covariant bounds
interface Producer<T> {
T produce();
}
interface Consumer<T> {
void consume(T item);
}
// PECS (Producer Extends, Consumer Super) pattern
public class GenericUtility {
public static <T> void copy(List<? extends T> source, List<? super T> destination) {
for (T item : source) {
destination.add(item);
}
}
public static <T extends Comparable<? super T>> void sort(List<T> list) {
Collections.sort(list);
}
}
"""
)
parsers, queries = load_parsers()
if "java" not in parsers:
pytest.skip("Java parser not available")
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=java_advanced_oop_project,
parsers=parsers,
queries=queries,
)
updater.run()
# Verify complex generic classes were parsed
all_calls = mock_ingestor.ensure_node_batch.call_args_list
class_calls = [call for call in all_calls if call[0][0] == "Class"]
interface_calls = [call for call in all_calls if call[0][0] == "Interface"]
created_classes = {call[0][1]["qualified_name"] for call in class_calls}
created_interfaces = {call[0][1]["qualified_name"] for call in interface_calls}
project_name = java_advanced_oop_project.name
assert (
f"{project_name}.src.main.java.com.example.ComplexGenerics.ComplexGenerics"
in created_classes
)
assert (
f"{project_name}.src.main.java.com.example.ComplexGenerics.GenericUtility"
in created_classes
)
assert (
f"{project_name}.src.main.java.com.example.ComplexGenerics.Producer"
in created_interfaces
)
assert (
f"{project_name}.src.main.java.com.example.ComplexGenerics.Consumer"
in created_interfaces
)
def test_abstract_classes_with_partial_implementation(
java_advanced_oop_project: Path,
mock_ingestor: MagicMock,
) -> None:
"""Test parsing of abstract classes with mix of abstract and concrete methods."""
test_file = (
java_advanced_oop_project
/ "src"
/ "main"
/ "java"
/ "com"
/ "example"
/ "AbstractClasses.java"
)
test_file.write_text(
"""
package com.example;
public abstract class AbstractShape {
protected String name;
protected String color;
public AbstractShape(String name) {
this.name = name;
this.color = "black";
}
// Abstract methods
public abstract double getArea();
public abstract double getPerimeter();
protected abstract void validate();
// Concrete methods
public String getName() {
return name;
}
public void setColor(String color) {
this.color = color;
}
public final void display() {
validate();
System.out.println(name + " (" + color + "): area=" + getArea() + ", perimeter=" + getPerimeter());
}
// Template method pattern
public final void process() {
beforeProcess();
doProcess();
afterProcess();
}
protected void beforeProcess() {
System.out.println("Before processing " + name);
}
protected abstract void doProcess();
protected void afterProcess() {
System.out.println("After processing " + name);
}
}
public abstract class AbstractRectangle extends AbstractShape {
protected double width, height;
public AbstractRectangle(String name, double width, double height) {
super(name);
this.width = width;
this.height = height;
}
@Override
public double getArea() {
return width * height;
}
@Override
public double getPerimeter() {
return 2 * (width + height);
}
@Override
protected void validate() {
if (width <= 0 || height <= 0) {
throw new IllegalArgumentException("Dimensions must be positive");
}
}
// Still abstract - concrete classes must implement
@Override
protected abstract void doProcess();
}
public class Rectangle extends AbstractRectangle {
public Rectangle(double width, double height) {
super("Rectangle", width, height);
}
@Override
protected void doProcess() {
System.out.println("Processing rectangle: " + width + "x" + height);
}
}
public class Square extends AbstractRectangle {
public Square(double side) {
super("Square", side, side);
}
@Override
protected void doProcess() {
System.out.println("Processing square: " + width + "x" + width);
}
public double getSide() {
return width;
}
}
"""
)
parsers, queries = load_parsers()
if "java" not in parsers:
pytest.skip("Java parser not available")
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=java_advanced_oop_project,
parsers=parsers,
queries=queries,
)
updater.run()
# Verify abstract and concrete classes were parsed
all_calls = mock_ingestor.ensure_node_batch.call_args_list
class_calls = [call for call in all_calls if call[0][0] == "Class"]
created_classes = {call[0][1]["qualified_name"] for call in class_calls}
project_name = java_advanced_oop_project.name
expected_classes = {
f"{project_name}.src.main.java.com.example.AbstractClasses.AbstractShape",
f"{project_name}.src.main.java.com.example.AbstractClasses.AbstractRectangle",
f"{project_name}.src.main.java.com.example.AbstractClasses.Rectangle",
f"{project_name}.src.main.java.com.example.AbstractClasses.Square",
}
assert expected_classes <= created_classes
def test_method_overloading_variations(
java_advanced_oop_project: Path,
mock_ingestor: MagicMock,
) -> None:
"""Test parsing of complex method overloading scenarios."""
test_file = (
java_advanced_oop_project
/ "src"
/ "main"
/ "java"
/ "com"
/ "example"
/ "MethodOverloading.java"
)
test_file.write_text(
"""
package com.example;
import java.util.*;
public class MethodOverloading {
// Basic overloading by parameter count
public void process() {
System.out.println("No parameters");
}
public void process(String value) {
System.out.println("String: " + value);
}
public void process(String value, int count) {
System.out.println("String and int: " + value + ", " + count);
}
// Overloading by parameter types
public void process(int value) {
System.out.println("int: " + value);
}
public void process(double value) {
System.out.println("double: " + value);
}
public void process(Integer value) {
System.out.println("Integer: " + value);
}
// Overloading with arrays vs varargs
public void process(String[] values) {
System.out.println("String array: " + Arrays.toString(values));
}
public void process(String... values) {
System.out.println("String varargs: " + Arrays.toString(values));
}
// Generic method overloading
public <T> void process(List<T> list) {
System.out.println("Generic list: " + list);
}
public void process(List<String> list) {
System.out.println("String list: " + list);
}
// Overloading with different generic bounds
public <T extends Number> void calculate(T value) {
System.out.println("Number: " + value);
}
public <T extends Comparable<T>> void calculate(T value, T other) {
System.out.println("Comparable: " + value + " vs " + other);
}
// Overloading constructors
public MethodOverloading() {
this("default");
}
public MethodOverloading(String name) {
this(name, 0);
}
public MethodOverloading(String name, int value) {
System.out.println("Constructor: " + name + ", " + value);
}
public MethodOverloading(int value, String name) {
System.out.println("Constructor (reversed): " + value + ", " + name);
}
}
"""
)
parsers, queries = load_parsers()
if "java" not in parsers:
pytest.skip("Java parser not available")
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=java_advanced_oop_project,
parsers=parsers,
queries=queries,
)
updater.run()
# Verify all overloaded methods were parsed
all_calls = mock_ingestor.ensure_node_batch.call_args_list
method_calls = [call for call in all_calls if call[0][0] == "Method"]
created_methods = {call[0][1]["qualified_name"] for call in method_calls}
# Check that multiple process methods were detected
process_methods = [qn for qn in created_methods if ".process" in qn]
calculate_methods = [qn for qn in created_methods if ".calculate" in qn]
constructor_methods = [qn for qn in created_methods if ".MethodOverloading" in qn]
assert len(process_methods) >= 3 # Should have multiple overloaded process methods
assert len(calculate_methods) >= 1 # Should have calculate methods
assert len(constructor_methods) >= 3 # Should have multiple constructors
def test_covariant_return_types(
java_advanced_oop_project: Path,
mock_ingestor: MagicMock,
) -> None:
"""Test parsing of methods with covariant return types."""
test_file = (
java_advanced_oop_project
/ "src"
/ "main"
/ "java"
/ "com"
/ "example"
/ "CovariantReturns.java"
)
test_file.write_text(
"""
package com.example;
class Animal {
public Animal reproduce() {
return new Animal();
}
public Animal[] getOffspring() {
return new Animal[]{new Animal()};
}
public Animal copy() {
return new Animal();
}
}
class Mammal extends Animal {
@Override
public Mammal reproduce() { // Covariant return type
return new Mammal();
}
@Override
public Mammal[] getOffspring() { // Covariant return array type
return new Mammal[]{new Mammal()};
}
@Override
public Mammal copy() {
return new Mammal();
}
}
class Dog extends Mammal {
@Override
public Dog reproduce() { // Further covariant return type
return new Dog();
}
@Override
public Dog[] getOffspring() {
return new Dog[]{new Dog(), new Dog()};
}
@Override
public Dog copy() {
return new Dog();
}
public Dog createPuppy() {
return new Dog();
}
}
// Generic covariant returns
abstract class Container<T> {
public abstract Container<T> copy();
public abstract T getContent();
}
class StringContainer extends Container<String> {
private String content;
@Override
public StringContainer copy() { // Covariant return
StringContainer copy = new StringContainer();
copy.content = this.content;
return copy;
}
@Override
public String getContent() {
return content;
}
}
class NumberContainer extends Container<Number> {
private Number content;
@Override
public NumberContainer copy() { // Covariant return
NumberContainer copy = new NumberContainer();
copy.content = this.content;
return copy;
}
@Override
public Number getContent() {
return content;
}
}
"""
)
parsers, queries = load_parsers()
if "java" not in parsers:
pytest.skip("Java parser not available")
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=java_advanced_oop_project,
parsers=parsers,
queries=queries,
)
updater.run()
# Verify all classes with covariant return types were parsed
all_calls = mock_ingestor.ensure_node_batch.call_args_list
class_calls = [call for call in all_calls if call[0][0] == "Class"]
created_classes = {call[0][1]["qualified_name"] for call in class_calls}
project_name = java_advanced_oop_project.name
expected_classes = {
f"{project_name}.src.main.java.com.example.CovariantReturns.Animal",
f"{project_name}.src.main.java.com.example.CovariantReturns.Mammal",
f"{project_name}.src.main.java.com.example.CovariantReturns.Dog",
f"{project_name}.src.main.java.com.example.CovariantReturns.Container",
f"{project_name}.src.main.java.com.example.CovariantReturns.StringContainer",
f"{project_name}.src.main.java.com.example.CovariantReturns.NumberContainer",
}
assert expected_classes <= created_classes
def test_diamond_problem_resolution(
java_advanced_oop_project: Path,
mock_ingestor: MagicMock,
) -> None:
"""Test parsing of diamond problem scenarios with default methods."""
test_file = (
java_advanced_oop_project
/ "src"
/ "main"
/ "java"
/ "com"
/ "example"
/ "DiamondProblem.java"
)
test_file.write_text(
"""
package com.example;
interface A {
default void method() {
System.out.println("A.method()");
}
void abstractMethod();
}
interface B extends A {
@Override
default void method() {
System.out.println("B.method()");
}
default void methodB() {
System.out.println("B.methodB()");
}
}
interface C extends A {
@Override
default void method() {
System.out.println("C.method()");
}
default void methodC() {
System.out.println("C.methodC()");
}
}
// Diamond problem: D extends both B and C
interface D extends B, C {
@Override
default void method() {
System.out.println("D.method() - resolving conflict");
B.super.method(); // Explicitly calling B's version
C.super.method(); // Explicitly calling C's version
}
}
public class DiamondImplementation implements D {
@Override
public void abstractMethod() {
System.out.println("DiamondImplementation.abstractMethod()");
}
// Inherits conflicting default methods, resolved by D
// Can optionally override the resolution
@Override
public void method() {
System.out.println("DiamondImplementation.method()");
D.super.method(); // Call interface's resolution
}
}
// More complex diamond with additional conflicts
interface Flyable {
default void move() {
System.out.println("Flying");
}
default String getMovementType() {
return "fly";
}
}
interface Swimmable {
default void move() {
System.out.println("Swimming");
}
default String getMovementType() {
return "swim";
}
}
interface Amphibious extends Flyable, Swimmable {
@Override
default void move() {
System.out.println("Can both fly and swim");
}
@Override
default String getMovementType() {
return "amphibious";
}
default void demonstrateMovement() {
System.out.println("Flying: ");
Flyable.super.move();
System.out.println("Swimming: ");
Swimmable.super.move();
}
}
public class Duck implements Amphibious {
@Override
public void move() {
System.out.println("Duck is moving");
Amphibious.super.move();
}
}
"""
)
parsers, queries = load_parsers()
if "java" not in parsers:
pytest.skip("Java parser not available")
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=java_advanced_oop_project,
parsers=parsers,
queries=queries,
)
updater.run()
# Verify all interfaces and classes in diamond hierarchy were parsed
all_calls = mock_ingestor.ensure_node_batch.call_args_list
interface_calls = [call for call in all_calls if call[0][0] == "Interface"]
class_calls = [call for call in all_calls if call[0][0] == "Class"]
created_interfaces = {call[0][1]["qualified_name"] for call in interface_calls}
created_classes = {call[0][1]["qualified_name"] for call in class_calls}
project_name = java_advanced_oop_project.name
expected_interfaces = {
f"{project_name}.src.main.java.com.example.DiamondProblem.A",
f"{project_name}.src.main.java.com.example.DiamondProblem.B",
f"{project_name}.src.main.java.com.example.DiamondProblem.C",
f"{project_name}.src.main.java.com.example.DiamondProblem.D",
f"{project_name}.src.main.java.com.example.DiamondProblem.Flyable",
f"{project_name}.src.main.java.com.example.DiamondProblem.Swimmable",
f"{project_name}.src.main.java.com.example.DiamondProblem.Amphibious",
}
expected_classes = {
f"{project_name}.src.main.java.com.example.DiamondProblem.DiamondImplementation",
f"{project_name}.src.main.java.com.example.DiamondProblem.Duck",
}
assert expected_interfaces <= created_interfaces
assert expected_classes <= created_classes
def test_nested_generic_bounds(
java_advanced_oop_project: Path,
mock_ingestor: MagicMock,
) -> None:
"""Test parsing of complex nested generic type bounds."""
test_file = (
java_advanced_oop_project
/ "src"
/ "main"
/ "java"
/ "com"
/ "example"
/ "NestedGenericBounds.java"
)
test_file.write_text(
"""
package com.example;
import java.util.*;
import java.util.function.*;
// Complex generic bounds with recursive types
public class NestedGenericBounds<T extends Comparable<T> & Cloneable> {
// Self-referencing generic bound
public static class Builder<B extends Builder<B>> {
protected String name;
@SuppressWarnings("unchecked")
public B setName(String name) {
this.name = name;
return (B) this;
}
public String getName() {
return name;
}
}
public static class PersonBuilder extends Builder<PersonBuilder> {
private int age;
public PersonBuilder setAge(int age) {
this.age = age;
return this;
}
public int getAge() {
return age;
}
}
// Deeply nested generic types
public Map<String, List<Set<Map<Integer, T>>>> complexNesting;
// Generic method with multiple complex bounds
public <U extends Number & Comparable<U>,
V extends Collection<U> & Serializable,
W extends Map<String, V> & Cloneable>
Optional<U> processComplexTypes(W data, Function<V, U> processor) {
return data.values().stream()
.map(processor)
.max(U::compareTo);
}
// Wildcard capture with complex bounds
public <T extends Enum<T> & Comparable<T>>
List<T> filterEnums(List<? extends T> enums, Predicate<? super T> filter) {
return enums.stream()
.filter(filter)
.collect(Collectors.toList());
}
// Recursive generic structure
public static class Node<T extends Node<T>> {
private T parent;
private List<T> children;
public Node() {
this.children = new ArrayList<>();
}
@SuppressWarnings("unchecked")
public T addChild(T child) {
children.add(child);
child.parent = (T) this;
return child;
}
public T getParent() {
return parent;
}
public List<T> getChildren() {
return new ArrayList<>(children);
}
}
public static class TreeNode extends Node<TreeNode> {
private String value;
public TreeNode(String value) {
super();
this.value = value;
}
public String getValue() {
return value;
}
}
}
// Generic interface with complex inheritance
interface Repository<T, ID extends Serializable> {
Optional<T> findById(ID id);
List<T> findAll();
T save(T entity);
void deleteById(ID id);
}
interface PagingRepository<T, ID extends Serializable> extends Repository<T, ID> {
List<T> findAll(int page, int size);
long count();
}
interface JpaRepository<T, ID extends Serializable> extends PagingRepository<T, ID> {
void flush();
T saveAndFlush(T entity);
List<T> saveAll(Iterable<T> entities);
}
// Complex generic implementation
public abstract class AbstractJpaRepository<T, ID extends Serializable>
implements JpaRepository<T, ID> {
protected Class<T> entityClass;
protected Class<ID> idClass;
@Override
public List<T> findAll() {
return new ArrayList<>();
}
@Override
public long count() {
return 0L;
}
@Override
public void flush() {
// Implementation
}
@Override
public List<T> saveAll(Iterable<T> entities) {