Skip to content

Commit 2e98aa2

Browse files
[#529] move to javaparser 3.x latest
1 parent 9a51e49 commit 2e98aa2

17 files changed

+255
-190
lines changed

basics/basic/src/main/java/org/jvnet/jaxb2_commons/plugin/inheritance/util/JavaTypeParser.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package org.jvnet.jaxb2_commons.plugin.inheritance.util;
22

3-
import com.github.javaparser.JavaParser;
4-
import com.github.javaparser.ParseException;
3+
import com.github.javaparser.ParseProblemException;
4+
import com.github.javaparser.StaticJavaParser;
55
import com.github.javaparser.ast.CompilationUnit;
66
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
77
import com.github.javaparser.ast.body.TypeDeclaration;
88
import com.github.javaparser.ast.type.ClassOrInterfaceType;
99

1010
import java.io.ByteArrayInputStream;
1111
import java.io.UnsupportedEncodingException;
12+
import java.nio.charset.StandardCharsets;
1213
import java.util.Collections;
1314
import java.util.List;
1415
import java.util.Map;
@@ -51,19 +52,17 @@ public JClass parseClass(String _class, JCodeModel codeModel) {
5152
private JType parseType(String type, JCodeModel codeModel) {
5253
final String text = "public class Ignored extends " + type + " {}";
5354
try {
54-
CompilationUnit compilationUnit = JavaParser.parse(
55-
new ByteArrayInputStream(text.getBytes("UTF-8")), "UTF-8");
56-
final List<TypeDeclaration> typeDeclarations = compilationUnit
57-
.getTypes();
55+
CompilationUnit compilationUnit = StaticJavaParser.parse(
56+
new ByteArrayInputStream(text.getBytes("UTF-8")), StandardCharsets.UTF_8);
57+
final List<TypeDeclaration<?>> typeDeclarations = compilationUnit.getTypes();
5858
final TypeDeclaration typeDeclaration = typeDeclarations.get(0);
5959
final ClassOrInterfaceDeclaration classDeclaration = (ClassOrInterfaceDeclaration) typeDeclaration;
60-
final List<ClassOrInterfaceType> _extended = classDeclaration
61-
.getExtends();
60+
final List<ClassOrInterfaceType> _extended = classDeclaration.getExtendedTypes();
6261
final ClassOrInterfaceType classOrInterfaceType = _extended.get(0);
6362

6463
return classOrInterfaceType.accept(
6564
this.typeToJTypeConvertingVisitor, codeModel);
66-
} catch (ParseException pex) {
65+
} catch (ParseProblemException pex) {
6766
throw new IllegalArgumentException(
6867
"Could not parse the type definition [" + type + "].", pex);
6968
} catch (UnsupportedEncodingException uex) {

basics/basic/src/main/java/org/jvnet/jaxb2_commons/plugin/inheritance/util/TypeToJTypeConvertingVisitor.java

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.jvnet.jaxb2_commons.plugin.inheritance.util;
22

3+
import com.github.javaparser.ast.type.ArrayType;
34
import com.github.javaparser.ast.type.ClassOrInterfaceType;
45
import com.github.javaparser.ast.type.PrimitiveType;
56
import com.github.javaparser.ast.type.ReferenceType;
@@ -36,21 +37,21 @@ public JType visit(VoidType type, JCodeModel codeModel) {
3637
@Override
3738
public JType visit(PrimitiveType type, JCodeModel codeModel) {
3839
switch (type.getType()) {
39-
case Boolean:
40+
case BOOLEAN:
4041
return codeModel.BOOLEAN;
41-
case Char:
42+
case CHAR:
4243
return codeModel.CHAR;
43-
case Byte:
44+
case BYTE:
4445
return codeModel.BYTE;
45-
case Short:
46+
case SHORT:
4647
return codeModel.SHORT;
47-
case Int:
48+
case INT:
4849
return codeModel.INT;
49-
case Long:
50+
case LONG:
5051
return codeModel.LONG;
51-
case Float:
52+
case FLOAT:
5253
return codeModel.FLOAT;
53-
case Double:
54+
case DOUBLE:
5455
return codeModel.DOUBLE;
5556
default:
5657
throw new AssertionError("Unknown primitive type ["
@@ -59,11 +60,11 @@ public JType visit(PrimitiveType type, JCodeModel codeModel) {
5960
}
6061

6162
@Override
62-
public JType visit(ReferenceType type, JCodeModel codeModel) {
63-
final JType referencedType = type.getType().accept(this, codeModel);
63+
public JType visit(ArrayType type, JCodeModel codeModel) {
64+
final JType referencedType = type.getElementType().accept(this, codeModel);
6465

6566
JType referencedTypeArray = referencedType;
66-
for (int index = 0; index < type.getArrayCount(); index++) {
67+
for (int index = 0; index < type.getArrayLevel(); index++) {
6768
referencedTypeArray = referencedTypeArray.array();
6869
}
6970
return referencedTypeArray;
@@ -72,8 +73,8 @@ public JType visit(ReferenceType type, JCodeModel codeModel) {
7273
@Override
7374
public JType visit(WildcardType type, JCodeModel codeModel) {
7475

75-
if (type.getExtends() != null) {
76-
final ReferenceType _extends = type.getExtends();
76+
if (type.getExtendedType().isPresent()) {
77+
final ReferenceType _extends = type.getExtendedType().get();
7778
final JType boundType = _extends.accept(this, codeModel);
7879

7980
if (!(boundType instanceof JClass)) {
@@ -83,7 +84,7 @@ public JType visit(WildcardType type, JCodeModel codeModel) {
8384

8485
final JClass boundClass = (JClass) boundType;
8586
return boundClass.wildcard();
86-
} else if (type.getSuper() != null) {
87+
} else if (type.getSuperType().isPresent()) {
8788
// TODO
8889
throw new IllegalArgumentException(
8990
"Wildcard types with super clause are not supported at the moment.");
@@ -99,7 +100,7 @@ public JType visit(ClassOrInterfaceType type, JCodeModel codeModel) {
99100
final JClass knownClass = this.knownClasses.get(name);
100101
final JClass jclass = knownClass != null ? knownClass : codeModel
101102
.ref(name);
102-
final List<Type> typeArgs = type.getTypeArgs();
103+
final List<Type> typeArgs = type.getTypeArguments().orElse(null);
103104
if (typeArgs == null || typeArgs.isEmpty()) {
104105
return jclass;
105106
} else {
@@ -119,8 +120,8 @@ public JType visit(ClassOrInterfaceType type, JCodeModel codeModel) {
119120
}
120121

121122
private String getName(ClassOrInterfaceType type) {
122-
final String name = type.getName();
123-
final ClassOrInterfaceType scope = type.getScope();
123+
final String name = type.getNameAsString();
124+
final ClassOrInterfaceType scope = type.getScope().orElse(null);
124125
if (scope == null) {
125126
return name;
126127
} else {
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package org.jvnet.jaxb2_commons.plugin.inheritance.tests;
22

3-
import com.github.javaparser.JavaParser;
3+
import com.github.javaparser.StaticJavaParser;
44
import com.github.javaparser.ast.CompilationUnit;
5+
import com.github.javaparser.ast.NodeList;
56
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
67
import com.github.javaparser.ast.body.TypeDeclaration;
78
import com.github.javaparser.ast.type.ClassOrInterfaceType;
89
import com.github.javaparser.ast.type.ReferenceType;
910
import com.github.javaparser.ast.type.Type;
1011

1112
import java.io.ByteArrayInputStream;
13+
import java.nio.charset.StandardCharsets;
1214
import java.util.List;
1315

1416
import junit.framework.TestCase;
@@ -19,30 +21,28 @@ public void testParse() throws Exception {
1921

2022
final String text = "public class Dummy implements java.util.Comparator<java.lang.Integer>{}";
2123

22-
final CompilationUnit compilationUnit = JavaParser.parse(
23-
new ByteArrayInputStream(text.getBytes("UTF-8")), "UTF-8");
24-
final List<TypeDeclaration> typeDeclarations = compilationUnit
25-
.getTypes();
24+
final CompilationUnit compilationUnit = StaticJavaParser.parse(
25+
new ByteArrayInputStream(text.getBytes("UTF-8")), StandardCharsets.UTF_8);
26+
final NodeList<TypeDeclaration<?>> typeDeclarations = compilationUnit.getTypes();
2627
assertEquals(1, typeDeclarations.size());
2728
final TypeDeclaration typeDeclaration = typeDeclarations.get(0);
2829
assertTrue(typeDeclaration instanceof ClassOrInterfaceDeclaration);
2930
final ClassOrInterfaceDeclaration classDeclaration = (ClassOrInterfaceDeclaration) typeDeclaration;
30-
assertEquals("Dummy", classDeclaration.getName());
31-
final List<ClassOrInterfaceType> implementedInterfaces = classDeclaration
32-
.getImplements();
31+
assertEquals("Dummy", classDeclaration.getNameAsString());
32+
final List<ClassOrInterfaceType> implementedInterfaces = classDeclaration.getImplementedTypes();
3333
assertEquals(1, implementedInterfaces.size());
3434
final ClassOrInterfaceType implementedInterface = implementedInterfaces
3535
.get(0);
36-
assertEquals("Comparator", implementedInterface.getName());
37-
final List<Type> typeArgs = implementedInterface.getTypeArgs();
36+
assertEquals("Comparator", implementedInterface.getNameAsString());
37+
final List<Type> typeArgs = implementedInterface.getTypeArguments().orElse(null);
3838
assertEquals(1, typeArgs.size());
3939
final Type typeArg = typeArgs.get(0);
4040
assertTrue(typeArg instanceof ReferenceType);
4141
final ReferenceType referenceTypeArg = (ReferenceType) typeArg;
42-
final Type referencedType = referenceTypeArg.getType();
42+
final Type referencedType = referenceTypeArg.getElementType();
4343
assertTrue(referencedType instanceof ClassOrInterfaceType);
4444
final ClassOrInterfaceType typeArgType = (ClassOrInterfaceType) referencedType;
45-
assertEquals("Integer", typeArgType.getName());
45+
assertEquals("Integer", typeArgType.getNameAsString());
4646

4747
}
4848
}

jaxb-annox-parent/jaxb-annox/src/main/java/org/jvnet/jaxb/annox/javaparser/AnnotationExprParser.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package org.jvnet.jaxb.annox.javaparser;
22

3-
import com.github.javaparser.JavaParser;
43
import com.github.javaparser.ParseException;
4+
import com.github.javaparser.StaticJavaParser;
55
import com.github.javaparser.ast.CompilationUnit;
6+
import com.github.javaparser.ast.NodeList;
67
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
78
import com.github.javaparser.ast.body.TypeDeclaration;
89
import com.github.javaparser.ast.expr.AnnotationExpr;
@@ -19,9 +20,8 @@ public List<AnnotationExpr> parse(String text) throws ParseException {
1920
Validate.notNull(text);
2021
final String classText = text + "\n" + "public class Dummy{}";
2122
final StringReader reader = new StringReader(classText);
22-
final CompilationUnit compilationUnit = JavaParser.parse(reader, true);
23-
final List<TypeDeclaration> typeDeclarations = compilationUnit
24-
.getTypes();
23+
final CompilationUnit compilationUnit = StaticJavaParser.parse(reader);
24+
final NodeList<TypeDeclaration<?>> typeDeclarations = compilationUnit.getTypes();
2525
if (typeDeclarations.size() > 1) {
2626
throw new ParseException(
2727
MessageFormat
@@ -35,7 +35,7 @@ public List<AnnotationExpr> parse(String text) throws ParseException {
3535
ClassOrInterfaceDeclaration.class.getName()));
3636
}
3737
final ClassOrInterfaceDeclaration classDeclaration = (ClassOrInterfaceDeclaration) typeDeclaration;
38-
if (!"Dummy".equals(classDeclaration.getName())) {
38+
if (!"Dummy".equals(classDeclaration.getNameAsString())) {
3939
throw new ParseException(MessageFormat.format(
4040
"Expected [{0}] as type declaration.", "Dummy"));
4141
}

0 commit comments

Comments
 (0)