diff options
Diffstat (limited to 'src/test/resources/com/puppycrawl/tools/checkstyle/checks')
24 files changed, 1134 insertions, 1 deletions
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/InputSuppressWarningsHolder5.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/InputSuppressWarningsHolder5.java new file mode 100644 index 000000000..f9e5f9503 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/InputSuppressWarningsHolder5.java @@ -0,0 +1,50 @@ +//Compilable with Java8 +package com.puppycrawl.tools.checkstyle.checks; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Target; +import java.util.List; +import java.util.Map; + +public class InputSuppressWarningsHolder5{ + + public static Object foo1(@SuppressWarnings("unsused") Object str) { + String myString = (@SuppressWarnings("unsused") String) str; + Object object = new @SuppressWarnings("unsused") Object(); + try { + return null; + + } catch (@SuppressWarnings("unsused") Exception ex) { + return ""; + } + } + + void foo2() throws @SuppressWarnings("unsused") Exception { } + + public void foo3() { + Map.@SuppressWarnings("unsused") Entry entry; + MyObject myObject = new MyObject(); + myObject.<@SuppressWarnings("unsused") String>myMethod(); + myObject.new @SuppressWarnings("unsused") MyObject2(); + } + + public static <T> void foo4(Object str) { + List<@SuppressWarnings("unsused") ? extends Comparable<T>> unchangeable; + } + + abstract class UnmodifiableList<T> + implements @SuppressWarnings("unsused") List<@SuppressWarnings("unsused") T> { + } + + class MyObject{ + + public void myMethod(){}; + + class MyObject2{} + } + + @Target(ElementType.TYPE_USE) + @interface SuppressWarnings { + String value(); + } +} diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/blocks/InputLeftCurlyNewLineOptionWithLambda.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/blocks/InputLeftCurlyNewLineOptionWithLambda.java new file mode 100644 index 000000000..8ce0e26fb --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/blocks/InputLeftCurlyNewLineOptionWithLambda.java @@ -0,0 +1,18 @@ +//Compilable with Java8 +package com.puppycrawl.tools.checkstyle.checks.blocks; + +public class InputLeftCurlyNewLineOptionWithLambda +{ + static Runnable r1 = () -> { + System.out.println("Hello world one!"); + }; + + static Runnable r2 = () -> System.out.println("Hello world two!"); + + static Runnable r3 = () -> {System.out.println("Hello world two!");}; + + static Runnable r4 = () -> + { + System.out.println("Hello world one!"); + }; +} diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/blocks/InputSingleLineLambda.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/blocks/InputSingleLineLambda.java new file mode 100644 index 000000000..d21550146 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/blocks/InputSingleLineLambda.java @@ -0,0 +1,10 @@ +//Compilable with Java8 +package com.puppycrawl.tools.checkstyle.checks.blocks; +public class InputSingleLineLambda { + + static Runnable r1 = ()->System.out.println("Hello world one!"); + static Runnable r2 = () -> System.out.println("Hello world two!"); + static Runnable r3 = () -> + System.out.println("Hello world two!"); + static Runnable r4 = () -> {System.out.println("Hello world two!");}; +}
\ No newline at end of file diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputDefaultComesLast2.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputDefaultComesLast2.java new file mode 100644 index 000000000..883f7706e --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputDefaultComesLast2.java @@ -0,0 +1,16 @@ +//Compilable with Java8 +package com.puppycrawl.tools.checkstyle.checks.coding; +public interface InputDefaultComesLast2 { + + String toJson(Object one, Object two, Object three); + + String toJson(String document); + + default String toJson(Object one) { + return toJson(one, one, one); + } + + default String toJson(Object one, Object two) { + return toJson(one, one, two); + } + }
\ No newline at end of file diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputFinalLocalVariable.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputFinalLocalVariable.java index 2fefa2799..9f461cf94 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputFinalLocalVariable.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputFinalLocalVariable.java @@ -164,7 +164,7 @@ class class1 } } -class A { +class AA { { int y = 0; y = 9; diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputFinalLocalVariableNameLambda.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputFinalLocalVariableNameLambda.java new file mode 100644 index 000000000..e012bc185 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputFinalLocalVariableNameLambda.java @@ -0,0 +1,36 @@ +//Compilable with Java8 +package com.puppycrawl.tools.checkstyle.checks.coding; + +import java.math.BigDecimal; + +public class InputFinalLocalVariableNameLambda { + private interface Lambda { + public Object op(AugmentedOrder a, AugmentedOrder b); + } + private static class AugmentedOrder { + public BigDecimal reduce(final BigDecimal zero, final Lambda l) { + return null; + } + public Object add(final Object amount) { + return null; + } + public Object getAmount() { + return null; + } + } + + private void addTotalValueOfOrder(final AugmentedOrder order) { + final BigDecimal totalValueOfOrder = order + .reduce(BigDecimal.ZERO, + (t, u) -> t.add(u.getAmount())); + } +} +interface Operation { + public Object apply(); + + public static final Operation OPERATION = () -> { + Object result; + result = null; + return result; + }; +}
\ No newline at end of file diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputHiddenFieldLambdas.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputHiddenFieldLambdas.java new file mode 100644 index 000000000..0fc0cc093 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputHiddenFieldLambdas.java @@ -0,0 +1,230 @@ +//Compilable with Java8 +package com.puppycrawl.tools.checkstyle.checks.coding; + +import java.lang.Integer; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +public class InputHiddenFieldLambdas { + /** + * Example 1: lambda parameter 'value' on line 16 + * hides a field 'value' on line 14. + */ + List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6); + Integer value = new Integer(1); + { + numbers.forEach((Integer value) -> System.out.println(value)); // 1 violation + } + + /** + * Example 2: lambda parameter 'name' on line 27 + * does not hide a field 'name' on line 25, because + * field 'name' can not be referenced from a static context. + */ + static List<String> firstNames = Arrays.asList("Andrei", "Michal", "Roman", "Vladislav"); + String name = new String(); + static { + firstNames.forEach((String name) -> System.out.println(name)); + } + + /** + * Example 3: lambda parameter 'brand' on line 38 (which type is omitted) + * does not hide a field 'brand' on line 36, because + * field 'brand' can not be referenced from a static context. + */ + static List<String> carBrands = Arrays.asList("BMW", "Mazda", "Volkswagen"); + String brand = new String(); + static { + carBrands.forEach(brand -> System.out.println(brand)); + } + + /** + * Example 4: lambda parameter 'languageCode' on line 48 + * hides a field 'languageCode' on line 46. + */ + static List<String> languageCodes = Arrays.asList("de", "ja", "fr", "pt"); + static String languageCode = new String(); + { + languageCodes.forEach(languageCode -> System.out.println(languageCode)); // 1 violation + } + + /** + * Example 5: lambda parameter 'number' on line 57 + * hides a field 'number' on line 55. + */ + int number = 1; + Optional<Object> foo1(int i) { + return Optional.of(5).map(number -> { // violation + if (number == 1) return true; + else if (number == 2) return true; + else return false; + }); + } + + /** + * Example 6: lambda parameter 'id' on line 70 + * hides a field 'id' on line 68. + */ + static long id = 1; + Optional<Object> foo2(int i) { + return Optional.of(5).map(id -> { // violation + if (id == 1) return true; + else if (id == 2) return true; + else return false; + }); + } + + /** + * Example 7: lambda parameter 'age' on line 84 + * does not hide a field 'age' on line 82, + * because field 'age' can not be referenced from a static context. + */ + int age = 21; + static Optional<Object> foo3(int i) { + return Optional.of(5).map(age -> { + if (age == 1) return true; + else if (age == 2) return true; + else return false; + }); + } + + /** + * Example 8: lambda parameter 'note' on line 98 + * hides a field 'note' on line 95. + */ + static String note = new String(); + private void foo4() { + List<String> acceptableNotes = Arrays.asList("C", "D", "E", "F", "G", "A", "B"); + acceptableNotes.forEach(note -> System.out.println(note)); // 1 violation + } + + /** + * Example 9: lambda parameter 'letter' on line 109 + * does not hide a field 'letter' on line 106, because + * field 'letter' can not be referenced from a static context. + */ + String letter = new String("a"); + private static void foo5() { + List<String> acceptableAlphabet = Arrays.asList("a", "b", "c"); + acceptableAlphabet.forEach(letter -> System.out.println(letter)); + } + + @FunctionalInterface + interface Function <A, B> { + public B apply (A a, B b); + } + + /** + * Example 10: typed parameters - two hide fields + */ + String stringValue = "248.3"; + int intValue = 2; + { + Function <String, Integer> multiAdder = (String stringValue, Integer intValue) -> { // 2 violations + return Integer.parseInt(stringValue) + intValue; + }; + System.out.println(multiAdder.apply ("22.4", 2)); + } + + /** + * Example 11: typed parameters - one hide field + */ + Double doubleValue = 8.5; + { + Function <Integer, Double> adder = (Integer integerValue, Double doubleValue) -> { // 1 violation + return integerValue + doubleValue; + }; + System.out.println(adder.apply(2, 2.2)); + } + + /** + * Example 11: untyped parameters - two hide fields + */ + String firstString = "Hello,"; + String secondString = " World!"; + { + Function <String, String> stringConcat = (firstString, secondString) -> { // 2 violations + return firstString + secondString; + }; + System.out.println(stringConcat.apply("A", "B")); + } + + @FunctionalInterface + interface SomeFunction<One, Two> { + public Two apply(One one, Two two); + } + + /** + * Example 11: untyped parameters - one hide field + */ + Integer first = 1; + { + Function<Integer, Character> turnToZ = (first, second) -> 'z'; // 1 violation + } + + @FunctionalInterface + public interface Foo { + public String apply(); + } + + /** + * Example 12: case when no parameters are given + */ + { + Foo foo = () -> ""; + } + @FunctionalInterface + interface FunctionWithOneParameter<One> { + public One apply(One one); + } + + /** + * Example 13: internal lambda hides a field + */ + Double mPi = Math.PI; + List<Double> simpleNumbers = Arrays.asList(1.0, 2.0, 3.0, 4.0, 5.0, 6.0); + { + simpleNumbers.forEach(digit -> { + FunctionWithOneParameter<Double> strangeAdder = (mPi -> mPi+= digit); // 1 violation + }); + } + + @FunctionalInterface + interface FunctionWithComplexGenerics<One, Two> { + public Two foo(One one, Two two); + } + + /** + * Example 14: lambda typed with complex generics + */ + List<Double> justSomeList; + Map<String, Object> justSomeMap; + { + FunctionWithComplexGenerics<List<Double>, Map<String, Object>> someWierdFunc = + (List<Double> justSomeList, Map<String, Object> justSomeMap) -> { // 2 violations + System.out.println(justSomeList); + System.out.println(justSomeMap); + return new HashMap<>(); + }; + } + + /** + * Example 15: lambda stored in field (with typed parameter) + * hides other field + */ + Object someObject = new Object(); + FunctionWithOneParameter objectToString = (Object someObject) -> { // 1 violation + return someObject.toString(); + }; + + /** + * Example 16: lambda stored in field (with untyped parameter) + * hides other field + */ + FunctionWithOneParameter otherObjectToString = someObject -> { // 1 violation + return someObject.toString(); + }; +} diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputIllegalInstantiation2.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputIllegalInstantiation2.java new file mode 100644 index 000000000..ae967fb79 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputIllegalInstantiation2.java @@ -0,0 +1,22 @@ +//Compilable with Java8 +package com.puppycrawl.tools.checkstyle.checks.coding; + +import java.util.function.Function; +import java.util.function.Supplier; + +public class InputIllegalInstantiation2 +{ + + public static void main(String[] args) + { + + Supplier<InputMethodReferencesTest2> supplier = InputMethodReferencesTest2::new; + Function<Integer, String[]> messageArrayFactory = String[]::new; + + } + + private static class InputMethodReferencesTest2<T> + { + + } +} diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputInnerAssignmentLambdaExpressions.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputInnerAssignmentLambdaExpressions.java new file mode 100644 index 000000000..25c894c15 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputInnerAssignmentLambdaExpressions.java @@ -0,0 +1,18 @@ +//Compilable with Java8 +package com.puppycrawl.tools.checkstyle.checks.coding; + +import java.util.function.Supplier; + +public class InputInnerAssignmentLambdaExpressions { + interface MyLambda { + void e(Object e); + } + private static class MyButton { + public void setOnAction(MyLambda my) { + } + } + private void setAction() { + MyButton button = new MyButton(); + button.setOnAction(e -> { boolean pressed = true; }); //No violation here + } +} diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputReturnCountLambda.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputReturnCountLambda.java new file mode 100644 index 000000000..ae9e427b4 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputReturnCountLambda.java @@ -0,0 +1,63 @@ +//Compilable with Java8 +package com.puppycrawl.tools.checkstyle.checks.coding; + +import java.lang.Integer; +import java.util.Optional; +import java.util.concurrent.Callable; +import java.util.function.Supplier; + +public class InputReturnCountLambda { + + Runnable fieldWithOneReturnInLambda = () -> { + return; + }; + + Callable<Integer> fieldWithTwoReturnInLambda = () -> { + if (hashCode() == 0) return 0; + else return 1; + }; + + Optional<Integer> methodWithOneReturnInLambda() { + return Optional.of(hashCode()).filter(i -> { + return i > 0; + }); + } + + Optional<Integer> methodWithTwoReturnInLambda() { + return Optional.of(hashCode()).filter(i -> { + if (i > 0) return true; + else return false; + }); + } + + Optional<Object> methodWithThreeReturnInLambda(int number) { + return Optional.of(number).map(i -> { + if (i == 42) return true; + else if (i == 7) return true; + else return false; + }); + } + + int methodWithTwoReturnWithLambdas(final int number) { + if (hashCode() > 0) { + new Thread( + () -> { + } + ).start(); + return number; + } else { + return Optional.of(hashCode()).orElseGet(() -> { + if (number > 0) return number; + else return 0; + }); + } + } + + Supplier<Supplier<Integer>> methodWithOneReturnPerLambda() { + return () -> { + return () -> { + return 1; + }; + }; + } +} diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputSuperClone.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputSuperClone.java new file mode 100644 index 000000000..16bfeaf90 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputSuperClone.java @@ -0,0 +1,39 @@ +//Compilable with Java8 +package com.puppycrawl.tools.checkstyle.checks.coding; + +interface InputSuperClone { + void clone(); +} + +class A { + public Object clone() { + return null; + } +} + +class B{ + public Object clone() throws CloneNotSupportedException { + super.clone(); + return null; + } + void clone(Object asd, Object asd2) { + } +} + +class C extends B { + void method() throws CloneNotSupportedException { + Object asd = null; + super.clone(asd,asd); + super.clone(); + Runnable a = () -> super.clone(null, null); + } + + void method2() { + new Runnable() { + @Override + public void run() { + C.super.clone(null, null); + } + }; + } +} diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputFinalInDefaultMethods.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputFinalInDefaultMethods.java new file mode 100644 index 000000000..de2f6c275 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputFinalInDefaultMethods.java @@ -0,0 +1,226 @@ +//Compilable with Java8 //indent:0 exp:0 +package com.puppycrawl.tools.checkstyle.checks.indentation; //indent:0 exp:0 + +import java.util.ArrayList; //indent:0 exp:0 +import java.util.List; //indent:0 exp:0 +import java.util.Map; //indent:0 exp:0 +import java.util.StringJoiner; //indent:0 exp:0 +import java.util.stream.Collector; //indent:0 exp:0 +import java.util.stream.Collectors; //indent:0 exp:0 +import java.util.stream.IntStream; //indent:0 exp:0 +import java.util.stream.Stream; //indent:0 exp:0 + +public class InputFinalInDefaultMethods { //indent:0 exp:0 + + interface Printer //indent:2 exp:2 + { //indent:2 exp:2 + void print(String s); //indent:4 exp:4 + } //indent:2 exp:2 + + class LongTypeName { //indent:2 exp:2 + } //indent:2 exp:2 + + interface SomeInterface { //indent:2 exp:2 + void someFunction(LongTypeName arg); //indent:4 exp:4 + } //indent:2 exp:2 + + void function1(Runnable x) { //indent:2 exp:2 + Runnable r1 = () -> { //indent:4 exp:4 + x.run(); //indent:6 exp:6 + }; //indent:4 exp:4 + + Runnable r2 = () -> { x.run(); }; //indent:4 exp:4 + + Runnable r3 = () -> //indent:4 exp:4 + x.run(); //indent:8 exp:8 + + Runnable r4 = () -> x.run(); //indent:4 exp:4 + + Printer r5 = s -> System.out.print(s); //indent:4 exp:4 + + Printer r6 = s -> System.out //indent:4 exp:4 + .print(s); //indent:8 exp:8 + + Runnable r7 = () //indent:4 exp:4 + -> //indent:8 exp:8 + { //indent:5 exp:4 warn + }; //indent:5 exp:4 warn + + Runnable r8 = //indent:4 exp:4 + () //indent:9 exp:8 warn + -> //indent:11 exp:12 warn + {}; //indent:9 exp:8 warn + + Runnable r9 = //indent:4 exp:4 + () //indent:8 exp:8 + -> //indent:12 exp:12 + {}; //indent:8 exp:8 + + Object o = new Thread(() -> { //indent:4 exp:4 + x.run(); //indent:6 exp:6 + }); //indent:4 exp:4 + + Runnable r01 = () -> { //indent:4 exp:4 + x.run(); //indent:7 exp:6 warn + }; //indent:5 exp:4 warn + + Runnable r11 = //indent:4 exp:4 + () -> { //indent:8 exp:8 + x.run(); //indent:10 exp:10 + }; //indent:8 exp:8 + + Runnable r21 = //indent:4 exp:4 + () -> { x.run(); }; //indent:8 exp:8 + + Runnable r31 = //indent:4 exp:4 + () -> x //indent:8 exp:8 + .run(); //indent:12 exp:12 + + Runnable r41 = //indent:4 exp:4 + () -> x.run(); //indent:8 exp:8 + + Printer r51 = //indent:4 exp:4 + s -> System.out.print(s); //indent:8 exp:8 + + Printer r61 = //indent:4 exp:4 + s -> System.out //indent:8 exp:8 + .print(s); //indent:12 exp:12 + + Object o1 = new Thread( //indent:4 exp:4 + () -> { //indent:8 exp:8 + x.run(); //indent:10 exp:10 + }); //indent:8 exp:8 + + Object o2 = new Thread(() -> { //indent:4 exp:4 + x.run(); //indent:6 exp:6 + }).toString(); //indent:4 exp:4 + + SomeInterface i1 = (LongTypeName //indent:4 exp:4 + arg) -> { //indent:8 exp:8 + System.out.print(arg.toString()); //indent:6 exp:6 + }; //indent:4 exp:4 + + Printer[] manyRunnable = new Printer[]{ //indent:4 exp:4 + s -> System.out.print(s), //indent:8 exp:6,8 + s -> { System.out.print(s); }, //indent:8 exp:6,8 + s -> System.out //indent:8 exp:6,8 + .print(s), //indent:12 exp:12 + s -> { //indent:8 exp:6,8 + System.out.print(s); //indent:10 exp:10 + }, //indent:8 exp:8 + }; //indent:4 exp:4 + } //indent:2 exp:2 + + void function3(Runnable x) { //indent:2 exp:2 + function1(() -> { //indent:4 exp:4 + x.run(); //indent:6 exp:6 + }); //indent:4 exp:4 + } //indent:2 exp:2 + + class Person { //indent:2 exp:2 + String name; //indent:4 exp:4 + int age; //indent:4 exp:4 + Person(String name, int age) { //indent:4 exp:4 + } //indent:4 exp:4 + } //indent:2 exp:2 + + class Foo { //indent:2 exp:2 + String name; //indent:4 exp:4 + List<Bar> bars = new ArrayList<>(); //indent:4 exp:4 + + Foo(String name) { //indent:4 exp:4 + this.name = name; //indent:6 exp:6 + } //indent:4 exp:4 + } //indent:2 exp:2 + + class Bar { //indent:2 exp:2 + String name; //indent:4 exp:4 + Bar(String name) { //indent:4 exp:4 + this.name = name; //indent:6 exp:6 + } //indent:4 exp:4 + } //indent:2 exp:2 + + public void f() { //indent:2 exp:2 + Stream.of("d2", "a2", "b1", "b3", "c") //indent:4 exp:4 + .map(s -> { //indent:8 exp:8 + System.out.println("map: " + s); //indent:10 exp:10 + return s.toUpperCase(); //indent:10 exp:10 + }) //indent:8 exp:8 + .anyMatch(s -> { //indent:8 exp:8 + System.out.println("anyMatch: " + s); //indent:10 exp:10 + return s.startsWith("A"); //indent:10 exp:10 + }); //indent:8 exp:8 + + List<Person> persons = null; //indent:4 exp:4 + + Map<Integer, List<Person>> personsByAge = persons //indent:4 exp:4 + .stream() //indent:8 exp:8 + .collect(Collectors.groupingBy(p -> p.age)); //indent:8 exp:8 + + personsByAge //indent:4 exp:4 + .forEach((age, p) -> System.out.format("age %s: %s\n", age, p)); //indent:8 exp:8 + + Collector<Person, StringJoiner, String> personNameCollector = //indent:4 exp:4 + Collector.of( //indent:8 exp:8 + () -> new StringJoiner(" | "), //indent:12 exp:12 + (j, p) -> j.add(p.name.toUpperCase()), //indent:12 exp:12 + (j1, j2) -> j1.merge(j2), //indent:12 exp:12 + StringJoiner::toString); //indent:12 exp:12 + + List<Foo> foos = new ArrayList<>(); //indent:4 exp:4 + + foos.forEach(f -> //indent:4 exp:4 + IntStream //indent:8 exp:8 + .range(1, 4) //indent:12 exp:12 + .forEach(i -> f.bars.add(new Bar("Bar" + i + " <- " + f.name)))); //indent:12 exp:12 + + Stream.of("d2", "a2", "b1", "b3", "c") //indent:4 exp:4 + .filter(s -> { //indent:8 exp:8 + System.out.println("filter: " + s); //indent:9 exp:10 warn + return s.startsWith("a"); //indent:11 exp:10 warn + }) //indent:8 exp:8 + .map(s -> { //indent:8 exp:8 + System.out.println("map: " + s); //indent:10 exp:10 + return s.toUpperCase(); //indent:10 exp:10 + }) //indent:7 exp:8 warn + .forEach(s -> //indent:8 exp:8 + System.out.println("forEach: " + s)); //indent:12 exp:12 + + IntStream.range(1, 4) //indent:4 exp:4 + .mapToObj(i -> new Foo("Foo" + i)) //indent:8 exp:8 + .peek(f -> IntStream.range(1, 4) //indent:8 exp:8 + .mapToObj(i -> new Bar("Bar" + i + " <- " + f.name)) //indent:12 exp:12 + .forEach(f.bars::add)) //indent:12 exp:12 + .flatMap(f -> f.bars.stream()) //indent:8 exp:8 + .forEach(b -> System.out.println(b.name)); //indent:8 exp:8 + + IntStream.range(1, 4) //indent:4 exp:4 + .mapToObj(i -> new Foo("Foo" + i)) //indent:8 exp:8 + .peek(f -> IntStream.range(1, 4) //indent:8 exp:8 + .mapToObj(i -> new Bar("Bar" + i + " <- " + f.name)) //indent:12 exp:12 + .forEach(f.bars::add)) //indent:12 exp:12 + .flatMap(f -> f.bars.stream()) //indent:8 exp:8 + .forEach(b -> System.out.println(b.name)); //indent:8 exp:8 + } //indent:2 exp:2 + + Runnable r2r(Runnable x) { //indent:2 exp:2 + return x; //indent:4 exp:4 + } //indent:2 exp:2 + + void function2(Runnable x) { //indent:2 exp:2 + Runnable r0 = r2r(() -> { //indent:4 exp:4 + int i = 1; //indent:6 exp:6 + }); //indent:4 exp:4 + + Runnable r1 = r2r(() -> { //indent:4 exp:4 + int i = 1; //indent:10 exp:10 + } //indent:8 exp:8 + ); //indent:4 exp:4 + + Runnable r2 = r2r(r2r(() -> { //indent:4 exp:4 + int i = 1; //indent:14 exp:14 + } //indent:12 exp:12 + ) //indent:8 exp:8 + ); //indent:4 exp:4 + } //indent:2 exp:2 +} //indent:0 exp:0 diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputLambda1.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputLambda1.java new file mode 100644 index 000000000..d50a6e609 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputLambda1.java @@ -0,0 +1,226 @@ +//Compilable with Java8 //indent:0 exp:0 +package com.puppycrawl.tools.checkstyle.checks.indentation; //indent:0 exp:0 + +import java.util.ArrayList; //indent:0 exp:0 +import java.util.List; //indent:0 exp:0 +import java.util.Map; //indent:0 exp:0 +import java.util.StringJoiner; //indent:0 exp:0 +import java.util.stream.Collector; //indent:0 exp:0 +import java.util.stream.Collectors; //indent:0 exp:0 +import java.util.stream.IntStream; //indent:0 exp:0 +import java.util.stream.Stream; //indent:0 exp:0 + +public class InputLambda1 { //indent:0 exp:0 + + interface Printer //indent:2 exp:2 + { //indent:2 exp:2 + void print(String s); //indent:4 exp:4 + } //indent:2 exp:2 + + class LongTypeName { //indent:2 exp:2 + } //indent:2 exp:2 + + interface SomeInterface { //indent:2 exp:2 + void someFunction(LongTypeName arg); //indent:4 exp:4 + } //indent:2 exp:2 + + void function1(Runnable x) { //indent:2 exp:2 + Runnable r1 = () -> { //indent:4 exp:4 + x.run(); //indent:6 exp:6 + }; //indent:4 exp:4 + + Runnable r2 = () -> { x.run(); }; //indent:4 exp:4 + + Runnable r3 = () -> //indent:4 exp:4 + x.run(); //indent:8 exp:8 + + Runnable r4 = () -> x.run(); //indent:4 exp:4 + + Printer r5 = s -> System.out.print(s); //indent:4 exp:4 + + Printer r6 = s -> System.out //indent:4 exp:4 + .print(s); //indent:8 exp:8 + + Runnable r7 = () //indent:4 exp:4 + -> //indent:8 exp:8 + { //indent:5 exp:4 warn + }; //indent:5 exp:4 warn + + Runnable r8 = //indent:4 exp:4 + () //indent:9 exp:8 warn + -> //indent:11 exp:12 warn + {}; //indent:9 exp:8 warn + + Runnable r9 = //indent:4 exp:4 + () //indent:8 exp:8 + -> //indent:12 exp:12 + {}; //indent:8 exp:8 + + Object o = new Thread(() -> { //indent:4 exp:4 + x.run(); //indent:6 exp:6 + }); //indent:4 exp:4 + + Runnable r01 = () -> { //indent:4 exp:4 + x.run(); //indent:7 exp:6 warn + }; //indent:5 exp:4 warn + + Runnable r11 = //indent:4 exp:4 + () -> { //indent:8 exp:8 + x.run(); //indent:10 exp:10 + }; //indent:8 exp:8 + + Runnable r21 = //indent:4 exp:4 + () -> { x.run(); }; //indent:8 exp:8 + + Runnable r31 = //indent:4 exp:4 + () -> x //indent:8 exp:8 + .run(); //indent:12 exp:12 + + Runnable r41 = //indent:4 exp:4 + () -> x.run(); //indent:8 exp:8 + + Printer r51 = //indent:4 exp:4 + s -> System.out.print(s); //indent:8 exp:8 + + Printer r61 = //indent:4 exp:4 + s -> System.out //indent:8 exp:8 + .print(s); //indent:12 exp:12 + + Object o1 = new Thread( //indent:4 exp:4 + () -> { //indent:8 exp:8 + x.run(); //indent:10 exp:10 + }); //indent:8 exp:8 + + Object o2 = new Thread(() -> { //indent:4 exp:4 + x.run(); //indent:6 exp:6 + }).toString(); //indent:4 exp:4 + + SomeInterface i1 = (LongTypeName //indent:4 exp:4 + arg) -> { //indent:8 exp:8 + System.out.print(arg.toString()); //indent:6 exp:6 + }; //indent:4 exp:4 + + Printer[] manyRunnable = new Printer[]{ //indent:4 exp:4 + s -> System.out.print(s), //indent:8 exp:6,8 + s -> { System.out.print(s); }, //indent:8 exp:6,8 + s -> System.out //indent:8 exp:6,8 + .print(s), //indent:12 exp:12 + s -> { //indent:8 exp:6,8 + System.out.print(s); //indent:10 exp:10 + }, //indent:8 exp:8 + }; //indent:4 exp:4 + } //indent:2 exp:2 + + void function3(Runnable x) { //indent:2 exp:2 + function1(() -> { //indent:4 exp:4 + x.run(); //indent:6 exp:6 + }); //indent:4 exp:4 + } //indent:2 exp:2 + + class Person { //indent:2 exp:2 + String name; //indent:4 exp:4 + int age; //indent:4 exp:4 + Person(String name, int age) { //indent:4 exp:4 + } //indent:4 exp:4 + } //indent:2 exp:2 + + class Foo { //indent:2 exp:2 + String name; //indent:4 exp:4 + List<Bar> bars = new ArrayList<>(); //indent:4 exp:4 + + Foo(String name) { //indent:4 exp:4 + this.name = name; //indent:6 exp:6 + } //indent:4 exp:4 + } //indent:2 exp:2 + + class Bar { //indent:2 exp:2 + String name; //indent:4 exp:4 + Bar(String name) { //indent:4 exp:4 + this.name = name; //indent:6 exp:6 + } //indent:4 exp:4 + } //indent:2 exp:2 + + public void f() { //indent:2 exp:2 + Stream.of("d2", "a2", "b1", "b3", "c") //indent:4 exp:4 + .map(s -> { //indent:8 exp:8 + System.out.println("map: " + s); //indent:10 exp:10 + return s.toUpperCase(); //indent:10 exp:10 + }) //indent:8 exp:8 + .anyMatch(s -> { //indent:8 exp:8 + System.out.println("anyMatch: " + s); //indent:10 exp:10 + return s.startsWith("A"); //indent:10 exp:10 + }); //indent:8 exp:8 + + List<Person> persons = null; //indent:4 exp:4 + + Map<Integer, List<Person>> personsByAge = persons //indent:4 exp:4 + .stream() //indent:8 exp:8 + .collect(Collectors.groupingBy(p -> p.age)); //indent:8 exp:8 + + personsByAge //indent:4 exp:4 + .forEach((age, p) -> System.out.format("age %s: %s\n", age, p)); //indent:8 exp:8 + + Collector<Person, StringJoiner, String> personNameCollector = //indent:4 exp:4 + Collector.of( //indent:8 exp:8 + () -> new StringJoiner(" | "), //indent:12 exp:12 + (j, p) -> j.add(p.name.toUpperCase()), //indent:12 exp:12 + (j1, j2) -> j1.merge(j2), //indent:12 exp:12 + StringJoiner::toString); //indent:12 exp:12 + + List<Foo> foos = new ArrayList<>(); //indent:4 exp:4 + + foos.forEach(f -> //indent:4 exp:4 + IntStream //indent:8 exp:8 + .range(1, 4) //indent:12 exp:12 + .forEach(i -> f.bars.add(new Bar("Bar" + i + " <- " + f.name)))); //indent:12 exp:12 + + Stream.of("d2", "a2", "b1", "b3", "c") //indent:4 exp:4 + .filter(s -> { //indent:8 exp:8 + System.out.println("filter: " + s); //indent:9 exp:10 warn + return s.startsWith("a"); //indent:11 exp:10 warn + }) //indent:8 exp:8 + .map(s -> { //indent:8 exp:8 + System.out.println("map: " + s); //indent:10 exp:10 + return s.toUpperCase(); //indent:10 exp:10 + }) //indent:7 exp:8 warn + .forEach(s -> //indent:8 exp:8 + System.out.println("forEach: " + s)); //indent:12 exp:12 + + IntStream.range(1, 4) //indent:4 exp:4 + .mapToObj(i -> new Foo("Foo" + i)) //indent:8 exp:8 + .peek(f -> IntStream.range(1, 4) //indent:8 exp:8 + .mapToObj(i -> new Bar("Bar" + i + " <- " + f.name)) //indent:12 exp:12 + .forEach(f.bars::add)) //indent:12 exp:12 + .flatMap(f -> f.bars.stream()) //indent:8 exp:8 + .forEach(b -> System.out.println(b.name)); //indent:8 exp:8 + + IntStream.range(1, 4) //indent:4 exp:4 + .mapToObj(i -> new Foo("Foo" + i)) //indent:8 exp:8 + .peek(f -> IntStream.range(1, 4) //indent:8 exp:8 + .mapToObj(i -> new Bar("Bar" + i + " <- " + f.name)) //indent:12 exp:12 + .forEach(f.bars::add)) //indent:12 exp:12 + .flatMap(f -> f.bars.stream()) //indent:8 exp:8 + .forEach(b -> System.out.println(b.name)); //indent:8 exp:8 + } //indent:2 exp:2 + + Runnable r2r(Runnable x) { //indent:2 exp:2 + return x; //indent:4 exp:4 + } //indent:2 exp:2 + + void function2(Runnable x) { //indent:2 exp:2 + Runnable r0 = r2r(() -> { //indent:4 exp:4 + int i = 1; //indent:6 exp:6 + }); //indent:4 exp:4 + + Runnable r1 = r2r(() -> { //indent:4 exp:4 + int i = 1; //indent:10 exp:10 + } //indent:8 exp:8 + ); //indent:4 exp:4 + + Runnable r2 = r2r(r2r(() -> { //indent:4 exp:4 + int i = 1; //indent:14 exp:14 + } //indent:12 exp:12 + ) //indent:8 exp:8 + ); //indent:4 exp:4 + } //indent:2 exp:2 +} //indent:0 exp:0 diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputLambda2.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputLambda2.java new file mode 100644 index 000000000..9bbf86617 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputLambda2.java @@ -0,0 +1,43 @@ +//Compilable with Java8 //indent:0 exp:0 +package com.puppycrawl.tools.checkstyle.checks.indentation; //indent:0 exp:0 + +import java.util.function.BinaryOperator; //indent:0 exp:0 +import java.util.function.Consumer; //indent:0 exp:0 + +public class InputLambda2 { //indent:0 exp:0 + public <T> Consumer<Integer> params(Consumer<Integer> f1, Consumer<Integer> f2) { //indent:4 exp:4 + return f2; //indent:8 exp:8 + } //indent:4 exp:4 + + private void print(int i) { //indent:4 exp:4 + } //indent:4 exp:4 + + public Consumer<Integer> returnFunctionOfLambda() { //indent:4 exp:4 + return params( //indent:8 exp:8 + (x) -> print(x * 1), //indent:16 exp:16 + (x) -> print(x * 2) //indent:16 exp:16 + ); //indent:8 exp:8 + } //indent:4 exp:4 + + public static <T> BinaryOperator<T> returnLambda() { //indent:4 exp:4 + return (t1, t2) -> { //indent:8 exp:8 + return t1; //indent:12 exp:12 + }; //indent:8 exp:8 + } //indent:4 exp:4 + + class TwoParams { //indent:4 exp:4 + TwoParams(Consumer<Integer> c1, Consumer<Integer> c2) { //indent:8 exp:8 + } //indent:8 exp:8 + } //indent:4 exp:4 + + public void makeTwoParams() { //indent:4 exp:4 + TwoParams t0 = new TwoParams( //indent:8 exp:8 + (x) -> print(x * 1), //indent:16 exp:16 + (x) -> print(x * 2) //indent:16 exp:16 + ); //indent:8 exp:8 + + TwoParams t1 = new TwoParams( //indent:8 exp:8 + (x) -> print(x * 1), //indent:16 exp:16 + (x) -> print(x * 2)); //indent:16 exp:16 + } //indent:4 exp:4 +} //indent:0 exp:0 diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputNewHandler.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputNewHandler.java new file mode 100644 index 000000000..594b7c168 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputNewHandler.java @@ -0,0 +1,21 @@ +//Compilable with Java8 +package com.puppycrawl.tools.checkstyle.checks.indentation; + +import java.util.ArrayList; +import java.util.function.Supplier; + +/** + * + * @author IljaDubinin + */ +public class InputNewHandler +{ + + public static void test() { + method(ArrayList::new); + } + + private static void method(Supplier<?> s) { + } + +} diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/modifier/InputFinalInDefaultMethods.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/modifier/InputFinalInDefaultMethods.java new file mode 100644 index 000000000..d931fa63b --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/modifier/InputFinalInDefaultMethods.java @@ -0,0 +1,17 @@ +//Compilable with Java8 +package com.puppycrawl.tools.checkstyle.checks.modifier; + +public interface InputFinalInDefaultMethods { + final int k = 5; // violation + + default int defaultMethod(final int x) { + if (k == 5) { + final int t = 24; //No violation here! + for (; ;) { + final String s = "some"; //No violation here! + } + } + final int square = x * x; //No violation here! + return square; + } +} diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/modifier/InputModifier2.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/modifier/InputModifier2.java new file mode 100644 index 000000000..2205619f8 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/modifier/InputModifier2.java @@ -0,0 +1,9 @@ +//Compilable with Java8 +package com.puppycrawl.tools.checkstyle.checks.modifier; +import java.util.Comparator; +public interface InputModifier2 extends Comparator<Integer> { + @Override + default int compare(Integer a, Integer b) { + return 0; + } + }
\ No newline at end of file diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/modifier/InputStaticModifierInInterface.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/modifier/InputStaticModifierInInterface.java new file mode 100644 index 000000000..e8210a415 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/modifier/InputStaticModifierInInterface.java @@ -0,0 +1,11 @@ +//Compilable with Java8 +package com.puppycrawl.tools.checkstyle.checks.modifier; + +public interface InputStaticModifierInInterface +{ + static int f() + { + int someName = 5; + return someName; + } +} diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/InputStaticModifierInInterface.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/InputStaticModifierInInterface.java new file mode 100644 index 000000000..5383c9ddc --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/InputStaticModifierInInterface.java @@ -0,0 +1,11 @@ +//Compilable with Java8 +package com.puppycrawl.tools.checkstyle.checks.naming; + +public interface InputStaticModifierInInterface +{ + static int f() + { + int someName = 5; + return someName; + } +} diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/InputAllowEmptyLambdaExpressions.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/InputAllowEmptyLambdaExpressions.java new file mode 100644 index 000000000..db71f6225 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/InputAllowEmptyLambdaExpressions.java @@ -0,0 +1,14 @@ +//Compilable with Java8 +package com.puppycrawl.tools.checkstyle.checks.whitespace; + +import java.util.function.*; + +public class InputAllowEmptyLambdaExpressions { + Runnable noop = () -> {}; + Runnable noop2 = () -> { + int x = 10; + }; + BinaryOperator<Integer> sum = (x, y) -> x + y; + Runnable noop3 = () -> {;}; + Runnable noop4 = () -> {new String();}; +} diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/InputGenericWhitespaceMethodRef.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/InputGenericWhitespaceMethodRef.java new file mode 100644 index 000000000..16d92e36b --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/InputGenericWhitespaceMethodRef.java @@ -0,0 +1,11 @@ +//Compilable with Java8 +package com.puppycrawl.tools.checkstyle.checks.whitespace; + +import java.util.Optional; +import java.util.function.Supplier; + +public class InputGenericWhitespaceMethodRef +{ + final Supplier<Optional<Integer>> function1 = Optional::<Integer>empty; + final Supplier<Optional<Integer>> function2 = Optional::<Integer> empty; +}
\ No newline at end of file diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/InputMethodReferences3.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/InputMethodReferences3.java new file mode 100644 index 000000000..836ca8379 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/InputMethodReferences3.java @@ -0,0 +1,21 @@ +//Compilable with Java8 +package com.puppycrawl.tools.checkstyle.checks.whitespace; +import java.util.function.Supplier; +public class InputMethodReferences3 +{ + public static class SomeClass { + public static class Nested<V> { + private Nested() { + } + } + } + + public static class Nested2<V> { + } + + public <V> void methodName(V value) { + Supplier<?> t = Nested2<V>::new; + Supplier<SomeClass.Nested<V>> passes = SomeClass.Nested::new; + Supplier<SomeClass.Nested<V>> fails = SomeClass.Nested<V>::new; + } +} diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/InputNoWhitespaceAfterMethodRef.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/InputNoWhitespaceAfterMethodRef.java new file mode 100644 index 000000000..b8bc4eb09 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/InputNoWhitespaceAfterMethodRef.java @@ -0,0 +1,11 @@ +//Compilable with Java8 +package com.puppycrawl.tools.checkstyle.checks.whitespace; + +import java.util.function.Function; +import java.util.function.IntFunction; + +public class InputNoWhitespaceAfterMethodRef +{ + IntFunction<int[]> arrayMaker = int []::new;//incorrect 10:40 + Function<Integer, String[]> messageArrayFactory = String []::new;//incorrect 11:63 +} diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/InputWhitespaceAroundLambda.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/InputWhitespaceAroundLambda.java new file mode 100644 index 000000000..5cb9d9891 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/InputWhitespaceAroundLambda.java @@ -0,0 +1,10 @@ +//Compilable with Java8 +package com.puppycrawl.tools.checkstyle.checks.whitespace; + +import java.util.function.Function; + +class InputWhitespaceAroundLambda { + public void foo() { + Function<Object, String> function = (o)->o.toString(); + } +}
\ No newline at end of file |