This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static String print(int n) { | |
return switch (n) { | |
case 0 -> "zero"; | |
case 1 -> "one"; | |
case 2 -> "two"; | |
default -> "many"; | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class InvalidSwitchExpressionWithoutDefault { | |
public static void main(String[] args) { | |
System.out.println(print(1)); | |
} | |
static String print(int n) { | |
return switch (n) { | |
case 0 -> "zero"; | |
case 1 -> "one"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WhoIsWho { | |
enum Person { | |
Mozart, Picasso, Goethe, Dostoevsky, Prokofiev, Dali, | |
Gaudi, Bach, Einstein | |
} | |
public static void main(String[] args) { | |
print(Person.Mozart); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static int factorial(int n) { | |
return switch (n) { | |
case 0, 1 -> 1; | |
case 2 -> 2; | |
default -> factorial(n - 1) * n; | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String title = switch (person) { | |
case Dali, Picasso -> "painter"; | |
case Mozart, Prokofiev -> "composer"; | |
case Goethe, Dostoevsky -> "writer"; | |
default -> "..."; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class InvalidWhoIsWho { | |
enum Person { | |
Mozart, Picasso, Goethe, Dostoevsky, Prokofiev, Dali, | |
// this element is not covered | |
// by any case in the switch expression below | |
// which results to compilation failure | |
Gaudi | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WhoIsWho { | |
enum Person { | |
Mozart, Picasso, Goethe, Dostoevsky, Prokofiev, Dali | |
} | |
public static void main(String[] args) { | |
print(Person.Mozart); | |
print(Person.Dali); | |
print(Person.Dostoevsky); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ClassicSwitchStatement { | |
enum Person { | |
Mozart, Picasso, Goethe, Dostoevsky, Prokofiev, Dali | |
} | |
public static void main(String[] args) { | |
print(Person.Mozart); | |
print(Person.Dali); | |
print(Person.Dostoevsky); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gypsyengineer.jackson.unsafe.one; | |
import com.fasterxml.jackson.annotation.JsonTypeInfo; | |
import java.io.Serializable; | |
public class Person { | |
public String name; | |
public int age; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gypsyengineer.jackson.unsafe.one; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.json.JsonMapper; | |
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator; | |
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator; | |
public class SaferPersonDeserialization { | |
private static final String bad = |