Skip to content

Commit 03fabfc

Browse files
authored
fix(jdk-codemodel): resolve instanceof, new-object, and new-array type references to TypeUsage (#11)
1 parent 7f70329 commit 03fabfc

5 files changed

Lines changed: 120 additions & 84 deletions

File tree

jdk-codemodel/src/main/java/build/codemodel/jdk/JdkExpressionConverter.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -285,27 +285,26 @@ public Expression visitNewClass(final NewClassTree t, final Void v) {
285285
final var args = t.getArguments().stream()
286286
.map(this::convert)
287287
.toList();
288-
final Expression typeExpr;
289-
final List<Expression> typeArgs;
288+
final TypeUsage type;
289+
final List<TypeUsage> typeArgs;
290290
if (t.getIdentifier() instanceof ParameterizedTypeTree pt) {
291-
typeExpr = Identifier.of(codeModel, pt.getType().toString());
291+
type = resolveTypeUsage(pt.getType());
292292
typeArgs = pt.getTypeArguments().stream()
293-
.map(typeArg -> (Expression) Identifier.of(codeModel, typeArg.toString()))
293+
.map(this::resolveTypeUsage)
294294
.toList();
295295
} else {
296-
typeExpr = Identifier.of(codeModel, t.getIdentifier().toString());
296+
type = resolveTypeUsage(t.getIdentifier());
297297
typeArgs = List.of();
298298
}
299-
return NewObject.of(codeModel, typeExpr, args.stream(), typeArgs.stream());
299+
return NewObject.of(codeModel, type, args.stream(), typeArgs.stream());
300300
}
301301

302302
@Override
303303
public Expression visitNewArray(final NewArrayTree t, final Void v) {
304304
final List<Expression> dims = t.getDimensions() == null
305305
? List.of()
306306
: t.getDimensions().stream().map(this::convert).toList();
307-
final String elementTypeName = t.getType() == null ? "?" : t.getType().toString();
308-
return NewArray.of(codeModel, Identifier.of(codeModel, elementTypeName), dims.stream());
307+
return NewArray.of(codeModel, resolveTypeUsage(t.getType()), dims.stream());
309308
}
310309

311310
@Override
@@ -421,7 +420,7 @@ public Expression visitInstanceOf(final InstanceOfTree t, final Void v) {
421420
}
422421
return InstanceOf.of(
423422
convert(t.getExpression()),
424-
Identifier.of(codeModel, t.getType().toString()),
423+
resolveTypeUsage(t.getType()),
425424
bindingVariable);
426425
}
427426

jdk-codemodel/src/main/java/build/codemodel/jdk/expression/InstanceOf.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* Licensed under the Apache License, Version 2.0 (the "License");
1010
* you may not use this file except in compliance with the License.
1111
* You may obtain a copy of the License at
12-
*
12+
*
1313
* http://www.apache.org/licenses/LICENSE-2.0
14-
*
14+
*
1515
* Unless required by applicable law or agreed to in writing, software
1616
* distributed under the License is distributed on an "AS IS" BASIS,
1717
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -31,6 +31,7 @@
3131
import build.codemodel.expression.Expression;
3232
import build.codemodel.foundation.CodeModel;
3333
import build.codemodel.foundation.descriptor.Trait;
34+
import build.codemodel.foundation.usage.TypeUsage;
3435

3536
import java.lang.invoke.MethodHandles;
3637
import java.util.Objects;
@@ -52,21 +53,21 @@ public final class InstanceOf
5253
private final Expression expression;
5354

5455
/**
55-
* The type expression on the right-hand side of {@code instanceof}.
56+
* The resolved type on the right-hand side of {@code instanceof}.
5657
*/
57-
private final Expression typeExpression;
58+
private final TypeUsage type;
5859

5960
/**
6061
* The optional pattern-binding variable name (Java 16+ pattern matching).
6162
*/
6263
private final Optional<String> bindingVariable;
6364

