Skip to content

Commit

Permalink
Again try to improve error message from AddPluginVisitor
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Dec 14, 2023
1 parent cba6919 commit 7259cc8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
15 changes: 14 additions & 1 deletion rewrite-core/src/main/java/org/openrewrite/tree/ParseError.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
*/
package org.openrewrite.tree;

import lombok.*;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Value;
import lombok.With;
import org.openrewrite.*;
import org.openrewrite.internal.EncodingDetectingInputStream;
import org.openrewrite.internal.lang.Nullable;
Expand Down Expand Up @@ -104,4 +107,14 @@ public static ParseError build(Parser parser,
null
);
}

public IllegalStateException toException() {
if (getErroneous() != null) {
return new IllegalStateException("Failed to parse " + getSourcePath() + " because of parse-to-print idempotence on: " + getText());
} else {
ParseExceptionResult ex = getMarkers().findFirst(ParseExceptionResult.class)
.orElseThrow(() -> new IllegalStateException("No ParseExceptionResult marker on parser failure"));
return new IllegalStateException(ex.getExceptionType() + ": " + ex.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
.orElseThrow(() -> new IllegalArgumentException("Could not parse as Gradle"));

if (parsed instanceof ParseError) {
ParseError error = (ParseError) parsed;
if (error.getErroneous() != null) {
throw new IllegalStateException("Failed to parse the generated dependency because of parse-to-print idempotence.");
} else {
ParseExceptionResult ex = error.getMarkers().findFirst(ParseExceptionResult.class)
.orElseThrow(() -> new IllegalStateException("No ParseExceptionResult marker on parser failure."));
throw new IllegalStateException(ex.getExceptionType() + ": " + ex.getMessage());
}
throw ((ParseError) parsed).toException();
}

J.MethodInvocation addDependencyInvocation = requireNonNull((J.MethodInvocation) ((J.Return) (((J.Block) ((J.Lambda) ((J.MethodInvocation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.openrewrite.maven.tree.MavenMetadata;
import org.openrewrite.maven.tree.MavenRepository;
import org.openrewrite.semver.*;
import org.openrewrite.tree.ParseError;

import java.nio.file.Paths;
import java.util.Collections;
Expand Down Expand Up @@ -118,7 +119,7 @@ private static MavenMetadata downloadMetadata(String groupId, String artifactId,

private static boolean isLicenseHeader(Comment comment) {
return comment instanceof TextComment && comment.isMultiline() &&
((TextComment) comment).getText().contains("License");
((TextComment) comment).getText().contains("License");
}

private static G.CompilationUnit removeLicenseHeader(G.CompilationUnit cu) {
Expand Down Expand Up @@ -177,17 +178,20 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Integ
Statement statement = GradleParser.builder().build()
.parseInputs(singletonList(Parser.Input.fromString(source)), null, ctx)
.findFirst()
.filter(G.CompilationUnit.class::isInstance)
.map(G.CompilationUnit.class::cast)
.orElseThrow(() -> new IllegalArgumentException("Could not parse: " + source))
.getStatements()
.get(0);
.map(parsed -> {
if (parsed instanceof ParseError) {
throw ((ParseError) parsed).toException();
}
return ((G.CompilationUnit) parsed);
})
.map(parsed -> parsed.getStatements().get(0))
.orElseThrow(() -> new IllegalArgumentException("Could not parse as Gradle"));

if (FindMethods.find(cu, "RewriteGradleProject plugins(..)").isEmpty() && FindMethods.find(cu, "RewriteSettings plugins(..)").isEmpty()) {
if (cu.getSourcePath().endsWith(Paths.get("settings.gradle"))
&& !cu.getStatements().isEmpty()
&& cu.getStatements().get(0) instanceof J.MethodInvocation
&& ((J.MethodInvocation) cu.getStatements().get(0)).getSimpleName().equals("pluginManagement")) {
&& !cu.getStatements().isEmpty()
&& cu.getStatements().get(0) instanceof J.MethodInvocation
&& ((J.MethodInvocation) cu.getStatements().get(0)).getSimpleName().equals("pluginManagement")) {
return cu.withStatements(ListUtils.insert(cu.getStatements(), autoFormat(statement.withPrefix(Space.format("\n\n")), ctx, getCursor()), 1));
} else {
int insertAtIdx = 0;
Expand Down

0 comments on commit 7259cc8

Please sign in to comment.