Skip to content

Commit

Permalink
Similarly for class expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Jan 26, 2025
1 parent e749ff1 commit 95644f7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -865,15 +865,19 @@ public boolean endsWithClosures(List<org.codehaus.groovy.ast.expr.Expression> li

@Override
public void visitClassExpression(ClassExpression clazz) {
String unresolvedName = clazz.getType().getUnresolvedName().replace('$', '.');
Space space = sourceBefore(unresolvedName);
if (source.substring(cursor).startsWith(".class")) {
unresolvedName += ".class";
Space prefix = whitespace();
String name = clazz.getType().getUnresolvedName().replace('$', '.');
if (!source.startsWith(name, cursor)) {
name = clazz.getType().getNameWithoutPackage().replace('$', '.');
}
skip(name);
if (source.startsWith(".class", cursor)) {
name += ".class";
skip(".class");
}
queue.add(TypeTree.build(unresolvedName)
queue.add(TypeTree.build(name)
.withType(typeMapping.type(clazz.getType()))
.withPrefix(space));
.withPrefix(prefix));
}

@Override
Expand Down Expand Up @@ -1587,7 +1591,7 @@ public void visitGStringExpression(GStringExpression gstring) {
skip(delimiter); // Opening delim for GString

NavigableMap<LineColumn, org.codehaus.groovy.ast.expr.Expression> sortedByPosition = new TreeMap<>();
for (org.codehaus.groovy.ast.expr.ConstantExpression e : gstring.getStrings()) {
for (ConstantExpression e : gstring.getStrings()) {
// There will always be constant expressions before and after any values
// No need to represent these empty strings
if (!e.getText().isEmpty()) {
Expand Down Expand Up @@ -1665,7 +1669,7 @@ public void visitMapExpression(MapExpression map) {
skip("[");
JContainer<G.MapEntry> entries;
if (map.getMapEntryExpressions().isEmpty()) {
entries = JContainer.build(Collections.singletonList(JRightPadded.build(
entries = JContainer.build(singletonList(JRightPadded.build(
new G.MapEntry(randomId(), whitespace(), Markers.EMPTY,
JRightPadded.build(new J.Empty(randomId(), sourceBefore(":"), Markers.EMPTY)),
new J.Empty(randomId(), sourceBefore("]"), Markers.EMPTY), null))));
Expand Down Expand Up @@ -2263,7 +2267,7 @@ private static class LineColumn implements Comparable<LineColumn> {
int column;

@Override
public int compareTo(GroovyParserVisitor.@NonNull LineColumn lc) {
public int compareTo(@NonNull LineColumn lc) {
return line != lc.line ? line - lc.line : column - lc.column;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ void innerClassViaImport() {
"""
)
);
}

@Test
void unqualified() {
rewriteRun(
groovy(
"""
package foo
interface MyEntity {
}
class Foo {
void setUp() {
e = MyEntity.class
}
}
"""
)
);
}
}

0 comments on commit 95644f7

Please sign in to comment.