Skip to content

Commit 12372fc

Browse files
committed
fix test cases, adapt Java-Redux
1 parent 7204b7e commit 12372fc

12 files changed

Lines changed: 212 additions & 94 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class MostSimpleInner {
2+
3+
public static class MyInnerClass {
4+
}
5+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
record Mapping(String from, String to) {
2+
3+
Mapping {
4+
// compact constructor!
5+
from = "abc";
6+
to = "def";
7+
}
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import java.lang.Object;
2+
3+
public class OuterClass {
4+
5+
final record MyRecord(String test) {
6+
}
7+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//https://mikemybytes.com/2022/02/16/java-records-and-compact-constructors/
2+
record // package-private
3+
Name(// package-private
4+
String name) {
5+
6+
// fails with: 'invalid canonical constructor in record Name
7+
// (attempting to assign stronger access privileges;
8+
// was package)'
9+
private Name(String name) {
10+
this.name = name;
11+
}
12+
13+
static Name of(String name) {
14+
return new Name(name);
15+
}
16+
}
17+
18+
record Point(int x, int y) {
19+
20+
Point(int x, int y) {
21+
// boring!
22+
this.x = x;
23+
this.y = y;
24+
}
25+
26+
Point(int x) {
27+
// a bit weird...
28+
// ... but perfectly fine for the compiler
29+
this(x, 0);
30+
}
31+
32+
Point() {
33+
// fails with: 'constructor is not canonical, so its first
34+
// statement must invoke another constructor'
35+
}
36+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
record Point3d(int x, int y, int z) {
2+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
record MyRecord(String test) {
2+
3+
public int hashCode() {
4+
return 0;
5+
}
6+
7+
public boolean equals(Object obj) {
8+
return obj instanceof MyRecord;
9+
}
10+
11+
public String test() {
12+
return "";
13+
}
14+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public record SimpleRecord(/*@ nullable */ String name) implements Serializable {
2+
3+
SimpleRecord(String name) {
4+
this.name = name;
5+
}
6+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class Test {
2+
3+
public static int abc;
4+
5+
static {
6+
// should be resolved to 2
7+
abc = 1 + 1;
8+
}
9+
10+
public int memberVar;
11+
12+
{
13+
memberVar = 42;
14+
}
15+
}
16+
17+
public class SubClass extends Test {
18+
19+
public int memberVar;
20+
21+
{
22+
memberVar = 41;
23+
}
24+
}

key.core/src/main/java/de/uka/ilkd/key/java/transformations/pipeline/TryWithResourceReducer.java

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
/* This file is part of KeY - https://key-project.org
2+
* KeY is licensed under the GNU General Public License Version 2
3+
* SPDX-License-Identifier: GPL-2.0-only */
14
package de.uka.ilkd.key.java.transformations.pipeline;
25

6+
import java.util.List;
7+
import java.util.concurrent.atomic.AtomicInteger;
8+
39
import com.github.javaparser.ast.NodeList;
410
import com.github.javaparser.ast.body.Parameter;
511
import com.github.javaparser.ast.body.TypeDeclaration;
@@ -10,41 +16,39 @@
1016
import com.github.javaparser.ast.visitor.ModifierVisitor;
1117
import com.github.javaparser.ast.visitor.Visitable;
1218

13-
import java.util.List;
14-
import java.util.concurrent.atomic.AtomicInteger;
15-
1619
/// Rewrites every try-with-resources statement into the equivalent nested
1720
/// try/finally form that javac itself generates, i.e. for
1821
/// ```java
1922
/// try (Resource r = open()) {
20-
/// body;
23+
/// body;
2124
/// }
2225
/// ```
2326
/// this produces:
2427
/// ```java
2528
/// try {
26-
/// Resource r = open();
27-
/// Throwable primaryExc$1 = null;
28-
/// try {
29-
/// body;
30-
/// } catch (Throwable t$1) {
31-
/// primaryExc$1 = t$1;
32-
/// throw t$1;
33-
/// } finally {
34-
/// if (r != null) {
35-
/// if (primaryExc$1 != null) {
36-
/// try { r.close(); } catch (Throwable suppressedExc$1) {
37-
/// primaryExc$1.addSuppressed(suppressedExc$1);
38-
/// }
39-
/// } else { r.close(); }
40-
/// }
41-
/// }
29+
/// Resource r = open();
30+
/// Throwable primaryExc$1 = null;
31+
/// try {
32+
/// body;
33+
/// } catch (Throwable t$1) {
34+
/// primaryExc$1 = t$1;
35+
/// throw t$1;
36+
/// } finally {
37+
/// if (r != null) {
38+
/// if (primaryExc$1 != null) {
39+
/// try { r.close(); } catch (Throwable suppressedExc$1) {
40+
/// primaryExc$1.addSuppressed(suppressedExc$1);
41+
/// }
42+
/// } else { r.close(); }
43+
/// }
44+
/// }
4245
/// }
4346
/// ```
4447
/// Multiple resources are handled by nesting (last-declared resource closes first),
4548
/// and any catch/finally clauses that were attached to the original try are kept
4649
/// on an outer try that wraps the whole resource-management block, exactly as
4750
/// javac does it.
51+
///
4852
/// @author weigl, Claude Sonnet
4953
public class TryWithResourceReducer implements JavaTransformer {
5054
private final AtomicInteger counter = new AtomicInteger();
@@ -91,18 +95,17 @@ private BlockStmt desugarResources(List<Expression> resources, int index, BlockS
9195
BlockStmt result = new BlockStmt();
9296

9397
Expression resourceExpr = resources.get(index);
94-
String resourceName;
98+
Expression resourceName;
9599

96-
if (resourceExpr instanceof VariableDeclarationExpr) {
100+
if (resourceExpr instanceof VariableDeclarationExpr vde) {
97101
// try (Resource r = new Resource()) { ... }
98-
VariableDeclarationExpr vde = (VariableDeclarationExpr) resourceExpr;
99102
VariableDeclarator vd = vde.getVariable(0);
100-
resourceName = vd.getNameAsString();
103+
resourceName = vd.getNameAsExpression();
101104
result.addStatement(new ExpressionStmt(vde.clone()));
102105
} else {
103106
// Java 9+ form: try (existingEffectivelyFinalVar) { ... }
104107
// No declaration needed -- the variable already exists in scope.
105-
resourceName = resourceExpr.toString();
108+
resourceName = resourceExpr;
106109
}
107110

108111
int id = counter.incrementAndGet();
@@ -118,14 +121,17 @@ private BlockStmt desugarResources(List<Expression> resources, int index, BlockS
118121

119122
// catch (Throwable t$n) { primaryExc$n = t$n; throw t$n; }
120123
CatchClause catchClause = new CatchClause();
121-
catchClause.setParameter(new Parameter(new ClassOrInterfaceType(null, "Throwable"), throwableName));
124+
catchClause.setParameter(
125+
new Parameter(new ClassOrInterfaceType(null, "Throwable"), throwableName));
122126
BlockStmt catchBody = new BlockStmt();
123127
catchBody.addStatement(new ExpressionStmt(
124-
new AssignExpr(new NameExpr(primaryExcName), new NameExpr(throwableName), AssignExpr.Operator.ASSIGN)));
128+
new AssignExpr(new NameExpr(primaryExcName), new NameExpr(throwableName),
129+
AssignExpr.Operator.ASSIGN)));
125130
catchBody.addStatement(new ThrowStmt(new NameExpr(throwableName)));
126131
catchClause.setBody(catchBody);
127132

128-
// finally { if (r != null) { if (primaryExc$n != null) { try { r.close(); } catch (Throwable s) { primaryExc$n.addSuppressed(s); } } else { r.close(); } } }
133+
// finally { if (r != null) { if (primaryExc$n != null) { try { r.close(); } catch
134+
// (Throwable s) { primaryExc$n.addSuppressed(s); } } else { r.close(); } } }
129135
BlockStmt finallyBlock = new BlockStmt();
130136

131137
IfStmt excCheck = new IfStmt();
@@ -137,10 +143,12 @@ private BlockStmt desugarResources(List<Expression> resources, int index, BlockS
137143
closeWithSuppression.setTryBlock(closeTryBlock);
138144

139145
CatchClause suppressCatch = new CatchClause();
140-
suppressCatch.setParameter(new Parameter(new ClassOrInterfaceType(null, "Throwable"), suppressedExcName));
146+
suppressCatch.setParameter(
147+
new Parameter(new ClassOrInterfaceType(null, "Throwable"), suppressedExcName));
141148
BlockStmt suppressBody = new BlockStmt();
142149
suppressBody.addStatement(new ExpressionStmt(new MethodCallExpr(
143-
new NameExpr(primaryExcName), "addSuppressed", NodeList.nodeList(new NameExpr(suppressedExcName)))));
150+
new NameExpr(primaryExcName), "addSuppressed",
151+
NodeList.nodeList(new NameExpr(suppressedExcName)))));
144152
suppressCatch.setBody(suppressBody);
145153
closeWithSuppression.setCatchClauses(NodeList.nodeList(suppressCatch));
146154

@@ -151,7 +159,7 @@ private BlockStmt desugarResources(List<Expression> resources, int index, BlockS
151159
excCheck.setElseStmt(plainClose);
152160

153161
IfStmt nullCheck = new IfStmt();
154-
nullCheck.setCondition(notNull(new NameExpr(resourceName)));
162+
nullCheck.setCondition(notNull(resourceName.clone()));
155163
BlockStmt nullCheckThen = new BlockStmt();
156164
nullCheckThen.addStatement(excCheck);
157165
nullCheck.setThenStmt(nullCheckThen);
@@ -171,13 +179,15 @@ private static BinaryExpr notNull(Expression e) {
171179
return new BinaryExpr(e, new NullLiteralExpr(), BinaryExpr.Operator.NOT_EQUALS);
172180
}
173181

174-
private static MethodCallExpr closeCall(String resourceName) {
175-
return new MethodCallExpr(new NameExpr(resourceName), "close");
182+
private static MethodCallExpr closeCall(Expression resourceName) {
183+
return new MethodCallExpr(resourceName.clone(), "close");
176184
}
177185

178-
private static Statement declareVar(String typeName, String varName, Expression initializer) {
179-
VariableDeclarator vd = new VariableDeclarator(new ClassOrInterfaceType(null, typeName), varName, initializer);
186+
private static Statement declareVar(String typeName, String varName,
187+
Expression initializer) {
188+
VariableDeclarator vd = new VariableDeclarator(new ClassOrInterfaceType(null, typeName),
189+
varName, initializer);
180190
return new ExpressionStmt(new VariableDeclarationExpr(vd));
181191
}
182192
}
183-
}
193+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package java.lang;
2+
3+
public interface AutoCloseable {
4+
void close() throws Exception;
5+
}

0 commit comments

Comments
 (0)