Skip to content

Commit 8d230c8

Browse files
committed
chore: upgrade javaparser to 3.28.0 - parser API changes
- Bump javaparser.version from 2.5.1 to 3.28.0 - Replace JavaParser.parse() with StaticJavaParser.parse() - Add source-upgrade module for incremental testing
1 parent eeedbc4 commit 8d230c8

17 files changed

Lines changed: 338 additions & 222 deletions

adapters/source/src/main/java/io/sundr/adapter/source/AnnotationExprToAnnotationRef.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class AnnotationExprToAnnotationRef implements Function<AnnotationExpr, A
3636

3737
@Override
3838
public AnnotationRef apply(AnnotationExpr annotation) {
39-
String name = annotation.getName().getName();
39+
String name = annotation.getNameAsString();
4040
String packageName = PACKAGENAME.apply(annotation);
4141
Map<String, Object> parameters = new HashMap<>();
4242

@@ -49,7 +49,7 @@ public AnnotationRef apply(AnnotationExpr annotation) {
4949
NormalAnnotationExpr normal = (NormalAnnotationExpr) annotation;
5050
for (MemberValuePair pair : normal.getPairs()) {
5151
String key = pair.getName().toString();
52-
Object value = pair.getData();
52+
Object value = pair.getValue().toString();
5353
parameters.put(key, value);
5454
}
5555
}

adapters/source/src/main/java/io/sundr/adapter/source/BlockStmtToBlock.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public class BlockStmtToBlock implements Function<BlockStmt, Block> {
3232

3333
@Override
3434
public Block apply(BlockStmt block) {
35-
List<Statement> statements = block != null && block.getStmts() != null ? block.getStmts() : Collections.emptyList();
35+
List<Statement> statements = block != null && block.getStatements() != null ? block.getStatements()
36+
: Collections.emptyList();
3637
List<io.sundr.model.Statement> converted = new ArrayList<>();
3738
for (Statement statement : statements) {
3839
converted.add(StatementConverter.convertStatement(statement));

adapters/source/src/main/java/io/sundr/adapter/source/ClassOrInterfaceToTypeRef.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
import java.util.function.Function;
2323

2424
import com.github.javaparser.ast.Node;
25+
import com.github.javaparser.ast.type.ArrayType;
2526
import com.github.javaparser.ast.type.ClassOrInterfaceType;
26-
import com.github.javaparser.ast.type.ReferenceType;
2727
import com.github.javaparser.ast.type.Type;
2828
import com.github.javaparser.ast.type.WildcardType;
2929

@@ -51,17 +51,17 @@ public ClassOrInterfaceToTypeRef(Function<Type, TypeRef> typeToTypeRef) {
5151
@Override
5252
public TypeRef apply(ClassOrInterfaceType classOrInterfaceType) {
5353
String boundPackage = PACKAGENAME.apply(classOrInterfaceType);
54-
String boundName = classOrInterfaceType.getName();
54+
String boundName = classOrInterfaceType.getNameAsString();
5555

5656
List<TypeRef> arguments = new ArrayList<TypeRef>();
57-
for (Type arg : classOrInterfaceType.getTypeArgs()) {
58-
if (arg instanceof ReferenceType) {
59-
// TODO: Need to check if this is valid for all cases...
60-
ReferenceType referenceType = (ReferenceType) arg;
61-
Type type = referenceType.getType();
62-
int dimensions = referenceType.getArrayCount();
63-
if (type instanceof ClassOrInterfaceType) {
64-
TypeRef intermediateRef = apply((ClassOrInterfaceType) type);
57+
List<Type> typeArgs = classOrInterfaceType.getTypeArguments().map(ArrayList::new).orElse(new ArrayList<>());
58+
for (Type arg : typeArgs) {
59+
if (arg instanceof ArrayType) {
60+
ArrayType arrayType = (ArrayType) arg;
61+
Type elementType = arrayType.getElementType();
62+
int dimensions = arrayType.getArrayLevel();
63+
if (elementType instanceof ClassOrInterfaceType) {
64+
TypeRef intermediateRef = apply((ClassOrInterfaceType) elementType);
6565
if (intermediateRef instanceof ClassRef) {
6666
arguments.add(new ClassRefBuilder((ClassRef) intermediateRef).withDimensions(dimensions).build());
6767
} else if (intermediateRef instanceof TypeParamRef) {
@@ -70,24 +70,26 @@ public TypeRef apply(ClassOrInterfaceType classOrInterfaceType) {
7070
throw new IllegalStateException("Expected class or type param reference");
7171
}
7272
} else {
73-
String name = referenceType.toString();
73+
String name = arrayType.toString();
7474
arguments.add(new TypeParamRefBuilder().withName(name).withDimensions(dimensions).build());
7575
}
76+
} else if (arg instanceof ClassOrInterfaceType) {
77+
arguments.add(apply((ClassOrInterfaceType) arg));
7678
} else if (arg instanceof WildcardType) {
7779
WildcardType wildcardType = (WildcardType) arg;
78-
if (wildcardType.getExtends() != null) {
79-
TypeRef bound = typeToTypeRef.apply(wildcardType.getExtends());
80+
if (wildcardType.getExtendedType().isPresent()) {
81+
TypeRef bound = typeToTypeRef.apply(wildcardType.getExtendedType().get());
8082
arguments.add(new WildcardRefBuilder().addToBounds(bound).build());
81-
} else if (wildcardType.getSuper() != null) {
82-
TypeRef bound = typeToTypeRef.apply(wildcardType.getSuper());
83+
} else if (wildcardType.getSuperType().isPresent()) {
84+
TypeRef bound = typeToTypeRef.apply(wildcardType.getSuperType().get());
8385
arguments.add(new WildcardRefBuilder().addToBounds(bound).withBoundKind(WildcardRef.BoundKind.SUPER).build());
8486
} else {
8587
arguments.add(new WildcardRef());
8688
}
8789
}
8890
}
8991

90-
if (classOrInterfaceType.getParentNode() == classOrInterfaceType) {
92+
if (classOrInterfaceType.getParentNode().orElse(null) == classOrInterfaceType) {
9193
return new TypeParamRefBuilder().withName(boundName).build();
9294
}
9395

@@ -98,7 +100,7 @@ public TypeRef apply(ClassOrInterfaceType classOrInterfaceType) {
98100
// return arguments.isEmpty()
99101
// ? new ClassRefBuilder().withDefinition(knownDefinition).build()
100102
// : knownDefinition.toReference(arguments);
101-
if (classOrInterfaceType.getTypeArgs().isEmpty() && boundName.length() == 1) {
103+
if (!classOrInterfaceType.getTypeArguments().isPresent() && boundName.length() == 1) {
102104
// We are doing our best here to distinguish between class refs and type
103105
// parameter refs.
104106
return new TypeParamRefBuilder().withName(boundName).build();

adapters/source/src/main/java/io/sundr/adapter/source/ExpressionConverter.java

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import java.util.Optional;
66
import java.util.stream.Collectors;
77

8+
import com.github.javaparser.ast.NodeList;
89
import com.github.javaparser.ast.body.Parameter;
910
import com.github.javaparser.ast.body.VariableDeclarator;
10-
import com.github.javaparser.ast.body.VariableDeclaratorId;
1111
import com.github.javaparser.ast.expr.ArrayAccessExpr;
1212
import com.github.javaparser.ast.expr.AssignExpr;
1313
import com.github.javaparser.ast.expr.BinaryExpr;
@@ -95,17 +95,17 @@ public static Declare convertVarDeclaration(VariableDeclarationExpr expr) {
9595
// Create adapters following the same pattern as TypeDeclarationToTypeDef
9696
ClassOrInterfaceToTypeRef classOrInterfaceToTypeRef = new ClassOrInterfaceToTypeRef();
9797
TypeToTypeRef typeAdapter = new TypeToTypeRef(classOrInterfaceToTypeRef);
98-
TypeRef typeRef = typeAdapter.apply(expr.getType());
98+
TypeRef typeRef = typeAdapter.apply(expr.getCommonType());
9999

100-
List<LocalVariable> variables = expr.getVars().stream()
101-
.map(v -> v.getId().getName())
100+
List<LocalVariable> variables = expr.getVariables().stream()
101+
.map(v -> v.getNameAsString())
102102
.map(n -> LocalVariable.newLocalVariable(typeRef, n))
103103
.collect(Collectors.toList());
104104

105105
// Handle initialization if present
106106
Optional<io.sundr.model.Expression> initValue = Optional.empty();
107-
if (!expr.getVars().isEmpty() && expr.getVars().get(0).getInit() != null) {
108-
initValue = Optional.of(convertExpression(expr.getVars().get(0).getInit()));
107+
if (!expr.getVariables().isEmpty() && expr.getVariables().get(0).getInitializer().isPresent()) {
108+
initValue = Optional.of(convertExpression(expr.getVariables().get(0).getInitializer().get()));
109109
}
110110

111111
return new Declare(variables, initValue);
@@ -117,7 +117,7 @@ public static Argument convertParameter(Parameter parameter) {
117117
TypeToTypeRef typeAdapter = new TypeToTypeRef(classOrInterfaceToTypeRef);
118118
TypeRef typeRef = typeAdapter.apply(parameter.getType());
119119

120-
return Argument.newArgument(typeRef, parameter.getId().getName());
120+
return Argument.newArgument(typeRef, parameter.getNameAsString());
121121
}
122122

123123
public static io.sundr.model.Expression convertExpression(Expression expression) {
@@ -127,22 +127,22 @@ public static io.sundr.model.Expression convertExpression(Expression expression)
127127
} else if (expression instanceof UnaryExpr) {
128128
UnaryExpr unaryExpr = (UnaryExpr) expression;
129129
switch (unaryExpr.getOperator()) {
130-
case positive:
131-
return new Positive(convertExpression(unaryExpr.getExpr()));
132-
case negative:
133-
return new Negative(convertExpression(unaryExpr.getExpr()));
134-
case preIncrement:
135-
return new PreIncrement(convertExpression(unaryExpr.getExpr()));
136-
case preDecrement:
137-
return new PreDecrement(convertExpression(unaryExpr.getExpr()));
138-
case posIncrement:
139-
return new PostIncrement(convertExpression(unaryExpr.getExpr()));
140-
case posDecrement:
141-
return new PostDecrement(convertExpression(unaryExpr.getExpr()));
142-
case not:
143-
return new Not(convertExpression(unaryExpr.getExpr()));
144-
case inverse:
145-
return new Inverse(convertExpression(unaryExpr.getExpr()));
130+
case PLUS:
131+
return new Positive(convertExpression(unaryExpr.getExpression()));
132+
case MINUS:
133+
return new Negative(convertExpression(unaryExpr.getExpression()));
134+
case PREFIX_INCREMENT:
135+
return new PreIncrement(convertExpression(unaryExpr.getExpression()));
136+
case PREFIX_DECREMENT:
137+
return new PreDecrement(convertExpression(unaryExpr.getExpression()));
138+
case POSTFIX_INCREMENT:
139+
return new PostIncrement(convertExpression(unaryExpr.getExpression()));
140+
case POSTFIX_DECREMENT:
141+
return new PostDecrement(convertExpression(unaryExpr.getExpression()));
142+
case LOGICAL_COMPLEMENT:
143+
return new Not(convertExpression(unaryExpr.getExpression()));
144+
case BITWISE_COMPLEMENT:
145+
return new Inverse(convertExpression(unaryExpr.getExpression()));
146146
}
147147
} else if (expression instanceof BinaryExpr) {
148148
BinaryExpr binaryExpr = (BinaryExpr) expression;
@@ -155,50 +155,50 @@ public static io.sundr.model.Expression convertExpression(Expression expression)
155155
}
156156

157157
switch (binaryExpr.getOperator()) {
158-
case plus:
158+
case PLUS:
159159
return new Plus(left, right);
160-
case minus:
160+
case MINUS:
161161
return new Minus(left, right);
162-
case and:
162+
case AND:
163163
return new LogicalAnd(left, right);
164-
case or:
164+
case OR:
165165
return new LogicalOr(left, right);
166-
case binOr:
166+
case BINARY_OR:
167167
return new BitwiseOr(left, right);
168-
case binAnd:
168+
case BINARY_AND:
169169
return new BitwiseAnd(left, right);
170-
case xor:
170+
case XOR:
171171
return new Xor(left, right);
172-
case equals:
172+
case EQUALS:
173173
return new Equals(left, right);
174-
case notEquals:
174+
case NOT_EQUALS:
175175
return new NotEquals(left, right);
176-
case greater:
176+
case GREATER:
177177
return new GreaterThan(left, right);
178-
case greaterEquals:
178+
case GREATER_EQUALS:
179179
return new GreaterThanOrEqual(left, right);
180-
case less:
180+
case LESS:
181181
return new LessThan(left, right);
182-
case lessEquals:
182+
case LESS_EQUALS:
183183
return new LessThanOrEqual(left, right);
184-
case times:
184+
case MULTIPLY:
185185
return new Multiply(left, right);
186-
case divide:
186+
case DIVIDE:
187187
return new Divide(left, right);
188-
case remainder:
188+
case REMAINDER:
189189
return new Modulo(left, right);
190-
case rSignedShift:
190+
case SIGNED_RIGHT_SHIFT:
191191
return new RightShift(left, right);
192-
case rUnsignedShift:
192+
case UNSIGNED_RIGHT_SHIFT:
193193
return new RightUnsignedShift(left, right);
194-
case lShift:
194+
case LEFT_SHIFT:
195195
return new LeftShift(left, right);
196196
}
197197
// If we get here, there's an unhandled binary operator
198198
return null;
199199
} else if (expression instanceof NameExpr) {
200200
NameExpr nameExpr = (NameExpr) expression;
201-
return new ContextRef(nameExpr.getName());
201+
return new ContextRef(nameExpr.getNameAsString());
202202
} else if (expression instanceof StringLiteralExpr) {
203203
StringLiteralExpr stringLiteralExpr = (StringLiteralExpr) expression;
204204
return new ValueRef(stringLiteralExpr.getValue());
@@ -223,29 +223,29 @@ public static io.sundr.model.Expression convertExpression(Expression expression)
223223
ClassRef classRef = (ClassRef) SOURCE_ADAPTER.getReferenceAdapterFunction().apply(objectCreationExpr.getType());
224224
List<TypeRef> parameters = new ArrayList<>();
225225
List<io.sundr.model.Expression> arguments = new ArrayList<>();
226-
for (Type type : objectCreationExpr.getTypeArgs()) {
226+
for (Type type : objectCreationExpr.getTypeArguments().orElse(new NodeList<>())) {
227227
if (type instanceof ClassOrInterfaceType) {
228228
parameters.add(SOURCE_ADAPTER.getReferenceAdapterFunction().apply((ClassOrInterfaceType) type));
229229
}
230230
}
231-
for (Expression argument : objectCreationExpr.getArgs()) {
231+
for (Expression argument : objectCreationExpr.getArguments()) {
232232
arguments.add(convertExpression(argument));
233233
}
234234
return new Construct(classRef, parameters, arguments);
235235
} else if (expression instanceof MethodCallExpr) {
236236
MethodCallExpr methodCallExpr = (MethodCallExpr) expression;
237-
String methodName = methodCallExpr.getName();
237+
String methodName = methodCallExpr.getNameAsString();
238238
List<TypeRef> parameters = new ArrayList<>();
239239
List<io.sundr.model.Expression> arguments = new ArrayList<>();
240-
for (Type type : methodCallExpr.getTypeArgs()) {
240+
for (Type type : methodCallExpr.getTypeArguments().orElse(new NodeList<>())) {
241241
if (type instanceof ClassOrInterfaceType) {
242242
parameters.add(SOURCE_ADAPTER.getReferenceAdapterFunction().apply((ClassOrInterfaceType) type));
243243
}
244244
}
245-
for (Expression argument : methodCallExpr.getArgs()) {
245+
for (Expression argument : methodCallExpr.getArguments()) {
246246
arguments.add(convertExpression(argument));
247247
}
248-
io.sundr.model.Expression scope = methodCallExpr.getScope() != null ? convertExpression(methodCallExpr.getScope()) : null;
248+
io.sundr.model.Expression scope = methodCallExpr.getScope().map(ExpressionConverter::convertExpression).orElse(null);
249249
return new MethodCall(methodName, scope, parameters, arguments);
250250
} else if (expression instanceof LambdaExpr) {
251251
LambdaExpr lambdaExpr = (LambdaExpr) expression;
@@ -263,7 +263,7 @@ public static io.sundr.model.Expression convertExpression(Expression expression)
263263
bodyStatement = StatementConverter.convertStatement(lambdaBody);
264264
}
265265

266-
return new Lambda(lambdaExpr.getParameters().stream().map(Parameter::getId).map(VariableDeclaratorId::getName)
266+
return new Lambda(lambdaExpr.getParameters().stream().map(Parameter::getNameAsString)
267267
.collect(Collectors.toList()), bodyStatement);
268268
} else if (expression instanceof EnclosedExpr) {
269269
EnclosedExpr enclosedExpr = (EnclosedExpr) expression;
@@ -272,17 +272,17 @@ public static io.sundr.model.Expression convertExpression(Expression expression)
272272
InstanceOfExpr instanceOfExpr = (InstanceOfExpr) expression;
273273
com.github.javaparser.ast.type.Type type = instanceOfExpr.getType();
274274
if (type instanceof com.github.javaparser.ast.type.ClassOrInterfaceType) {
275-
return new InstanceOf(convertExpression(instanceOfExpr.getExpr()),
275+
return new InstanceOf(convertExpression(instanceOfExpr.getExpression()),
276276
(ClassRef) SOURCE_ADAPTER.getReferenceAdapterFunction()
277277
.apply((com.github.javaparser.ast.type.ClassOrInterfaceType) type));
278278
} else {
279279
// Handle other reference types by converting to TypeRef first
280280
TypeRef typeRef = TYPEREF_ADAPTER.apply(type);
281281
if (typeRef instanceof ClassRef) {
282-
return new InstanceOf(convertExpression(instanceOfExpr.getExpr()), (ClassRef) typeRef);
282+
return new InstanceOf(convertExpression(instanceOfExpr.getExpression()), (ClassRef) typeRef);
283283
} else {
284284
// For non-class types, fall back to Object
285-
return new InstanceOf(convertExpression(instanceOfExpr.getExpr()), OBJECT);
285+
return new InstanceOf(convertExpression(instanceOfExpr.getExpression()), OBJECT);
286286
}
287287
}
288288
} else if (expression instanceof ArrayAccessExpr) {
@@ -307,14 +307,14 @@ public static io.sundr.model.Expression convertExpression(Expression expression)
307307
VariableDeclarationExpr varDeclExpr = (VariableDeclarationExpr) expression;
308308
// For variable declarations in expression context, we create a simple declaration
309309
// without trying to resolve the full type, to avoid context issues
310-
if (!varDeclExpr.getVars().isEmpty()) {
311-
VariableDeclarator var = varDeclExpr.getVars().get(0);
312-
String varName = var.getId().getName();
313-
TypeRef typeRef = TYPEREF_ADAPTER.apply(varDeclExpr.getType());
310+
if (!varDeclExpr.getVariables().isEmpty()) {
311+
VariableDeclarator var = varDeclExpr.getVariables().get(0);
312+
String varName = var.getNameAsString();
313+
TypeRef typeRef = TYPEREF_ADAPTER.apply(varDeclExpr.getCommonType());
314314
LocalVariable variable = LocalVariable.newLocalVariable(typeRef, varName);
315315

316-
if (var.getInit() != null) {
317-
io.sundr.model.Expression initExpr = convertExpression(var.getInit());
316+
if (var.getInitializer().isPresent()) {
317+
io.sundr.model.Expression initExpr = convertExpression(var.getInitializer().get());
318318
return new Declare(variable, initExpr);
319319
} else {
320320
return new Declare(variable);
@@ -323,7 +323,7 @@ public static io.sundr.model.Expression convertExpression(Expression expression)
323323
return null;
324324
} else if (expression instanceof FieldAccessExpr) {
325325
FieldAccessExpr fieldAccessExpr = (FieldAccessExpr) expression;
326-
String fieldName = fieldAccessExpr.getFieldExpr().getName();
326+
String fieldName = fieldAccessExpr.getNameAsString();
327327
io.sundr.model.Expression scope = convertExpression(fieldAccessExpr.getScope());
328328
return new PropertyRef(fieldName, scope);
329329
} else if (expression instanceof TypeExpr) {
@@ -342,7 +342,7 @@ public static io.sundr.model.Expression convertExpression(Expression expression)
342342
} else if (expression instanceof CastExpr) {
343343
CastExpr castExpr = (CastExpr) expression;
344344
TypeRef targetType = TYPEREF_ADAPTER.apply(castExpr.getType());
345-
io.sundr.model.Expression expr = convertExpression(castExpr.getExpr());
345+
io.sundr.model.Expression expr = convertExpression(castExpr.getExpression());
346346
return new Cast(targetType, expr);
347347
}
348348
return null;

adapters/source/src/main/java/io/sundr/adapter/source/FileToTypeDef.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import java.io.FileInputStream;
2222
import java.util.function.Function;
2323

24-
import com.github.javaparser.JavaParser;
24+
import com.github.javaparser.StaticJavaParser;
2525
import com.github.javaparser.ast.CompilationUnit;
2626
import com.github.javaparser.ast.body.TypeDeclaration;
2727

@@ -44,7 +44,7 @@ public FileToTypeDef(Function<TypeDeclaration, TypeDef> typeDeclarationToTypeDef
4444
@Override
4545
public TypeDef apply(File file) {
4646
try (FileInputStream is = new FileInputStream(file)) {
47-
CompilationUnit cu = JavaParser.parse(is);
47+
CompilationUnit cu = StaticJavaParser.parse(is);
4848
TypeDeclaration typeDeclaration = cu.getTypes().get(0);
4949
return typeDeclarationToTypeDef.apply(typeDeclaration);
5050
} catch (Exception e) {

0 commit comments

Comments
 (0)