6465
private InstanceOf(final Expression expression,
65-
final Expression typeExpression,
66+
final TypeUsage type,
6667
final Optional<String> bindingVariable) {
6768
super(Objects.requireNonNull(expression, "expression must not be null").codeModel());
6869
this.expression = expression;
69-
this.typeExpression = Objects.requireNonNull(typeExpression, "typeExpression must not be null");
70+
this.type = Objects.requireNonNull(type, "type must not be null");
7071
this.bindingVariable = bindingVariable == null ? Optional.empty() : bindingVariable;
7172
}
7273

@@ -75,23 +76,23 @@ public InstanceOf(@Bound final CodeModel codeModel,
7576
final Marshaller marshaller,
7677
final Stream<Marshalled<Trait>> traits,
7778
final Marshalled<Expression> expression,
78-
final Marshalled<Expression> typeExpression,
79+
final Marshalled<TypeUsage> type,
7980
final Optional<String> bindingVariable) {
8081
super(codeModel, marshaller, traits);
8182
this.expression = marshaller.unmarshal(expression);
82-
this.typeExpression = marshaller.unmarshal(typeExpression);
83+
this.type = marshaller.unmarshal(type);
8384
this.bindingVariable = bindingVariable == null ? Optional.empty() : bindingVariable;
8485
}
8586

8687
@Marshal
8788
public void destructor(final Marshaller marshaller,
8889
final Out<Stream<Marshalled<Trait>>> traits,
8990
final Out<Marshalled<Expression>> expression,
90-
final Out<Marshalled<Expression>> typeExpression,
91+
final Out<Marshalled<TypeUsage>> type,
9192
final Out<Optional<String>> bindingVariable) {
9293
super.destructor(marshaller, traits);
9394
expression.set(marshaller.marshal(this.expression));
94-
typeExpression.set(marshaller.marshal(this.typeExpression));
95+
type.set(marshaller.marshal(this.type));
9596
bindingVariable.set(this.bindingVariable);
9697
}
9798

@@ -105,12 +106,12 @@ public Expression expression() {
105106
}
106107

