Skip to content

Commit 7c87365

Browse files
authored
Merge pull request eugenp#4 from eugenp/master
Update from master
2 parents 1f45a31 + d172168 commit 7c87365

148 files changed

Lines changed: 4368 additions & 252 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎checker-plugin/pom.xml‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>checker-plugin</artifactId>
6+
<packaging>jar</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>checker-plugin</name>
9+
<url>http://maven.apache.org</url>
10+
11+
<!-- https://checkerframework.org/manual/#maven -->
12+
13+
<properties>
14+
<!-- These properties will be set by the Maven Dependency plugin -->
15+
<annotatedJdk>${org.checkerframework:jdk8:jar}</annotatedJdk>
16+
<!-- Uncomment to use the Type Annotations compiler. -->
17+
<!--
18+
<typeAnnotationsJavac>${org.checkerframework:compiler:jar}</typeAnnotationsJavac>
19+
-->
20+
</properties>
21+
22+
<dependencies>
23+
24+
25+
<!-- Annotations from the Checker Framework: nullness, interning, locking, ... -->
26+
<dependency>
27+
<groupId>org.checkerframework</groupId>
28+
<artifactId>checker-qual</artifactId>
29+
<version>2.3.1</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.checkerframework</groupId>
33+
<artifactId>checker</artifactId>
34+
<version>2.3.1</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.checkerframework</groupId>
38+
<artifactId>jdk8</artifactId>
39+
<version>2.3.1</version>
40+
</dependency>
41+
<!-- The Type Annotations compiler. Uncomment if using annotations in comments. -->
42+
<dependency>
43+
<groupId>org.checkerframework</groupId>
44+
<artifactId>compiler</artifactId>
45+
<version>2.3.1</version>
46+
</dependency>
47+
48+
</dependencies>
49+
50+
<build>
51+
<plugins>
52+
53+
<plugin>
54+
<!-- This plugin will set properties values using dependency information -->
55+
<groupId>org.apache.maven.plugins</groupId>
56+
<artifactId>maven-dependency-plugin</artifactId>
57+
<executions>
58+
<execution>
59+
<goals>
60+
<!--
61+
Goal that sets a property pointing to the artifact file for each project dependency.
62+
For each dependency (direct and transitive) a project property will be set which
63+
follows the:
64+
65+
groupId:artifactId:type:[classifier]
66+
67+
form and contains the path to the resolved artifact. -->
68+
<goal>properties</goal>
69+
</goals>
70+
</execution>
71+
</executions>
72+
</plugin>
73+
74+
<plugin>
75+
<artifactId>maven-compiler-plugin</artifactId>
76+
<version>3.6.1</version>
77+
<configuration>
78+
<source>1.8</source>
79+
<target>1.8</target>
80+
<!-- Uncomment the following line to use the type annotations compiler. -->
81+
<!-- <fork>true</fork> -->
82+
<compilerArguments>
83+
<Xmaxerrs>10000</Xmaxerrs>
84+
<Xmaxwarns>10000</Xmaxwarns>
85+
</compilerArguments>
86+
<annotationProcessors>
87+
<!-- Add all the checkers you want to enable here -->
88+
<annotationProcessor>org.checkerframework.checker.nullness.NullnessChecker</annotationProcessor>
89+
<annotationProcessor>org.checkerframework.checker.interning.InterningChecker</annotationProcessor>
90+
<annotationProcessor>org.checkerframework.checker.fenum.FenumChecker</annotationProcessor>
91+
<annotationProcessor>org.checkerframework.checker.formatter.FormatterChecker</annotationProcessor>
92+
<annotationProcessor>org.checkerframework.checker.regex.RegexChecker</annotationProcessor>
93+
</annotationProcessors>
94+
<compilerArgs>
95+
<arg>-AprintErrorStack</arg>
96+
97+
<!-- location of the annotated JDK, which comes from a Maven dependency -->
98+
<arg>-Xbootclasspath/p:${annotatedJdk}</arg>
99+
<!--
100+
-->
101+
102+
<!-- Uncomment the following line to use the type annotations compiler. -->
103+
<!--
104+
<arg>-J-Xbootclasspath/p:${typeAnnotationsJavac}</arg>
105+
-->
106+
<!-- Uncomment the following line to turn type-checking warnings into errors. -->
107+
<arg>-Awarns</arg>
108+
</compilerArgs>
109+
</configuration>
110+
</plugin>
111+
</plugins>
112+
</build>
113+
114+
</project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.typechecker;
2+
3+
import org.checkerframework.checker.fenum.qual.Fenum;
4+
5+
//@SuppressWarnings("fenum:assignment.type.incompatible")
6+
public class FakeNumExample {
7+
8+
// Here we use some String constants to represents countries.
9+
static final @Fenum("country") String ITALY = "IT";
10+
static final @Fenum("country") String US = "US";
11+
static final @Fenum("country") String UNITED_KINGDOM = "UK";
12+
13+
// Here we use other String constants to represent planets instead.
14+
static final @Fenum("planet") String MARS = "Mars";
15+
static final @Fenum("planet") String EARTH = "Earth";
16+
static final @Fenum("planet") String VENUS = "Venus";
17+
18+
// Now we write this method and we want to be sure that
19+
// the String parameter has a value of a country, not that is just a String.
20+
void greetCountries(@Fenum("country") String country) {
21+
System.out.println("Hello " + country);
22+
}
23+
24+
// Similarly we're enforcing here that the provided
25+
// parameter is a String that represent a planet.
26+
void greetPlanets(@Fenum("planet") String planet) {
27+
System.out.println("Hello " + planet);
28+
}
29+
30+
public static void main(String[] args) {
31+
32+
FakeNumExample obj = new FakeNumExample();
33+
34+
// This will fail because we pass a planet-String to a method that
35+
// accept a country-String.
36+
obj.greetCountries(MARS);
37+
38+
// Here the opposite happens.
39+
obj.greetPlanets(US);
40+
}
41+
42+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.typechecker;
2+
3+
public class FormatExample {
4+
5+
public static void main(String[] args) {
6+
7+
// Just enabling org.checkerframework.checker.formatter.FormatterChecker
8+
// we can be sure at compile time that the format strings we pass to format()
9+
// are correct. Normally we would have those errors raised just at runtime.
10+
// All those formats are in fact wrong.
11+
12+
String.format("%y", 7); // error: invalid format string
13+
14+
String.format("%d", "a string"); // error: invalid argument type for %d
15+
16+
String.format("%d %s", 7); // error: missing argument for %s
17+
18+
String.format("%d", 7, 3); // warning: unused argument 3
19+
20+
String.format("{0}", 7); // warning: unused argument 7, because {0} is wrong syntax
21+
}
22+
23+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.typechecker;
2+
3+
import org.checkerframework.checker.nullness.qual.KeyFor;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class KeyForExample {
9+
10+
private final Map<String, String> config = new HashMap<>();
11+
12+
KeyForExample() {
13+
14+
// Here we initialize a map to store
15+
// some config data.
16+
config.put("url", "http://1.2.3.4");
17+
config.put("name", "foobaz");
18+
}
19+
20+
public void dumpPort() {
21+
22+
// Here, we want to dump the port value stored in the
23+
// config, so we declare that this key has to be
24+
// present in the config map.
25+
// Obviously that will fail because such key is not present
26+
// in the map.
27+
@KeyFor("config") String key = "port";
28+
System.out.println( config.get(key) );
29+
}
30+
31+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.typechecker;
2+
3+
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
4+
5+
import java.util.Date;
6+
7+
public class MonotonicNotNullExample {
8+
9+
// The idea is we need this field to be to lazily initialized,
10+
// so it starts as null but once it becomes not null
11+
// it cannot return null.
12+
// In these cases, we can use @MonotonicNonNull
13+
@MonotonicNonNull private Date firstCall;
14+
15+
public Date getFirstCall() {
16+
if (firstCall == null) {
17+
firstCall = new Date();
18+
}
19+
return firstCall;
20+
}
21+
22+
public void reset() {
23+
// This is reported as error because
24+
// we wrongly set the field back to null.
25+
firstCall = null;
26+
}
27+
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.typechecker;
2+
3+
import org.checkerframework.checker.nullness.qual.NonNull;
4+
import org.checkerframework.checker.nullness.qual.Nullable;
5+
6+
public class NonNullExample {
7+
8+
// This method's parameter is annotated in order
9+
// to tell the pluggable type system that it can never be null.
10+
private static int countArgs(@NonNull String[] args) {
11+
return args.length;
12+
}
13+
14+
// This method's parameter is annotated in order
15+
// to tell the pluggable type system that it may be null.
16+
public static void main(@Nullable String[] args) {
17+
18+
// Here lies a potential error,
19+
// because we pass a potential null reference to a method
20+
// that does not accept nulls.
21+
// The Checker Framework will spot this problem at compile time
22+
// instead of runtime.
23+
System.out.println(countArgs(args));
24+
25+
}
26+
27+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.typechecker;
2+
3+
import org.checkerframework.checker.regex.qual.Regex;
4+
5+
public class RegexExample {
6+
7+
// For some reason we want to be sure that this regex
8+
// contains at least one capturing group.
9+
// However, we do an error and we forgot to define such
10+
// capturing group in it.
11+
@Regex(1) private static String findNumbers = "\\d*";
12+
13+
public static void main(String[] args) {
14+
String message = "My phone number is +3911223344.";
15+
message.matches(findNumbers);
16+
}
17+
18+
}

‎core-groovy/README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Groovy
2+
3+
## Relevant articles:
4+

0 commit comments

Comments
 (0)