Revert changes to pretty printing
This commit is contained in:
2 files changed
+9
-89
No files matched your search
+9
-63
@@ -7,25 +7,21 @@ package net.sourceforge.pmd.lang.java.ast.internal;
|
||||
import static net.sourceforge.pmd.util.AssertionUtil.shouldNotReachHere;
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTAmbiguousName;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTAnnotationTypeDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTArgumentList;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTArrayAccess;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTArrayType;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTCastExpression;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTClassDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTClassLiteral;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTClassType;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTConstructorCall;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTEnumDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTExecutableDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTExpression;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTFieldAccess;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTForInit;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTFormalParameter;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTFormalParameters;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
|
||||
@@ -34,7 +30,6 @@ import net.sourceforge.pmd.lang.java.ast.ASTList;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTLiteral;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTMethodCall;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTMethodReference;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTRecordDeclaration;
|
||||
@@ -127,10 +122,10 @@ public final class PrettyPrintingUtil {
|
||||
}
|
||||
} else if (t instanceof ASTUnionType) {
|
||||
CollectionUtil.joinOn(sb, ((ASTUnionType) t).getComponents(),
|
||||
PrettyPrintingUtil::prettyPrintTypeNode, " | ");
|
||||
PrettyPrintingUtil::prettyPrintTypeNode, " | ");
|
||||
} else if (t instanceof ASTIntersectionType) {
|
||||
CollectionUtil.joinOn(sb, ((ASTIntersectionType) t).getComponents(),
|
||||
PrettyPrintingUtil::prettyPrintTypeNode, " & ");
|
||||
PrettyPrintingUtil::prettyPrintTypeNode, " & ");
|
||||
} else if (t instanceof ASTAmbiguousName) {
|
||||
sb.append(((ASTAmbiguousName) t).getName());
|
||||
} else {
|
||||
@@ -254,7 +249,6 @@ public final class PrettyPrintingUtil {
|
||||
|
||||
@Override
|
||||
public Void visitJavaNode(JavaNode node, StringBuilder data) {
|
||||
data.append("<<NOT_IMPLEMENTED: ").append(node).append(">>");
|
||||
return null; // don't recurse
|
||||
}
|
||||
|
||||
@@ -332,39 +326,17 @@ public final class PrettyPrintingUtil {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit(ASTAmbiguousName node, StringBuilder data) {
|
||||
data.append(node.getName());
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit(ASTMethodCall node, StringBuilder sb) {
|
||||
addQualifier(node, sb);
|
||||
ppTypeArgs(sb, node.getExplicitTypeArguments());
|
||||
sb.append(node.getMethodName());
|
||||
ppArguments(sb, node.getArguments());
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit(ASTConstructorCall node, StringBuilder sb) {
|
||||
addQualifier(node, sb);
|
||||
sb.append("new ");
|
||||
ppTypeArgs(sb, node.getExplicitTypeArguments());
|
||||
prettyPrintTypeNode(sb, node.getTypeNode());
|
||||
ppArguments(sb, node.getArguments());
|
||||
return null;
|
||||
}
|
||||
|
||||
private void ppArguments(StringBuilder sb, ASTArgumentList arguments) {
|
||||
if (arguments.isEmpty()) {
|
||||
if (node.getArguments().isEmpty()) {
|
||||
sb.append("()");
|
||||
} else {
|
||||
final int argStart = sb.length();
|
||||
sb.append('(');
|
||||
boolean first = true;
|
||||
for (ASTExpression arg : arguments) {
|
||||
for (ASTExpression arg : node.getArguments()) {
|
||||
if (sb.length() - argStart >= MAX_ARG_LENGTH) {
|
||||
sb.append("...");
|
||||
break;
|
||||
@@ -376,30 +348,18 @@ public final class PrettyPrintingUtil {
|
||||
}
|
||||
sb.append(')');
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit(ASTMethodReference node, StringBuilder sb) {
|
||||
ppMaybeInParens(sb, node.getQualifier());
|
||||
sb.append("::");
|
||||
ppTypeArgs(sb, node.getExplicitTypeArguments());
|
||||
sb.append(node.getMethodName());
|
||||
return null;
|
||||
}
|
||||
|
||||
private void ppMaybeInParens(StringBuilder sb, ASTExpression qualifier) {
|
||||
if (!(qualifier instanceof ASTPrimaryExpression)) {
|
||||
ppInParens(sb, qualifier);
|
||||
} else {
|
||||
qualifier.acceptVisitor(this, sb);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void addQualifier(QualifiableExpression node, StringBuilder data) {
|
||||
ASTExpression qualifier = node.getQualifier();
|
||||
if (qualifier != null) {
|
||||
ppMaybeInParens(data, qualifier);
|
||||
if (!(qualifier instanceof ASTPrimaryExpression)) {
|
||||
ppInParens(data, qualifier);
|
||||
} else {
|
||||
qualifier.acceptVisitor(this, data);
|
||||
}
|
||||
data.append('.');
|
||||
}
|
||||
|
||||
@@ -411,20 +371,6 @@ public final class PrettyPrintingUtil {
|
||||
return data.append(')');
|
||||
}
|
||||
|
||||
|
||||
private void ppTypeArgs(StringBuilder data, @Nullable ASTTypeArguments targs) {
|
||||
if (targs == null) {
|
||||
return;
|
||||
}
|
||||
data.append('<');
|
||||
prettyPrintTypeNode(data, targs.get(0));
|
||||
for (int i = 1; i < targs.size(); i++) {
|
||||
data.append(", ");
|
||||
prettyPrintTypeNode(data, targs.get(i));
|
||||
}
|
||||
data.append('>');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
-26
@@ -19,10 +19,8 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import net.sourceforge.pmd.lang.java.BaseParserTest;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTConstructorCall;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTMethodCall;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTMethodReference;
|
||||
import net.sourceforge.pmd.util.StringUtil;
|
||||
|
||||
class PrettyPrintingUtilTest extends BaseParserTest {
|
||||
@@ -61,30 +59,6 @@ class PrettyPrintingUtilTest extends BaseParserTest {
|
||||
assertThat(prettyPrint(m), contentEquals("((Object) this).foo(12)"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void ppMethodRef() {
|
||||
ASTCompilationUnit root = java.parse("class A { { foo(ASTW::meth); } }");
|
||||
@NonNull ASTMethodReference m = root.descendants(ASTMethodReference.class).firstOrThrow();
|
||||
|
||||
assertThat(prettyPrint(m), contentEquals("ASTW::meth"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void ppCtorCall() {
|
||||
ASTCompilationUnit root = java.parse("class A { { new Foo(1); } }");
|
||||
@NonNull ASTConstructorCall m = root.descendants(ASTConstructorCall.class).firstOrThrow();
|
||||
|
||||
assertThat(prettyPrint(m), contentEquals("new Foo(1)"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void ppMethodRefWithTyArgs() {
|
||||
ASTCompilationUnit root = java.parse("class A { { foo(ASTW::<String>meth); } }");
|
||||
@NonNull ASTMethodReference m = root.descendants(ASTMethodReference.class).firstOrThrow();
|
||||
|
||||
assertThat(prettyPrint(m), contentEquals("ASTW::<String>meth"));
|
||||
}
|
||||
|
||||
private static Matcher<CharSequence> contentEquals(String str) {
|
||||
return new BaseMatcher<CharSequence>() {
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user