repo
stringlengths
1
191
file
stringlengths
23
351
code
stringlengths
0
5.32M
file_length
int64
0
5.32M
avg_line_length
float64
0
2.9k
max_line_length
int64
0
288k
extension_type
stringclasses
1 value
pmd
pmd-master/pmd-java/src/test/java/javasymbols/testdata/impls/GenericClass.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ package javasymbols.testdata.impls; public class GenericClass<T, F extends T> { public <U, O> void anOverload(int bb) { } public <U, O> void anOverload(int bb, String bachir) { } }
294
13.047619
79
java
pmd
pmd-master/pmd-java/src/test/java/javasymbols/testdata/impls/Overloads.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ package javasymbols.testdata.impls; public class Overloads { public void anOverload(int bb) { } public void anOverload(int bb, String bachir) { } }
261
11.47619
79
java
pmd
pmd-master/pmd-java/src/test/java/javasymbols/testdata/impls/WithSuperClass.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ package javasymbols.testdata.impls; import net.sourceforge.pmd.lang.ast.Node; public class WithSuperClass<T extends Node> extends GenericClass<T, T> { @Override public <U, O> void anOverload(int bb) { } public <U, O> void anOverload(int bb, String bachir, int other) { } }
391
15.333333
79
java
pmd
pmd-master/pmd-java/src/test/java/javasymbols/testdata/impls/SomeInnerClasses.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ package javasymbols.testdata.impls; public class SomeInnerClasses<O> { public static class StaticInner<O> { } public class Inner<T extends O> { } }
259
12.684211
79
java
pmd
pmd-master/pmd-java/src/test/java/javasymbols/testdata/impls/IdenticalToSomeFields.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ package javasymbols.testdata.impls; public class IdenticalToSomeFields { public final String foo = ""; protected volatile int bb; private int a; public final String foo() { return ""; } public interface Other { default int defaultMethod() { return 1; } } }
419
13.482759
79
java
pmd
pmd-master/pmd-java/src/test/java/javasymbols/testdata/impls/SomeFields.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ package javasymbols.testdata.impls; public class SomeFields { @Deprecated public final String foo = ""; private int a; protected volatile int bb; }
257
14.176471
79
java
pmd
pmd-master/pmd-java/src/test/java/javasymbols/testdata/impls/SomeMethodsNoOverloads.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ package javasymbols.testdata.impls; public class SomeMethodsNoOverloads { public final String foo() { return ""; } public interface Other { default int defaultMethod() { return 1; } } }
333
13.521739
79
java
pmd
pmd-master/pmd-java/src/test/java/javasymbols/testdata/impls/GenericClassCopy.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ package javasymbols.testdata.impls; import net.sourceforge.pmd.lang.ast.Node; public class GenericClassCopy<T, F extends Node> { public <U, O> void anOverload(int bb) { } public <U, O> void anOverload(int bb, String bachir) { } }
345
13.416667
79
java
pmd
pmd-master/pmd-java/src/test/java/javasymbols/testdata/impls/AnnotationTests.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ package javasymbols.testdata.impls; import net.sourceforge.pmd.lang.java.symbols.testdata.AnnotWithDefaults; public class AnnotationTests { @Deprecated public final String foo = ""; @Deprecated private AnnotationTests() { } @AnnotWithDefaults(valueNoDefault = "", stringArrayDefault = {}) public void someMethod(@AnnotWithDefaults(valueNoDefault = "", stringArrayDefault = {"oio"}) int formal) { } }
528
20.16
110
java
pmd
pmd-master/pmd-java/src/test/java/javasymbols/testdata/deep/StaticCollisionImport.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ package javasymbols.testdata.deep; import static javasymbols.testdata.StaticNameCollision.Ola; import static javasymbols.testdata.Statics.oha; public class StaticCollisionImport { Ola o; static { oha(); } }
321
15.1
79
java
pmd
pmd-master/pmd-java/src/test/java/javasymbols/testdata/deep/SomeClassA.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ package javasymbols.testdata.deep; public class SomeClassA { }
153
14.4
79
java
pmd
pmd-master/pmd-java/src/test/java/javasymbols/testdata/deep/SomewhereElse.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ package javasymbols.testdata.deep; import javasymbols.testdata.SomeClassA; import javasymbols.testdata.Thread; public class SomewhereElse { SomeClassA a; Thread b; }
266
15.6875
79
java
pmd
pmd-master/pmd-java/src/test/java/javasymbols/testdata/deep/AClassWithLocals.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ package javasymbols.testdata.deep; /** * @author Clément Fournier */ public class AClassWithLocals { private static Object foo0 = new Object() {}; // AClassWithLocals$0 private final Object foo1 = new Object() { class Member {} // AClassWithLocals$1$Member }; // AClassWithLocals$1 public AClassWithLocals() { class Local { // AClassWithLocals$0Local } new Object() {}; // // AClassWithLocals$2 } static { class Local { // AClassWithLocals$1Local } new Object() {}; // // AClassWithLocals$3 } void method() { class Local { // AClassWithLocals$2Local } new Object() {}; // // AClassWithLocals$4 } }
813
20.421053
79
java
pmd
pmd-master/pmd-java/src/test/java/javasymbols/testdata/deep/StaticIOD2.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ package javasymbols.testdata.deep; import static javasymbols.testdata.Statics.*; import javasymbols.testdata.SomeClassA; public class StaticIOD2 { }
241
16.285714
79
java
pmd
pmd-master/pmd-java/src/test/java/javasymbols/testdata/deep/TypeImportsOnDemand.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ package javasymbols.testdata.deep; // SomeClassB is imported // SomeClassA is imported but shadowed by same package scope // TestCase1 is imported but shadowed by single type import // Thread is shadowed by java.lang.Thread and not imported import javasymbols.testdata.*; import javasymbols.testdata.TestCase1; public class TypeImportsOnDemand { SomeClassA a; }
458
23.157895
79
java
pmd
pmd-master/pmd-java/src/test/java/javasymbols/testdata/deep/StaticImportOnDemand.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ package javasymbols.testdata.deep; import static javasymbols.testdata.Statics.*; public class StaticImportOnDemand { }
209
18.090909
79
java
pmd
pmd-master/pmd-java/src/test/java/javasymbols/testdata/deep/StaticContainer.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ package javasymbols.testdata.deep; public class StaticContainer { /** * Shadows a java.lang member */ public static class Exception { } }
251
14.75
79
java
pmd
pmd-master/pmd-java/src/test/java/javasymbols/testdata/deep/OuterWithoutDollar.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ package javasymbols.testdata.deep; /** * @author Clément Fournier */ public class OuterWithoutDollar { final class Inner {} }
222
14.928571
79
java
pmd
pmd-master/pmd-java/src/test/java/javasymbols/testdata/deep/ClassWithDollar$.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ package javasymbols.testdata.deep; /** * @author Clément Fournier */ public class ClassWithDollar$ { public static final Object MODULE$ = null; }
242
16.357143
79
java
pmd
pmd-master/pmd-java/src/test/java/javasymbols/testdata/deep/PublicShadowed.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ package javasymbols.testdata.deep; public class PublicShadowed { }
157
14.8
79
java
pmd
pmd-master/pmd-java/src/test/java/javasymbols/testdata/deep/Another$ClassWith$Dollar.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ package javasymbols.testdata.deep; /** * @author Clément Fournier */ public class Another$ClassWith$Dollar { public static class AnInner$ClassWithDollar { public static class ADeeper$ClassWithDollar { } } // looks like an anonymous class but isn't. public static class DollarsAndNumbers$0 { } }
427
16.833333
79
java
pmd
pmd-master/pmd-java/src/test/resources/ant/java/PMDTaskTestExample.java
package ant.java; public class PMDTaskTestExample { // must be 100+ lines long public void veryLongMethod() { int a; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; a -= 1; a += 1; } private int testField; // this will trigger a "Fields should be declared at top" }
1,812
15.481818
84
java
pmd
pmd-master/pmd-java/src/test/resources/ant/java/EncodingTestClass.java
public class EncodingTestClass { public static void main(String[] args) { // UnusedLocalVariable: the rule violation message includes the variable name // so, the encoding matters // NOTE: This file is stored with encoding windows-1252 or cp1252 // The Umlaut &Uuml; has codepoint U+00DC, which is the same in cp1252 and iso-8859-1 String someVariableWithmlaut = "unused"; } }
425
41.6
93
java
pmd
pmd-master/pmd-java/src/test/resources/ant/java/MoreThanThousandLinesOfCodeWithDuplicateLiterals.java
package org.example.pmd; /** * This class with * <a href="https://pmd.github.io/pmd-5.5.2/pmd-java/rules/java/strings.html#AvoidDuplicateLiterals">duplicated litterals</a> * after more than one thousand lines should generate a warning, ending with a line number greater than 1000. * * This number is formatted with the thousand separator, and given the platform's locale, it may or not be in the ASCII * subset. For instance: * <ul> * <li>en-US: u002c, comma, included in the ASCII subset. * <code>The String literal &quot;duplicated literal&quot; appears 4 times in this file; the first occurrence is on line 1,023</code> * </li> * <li>fr-FR: u00a0, unbreakable space, not part of the ASCII subset. * <code>The String literal &quot;duplicated literal&quot; appears 4 times in this file; the first occurrence is on line 1&#160;023</code> * </li> * </ul> * * @author Olivier Parent */ public class MoreThanThousandLinesOfCodeWithDuplicateLiterals { /* 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 */ protected String s1 = "duplicated literal"; protected String s2 = "duplicated literal"; protected String s3 = "duplicated literal"; protected String s4 = "duplicated literal"; }
5,067
3.934761
141
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/cli/EmptyIfStatement.java
/** * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ /** * Source code file used for * {@link net.sourceforge.pmd.cli.CLITest} */ public class EmptyIfStatement { public void foo() { if (1 == 2) { } } }
256
18.769231
79
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/GitHubBug650.java
package com.cb4.common.utils; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.NavigableMap; import java.util.Objects; import java.util.Properties; import java.util.Random; import java.util.TreeMap; import java.util.function.Predicate; import org.apache.commons.collections4.Transformer; import org.apache.commons.lang3.math.NumberUtils; import org.apache.commons.lang3.tuple.Pair; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runners.MethodSorters; import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import com.cb4.common.test.JUnitTestSupport; @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class MapUtilsTest extends JUnitTestSupport { public MapUtilsTest() { super(); } @Test public void testPutUniqueValuesForNoSource() { @SuppressWarnings("unchecked") Map<Object, Object>[] maps = new Map[] { null, Collections.emptyMap() }; for (Map<Object, Object> dst : maps) { for (Map<?, ?> src : maps) { assertSame("Mismatched result for src=" + src + ", dst=" + dst, dst, MapUtils.putUniqueValues(src, dst)); } } } @Test(expected = IllegalStateException.class) public void testPutUniqueValuesWithDuplicates() { Map<Object, Object> dst = new HashMap<>(Collections.singletonMap("test", getCurrentTestName())); assertSame("Mismatched result for self update", dst, MapUtils.putUniqueValues(dst, dst)); for (Map.Entry<?, ?> de : dst.entrySet()) { Map<?, ?> result = MapUtils.putUniqueValues(Collections.singletonMap(de.getKey(), de.getValue()), dst); fail("Unexpected success for entry=" + de + ": " + result); } } @Test public void testPutUniqueValues() { final Class<?> anchor = getClass(); Map<String, String> extra = new TreeMap<String, String>() { // Not serializing it private static final long serialVersionUID = 1L; { put("class", anchor.getSimpleName()); put("package", anchor.getPackage().getName()); put("test", getCurrentTestName()); } }; Map<String, String> original = Collections.singletonMap("now", new Date().toString()); Map<String, String> dest = new HashMap<>(original); // NOTE: we use a hash map on purpose so we get a map with different keys order than original Map<String, String> merged = MapUtils.putUniqueValues(extra, dest); assertSame("Not same destination result", dest, merged); assertEquals("Mismatched merged size", extra.size() + original.size(), merged.size()); for (@SuppressWarnings("unchecked") Map<String, String> m : new Map[] { original, extra }) { for (Map.Entry<String, String> me : m.entrySet()) { String key = me.getKey(); String expected = me.getValue(); String actual = merged.get(key); assertEquals("Mismatched merged result for key=" + key, expected, actual); } } } @Test public void testSort() { Map<Integer, String> map = new HashMap<>(); map.put(5, "aa"); map.put(1, "ss"); map.put(3, "ww"); Map<Integer, String> actualMap = MapUtils.sort(map); Map<Integer, String> expectedMap = new LinkedHashMap<>(); expectedMap.put(1, "ss"); expectedMap.put(3, "ww"); expectedMap.put(5, "aa"); assertEquals(expectedMap, actualMap); } @Test public void testAddValuesToMap() { Map<Integer, ArrayList<String>> map = new HashMap<>(); MapUtils.addValueToMapList(map, 5, "aa"); assertTrue("Missing '5' key", map.containsKey(5)); assertTrue("Missing 'aa' sub-key", map.get(5).contains("aa")); MapUtils.addValueToMapList(map, 5, "bb"); assertTrue("Missing 2nd 'aa' sub-key", map.get(5).contains("aa")); assertTrue("Missing 'bb' sub-key", map.get(5).contains("bb")); MapUtils.addValueToMapList(map, 1, "aa"); assertTrue("Missing 'aa' sub-key of '1'", map.get(1).contains("aa")); } @Test public void testSortKeys() { Map<Integer, String> map = new HashMap<>(); map.put(5, "aa"); map.put(1, "ss"); map.put(3, "ww"); Map<Integer, String> actualMap = MapUtils.sort(map); Map<Integer, String> expectedMap = new LinkedHashMap<>(); expectedMap.put(1, "ss"); expectedMap.put(3, "ww"); expectedMap.put(5, "aa"); assertEquals(expectedMap, actualMap); } @Test public void testValuesSort() { Map<Integer, String> map = new HashMap<>(); map.put(5, "aa"); map.put(1, "ss"); map.put(3, "ww"); Map<Integer, String> actualMap = MapUtils.sort(map); Map<Integer, String> expectedMap = new LinkedHashMap<>(); expectedMap.put(5, "aa"); expectedMap.put(1, "ss"); expectedMap.put(3, "ww"); assertEquals(expectedMap, actualMap); } @Test public void testRandomSortByValue() { Random random = new Random(System.currentTimeMillis()); Map<String, Integer> testMap = new HashMap<>(1000); for (int i = 0; i < 1000; ++i) { testMap.put("SomeString" + random.nextInt(), random.nextInt()); } testMap = MapUtils.sortByValues(testMap); assertEquals(1000, testMap.size()); Integer previous = null; for (Map.Entry<String, Integer> entry : testMap.entrySet()) { assertNotNull(entry.getValue()); if (previous != null) { assertTrue(entry.getValue() >= previous); } previous = entry.getValue(); } } @Test public void testByKeyComparator() { Map<String, Object> original = new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER) { // not serializing it private static final long serialVersionUID = 1L; { Class<?> anchor = getCurrentTestClass(); put("class", anchor); put("package", anchor.getPackage()); put("method", getCurrentTestName()); put("now", new Date()); put("nanos", Long.valueOf(System.nanoTime())); put("pi", Math.PI); } }; List<Map.Entry<String, Object>> expected = new ArrayList<>(original.entrySet()); Map<String, Object> hashed = new HashMap<>(original); List<Map.Entry<String, Object>> actual = new ArrayList<>(hashed.entrySet()); Comparator<Map.Entry<String, Object>> comp = MapUtils.byKeyComparator(String.CASE_INSENSITIVE_ORDER); Collections.sort(actual, comp); assertListEquals(getCurrentTestName(), expected, actual); } @Test public void testFlip() { Map<String, Long> src = new TreeMap<String, Long>() { private static final long serialVersionUID = -3686693573082540693L; { put("sysTime", Long.valueOf(7365L)); put("nanoTime", Long.valueOf(3777347L)); } }; Map<Number, CharSequence> dst = MapUtils.flip(false, src, new HashMap<Number, CharSequence>(src.size())); assertEquals("Mismatched size", src.size(), dst.size()); for (Map.Entry<String, Long> se : src.entrySet()) { String expected = se.getKey(); Long value = se.getValue(); CharSequence actual = dst.remove(value); assertSame("Mismatched key for value=" + value, expected, actual); } } @Test public void testFlipNullOrEmpty() { Map<Object, Object> dst = Collections.unmodifiableMap(new HashMap<>()); assertSame("Mismatached instance for null source", dst, MapUtils.flip(false, null, dst)); assertSame("Mismatached instance for empty source", dst, MapUtils.flip(false, Collections.emptyMap(), dst)); } @Test(expected = IllegalStateException.class) public void testFlipOnDuplicateKeys() { Map<String, String> map = new HashMap<>(); map.put("testName1", getCurrentTestName()); map.put("testName2", getCurrentTestName()); Map<String, String> flipped = MapUtils.flip(false, map, new TreeMap<String, String>()); fail("Unexpected success: " + flipped); } @Test public void testGetMandatoryValue() { Map<String, Object> map = new HashMap<>(); map.put("A", 1); map.put("B", 2); for (Map.Entry<String, ?> me : map.entrySet()) { String key = me.getKey(); Object expected = me.getValue(); Object actual = MapUtils.getMandatoryValue(map, key); assertSame("Mismatched mandatory value instance for key=" + key, expected, actual); } } @Test(expected = NullPointerException.class) public void testGetMandatoryValueWithoutValue() { Map<String, Object> map = new HashMap<>(); map.put("A", 1); map.put("B", 2); Object mandatoryValue = MapUtils.getMandatoryValue(map, getCurrentTestName()); fail("Unexpected success: " + mandatoryValue); } @Test(expected = NullPointerException.class) public void testGetMandatoryValueWithEmptyMap() { Map<String, Object> map = new HashMap<>(); Object mandatoryValue = MapUtils.getMandatoryValue(map, getCurrentTestName()); fail("Unexpected success: " + mandatoryValue); } @Test public void testGetMandatoryIntValue() { Map<String, Object> map = new HashMap<>(); map.put("A", 1); map.put("B", "2"); int mandatoryIntValueA = MapUtils.getMandatoryIntValue(map, "A"); assertEquals("Mismatched value for the 'A' key", 1, mandatoryIntValueA); int mandatoryIntValueB = MapUtils.getMandatoryIntValue(map, "B"); assertEquals("Mismatched value for the 'B' key", 2, mandatoryIntValueB); } @Test public void testGetMandatoryDoubleValue() { Map<String, Object> map = new HashMap<>(); map.put("A", 1.2); map.put("B", "2.2"); double mandatoryIntValueA = MapUtils.getMandatoryDoubleValue(map, "A"); assertEquals("Mismatched value for the 'A' key", 1.2, mandatoryIntValueA, 0); double mandatoryIntValueB = MapUtils.getMandatoryDoubleValue(map, "B"); assertEquals("Mismatched value for the 'B' key", 2.2, mandatoryIntValueB, 0); } @Test public void testNestedLen() { Map<String, Collection<Object>> mapOfColls = new HashMap<>(); Collection<Object> col1 = new ArrayList<>(); col1.add(1L); col1.add(2L); Collection<Object> col2 = new LinkedList<>(); col2.add(1L); col2.add(2L); col2.add(3L); mapOfColls.put("a", col1); mapOfColls.put("b", col2); assertEquals("Mismatched nested entries count", 5, MapUtils.nestedLen(mapOfColls)); } @Test public void testEmptyNestedLen() { assertEquals("Mismatched empty nested entries count", 0, MapUtils.nestedLen(new HashMap<String, Collection<Object>>())); } @Test public void testNullNestedLen() { assertEquals("Mismatched null 0 nested entries count", 0, MapUtils.nestedLen((Map<String, Collection<Object>>) null)); } @Test public void testClearNullOrEmptyMap() { assertNull("Unexpected null map clear result", MapUtils.clear(null)); @SuppressWarnings("unchecked") Map<String, String> expected = Mockito.mock(Map.class); Mockito.when(expected.size()).thenReturn(NumberUtils.INTEGER_ZERO); Mockito.when(expected.isEmpty()).thenReturn(Boolean.TRUE); Mockito.when(expected.toString()).thenReturn(getCurrentTestName()); Mockito.doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { fail("Unexpected clear method invocation"); return null; } }).when(expected).clear(); Map<String, String> actual = MapUtils.clear(expected); assertSame("Mismatched cleared result reference", expected, actual); } @Test public void testClearNonEmptyMap() { Map<String, String> expected = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER) { // Not serializing it private static final long serialVersionUID = 1L; { put("testName", getCurrentTestName()); put("className", SafeUtils.safeShortName(getCurrentTestClass())); } }; Map<String, String> actual = MapUtils.clear(expected); assertSame("Mismatched cleared result reference", expected, actual); assertTrue("Map not cleared", actual.isEmpty()); assertEquals("Size not zeroed", 0, actual.size()); } @Test public void testClearAndReplace() { for (@SuppressWarnings("unchecked") Map<String, String> expected : new Map[] { null, Collections.emptyMap(), new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER) { // Not serializing it private static final long serialVersionUID = 1L; { put("testName", getCurrentTestName()); put("className", SafeUtils.safeShortName(getCurrentTestClass())); } } }) { Map<String, String> original = new HashMap<String, String>() { // Not serializing it private static final long serialVersionUID = 1L; { put("expected", Objects.toString(expected)); } }; Map<String, String> actual = MapUtils.clearAndReplace(original, expected); assertTrue("Original not cleared: " + original, original.isEmpty()); assertSame("Mismatched replacement", expected, actual); } } @Test public void testKeyExtractor() { Map<String, Object> map = new TreeMap<String, Object>() { // Not serializing it private static final long serialVersionUID = 1L; { put("testName", getCurrentTestName()); put("className", getCurrentTestClass()); } }; Transformer<Map.Entry<String, Object>, String> keyExtractor = MapUtils.keyExtractor(); for (Map.Entry<String, Object> entry : map.entrySet()) { String expected = entry.getKey(); String actual = keyExtractor.transform(entry); assertSame(expected, actual); } } @Test public void testValueExtractor() { Map<String, Object> map = new TreeMap<String, Object>() { // Not serializing it private static final long serialVersionUID = 1L; { put("testName", getCurrentTestName()); put("className", getCurrentTestClass()); } }; Transformer<Map.Entry<String, Object>, Object> valueExtractor = MapUtils.valueExtractor(); for (Map.Entry<String, Object> entry : map.entrySet()) { String key = entry.getKey(); Object expected = entry.getValue(); Object actual = valueExtractor.transform(entry); assertSame(key, expected, actual); } } @Test public void testFilterByKey() { String expectedKey = "testName"; Map<String, Object> map = new HashMap<String, Object>() { // Not serializing it private static final long serialVersionUID = 1L; { put(expectedKey, getCurrentTestName()); put("className", getCurrentTestClass()); } }; Map.Entry<String, Object> expEntry = Pair.of(expectedKey, map.get(expectedKey)); NavigableMap<String, Object> actual = MapUtils.filterByKey(map, k -> expectedKey.equals(k), TreeMap::new); assertMapEquals(getCurrentTestName(), Collections.singletonMap(expEntry.getKey(), expEntry.getValue()), actual); Map.Entry<String, Object> actEntry = SafeUtils.head(actual.entrySet()); assertSame("Mismatched key instance", expEntry.getKey(), actEntry.getKey()); assertSame("Mismatched value instance", expEntry.getValue(), actEntry.getValue()); } @Test public void testFilterByEntry() { Map<Integer, Integer> map = new HashMap<>(); for (int index = 1; index <= Byte.SIZE; index++) { map.put(index, index & 0x01); } Map<Integer, Integer> expected = new HashMap<>(); Predicate<Map.Entry<Integer, Integer>> selector = e -> (e.getKey() > 3) && (e.getKey() <= 7) && ((e.getValue() & 0x01) == 0); map.forEach((key, value) -> { if (selector.test(Pair.of(key, value))) { expected.put(key, value); } }); Map<Integer, Integer> actual = MapUtils.filterByEntry(map, selector, TreeMap::new); assertMapEquals(getCurrentTestName(), expected, actual); } @Test public void testEqualPropertiesCaseSensitiveName() { Properties p1 = new Properties() { // Not serializing it private static final long serialVersionUID = 1L; { setProperty(getCurrentTestName(), getCurrentTestName()); Class<?> c = getCurrentTestClass(); setProperty(c.getSimpleName(), c.getSimpleName()); Package pkg = c.getPackage(); setProperty(pkg.getName(), pkg.getName()); } }; Properties p2 = new Properties(); for (String name : p1.stringPropertyNames()) { String key = shuffleCase(name); String value = p1.getProperty(name); p2.setProperty(key, value); } assertFalse("Unexpected case sensitive equality", MapUtils.equalProperties(p1, p2)); assertTrue("Non-equal case insensitive result", MapUtils.equalProperties(p1, p2, String.CASE_INSENSITIVE_ORDER, Comparator.naturalOrder())); } @Test public void testEqualPropertiesCaseSensitiveValue() { Properties p1 = new Properties() { // Not serializing it private static final long serialVersionUID = 1L; { setProperty(getCurrentTestName(), getCurrentTestName()); Class<?> c = getCurrentTestClass(); setProperty(c.getSimpleName(), c.getSimpleName()); Package pkg = c.getPackage(); setProperty(pkg.getName(), pkg.getName()); } }; Properties p2 = new Properties(); for (String key : p1.stringPropertyNames()) { String value = p1.getProperty(key); p2.setProperty(key, shuffleCase(value)); } assertFalse("Unexpected case sensitive equality", MapUtils.equalProperties(p1, p2)); assertTrue("Non-equal case insensitive result", MapUtils.equalProperties(p1, p2, Comparator.naturalOrder(), String.CASE_INSENSITIVE_ORDER)); } @Test public void testSubMapWithEmptyMaps() { Map<String, Integer> m1 = new HashMap<>(); m1.put(getCurrentTestName(), 1); Collection<Map<String, Integer>> empties = Arrays.asList(null, Collections.emptyMap()); for (Map<String, Integer> empty : empties) { assertTrue("empty is submap of any map", MapUtils.isSubMap(m1, empty)); assertFalse("nothing is a submap of empty", MapUtils.isSubMap(empty, m1)); assertTrue("empty is submap of empty", MapUtils.isSubMap(empty, empty)); } } @Test public void testSubMapWithSubs() { Map<String, Integer> m1 = new HashMap<>(); m1.put(getCurrentTestName(), 1); Map<String, Integer> m2 = new HashMap<>(); m2.put(getCurrentTestName(), 1); assertTrue("equal maps are sub maps", MapUtils.isSubMap(m1, m2)); assertTrue("equal maps are sub maps", MapUtils.isSubMap(m2, m1)); m1.put(JAVA_SUFFIX, 2); assertTrue("strictly sub", MapUtils.isSubMap(m1, m2)); assertFalse("non sub", MapUtils.isSubMap(m2, m1)); int differentValueForKey = m1.get(JAVA_SUFFIX) + 1; m2.put(JAVA_SUFFIX, differentValueForKey); assertFalse("non sub", MapUtils.isSubMap(m1, m2)); assertFalse("non sub", MapUtils.isSubMap(m2, m1)); } @Test public void testGetSubMap() { Map<String, Integer> hashMap = new HashMap<>(); hashMap.put(getCurrentTestName(), 1); hashMap.put(JAVA_SUFFIX, 2); hashMap.put(JAVA_TYPE, 3); NavigableMap<String, Integer> expectedSingletonMap = MapUtils.getSubMap(hashMap, e -> e.getValue() > 2, TreeMap::new); assertEquals(Collections.singletonMap(JAVA_TYPE, 3), expectedSingletonMap); Map<String, Integer> treeMap = new TreeMap<>(hashMap); Map<String, Integer> expectedEmpty = MapUtils.getSubMap(treeMap, e -> e.getValue() > 3, HashMap::new); assertEquals(Collections.emptyMap(), expectedEmpty); Map<String, Integer> expectedAllEntries = MapUtils.getSubMap(treeMap, e -> e.getValue() < 10, HashMap::new); assertEquals(treeMap, expectedAllEntries); } @Test public void testGetSubMapEmpty() { assertEquals(Collections.emptyMap(), MapUtils.getSubMap(Collections.emptyMap(), e -> true, TreeMap::new)); assertEquals(Collections.emptyMap(), MapUtils.getSubMap(null, e -> true, HashMap::new)); } }
22,073
36.797945
148
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/GitHubBug207.java
public class GitHubBug207 { private static HttpMessageWriter<Resource> resourceHttpMessageWriter(BodyInserter.Context context) { return context.map(BodyInserters::<Resource>cast); } }
199
39
104
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/MethodReferenceConfused.java
import java.security.AccessController; import java.security.PrivilegedAction; public class MethodReferenceConfused { public void wrongVariableAccessor() { Object method = null; Object someObject = null; String result = AccessController.doPrivileged( (PrivilegedAction<String>) ((I) someObject)::method); } interface I { String method(); } }
405
21.555556
65
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/UnicodeIdentifier.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ // https://github.com/pmd/pmd/issues/3423 package com.example.pmdtest; public class PmdTest { private static final int lᵤ = 1; private static final int μᵤ = 2; public static void main(String[] args) { System.out.println(lᵤ + μᵤ); } } enum CodeSet { START_CODE_A('Ë'), START_CODE_B('Ì'), START_CODE_C('Í'), A_TILDE('\u00c3'), STOP_CODE('Î'); private final char encoding; CodeSet(final char encoding) { this.encoding = encoding; } public char getEncoding() { return encoding; } }
656
16.289474
79
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/ParserCornerCases17.java
/** * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.zip.ZipEntry; /* * This file is to test the JavaCC java grammer, whether we can parse specific java constructs without * throwing a syntax error. * * Java 7, see: http://docs.oracle.com/javase/7/docs/technotes/guides/language/enhancements.html#javase7 */ public class ParserCornerCases17 { public ParserCornerCases17() { super(); } public void binaryLiterals() { // An 8-bit 'byte' value: byte aByte = (byte)0b00100001; // A 16-bit 'short' value: short aShort = (short)0b1010000101000101; // Some 32-bit 'int' values: int anInt1 = 0b00000000100000000010100001010001; int anInt2 = 0b101; int anInt3 = 0B101; // The B can be upper or lower case. // A 64-bit 'long' value. Note the "L" suffix: long aLong = 0b0000000000000000000000000000000000000000100010001000010000010000L; int[] phases = { 0b00110001, 0b01100010, 0b11000100, 0b10001001, 0b00010011, 0b00100110, 0b01001100, 0b10011000 }; int instruction = 0; if ((instruction & 0b11100000) == 0b00000000) { final int register = instruction & 0b00001111; switch (instruction & 0b11110000) { case 0b00000000: break; case 0b00010000: break; case 0b00100000: break; case 0b00110000: break; case 0b01000000: break; case 0b01010000: break; case 0b01100000: break; case 0b01110000: break; default: throw new IllegalArgumentException(); } } } public void underscoreInNumericLiterals() { long creditCardNumber = 1111_2222_3333_4444L; long socialSecurityNumber = 999_99_9999L; float pi = 3.14_15F; long hexBytes = 0x00_11_22_33; long hexWords = 0x0001_CAFE; long maxLong = 0x7fff_ffff_ffff_ffffL; byte nybbles = 0b0010_0101; long bytes = 0b00000000_10001000_10000100_00010000; int _52 = 1; int x1 = _52; // This is an identifier, not a numeric literal int x2 = 5_2; // OK (decimal literal) int x4 = 5_______2; // OK (decimal literal) int x7 = 0x5_2; // OK (hexadecimal literal) int x9 = 0_52; // OK (octal literal) int x10 = 05_2; // OK (octal literal) } public String stringsInSwitchStatements() { String dayOfWeekArg = "Wednesday"; String typeOfDay; switch (dayOfWeekArg) { case "Monday": typeOfDay = "Start of work week"; break; case "Tuesday": case "Wednesday": case "Thursday": typeOfDay = "Midweek"; break; case "Friday": typeOfDay = "End of work week"; break; case "Saturday": case "Sunday": typeOfDay = "Weekend"; break; default: throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg); } return typeOfDay; } class MyClass<X> { <T> MyClass(T t) { } } public void typeInferenceForGenericInstanceCreation() { Map<String, List<String>> myMap = new HashMap<>(); List<String> list = new ArrayList<>(); list.add("A"); List<? extends String> list2 = new ArrayList<>(); list.addAll(list2); MyClass<Integer> myObject = new MyClass<>(""); } public void theTryWithResourcesStatement() throws IOException { String path = "/foo"; try (BufferedReader br = new BufferedReader(new FileReader(path))) { String first = br.readLine(); } // Two resources declared String outputFileName = "/foo-out"; String zipFileName = "/foo.zip"; java.nio.charset.Charset charset = java.nio.charset.Charset.forName("US-ASCII"); java.nio.file.Path outputFilePath = java.nio.file.Paths.get(outputFileName); // Open zip file and create output file with try-with-resources statement try ( java.util.zip.ZipFile zf = new java.util.zip.ZipFile(zipFileName); java.io.BufferedWriter writer = java.nio.file.Files.newBufferedWriter(outputFilePath, charset) ) { // Enumerate each entry for (Enumeration<? extends ZipEntry> entries = zf.entries(); entries.hasMoreElements();) { // Get the entry name and write it to the output file String newLine = System.getProperty("line.separator"); String zipEntryName = ((java.util.zip.ZipEntry)entries.nextElement()).getName() + newLine; writer.write(zipEntryName, 0, zipEntryName.length()); } } } public void catchingMultipleExceptionTypes() throws IOException, SQLException { try { if (new File("foo").createNewFile()) { throw new SQLException(); } } catch (IOException|SQLException ex) { ex.printStackTrace(); throw ex; } } // With java 8 lambda grammar enhancement, this caused a problem, to not be identified as lambda... public void expressionInCastExpression() { // grammar/parser: don't get confused with this... int initialSizeGlobal = (int) (profilingContext.m_profileItems.size() * (150.0 * 0.30)); } }
5,363
28.635359
104
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/EmptyStmts2.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ package c;; import a; import b;
119
23
79
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/InfiniteLoopInLookahead.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ import java.util.*; public class InfiniteLoopInLookahead { public void exam1(List resList) { resList.forEach(a -> { resList.forEach(b -> { resList.forEach(c -> { resList.forEach(d -> { resList.forEach(e -> { resList.forEach(f -> { resList.forEach(g -> { resList.forEach(h -> { resList // note: missing semicolon -> parse error here... }); }); }); }); }); }); }); }); } }
861
28.724138
97
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/ParserCornerCases.java
/** * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ /* * This file is to test the JavaCC java grammer, whether we can parse specific java constructs without * throwing a syntax error. */ class Superclass { public Superclass() { } public <V> Superclass(Class<V> clazz) { } <T> T doStuff(T s) { return s; } } class Outer { Outer() { System.out.println("Outer constructor"); } class Inner { Inner() { System.out.println("Inner constructor"); } } } class Child extends Outer.Inner { Child(Outer o) { o.super(); System.out.println("Child constructor"); } } public class ParserCornerCases extends Superclass { public ParserCornerCases() { super(); } public ParserCornerCases(int a) { <Integer> this(a, 2); } public <W> ParserCornerCases(int a, int b) { <String> super(String.class); } public ParserCornerCases(String title) { this(); } public strictfp void testGeneric() { String o = super.<String> doStuff("foo"); String v = this.<String> thisGeneric("bar"); } <X> X thisGeneric(X x) { return x; } Class getByteArrayClass() { return (byte[].class); } public void bitwiseOperator() { if ((modifiers & InputEvent.SHIFT_DOWN_MASK) != 0 ) { buf.append("shift "); } } } /** * Test case from http://jira.codehaus.org/browse/MPMD-126 */ class PmdTestParent { public PmdTestParent(Object obj) {} } class PmdTestChild extends PmdTestParent { public PmdTestChild() { // the following line produced a parsing problem super(new Object() { public Object create() { Object memoryMonitor = null; if (memoryMonitor == null) { memoryMonitor = new Object(); } return memoryMonitor; } }); } } /* * Test cases for bug #1020 Parsing Error */ class SimpleBean { String name; } class SimpleBeanUser { SimpleBeanUser(SimpleBean o) { } SimpleBeanUser() { this(new SimpleBean() {{ name = "test"; }}); } } class SimpleBeanUser2 extends SimpleBeanUser { SimpleBeanUser2() { super(new SimpleBean(){{ name = "test2"; }}); } } /* * Test case for bug #1007 Parse Exception with annotation */ class TestParseAnnototation { void parse() { for (@SuppressWarnings("unchecked") int i = 0; i < 10; i++) { } for (@SuppressWarnings("unchecked") Iterator it = Fachabteilung.values().iterator(); it.hasNext();) { } List<String> l = new ArrayList<String>(); for (@SuppressWarnings("unchecked") String s : l) { } } } /* * Test case for bug #956 PMD Parse Exception */ class FooBlock {} class MyFoo { MyFoo(FooBlock b) { } } class Foo extends MyFoo { public Foo() { super(new FooBlock() { public Object valueOf(Object object) { String fish = "salmon"; return fish; } }); } } /* * Verifies #1122 parse error at class.super */ class SuperTest { /** * @throws UnsupportedOperationException */ public Iterator<E> iterator() { if (this.mods.contains(Modification.Iterator)) { return new Iterator<E>() { Iterator<E> wrapped = ImmutableSet.super.iterator(); public boolean hasNext() { return this.wrapped.hasNext(); } public E next() { return this.wrapped.next(); } public void remove() { if (ImmutableSet.this.mods.contains(Modification.RemoveIter)) { this.wrapped.remove(); } throw new UnsupportedOperationException(); } }; } throw new UnsupportedOperationException(); } } /* * Test case for #1310 PMD cannot parse int.class */ class ClazzPropertyOfPrimitiveTypes { public void test() { Class<?> c = int.class; c = short.class; c = long.class; c = float.class; c = double.class; c = char.class; c = byte.class; c = void.class; if (c == int.class || c == short.class || c == long.class || c == double.class || c == char.class || c == byte.class || c == void.class) { } if ("a".equals((int.class).getName())) { } if ((Integer.class.equals(clazz)) || (int.class.equals(clazz))) { } } }
4,510
19.048889
146
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/GitHubBug208.java
public class GitHubBug208 { public void testMethod() { @Lazy @Configuration class LocalClass { @Bean Object foo() { return null; } } } }
204
17.636364
32
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/Bug1429.java
public class Bug1429 { public Set<String> getAttributeTuples() { return (Set<String>) (this.attributes == null ? Collections.<String> emptySet() : new HashSet<String>( CollectionUtils.collect(this.attributes.keySet(), new Transformer() { @Override public Object transform(final Object obj) { final String key = (String) obj; final String value = HGXLIFFTypeConfiguration.this.attributes.get(key); String result = key; if (StringUtils.isNotEmpty(value)) { result = result.concat(":").concat(value); } return result; } }))); } }
808
41.578947
110
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/LambdaBug1333.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ final class Bug1333 { private static final Logger LOG = LoggerFactory.getLogger(Foo.class); public void deleteDirectoriesByNamePattern() { delete(path -> deleteDirectory(path)); } private void delete(Consumer<? super String> consumer) { LOG.debug(consumer.toString()); } private void deleteDirectory(String path) { LOG.debug(path); } }
479
23
79
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/LambdaBug2783.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ import java.util.List; public class LambdaBug2783 { // https://github.com/pmd/pmd/issues/2783 public Spec<String> test() { // cast, block body (the failing case) Spec<String> result = (Spec<String>) (a, b) -> { return a.toArray(String[]::new); }; // no cast, block body result = (a, b) -> { return a.toArray(String[]::new); }; // cast, expression body result = (Spec<String>) (a, b) -> a.toArray(String[]::new); // return position? return (Spec<String>) (a, b) -> { return a.toArray(String[]::new); }; } interface Spec<T> { String[] process(List<T> var1, List<?> var2); } }
818
24.59375
79
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/ParserCornerCases18.java
/** * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ import java.awt.Button; import java.io.File; import java.io.FileFilter; import java.security.PrivilegedAction; import java.util.Comparator; import java.util.concurrent.Callable; import java.util.function.Function; import java.util.function.IntFunction; import java.util.function.Supplier; import java.util.stream.Stream; /** * Java 8 language syntax * * @see <a href="http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-final.html">State of the Lambda</a> * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html">java.util.function</a> */ public class ParserCornerCases18 { public void lambdas() { FileFilter java = (File f) -> f.getName().endsWith(".java"); FileFilter java2 = f -> f.getName().endsWith(".java"); FileFilter java3 = (f) -> f.getName().endsWith(".java"); FileFilter java4 = (f -> f.getName().endsWith(".java")); IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.apply(i); }); FileFilter[] filters = new FileFilter[] { f -> f.exists(), f -> f.canRead(), f -> f.getName().startsWith("q") }; filterFiles(new FileFilter[] { f -> f.exists(), f -> f.canRead(), f -> f.getName().startsWith("q") }); String user = doPrivileged(() -> System.getProperty("user.name")); Callable<String> c = () -> "done"; Runnable r = () -> { System.out.println("done"); }; Supplier<Runnable> sup = () -> () -> { System.out.println("hi"); }; boolean flag = 1 > 2; Callable<Integer> c2 = flag ? (() -> 23) : (() -> 42); Object o = (Runnable) () -> { System.out.println("hi"); }; new ParserCornerCases18().r1.run(); Comparator<String> comparer = (s1, s2) -> s1.compareToIgnoreCase(s2); comparer = (s1, s2) -> s1.compareToIgnoreCase(s2); Button button = new Button(); button.addActionListener(e -> System.out.println(e.getModifiers())); // grammar/parser: don't get confused with this... int initialSizeGlobal = (int) (profilingContext.m_profileItems.size() * (150.0 * 0.30)); BiConsumer<String, Integer> lambda2 = (String s, Integer i) -> { i++; }; BiConsumer<String, Integer> lambda2a = (s, i) -> { i++; }; TriConsumer<String, Integer, Double> lambda3 = (String s, Integer i, Double d) -> { d += i; }; TriConsumer<String, Integer, Double> lambda3a = (s, i, d) -> { d += i; }; } @FunctionalInterface public interface TriConsumer<A, B, C> { void accept(A a, B b, C c); } Runnable r1 = () -> { System.out.println(this); }; public Runnable toDoLater() { return () -> { System.out.println("later"); }; } private String doPrivileged(PrivilegedAction<String> action) { return action.run(); } private void filterFiles(FileFilter[] filters) { } /* Example from java.util.Comparator. */ public static <K extends Comparable<? super K>, V> Comparator<Map.Entry<K,V>> comparingByKey() { // intersection types in cast return (Comparator<Map.Entry<K, V>> & Serializable) (c1, c2) -> c1.getKey().compareTo(c2.getKey()); } /* TODO: This construct can't be parsed. Either the cast expression is not detected, or the following Lambda Expression. /* Example from java.time.chrono.AbstractChronology */ // static final Comparator<ChronoLocalDateTime<? extends ChronoLocalDate>> DATE_TIME_ORDER = // (Comparator<ChronoLocalDateTime<? extends ChronoLocalDate>> & Serializable) (dateTime1, dateTime2) -> { // int cmp = Long.compare(dateTime1.toLocalDate().toEpochDay(), dateTime2.toLocalDate().toEpochDay()); // if (cmp == 0) { // cmp = Long.compare(dateTime1.toLocalTime().toNanoOfDay(), dateTime2.toLocalTime().toNanoOfDay()); // } // return cmp; // }; public void methodReferences() { Runnable r = new ParserCornerCases18()::toDoLater; Runnable r1 = this::toDoLater; ParserCornerCases18 pc = new ParserCornerCases18(); Runnable r11 = pc::toDoLater; Supplier<String> s = super::toString; Runnable r2 = ParserCornerCases18::staticMethod; IntFunction<int[]> arrayMaker = int[]::new; int[] array = arrayMaker.apply(10); // creates an int[10] } // https://sourceforge.net/p/pmd/bugs/1173/ public static class PmdMethodReferenceTest { Function<Integer, Integer> theFunction; public PmdTest() { theFunction = this::foo; } private int foo(int i) { return i; } } public static Runnable staticMethod() { return () -> System.out.println("run"); } public void typeAnnotations() { String myString = (@NonNull String) str; Object o = new @Interned MyObject(); } class UnmodifiableList<T> implements @Readonly List<@Readonly T> {} void monitorTemperature() throws @Critical TemperatureException {} // https://sourceforge.net/p/pmd/bugs/1205/ public static class X { public void lambaWithIf() { Stream.of(1, 2, 3) .sorted((a, b) -> { int x = a.hashCode() - b.hashCode(); if(a.equals(new X())) x = 1; return x; }) .count(); } public void lambaWithIf2() { Stream.of(1, 2, 3) .sorted((Integer a, Integer b) -> { int x = a.hashCode() - b.hashCode(); if(a.equals(new X())) x = 1; return x; }) .count(); } // https://sourceforge.net/p/pmd/bugs/1258/ public void lambdaWithPropertyAssignment() { object.event = () -> { Request request = new Request(); request.id = 42; // request.setId(42); }; } } public List<@AnnotatedUsage ?> testWildCardWithAnnotation() { return null; } public Object @Nullable [] testAnnotationsToArrayElements() { return null; } private byte @Nullable [] getBytes(){ return null; } public static <T extends @NonNull Enum<?>> T getEnum() { return null; } public static <T> @Nullable T getNullableEnum() { return null; } public Object[] createNonNullArray() { return new Object @NonNull[0]; } // this comes from https://github.com/pmd/pmd/issues/4152 public static <T> T[][] check(T @Anno[] @Anno [] arr) { if (arr == null) { throw new NullPointerException(); } return arr; } // this comes from https://github.com/pmd/pmd/issues/4152 public Function func(Main this) { return @A Main.Inner::new; // this line lead to the crash } // this comes from https://github.com/pmd/pmd/issues/4152 public static byte max(final byte @NotNull ... array) { return 0;} // this comes from https://github.com/pmd/pmd/issues/4152 @Retention(RetentionPolicy.CLASS) @Target({ TYPE_USE }) @interface Anno { } private static void testMultiDimArrayWithAnnotations() { // ever used a 3D-Array in java?? Object x = new Object @NonNull[2] @Nullable[1] @NonNull[3]; } /** * Explicit receiver Parameters * see: http://blog.joda.org/2015/12/explicit-receiver-parameters.html * and: https://sourceforge.net/p/pmd/bugs/1455/ */ public void methodWithReceiverParameter(ParserCornerCases18 this) { } public void methodWithReceiverAndOtherParameters(ParserCornerCases18 this, String other) { } public void methodWithReceiverParameterWithAnnotation(@AnnotatedUsage ParserCornerCases18 this, String other) { } @Target(ElementType.TYPE_USE) public @interface AnnotatedUsage {} class Inner { Inner(ParserCornerCases18 ParserCornerCases18.this) {} } } interface DefaultIterator<E> { boolean hasNext(); E next(); void remove(); default void skip(int i) { for (; i > 0 && hasNext(); i--) next(); } static void staticInterfaceMethods() { System.out.println(""); } }
8,481
32.792829
124
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/SwitchStatements.java
public class SwitchStatements { public void myMethod() { int a = 1; // only fall through, no block statements. switch (a) { case 1: default: } // empty switch statement switch (a) { } // last label without block statement switch (a) { case 1: System.out.println("1"); break; default: } } }
437
18.043478
50
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/Bug1530.java
public class Bug1530 { public void incChild() { ((PathElement) stack.getLastLeaf().getUserObject()).currentChild++; } }
132
25.6
75
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/SynchronizedStmts.java
class Sync { public static void getInstance() { synchronized (0) { // note that synchronized is also a modifier return; } } }
163
15.4
71
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/LambdaBug206.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ public @interface Foo { static final ThreadLocal<Interner<Integer>> interner = ThreadLocal.withInitial(Interners::newStrongInterner); }
236
25.333333
79
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/EmptyStmts1.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ import a;;import b; public class Foo {}
127
24.6
79
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/SwitchWithFallthrough.java
public class SwitchWithFallthrough { // only fall through, no block statements. public void myMethod() { int a = 1; switch (a) { case 1: default: } } }
212
18.363636
46
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/GitHubBug3642.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ // see https://github.com/pmd/pmd/issues/3642 public class GitHubBug3642 { @interface Foo { String v1()[]; // parse error // equivalent to String[] v1(); } }
270
21.583333
79
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/GitHubBug3101.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ import net.sourceforge.pmd.lang.java.types.testdata.MyList; import net.sourceforge.pmd.lang.java.types.testdata.MyListAbstract; /** * Note: This class is not compiled (the source is only in src/test/resources). Hence we can't * resolve {@code GitHubBug3101.Inner} and the explicit type arguments is not resolved. * * @see <a href="https://github.com/pmd/pmd/issues/3101">[java] NullPointerException when running PMD under JRE 11 #3101</a> */ public class GitHubBug3101 { { MyList<Inner> a = MyListAbstract.<Inner>of(new Inner()); } private static class Inner { } }
680
31.428571
124
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/EmptyStmts3.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ package c; import a; public class Foo {};;
130
25.2
79
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/LambdaBug1470.java
package com.sample.test; import rx.Observable; import rx.Subscriber; public class pmdTest { private boolean stuff; public Observable<Boolean> testSuper() { return Observable.create( (Subscriber<? super String> subscriber) -> { stuff=true; }) .map(authToken -> false); } public Observable<Boolean> testSuper2() { return Observable.create( (subscriber) -> { stuff=true; }) .map(authToken -> false); } }
548
18.607143
56
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/GitHubBug309.java
import java.util.*; public class GitHubBug309 { public static void main(String[] args) { Runnable r11 = Main::<String>new; IntFunction<int[]> r13 = int[]::<String>new; // produces the same results } }
226
24.222222
81
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/generic_return_type.java
public class Test { public static <String> String test(String x) { return x; } }
87
16.6
48
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/jdk17_try_with_resources_multi.java
public class InputJava7TryWithResources { public static void main() { try (MyResource resource = new MyResource(); MyResource2 resource2 = new MyResource2()) { } } }
169
33
93
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/assert_test3.java
public class Foo { void bar() { assert (x==2) : "hi!"; } }
62
11.6
24
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/jdk17_try_with_resources.java
public class InputJava7TryWithResources { public static void main() { try (MyResource resource = new MyResource()) { } } }
126
24.4
50
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/annotations_bug.java
@Target(ElementType.METHOD) public @interface Foo { }
53
17
27
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/jdk17_generic_diamond.java
public class InputJava7Diamond { HashMap<String> map = new HashMap<>(); }
74
24
39
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/jdk15_forloop.java
public class Test { void foo(List list) { for (Integer i : list) {} } }
75
14.2
27
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/jdk15_varargs.java
public class Test { void bar(Object... args) {} }
50
16
28
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/fields_bug.java
public class Test { private Foo bar; }
40
12.666667
19
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/generic_in_field.java
public class Foo { Class<Double> foo = (Class<Double>)clazz; }
63
20.333333
42
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/jdk15_generics.java
public class Test { ArrayList<Integer> list = new ArrayList<Integer>(); }
76
24.666667
54
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/generic_in_method_call.java
public class Test { List<String> test() { return Collections.<String>emptyList(); } }
92
17.6
42
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/private_method_in_inner_class_interface2.java
/** * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ package net.sourceforge.pmd.lang.java.ast.testdata; /** * Prior to Java9, private methods are not possible. * Make sure, they are detected. * * @see https://github.com/pmd/pmd/issues/793 */ public class PrivateMethodsInInterface2 { public interface Interface1 { Object FOO = new Object() { private void privateMethod() { } }; private void privateMethodInInterface1() { } // note: this will be a parser error! } public interface Interface2 { class InnerClass { private void privateMethod() { } } } public interface Interface3 { enum InnerEnum { VALUE; private void privateMethod() { } } } }
816
22.342857
90
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/jdk17_string_in_switch.java
public class Test { public static void main(String[] args) { String mystr = "value" + "2"; switch (mystr) { case "value1": break; case "value2": break; default: break; } } }
203
13.571429
41
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/jdk17_numerical_literals.java
public class Test { int i1 = 0b00011110; int i2 = 0B00011110; int i3 = 0xA; int i4 = 0x1___A_F; int i5 = 0b1; int i6 = 0b1___1_0; int i7 = 0; int i8 = 02; int i9 = 0_123; int i10 = 1; int i11 = 1___3; int i12 = 1_43_43598_7; long l1 = 0b00011110L; long l2 = 0B00011110l; long l3 = 0xAL; long l4 = 0x1___A_FL; long l5 = 0b1L; long l6 = 0b1___1_0L; long l7 = 0l; long l8 = 02L; long l9 = 0_123l; long l10 = 1l; long l11 = 1___3l; long l12 = 1_43_43598_7L; long l13 = 1_43_43598_7; float f1 = .1f; float f2 = 1.f; float f3 = 0f; float f4 = 1e0F; float f5 = 1e0f; float f6 = 12.345F; float f7 = .5____2_1f; float f8 = 1__42__3.f; float f9 = 0__2_4__324f; float f10 = 1_34e0F; float f11 = 1__1_2e0f; float f12 = 2_1___2.3__4_5F; float f13 = 1_34e0__4__3f; float f14 = 1__1_2e00__000_4f; float f15 = 2_1___2.3__4_5e00______0_5F; double d1 = .1d; double d2 = 1.D; double d3 = 0d; double d4 = 1e0D; double d5 = 1e0d; double d6 = 12.345D; double d7 = .5____2_1d; double d8 = 1__42__3.D; double d9 = 0__2_4__324d; double d10 = 1_34e0d; double d11 = 1__1_2e0d; double d12 = 2_1___2.3__4_5D; double d13 = 1_34e0__4__3d; double d14 = 1__1_2e00__000_4d; double d15 = 2_1___2.3__4_5e00______0_5D; double d16 = 0.12___34; float hf1 = 0x.1___AFp1; float hf2 = 0x.1___AFp0__0__0f; float hf3 = 0x2__3_34.4___AFP00_00f; double hd1 = 0x.1___AFp1; double hd2 = 0x.1___AFp0__0__0d; double hd3 = 0x2__3_34.4___AFP00_00d; int doc1 = 1234_5678; long doc2 = 1_2_3_4__5_6_7_8L; int doc3 = 0b0001_0010_0100_1000; double doc4 = 3.141_592_653_589_793d; double doc5 = 0x1.ffff_ffff_ffff_fP1_023; }
1,707
21.181818
43
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/assert_test5.java
public class Foo { int assert = 2; }
38
12
18
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/generic_in_annotation.java
public class Foo { public <A extends Annotation> A foo(Class<A> c) { return null; } }
89
17
50
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/jdk17_try_with_resources_with_annotations.java
public class InputJava7TryWithResources { public static void main() { try (@SuppressWarnings("all") final MyResource resource = new MyResource()) { } } }
157
30.6
81
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/inner_bug2.java
public class Test { void bar() { class Inner {}; Inner i = new Inner(); } }
85
13.333333
25
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/annotated_params.java
public class Foo { void bar(@SuppressWarnings("foo") int x) {} }
65
21
44
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/annotated_locals.java
public class Foo { void bar() { @SuppressWarnings("foo") int y = 5; } }
75
14.2
37
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/assert_test5_a.java
public class Foo { void bar() { assert(); } }
47
15
26
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/assert_test6.java
public class Foo { void foo() { assert (x == 2) : "hi!"; } }
64
12
26
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/constant_field_in_annotation_bug.java
public @interface Foo { String CONST = "foo"; }
49
15.666667
23
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/assert_test1.java
public class Foo { void bar() { assert x == 2; } }
55
8.333333
18
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/private_method_in_inner_class_interface1.java
/** * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ package net.sourceforge.pmd.lang.java.ast.testdata; /** * With Java9, private methods in interfaces are possible. * But they must not be confused with private methods in inner classes of interfaces * when using older java version. * * @see https://github.com/pmd/pmd/issues/793 */ public class PrivateMethodsInInterface1 { public interface Interface1 { Object FOO = new Object() { private void privateMethod() { } }; } public interface Interface2 { class InnerClass { private void privateMethod() { } } } public interface Interface3 { enum InnerEnum { VALUE; private void privateMethod() { } } } }
817
22.371429
84
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/jdk15_forloop_with_modifier.java
public class Test { void foo(List list) { for (final Integer i : list) {} } }
81
15.4
33
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/jdk17_try_with_resources_semi.java
public class InputJava7TryWithResources { public static void main() { try (MyResource resource = new MyResource();) { } } }
127
24.6
51
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/jdk15_enum.java
public class Test { enum Season { winter, spring, summer, fall }; }
69
16.5
46
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/jdk17_multicatch.java
public class InputJava7Multicatch { public static void main() { try { } catch (FileNotFoundException | CustomException e) { } } }
135
21.666667
55
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/jdk17_multicatch_with_annotations.java
public class InputJava7Multicatch { public static void main() { try { } catch (final @SuppressWarnings("all") FileNotFoundException | CustomException e) { } } }
166
26.833333
86
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/multiple_generics.java
public class Foo<K,V> { public <A extends K, B extends V> Foo(Bar<A,B> t) {} }
80
26
54
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/assert_test7.java
public class Foo { void assert() {} }
38
12
18
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/assert_test2.java
public class Foo { void bar() { assert (x == 2); } }
56
10.4
18
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/gt_bug.java
public class Test { int y = x > 32; }
39
12.333333
19
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/jdk14_enum.java
public class Test { int enum; }
33
7.5
19
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/inner_bug.java
public class Test { void bar() { final class Inner {}; Inner i = new Inner(); } }
91
14.333333
25
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/funky_generics.java
public class Foo { public <T extends E> Foo() {} }
52
16.666667
31
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/assert_test4.java
public class Foo { void bar() { assert (x==2) : "hi!"; } }
62
11.6
24
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java11/LocalVariableSyntaxForLambdaParameters.java
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.util.function.BiFunction; import java.util.function.Function; public class LocalVariableSyntaxForLambdaParameters { @Target({ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) public @interface Nonnull { } public void createLambdas() { //var lambda = (var x, var y) -> x.process(y); Function<Integer, String> lambda1 = (var x) -> String.valueOf(x); BiFunction<Integer, Integer, Integer> lambda2 = (var x, var y) -> x + y; } public void createAnnotatedLambdaParameters() { //@Nonnull var x = new Foo(); //(@Nonnull var x, @Nullable var y) -> x.process(y) Function<Integer, String> lambda1 = (@Nonnull var x) -> String.valueOf(x); BiFunction<Integer, Integer, Integer> lambda2 = (@Nonnull var x, @Nonnull var y) -> x + y; } }
995
34.571429
98
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java16/NonSealedIdentifier.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ public class NonSealedIdentifier { public static void main(String[] args) { int result = 0; int non = 1; // sealed is a valid identifier name in both Java16 and Java16 Preview int sealed = 2; // non-sealed is a valid subtraction expression in both Java16 and Java16 Preview result = non-sealed; System.out.println(result); } }
479
29
89
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java16/LocalClassAndInterfaceDeclarations.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ /** * @see <a href="http://cr.openjdk.java.net/~gbierman/jep395/jep395-20201019/specs/local-statics-jls.html">Local and Nested Static Declarations</a> */ public class LocalClassAndInterfaceDeclarations { { class MyLocalClass { // constant fields are always allowed static final int constantField = 1; // static members in local classes are allowed with Java16 static int staticField; static void staticMethod() { } } // static local classes are not allowed (neither Java16 nor Java16 Preview) // Note: PMD's parser allows this, but it would actually be a compile error //static class MyLocalStaticClass {} // local interfaces are allowed with Java16 interface MyLocalInterface {} // local enums are allowed with Java16 enum MyLocalEnum { A } // local annotation types are not allowed in Java16 (have been with Java15 Preview) //@interface MyLocalAnnotation {} } }
1,115
32.818182
147
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java16/Records.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ import java.io.IOException; import java.lang.annotation.Target; import java.lang.annotation.ElementType; import java.util.Objects; /** * @see <a href="https://openjdk.java.net/jeps/395">JEP 395: Records</a> */ public class Records { @Target(ElementType.TYPE_USE) @interface Nullable { } @Target({ElementType.CONSTRUCTOR, ElementType.PARAMETER}) @interface MyAnnotation { } public record MyComplex(int real, @Deprecated int imaginary) { // explicit declaration of a canonical constructor @MyAnnotation public MyComplex(@MyAnnotation int real, int imaginary) { if (real > 100) throw new IllegalArgumentException("too big"); this.real = real; this.imaginary = imaginary; } public record Nested(int a) {} public static class NestedClass { } } public record Range(int lo, int hi) { // compact record constructor @MyAnnotation public Range { if (lo > hi) /* referring here to the implicit constructor parameters */ throw new IllegalArgumentException(String.format("(%d,%d)", lo, hi)); } public void foo() { } } public record RecordWithLambdaInCompactConstructor(String foo) { public RecordWithLambdaInCompactConstructor { Objects.requireNonNull(foo, () -> "foo"); } } public record VarRec(@Nullable @Deprecated String @Nullable... x) {} public record ArrayRec(int[] x) {} public record EmptyRec<Type>() { public void foo() { } public Type bar() { return null; } public static void baz() { EmptyRec<String> r = new EmptyRec<>(); System.out.println(r); } } // see https://www.javaspecialists.eu/archive/Issue276.html public interface Person { String firstName(); String lastName(); } public record PersonRecord(String firstName, String lastName) implements Person, java.io.Serializable { } }
2,120
26.907895
83
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java16/LocalRecords.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ import java.util.stream.Collectors; import java.util.List; /** * @see <a href="https://openjdk.java.net/jeps/395">JEP 395: Records</a> */ public class LocalRecords { public interface Merchant {} public static double computeSales(Merchant merchant, int month) { return month; } List<Merchant> findTopMerchants(List<Merchant> merchants, int month) { // Local record record MerchantSales(Merchant merchant, double sales) {} return merchants.stream() .map(merchant -> new MerchantSales(merchant, computeSales(merchant, month))) .sorted((m1, m2) -> Double.compare(m2.sales(), m1.sales())) .map(MerchantSales::merchant) .collect(Collectors.toList()); } void methodWithLocalRecordAndModifiers() { final record MyRecord1(String a) {} final static record MyRecord2(String a) {} @Deprecated record MyRecord3(String a) {} final @Deprecated static record MyRecord4(String a) {} } void methodWithLocalClass() { class MyLocalClass {} } void methodWithLocalVarsNamedSealed() { int result = 0; int non = 1; int sealed = 2; result = non-sealed; System.out.println(result); } }
1,359
28.565217
88
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java16/Point.java
/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ /** * @see <a href="https://openjdk.java.net/jeps/395">JEP 395: Records</a> */ public record Point(int x, int y) { public static void main(String[] args) { Point p = new Point(1, 2); System.out.println("p = " + p); } }
334
21.333333
79
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java16/PatternMatchingInstanceof.java
import static java.lang.annotation.ElementType.CONSTRUCTOR; import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.ElementType.LOCAL_VARIABLE; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.ElementType.MODULE; import static java.lang.annotation.ElementType.PACKAGE; import static java.lang.annotation.ElementType.PARAMETER; import static java.lang.annotation.ElementType.TYPE; import java.lang.annotation.ElementType; import java.lang.annotation.Target; /** * * @see <a href="https://openjdk.java.net/jeps/394">JEP 394: Pattern Matching for instanceof</a> */ public class PatternMatchingInstanceof { private String s = "other string"; public void test() { Object obj = "abc"; //obj = 1; if (obj instanceof String s) { System.out.println("a) obj == s: " + (obj == s)); // true s = "other value"; // not a compile error - s is only effectively final System.out.println("changed s to " + s + ": obj == s: " + (obj == s)); } else { System.out.println("b) obj == s: " + (obj == s)); // false } if (!(obj instanceof String s)) { System.out.println("c) obj == s: " + (obj == s)); // false } else { System.out.println("d) obj == s: " + (obj == s)); // true } if (obj instanceof String s && s.length() > 2) { System.out.println("e) obj == s: " + (obj == s)); // true } if (obj instanceof String s || s.length() > 5) { System.out.println("f) obj == s: " + (obj == s)); // false } // With Java16 there can be final and annotations if (obj instanceof final String s) { System.out.println("g) obj == s: " + (obj == s)); // true //s = "another value"; // compile error: error: cannot assign a value to final variable s } else { System.out.println("h) obj == s: " + (obj == s)); // false } if (obj instanceof @Deprecated String s) { System.out.println("i) obj == s: " + (obj == s)); // true } else { System.out.println("j) obj == s: " + (obj == s)); // false } if (obj instanceof final @Deprecated String s) { System.out.println("k) obj == s: " + (obj == s)); // true //s = "another value"; // compile error: error: cannot assign a value to final variable s } else { System.out.println("l) obj == s: " + (obj == s)); // false } } public static void main(String[] args) { new PatternMatchingInstanceof().test(); } // InstanceofExpression can be annotated class Foo { { Object f = null; Object o = f instanceof @Nullable Foo; } } @Target(value=ElementType.TYPE_USE) @interface Nullable { } }
2,920
36.448718
101
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java10/LocalVariableTypeInferenceForLoopEnhanced2.java
import java.util.ArrayList; import java.util.List; public class LocalVariableTypeInferenceForLoopEnhanced2 { public void listMethod() { List<String> test = new ArrayList<>(); for (var s : test) { System.out.println(s); } } public void arrayMethod() { int values[] = {2, 4, 6}; for (var s : values) { System.out.println(s); } } }
422
19.142857
57
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java10/LocalVariableTypeInferenceForLoopEnhanced.java
import java.util.Arrays; import java.util.List; public class LocalVariableTypeInferenceForLoopEnhanced { private List<String> data = Arrays.asList("a", "b", "c"); public void aMethod() { for (var s : data) { System.out.println(s); } } }
278
22.25
61
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java10/LocalVariableTypeInference_typeres.java
package net.sourceforge.pmd.typeresolution.testdata.dummytypes; import java.util.List; import java.util.ArrayList; // note: This source is java10. // the same class exists in src/test/java/net/sourceforge/pmd/typeresolution/testdata/dummytypes/MyList.java // compilable by java7. This is important, so that // this class has a type. public class MyList { public void checkIterator(List<?> other) { var oit = other.iterator(); oit.hasNext(); } }
472
26.823529
108
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java10/LocalVariableTypeInferenceTryWithResources.java
import java.io.FileInputStream; public class LocalVariableTypeInferenceTryWithResources { public void aMethod() throws Exception { String filename = "file.txt"; try (var in = new FileInputStream(filename)) { } } }
249
19.833333
57
java
pmd
pmd-master/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java10/LocalVariableTypeInference.java
import java.util.ArrayList; public class LocalVariableTypeInference { public void aMethod() { var list = new ArrayList<String>(); // infers ArrayList<String> var stream = list.stream(); // infers Stream<String> } public void asMethodParameter() { var s = "Java 10"; print(s); } private void print(String text) { System.out.println(text); } }
419
22.333333
72
java