From 07fb116a470b4939d1c234fb1dcde1dcc22fa975 Mon Sep 17 00:00:00 2001 From: Tristan Tarrant Date: Mon, 24 Jun 2024 12:25:58 +0200 Subject: [PATCH] Adapt to new JavaParser API --- .../org/infinispan/ppg/generator/Parser.java | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/protocol-parser-generator/src/main/java/org/infinispan/ppg/generator/Parser.java b/protocol-parser-generator/src/main/java/org/infinispan/ppg/generator/Parser.java index b655022..f4108c8 100644 --- a/protocol-parser-generator/src/main/java/org/infinispan/ppg/generator/Parser.java +++ b/protocol-parser-generator/src/main/java/org/infinispan/ppg/generator/Parser.java @@ -14,6 +14,7 @@ import java.util.function.Consumer; import com.github.javaparser.JavaParser; +import com.github.javaparser.ParseResult; import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.Modifier; import com.github.javaparser.ast.NodeList; @@ -27,8 +28,8 @@ public class Parser { private final List sourceDirectories; private final Consumer debug; - private Set processedFiles = new HashSet<>(); - private Grammar grammar = new Grammar(); + private final Set processedFiles = new HashSet<>(); + private final Grammar grammar = new Grammar(); public Parser(Consumer debug, List sourceDirectories) { this.debug = debug; @@ -158,11 +159,11 @@ private void addConstants(ParseContext ctx, String className, ClassOrInterfaceDe private void addIntrinsics(ParseContext ctx, String className, ClassOrInterfaceDeclaration clazz) { for (MethodDeclaration m : clazz.getMethods()) { - if (!m.getModifiers().contains(Modifier.STATIC)) { + if (!m.getModifiers().contains(Modifier.staticModifier())) { continue; } NodeList parameterTypes = m.getParameters(); - if (parameterTypes.size() < 1 || !"ByteBuf".equals(parameterTypes.get(0).getTypeAsString())) { + if (parameterTypes.isEmpty() || !"ByteBuf".equals(parameterTypes.get(0).getTypeAsString())) { continue; } if (ctx.ns == null) { @@ -185,19 +186,24 @@ private ClassOrInterfaceDeclaration loadClass(ParseContext ctx, String className debug.accept("Looking up file " + file); return file.exists() && file.isFile(); }).findFirst(); - if (!classFile.isPresent()) { + if (classFile.isEmpty()) { throw ctx.fail("Cannot find " + className + " in any of " + sourceDirectories); } try { - CompilationUnit compilationUnit = JavaParser.parse(classFile.get()); int dotIndex = className.lastIndexOf('.'); String simpleName = dotIndex < 0 ? className : className.substring(dotIndex + 1); - - Optional classByName = compilationUnit.getClassByName(simpleName); - if (!classByName.isPresent()) { - classByName = compilationUnit.getInterfaceByName(simpleName); + JavaParser javaParser = new JavaParser(); + ParseResult result = javaParser.parse(classFile.get()); + if (result.isSuccessful()) { + CompilationUnit compilationUnit = result.getResult().get(); + Optional classByName = compilationUnit.getClassByName(simpleName); + if (classByName.isEmpty()) { + classByName = compilationUnit.getInterfaceByName(simpleName); + } + return classByName.orElseThrow(() -> ctx.fail("Cannot find class " + className + " in " + classFile.get())); + } else { + throw new RuntimeException(String.valueOf(result.getProblems())); } - return classByName.orElseThrow(() -> ctx.fail("Cannot find class " + className + " in " + classFile.get())); } catch (FileNotFoundException e) { throw ctx.fail("Cannot parse file " + classFile); }