107108
/**
108-
* Obtains the type expression on the right-hand side of {@code instanceof}.
109+
* Obtains the resolved type on the right-hand side of {@code instanceof}.
109110
*
110-
* @return the type {@link Expression}
111+
* @return the {@link TypeUsage}
111112
*/
112-
public Expression typeExpression() {
113-
return this.typeExpression;
113+
public TypeUsage checkedType() {
114+
return this.type;
114115
}
115116

116117
/**
@@ -126,7 +127,7 @@ public Optional<String> bindingVariable() {
126127
public boolean equals(final Object object) {
127128
return object instanceof InstanceOf other
128129
&& Objects.equals(this.expression, other.expression)
129-
&& Objects.equals(this.typeExpression, other.typeExpression)
130+
&& Objects.equals(this.type, other.type)
130131
&& Objects.equals(this.bindingVariable, other.bindingVariable)
131132
&& super.equals(other);
132133
}
@@ -135,25 +136,25 @@ public boolean equals(final Object object) {
135136
* Creates an {@link InstanceOf} expression.
136137
*
137138
* @param expression the expression being tested
138-
* @param typeExpression the type expression on the right-hand side
139+
* @param type the resolved {@link TypeUsage} on the right-hand side
139140
* @param bindingVariable the optional pattern-binding variable name
140141
* @return a new {@link InstanceOf}
141142
*/
142143
public static InstanceOf of(final Expression expression,
143-
final Expression typeExpression,
144+
final TypeUsage type,
144145
final Optional<String> bindingVariable) {
145-
return new InstanceOf(expression, typeExpression, bindingVariable);
146+
return new InstanceOf(expression, type, bindingVariable);
146147
}
147148

148149
/**
149150
* Creates a classic (non-pattern) {@link InstanceOf} expression.
150151
*
151-
* @param expression the expression being tested
152-
* @param typeExpression the type expression on the right-hand side
152+
* @param expression the expression being tested
153+
* @param type the resolved {@link TypeUsage} on the right-hand side
153154
* @return a new {@link InstanceOf}
154155
*/
155-
public static InstanceOf of(final Expression expression, final Expression typeExpression) {
156-
return new InstanceOf(expression, typeExpression, Optional.empty());
156+
public static InstanceOf of(final Expression expression, final TypeUsage type) {
157+
return new InstanceOf(expression, type, Optional.empty());
157158
}
158159

159160
static {

jdk-codemodel/src/main/java/build/codemodel/jdk/expression/NewArray.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* Licensed under the Apache License, Version 2.0 (the "License");
1010
* you may not use this file except in compliance with the License.
1111
* You may obtain a copy of the License at
12-
*
12+
*
1313
* http://www.apache.org/licenses/LICENSE-2.0
14-
*
14+
*
1515
* Unless required by applicable law or agreed to in writing, software
1616
* distributed under the License is distributed on an "AS IS" BASIS,
1717
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -31,6 +31,7 @@
3131
import build.codemodel.expression.Expression;
3232
import build.codemodel.foundation.CodeModel;
3333
import build.codemodel.foundation.descriptor.Trait;
34+
import build.codemodel.foundation.usage.TypeUsage;
3435

3536
import java.lang.invoke.MethodHandles;
3637
import java.util.ArrayList;
@@ -48,17 +49,17 @@ public final class NewArray
4849
extends AbstractExpression {
4950

5051
/**
51-
* The element type expression.
52+
* The resolved element type.
5253
*/
53-
private final Expression elementType;
54+
private final TypeUsage elementType;
5455

5556
/**
5657
* The dimension expressions.
5758
*/
5859
private final ArrayList<Expression> dimensions;
5960

6061
private NewArray(final CodeModel codeModel,
61-
final Expression elementType,
62+
final TypeUsage elementType,
6263
final Stream<Expression> dimensions) {
6364
super(codeModel);
6465
this.elementType = Objects.requireNonNull(elementType, "elementType must not be null");
@@ -71,7 +72,7 @@ private NewArray(final CodeModel codeModel,
7172
public NewArray(@Bound final CodeModel codeModel,
7273
final Marshaller marshaller,
7374
final Stream<Marshalled<Trait>> traits,
74-
final Marshalled<Expression> elementType,
75+
final Marshalled<TypeUsage> elementType,
7576
final Stream<Marshalled<Expression>> dimensions) {
7677
super(codeModel, marshaller, traits);
7778
this.elementType = marshaller.unmarshal(elementType);
@@ -83,19 +84,19 @@ public NewArray(@Bound final CodeModel codeModel,
8384
@Marshal
8485
public void destructor(final Marshaller marshaller,
8586
final Out<Stream<Marshalled<Trait>>> traits,
86-
final Out<Marshalled<Expression>> elementType,
87+
final Out<Marshalled<TypeUsage>> elementType,
8788
final Out<Stream<Marshalled<Expression>>> dimensions) {
8889
super.destructor(marshaller, traits);
8990
elementType.set(marshaller.marshal(this.elementType));
9091
dimensions.set(this.dimensions.stream().map(marshaller::marshal));
9192
}
9293

9394
/**
94-
* Obtains the element type expression.
95+
* Obtains the resolved element type.
9596
*
96-
* @return the element type {@link Expression}
97+
* @return the element {@link TypeUsage}
9798
*/
98-
public Expression elementType() {
99+
public TypeUsage elementType() {
99100
return this.elementType;
100101
}
101102

@@ -119,13 +120,13 @@ public boolean equals(final Object object) {
119120
/**
120121
* Creates a {@link NewArray} expression.
121122
*
122-
* @param codeModel the {@link CodeModel}
123-
* @param elementType the element type {@link Expression}
123+
* @param codeModel the {@link CodeModel}
124+
* @param elementType the resolved element {@link TypeUsage}
124125
* @param dimensions the dimension {@link Expression}s
125126
* @return a new {@link NewArray}
126127
*/
127128
public static NewArray of(final CodeModel codeModel,
128-
final Expression elementType,
129+
final TypeUsage elementType,
129130
final Stream<Expression> dimensions) {
130131
return new NewArray(codeModel, elementType, dimensions);
131132
}

0 commit comments

Comments
 (0)