Skip to content

Commit 073561f

Browse files
committed
Add java25 samples from PMD
1 parent f877241 commit 073561f

File tree

5 files changed

+152
-1
lines changed

5 files changed

+152
-1
lines changed
12.2 KB
Binary file not shown.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<packaging>jar</packaging>
88

99
<properties>
10-
<maven.compiler.release>22</maven.compiler.release>
10+
<maven.compiler.release>25</maven.compiler.release>
1111
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1212
<!-- fixing the build timestamp of the jar so that the jar file only changes, if content changes.
1313
the actual timestamp is irrelevant. -->
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
package net.sourceforge.pmd.java.regression.tests.java25;
3+
4+
import module java.base;
5+
import module java.desktop;
6+
7+
import java.util.List;
8+
9+
/**
10+
* @see <a href="https://openjdk.org/jeps/511">JEP 511: Module Import Declarations</a> (Java 25)
11+
*/
12+
public class Jep511_ModuleImportDeclarations {
13+
public static void main(String[] args) {
14+
File f = new File(".");
15+
List<File> myList = new ArrayList<>();
16+
myList.add(f);
17+
System.out.println("myList = " + myList);
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
3+
// explicit imports are possible as well (although not really needed in this example)
4+
import java.util.Arrays;
5+
import java.util.stream.Collectors;
6+
7+
import static java.lang.IO.println;
8+
9+
/**
10+
* @see <a href="https://openjdk.org/jeps/512">JEP 512: Compact Source Files and Instance Main Methods</a> (Java 25)
11+
*/
12+
13+
// Top-level members are interpreted as members of the implicit class (methods and fields)
14+
String greetingField = "Hello, World! (Field)";
15+
String greeting() {
16+
return "Hello, World! (Method)";
17+
}
18+
String greetingText2 = Arrays.asList("Hello", "World!", "(with imports)").stream().collect(Collectors.joining(", "));
19+
20+
void main() {
21+
System.out.println(greetingField);
22+
IO.println(greeting()); // java.lang.IO.println via qualifier
23+
println(greetingText2); // java.lang.IO.println via static import
24+
25+
// java.lang.IO.readln
26+
String name = IO.readln("Please enter your name: ");
27+
IO.print("Pleased to meet you, ");
28+
println(name);
29+
30+
// java.util.List is automatically available via implicit "import module java.base"
31+
var authors = List.of("James", "Bill", "Guy", "Alex", "Dan", "Gavin");
32+
for (var authorName : authors) {
33+
println(authorName + ": " + authorName.length());
34+
}
35+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
package net.sourceforge.pmd.java.regression.tests.java25;
3+
4+
import java.math.BigInteger;
5+
import java.security.cert.Certificate;
6+
import java.security.interfaces.DSAPublicKey;
7+
import java.security.interfaces.RSAKey;
8+
9+
/**
10+
* @see <a href="https://openjdk.org/jeps/513">JEP 513: Flexible Constructor Bodies</a> (Java 25)
11+
*/
12+
public class Jep513_FlexibleConstructorBodies {
13+
14+
// Example: Validating superclass constructor arguments
15+
public class PositiveBigInteger extends BigInteger {
16+
public PositiveBigInteger(long value) {
17+
if (value <= 0) throw new IllegalArgumentException("value must be positive");
18+
super(String.valueOf(value));
19+
}
20+
}
21+
22+
// Example: Preparing superclass constructor arguments
23+
public class Super {
24+
public Super(byte[] bytes) {}
25+
}
26+
public class Sub extends Super {
27+
public Sub(Certificate certificate) {
28+
var publicKey = certificate.getPublicKey();
29+
if (publicKey == null) throw new NullPointerException();
30+
byte[] certBytes = switch (publicKey) {
31+
case RSAKey rsaKey -> rsaKey.getModulus().toByteArray();
32+
case DSAPublicKey dsaKey -> dsaKey.getY().toByteArray();
33+
default -> new byte[0];
34+
};
35+
super(certBytes);
36+
}
37+
}
38+
39+
// Example: Sharing superclass constructor arguments
40+
public class C {
41+
private final int i;
42+
public C(int i) {
43+
this.i = i;
44+
}
45+
}
46+
public class Super2 {
47+
private final C x;
48+
private final C y;
49+
public Super2(C x, C y) {
50+
this.x = x;
51+
this.y = y;
52+
}
53+
}
54+
public class Sub2 extends Super2 {
55+
public Sub2(int i) {
56+
var x = new C(i);
57+
super(x, x);
58+
}
59+
}
60+
61+
// Using enclosing instances in early construction contexts
62+
class Outer {
63+
int i;
64+
void hello() { System.out.println("Hello"); }
65+
66+
class Inner {
67+
int j;
68+
69+
Inner() {
70+
var x = i; // OK - implicitly refers to field of enclosing instance
71+
var y = Outer.this.i; // OK - explicitly refers to field of enclosing instance
72+
hello(); // OK - implicitly refers to method of enclosing instance
73+
Outer.this.hello(); // OK - explicitly refers to method of enclosing instance
74+
super();
75+
}
76+
}
77+
}
78+
79+
// Early assignment to fields
80+
class Super3 {
81+
Super3() { overriddenMethod(); }
82+
void overriddenMethod() { System.out.println("hello"); }
83+
}
84+
class Sub3 extends Super3 {
85+
final int x;
86+
87+
Sub3(int x) {
88+
this.x = x; // Initialize the field
89+
super(); // Then invoke the Super constructor explicitly
90+
}
91+
92+
@Override
93+
void overriddenMethod() { System.out.println(x); }
94+
}
95+
96+
97+
}

0 commit comments

Comments
 (0)