Skip to content

Commit

Permalink
improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
jevanlingen committed Feb 4, 2025
1 parent 59e00c2 commit 03fbc66
Showing 1 changed file with 10 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,7 @@ public G.CompilationUnit visit(SourceUnit unit, ModuleNode ast) throws GroovyPar
for (ImportNode anImport : ast.getStaticImports().values()) {
sortedByPosition.computeIfAbsent(pos(anImport), i -> new ArrayList<>()).add(anImport);
}

// Duplicate imports do work out of the box for import, star-import and static-import.
// For static-star-import, this does not work though.
// The groovy compiler does only save the last duplicate import instead of all, so parse all static star imports by hand.
// The groovy compiler does not memoize duplicate static star imports, thus walk through the source code and create all ImportNodes by hand
Map<String, ImportNode> staticStarImports = ast.getStaticStarImports();
if (!staticStarImports.isEmpty()) {
// Take source code until last static star import for performance reasons
Expand Down Expand Up @@ -2265,40 +2262,17 @@ private JRightPadded<Statement> convertTopLevelStatement(SourceUnit unit, ASTNod
return JRightPadded.build(classVisitor.pollQueue());
} else if (node instanceof ImportNode) {
ImportNode importNode = (ImportNode) node;
Space prefix = sourceBefore("import");
JLeftPadded<Boolean> statik;
if (importNode.isStatic()) {
statik = padLeft(sourceBefore("static"), true);
} else {
statik = padLeft(EMPTY, false);
}
String packageName = importNode.getPackageName();
J.FieldAccess qualid;
if (packageName == null) {
String type = importNode.getType().getName().replace('$', '.');
if (importNode.isStar()) {
type += ".*";
} else if (importNode.getFieldName() != null) {
type += "." + importNode.getFieldName();
}
Space space = sourceBefore(type);
qualid = TypeTree.build(type).withPrefix(space);
} else {
if (importNode.isStar()) {
packageName += "*";
}
qualid = TypeTree.build(packageName).withPrefix(sourceBefore(packageName));
}

Space importPrefix = sourceBefore("import");
JLeftPadded<Boolean> statik = importNode.isStatic() ? padLeft(sourceBefore("static"), true) : padLeft(EMPTY, false);
Space space = whitespace();
J.FieldAccess qualid = TypeTree.build(name()).withPrefix(space);
JLeftPadded<J.Identifier> alias = null;
int endOfWhitespace = indexOfNextNonWhitespace(cursor, source);
if (endOfWhitespace + 2 <= source.length() && "as".equals(source.substring(endOfWhitespace, endOfWhitespace + 2))) {
String simpleName = importNode.getAlias();
alias = padLeft(sourceBefore("as"), new J.Identifier(randomId(), sourceBefore(simpleName), Markers.EMPTY, emptyList(), simpleName, null, null));
if (sourceStartsWith("as")) {
Space beforeAs = sourceBefore("as");
Space prefixIdentifier = whitespace();
alias = padLeft(beforeAs, new J.Identifier(randomId(), prefixIdentifier, Markers.EMPTY, emptyList(), name(), null, null));
}

J.Import anImport = new J.Import(randomId(), prefix, Markers.EMPTY, statik, qualid, alias);
return maybeSemicolon(anImport);
return maybeSemicolon(new J.Import(randomId(), importPrefix, Markers.EMPTY, statik, qualid, alias));
}

RewriteGroovyVisitor groovyVisitor = new RewriteGroovyVisitor(node, new RewriteGroovyClassVisitor(unit));
Expand Down

0 comments on commit 03fbc66

Please sign in to comment.