99import com .github .javaparser .ast .Node ;
1010import com .github .javaparser .ast .PackageDeclaration ;
1111import com .github .javaparser .ast .body .ClassOrInterfaceDeclaration ;
12+ import com .github .javaparser .ast .body .ConstructorDeclaration ;
13+ import com .github .javaparser .ast .body .FieldDeclaration ;
14+ import com .github .javaparser .ast .body .MethodDeclaration ;
1215import com .github .javaparser .ast .body .VariableDeclarator ;
1316import com .github .javaparser .ast .comments .JavadocComment ;
1417import com .github .javaparser .ast .expr .CastExpr ;
2831import com .github .javaparser .ast .modules .ModuleRequiresDirective ;
2932import com .github .javaparser .ast .modules .ModuleUsesDirective ;
3033import com .github .javaparser .ast .nodeTypes .NodeWithName ;
34+ import com .github .javaparser .ast .nodeTypes .NodeWithSimpleName ;
3135import com .github .javaparser .ast .nodeTypes .NodeWithType ;
3236import com .github .javaparser .ast .type .ClassOrInterfaceType ;
37+ import com .github .javaparser .ast .type .TypeParameter ;
3338import com .github .javaparser .ast .visitor .ModifierVisitor ;
3439import com .github .javaparser .ast .visitor .Visitable ;
3540import com .github .javaparser .utils .SourceRoot ;
4449
4550import java .io .File ;
4651import java .io .IOException ;
52+ import java .nio .charset .StandardCharsets ;
4753import java .nio .file .Files ;
4854import java .nio .file .Path ;
55+ import java .nio .file .Paths ;
56+ import java .nio .file .StandardOpenOption ;
57+ import java .util .ArrayList ;
4958import java .util .Collection ;
5059import java .util .Comparator ;
5160import java .util .List ;
@@ -119,12 +128,13 @@ public void execute()
119128
120129 getLog ().info ("Transforming sources from " + sourceProjectLocation + " to " + outputDirectory );
121130
131+ List <CompilationUnit > compilationUnitsToRename = new ArrayList <>();
132+
122133 try
123134 {
124135
125136 SourceRoot sourceRoot = new SourceRoot (sourceProjectLocation .toPath ());
126137 sourceRoot .getParserConfiguration ().setLanguageLevel (ParserConfiguration .LanguageLevel .JAVA_17 );
127- //sourceRoot.
128138
129139 // Our sample is in the root of this directory, so no package name.
130140 List <ParseResult <CompilationUnit >> parseResults = sourceRoot .tryToParse ( "" );
@@ -135,9 +145,64 @@ public void execute()
135145 Files .walk (out ).sorted (Comparator .reverseOrder ()).map (Path ::toFile ).forEach (File ::delete );
136146 }
137147
148+ Files .createDirectories (out );
149+
138150 for (ParseResult <CompilationUnit > parseResult : parseResults ) {
139151 CompilationUnit cu = parseResult .getResult ().get ();
140152
153+ // as the classes with names started with Jakarta will be renamed to Javax we need to rename all usage of those classes
154+ cu .findAll (VariableDeclarator .class ).forEach (vd -> {
155+ if (vd .getType () instanceof ClassOrInterfaceType ) {
156+ String nameAsString = ((ClassOrInterfaceType )vd .getType ()).getNameAsString ();
157+ if (nameAsString .startsWith ("Jakarta" )) {
158+ ((ClassOrInterfaceType )vd .getType ()).setName (nameAsString .replaceFirst ("Jakarta" , "Javax" ));
159+ }
160+ }
161+ if (StringUtils .equals (vd .getNameAsString (), "SERVLET_MAJOR_VERSION" )) {
162+ vd .setInitializer (new IntegerLiteralExpr ("4" ));
163+ }
164+ //changeEE9TypeToEE8(vd);
165+ });
166+
167+ cu .findAll (ConstructorDeclaration .class ).forEach (cd -> {
168+ String typeName = cd .getNameAsString ();
169+ if (typeName .startsWith ("Jakarta" )) {
170+ cd .setName (typeName .replaceFirst ("Jakarta" , "Javax" ));
171+ }
172+ cd .getParameters ().forEach (parameter -> {
173+ if (parameter .getType () instanceof ClassOrInterfaceType ) {
174+ ClassOrInterfaceType cit = ((ClassOrInterfaceType )parameter .getType ());
175+ String name = cit .getNameAsString ();
176+ if (name .startsWith ("Jakarta" )) {
177+ cit .setName (name .replaceFirst ("Jakarta" , "Javax" ));
178+ }
179+ }
180+ });
181+ });
182+
183+ cu .findAll (FieldDeclaration .class ).stream ()
184+ .filter (fd -> fd .getVariables ().get (0 ).getType () instanceof ClassOrInterfaceType )
185+ .map (fd -> (ClassOrInterfaceType ) fd .getVariables ().get (0 ).getType ())
186+ .filter (cif -> cif .getTypeArguments ().isPresent ())
187+ .forEach (cif ->
188+ cif .getTypeArguments ().get ().stream ()
189+ .filter (type -> type instanceof ClassOrInterfaceType )
190+ .map (type -> (ClassOrInterfaceType ) type )
191+ .forEach (classOrInterfaceType -> {
192+ String currentName = classOrInterfaceType .getNameAsString ();
193+ if (currentName .startsWith ("Jakarta" )) {
194+ classOrInterfaceType .setName (currentName .replaceFirst ("Jakarta" , "Javax" ));
195+ }
196+ })
197+ );
198+
199+ cu .findAll (NameExpr .class ).stream ()
200+ .filter (nameExpr -> nameExpr .getNameAsString ().startsWith ("Jakarta" ))
201+ .forEach (nameExpr -> {
202+ String className = nameExpr .getNameAsString ();
203+ nameExpr .setName (className .replaceFirst ("Jakarta" , "Javax" ));
204+ });
205+
141206 cu .accept (
142207 new ModifierVisitor <Void >()
143208 {
@@ -160,14 +225,7 @@ public Visitable visit(com.github.javaparser.ast.body.Parameter n, Void arg) {
160225 return super .visit (n , arg );
161226 }
162227
163- @ Override
164- public Visitable visit (VariableDeclarator n , Void arg ) {
165- if (StringUtils .equals (n .getNameAsString (), "SERVLET_MAJOR_VERSION" )) {
166- n .setInitializer (new IntegerLiteralExpr ("4" ));
167- }
168- changeEE9TypeToEE8 (n );
169- return super .visit (n , arg );
170- }
228+
171229
172230 @ Override
173231 public Visitable visit (VariableDeclarationExpr n , Void arg ) {
@@ -181,9 +239,9 @@ public Visitable visit(VariableDeclarationExpr n, Void arg) {
181239
182240 @ Override
183241 public Visitable visit (MethodCallExpr n , Void arg ) {
184- if (StringUtils .startsWith (n .toString (), "org.eclipse.jetty.ee9." ) && n .getScope ().isPresent ()) {
242+ String fullString = n .toString ();
243+ if (StringUtils .startsWith (fullString , "org.eclipse.jetty.ee9." ) && n .getScope ().isPresent ()) {
185244 // org.eclipse.jetty.ee9.nested.Response.unwrap(event.getSuppliedResponse())
186- String fullString = n .toString ();
187245 Expression expression = n .getScope ().get ();
188246 if (expression .isFieldAccessExpr ()) {
189247 FieldAccessExpr fieldAccessExpr = expression .asFieldAccessExpr ();
@@ -198,7 +256,43 @@ public Visitable visit(MethodCallExpr n, Void arg) {
198256 NameExpr nameExpr = new NameExpr (ee8PackageName + "." + classSimpleName );
199257 n .setScope (nameExpr );
200258 }
201-
259+ }
260+ if (StringUtils .contains (fullString , "Jakarta" ) && n .getScope ().isPresent ()){
261+
262+ n .getArguments ().stream ().filter (node -> node instanceof NodeWithSimpleName )
263+ .map (node -> (NodeWithSimpleName <?>)node )
264+ .filter (nameExpr -> nameExpr .getNameAsString ().startsWith ("Jakarta" ))
265+ .forEach (nameExpr -> {
266+ String className = nameExpr .getNameAsString ();
267+ nameExpr .setName (className .replaceFirst ("Jakarta" , "Javax" ));
268+ });
269+
270+ n .getChildNodes ().stream ().filter (node -> node instanceof FieldAccessExpr )
271+ .map (node -> (FieldAccessExpr )node )
272+ .filter (fieldAccessExpr -> fieldAccessExpr .getScope ().isNameExpr ())
273+ .map (nameExpr -> (NameExpr )nameExpr .getScope ())
274+ .forEach (nameExpr -> {
275+ String fullClassName = nameExpr .getNameAsString ();
276+ if (fullClassName .startsWith ("Jakarta" )) {
277+ nameExpr .setName (fullClassName .replaceFirst ("Jakarta" , "Javax" ));
278+ }
279+ });
280+
281+ n .getChildNodes ().stream ().filter (node -> node instanceof NameExpr )
282+ .map (node -> (NameExpr )node )
283+ .filter (nameExpr -> nameExpr .getNameAsString ().startsWith ("Jakarta" ))
284+ .forEach (nameExpr -> {
285+ String className = nameExpr .getNameAsString ();
286+ nameExpr .setName (className .replaceFirst ("Jakarta" , "Javax" ));
287+ });
288+
289+ n .getChildNodes ().stream ().filter (node -> node instanceof ClassExpr )
290+ .map (node -> (ClassExpr )node )
291+ .filter (classExpr -> classExpr .getTypeAsString ().startsWith ("Jakarta" ))
292+ .forEach (classExpr -> {
293+ String className = classExpr .getTypeAsString ();
294+ classExpr .setType (className .replaceFirst ("Jakarta" , "Javax" ));
295+ });
202296 }
203297 return super .visit (n , arg );
204298 }
@@ -209,6 +303,41 @@ public Visitable visit(ClassExpr n, Void arg) {
209303 return super .visit (n , arg );
210304 }
211305
306+ @ Override
307+ public Visitable visit (ClassOrInterfaceDeclaration n , Void arg ) {
308+ return super .visit (n , arg );
309+ }
310+
311+ @ Override
312+ public Visitable visit (ClassOrInterfaceType n , Void arg ) {
313+ String currentName = n .toString ();
314+ JavaParser javaParser = new JavaParser ();
315+
316+ if (currentName .startsWith ("org.eclipse.jetty.ee9." )) {
317+ String newName = StringUtils .replace (currentName , "org.eclipse.jetty.ee9." , "org.eclipse.jetty.ee8." );
318+ ParseResult <ClassOrInterfaceType > parseResult = javaParser .parseClassOrInterfaceType (newName );
319+ if (parseResult .isSuccessful () && parseResult .getResult ().isPresent ()) {
320+ n = parseResult .getResult ().get ();
321+ }
322+
323+ }
324+ if (currentName .contains ("jakarta" )) {
325+ String newName = StringUtils .replace (currentName , "jakarta" , "javax" );
326+ ParseResult <ClassOrInterfaceType > parseResult = javaParser .parseClassOrInterfaceType (newName );
327+ if (parseResult .isSuccessful () && parseResult .getResult ().isPresent ()) {
328+ n = parseResult .getResult ().get ();
329+ }
330+ }
331+ if (currentName .startsWith ("Jakarta" )) {
332+ String newName = StringUtils .replace (currentName , "Jakarta" , "Javax" );
333+ ParseResult <ClassOrInterfaceType > parseResult = javaParser .parseClassOrInterfaceType (newName );
334+ if (parseResult .isSuccessful () && parseResult .getResult ().isPresent ()) {
335+ n = parseResult .getResult ().get ();
336+ }
337+ }
338+ return super .visit (n , arg );
339+ }
340+
212341 @ Override
213342 public Visitable visit (StringLiteralExpr n , Void arg ) {
214343 if (startsWith (n .getValue (), notTranslateStartsWith )) {
@@ -309,6 +438,13 @@ public Visitable visit(ModuleProvidesDirective n, Void arg) {
309438 .forEach (name -> name .setQualifier (new Name (StringUtils .replace (name .getQualifier ().get ().asString (),
310439 ".jakarta" ,
311440 ".javax" ))));
441+ n .getWith ().stream ()
442+ .filter (name -> StringUtils .contains (name .getIdentifier (),"Jakarta" ))
443+ .forEach (name -> name .setIdentifier (StringUtils .replace (name .getIdentifier (),
444+ "Jakarta" ,
445+ "Javax" )));
446+
447+
312448 }
313449 return super .visit (n , arg );
314450 }
@@ -331,44 +467,23 @@ public Visitable visit(JavadocComment n, Void arg) {
331467 if (StringUtils .contains (n .getContent (), "jakarta" )) {
332468 n .setContent (StringUtils .replace (n .getContent (),"jakarta" , "javax" ));
333469 }
334- return super .visit (n , arg );
335- }
336-
337- @ Override
338- public Visitable visit (ClassOrInterfaceDeclaration n , Void arg ) {
339- return super .visit (n , arg );
340- }
341-
342- @ Override
343- public Visitable visit (ClassOrInterfaceType n , Void arg ) {
344- String currentName = n .toString ();
345- JavaParser javaParser = new JavaParser ();
346-
347- if (currentName .startsWith ("org.eclipse.jetty.ee9." )) {
348- String newName = StringUtils .replace (currentName , "org.eclipse.jetty.ee9." , "org.eclipse.jetty.ee8." );
349- ParseResult <ClassOrInterfaceType > parseResult = javaParser .parseClassOrInterfaceType (newName );
350- if (parseResult .isSuccessful () && parseResult .getResult ().isPresent ()) {
351- n = parseResult .getResult ().get ();
352- }
353-
354- }
355- if (currentName .contains ("jakarta" )) {
356- String newName = StringUtils .replace (currentName , "jakarta" , "javax" );
357- ParseResult <ClassOrInterfaceType > parseResult = javaParser .parseClassOrInterfaceType (newName );
358- if (parseResult .isSuccessful () && parseResult .getResult ().isPresent ()) {
359- n = parseResult .getResult ().get ();
360- }
470+ if (StringUtils .contains (n .getContent (), "Jakarta" )) {
471+ n .setContent (StringUtils .replace (n .getContent (),"Jakarta" , "Javax" ));
361472 }
362473 return super .visit (n , arg );
363474 }
364475
476+ }, null );
365477
366478
367- }, null );
479+ if (cu .getPrimaryTypeName ().isPresent () && cu .getPrimaryTypeName ().get ().startsWith ("Jakarta" )) {
480+ // we cannot as we have some demo packages as well
481+ //&& cu.getPackageDeclaration().get().getName().toString().startsWith("org.eclipse.jetty.ee8")) {
482+ compilationUnitsToRename .add (cu );
483+ }
368484
369485 }
370486
371- Files .createDirectories (out );
372487 sourceRoot .saveAll (out );
373488
374489 File ee9Directory = new File (outputDirectory , "org/eclipse/jetty/ee9" );
@@ -390,6 +505,28 @@ public Visitable visit(ClassOrInterfaceType n, Void arg) {
390505 });
391506 }
392507
508+ for (CompilationUnit cu : compilationUnitsToRename ) {
509+
510+ // at this stage everything has been renamed but the old file will be still save as we cannot change that
511+ String previousPackage = cu .getPackageDeclaration ().get ().getName ().toString ();
512+ String previousFullClassName = previousPackage + "." + cu .getPrimaryTypeName ().get ();
513+ String fullClassName = previousPackage + "." + //
514+ StringUtils .replaceFirst (cu .getPrimaryTypeName ().get (), "Jakarta" , "Javax" );
515+ String className = StringUtils .replaceFirst (cu .getPrimaryTypeName ().get (), "Jakarta" , "Javax" );
516+ cu .getPrimaryType ().get ().setName (className );
517+
518+ Path newPath = out .resolve (fullClassName .replace ('.' , '/' ) + ".java" );
519+
520+ String newClassSource = cu .toString ();
521+
522+ Files .createDirectories (newPath .getParent ());
523+ Files .createFile (newPath );
524+ Files .write (newPath , newClassSource .getBytes (StandardCharsets .UTF_8 ));
525+
526+ Path oldPath = out .resolve (previousFullClassName .replace ('.' , '/' ) + ".java" );
527+ Files .deleteIfExists (oldPath );
528+ }
529+
393530 if (addToCompileSourceRoot ) {
394531 if (testSources ) {
395532 project .getTestCompileSourceRoots ().add (outputDirectory .getAbsolutePath ());
@@ -431,6 +568,7 @@ private static void changeEE9TypeToEE8(NodeWithType n) {
431568 * @return will return <code>null</code> if there is nothing to change
432569 */
433570 public static String changeEE9TypeToEE8 (String currentType ) {
571+ currentType = currentType .replaceFirst ("Jakarta" , "Javax" );
434572 if (currentType .contains ("jakarta" )) {
435573 String newType = StringUtils .replace (currentType , "jakarta" ,
436574 "javax" );
@@ -448,7 +586,7 @@ public static String changeEE9TypeToEE8(String currentType) {
448586 return null ;
449587 }
450588
451- private boolean startsWith (String str , Collection <String > startList ) {
589+ private static boolean startsWith (String str , Collection <String > startList ) {
452590 return startList .stream ().anyMatch (str ::startsWith );
453591 }
454592
0 commit comments