Skip to content

Commit

Permalink
Merge branch 'main' into 4660-support-jep-440-and-441
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsdebruin authored Nov 15, 2024
2 parents 212fd51 + 3e89b4a commit 3a2b164
Show file tree
Hide file tree
Showing 57 changed files with 3,238 additions and 483 deletions.
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionSha256Sum=31c55713e40233a8303827ceb42ca48a47267a0ad4bab9177123121e71524c26
distributionSha256Sum=57dafb5c2622c6cc08b993c85b7c06956a2f53536432a30ead46166dbca0f1e9
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public PlainText visitText(PlainText text, ExecutionContext ctx) {
private List<String> sortRules(String[] originalRules, List<String> newRules) {
LinkedList<String> results = new LinkedList<>();
Arrays.stream(originalRules).filter(line -> {
if (line.startsWith("#") || StringUtils.isBlank(line)) {
if (StringUtils.isBlank(line) || line.startsWith("#")) {
return true;
}
return newRules.stream().anyMatch(line::equalsIgnoreCase);
Expand All @@ -127,17 +127,18 @@ private List<String> sortRules(String[] originalRules, List<String> newRules) {
return distinctValuesStartingReversed(results);
}

private <T> List<T> distinctValuesStartingReversed(List<T> list) {
Set<T> set = new LinkedHashSet<>();
ListIterator<T> iterator = list.listIterator(list.size());
private List<String> distinctValuesStartingReversed(List<String> list) {
LinkedList<String> filteredList = new LinkedList<>();
ListIterator<String> iterator = list.listIterator(list.size());

while (iterator.hasPrevious()) {
set.add(iterator.previous());
String previous = iterator.previous();
if (StringUtils.isBlank(previous) || previous.startsWith("#") || !filteredList.contains(previous)) {
filteredList.addFirst(previous);
}
}

List<T> result = new ArrayList<>(set);
Collections.reverse(result);
return result;
return filteredList;
}
});
}
Expand Down
80 changes: 51 additions & 29 deletions rewrite-core/src/main/java/org/openrewrite/FindParseFailures.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.marker.DeserializationError;
import org.openrewrite.marker.Markup;
import org.openrewrite.table.ParseFailures;

Expand All @@ -35,9 +36,9 @@ public class FindParseFailures extends Recipe {
Integer maxSnippetLength;

@Option(displayName = "Parser type",
description = "Only display failures from parsers with this fully qualified name.",
description = "Only display failures from parsers with this simple name.",
required = false,
example = "org.openrewrite.yaml.YamlParser")
example = "YamlParser")
@Nullable
String parserType;

Expand Down Expand Up @@ -67,33 +68,54 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
@Override
public Tree postVisit(Tree tree, ExecutionContext ctx) {
return tree.getMarkers().findFirst(ParseExceptionResult.class)
.map(exceptionResult -> {
if (parserType != null && !Objects.equals(exceptionResult.getParserType(), parserType)) {
return tree;
}

if (stackTrace != null && !exceptionResult.getMessage().contains(stackTrace)) {
return tree;
}

String snippet = tree instanceof SourceFile ? null : tree.printTrimmed(getCursor().getParentTreeCursor());
if (snippet != null && maxSnippetLength != null && snippet.length() > maxSnippetLength) {
snippet = snippet.substring(0, maxSnippetLength);
}

failures.insertRow(ctx, new ParseFailures.Row(
exceptionResult.getParserType(),
(tree instanceof SourceFile ? (SourceFile) tree : getCursor().firstEnclosingOrThrow(SourceFile.class))
.getSourcePath().toString(),
exceptionResult.getExceptionType(),
exceptionResult.getTreeType(),
snippet,
exceptionResult.getMessage()
));

return Markup.info(tree, exceptionResult.getMessage());
})
.orElse(tree);
.map(exceptionResult -> report(tree, exceptionResult, ctx))
.orElse(tree.getMarkers().findFirst(DeserializationError.class)
.map(error -> report(tree, error, ctx))
.orElse(tree)
);
}

private Tree report(Tree tree, DeserializationError error, ExecutionContext ctx) {
if (stackTrace != null && !error.getDetail().contains(stackTrace)) {
return tree;
}

failures.insertRow(ctx, new ParseFailures.Row(
"Unknown",
(tree instanceof SourceFile ? (SourceFile) tree : getCursor().firstEnclosingOrThrow(SourceFile.class))
.getSourcePath().toString(),
"DeserializationError",
null,
null,
error.getDetail()
));

return Markup.info(tree, error.getMessage());
}

private Tree report(Tree tree, ParseExceptionResult exceptionResult, ExecutionContext ctx) {
if (parserType != null && !Objects.equals(exceptionResult.getParserType(), parserType)) {
return tree;
} else if (stackTrace != null && !exceptionResult.getMessage().contains(stackTrace)) {
return tree;
}

String snippet = tree instanceof SourceFile ? null : tree.printTrimmed(getCursor().getParentTreeCursor());
if (snippet != null && maxSnippetLength != null && snippet.length() > maxSnippetLength) {
snippet = snippet.substring(0, maxSnippetLength);
}

failures.insertRow(ctx, new ParseFailures.Row(
exceptionResult.getParserType(),
(tree instanceof SourceFile ? (SourceFile) tree : getCursor().firstEnclosingOrThrow(SourceFile.class))
.getSourcePath().toString(),
exceptionResult.getExceptionType(),
exceptionResult.getTreeType(),
snippet,
exceptionResult.getMessage()
));

return Markup.info(tree, exceptionResult.getMessage());
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.jspecify.annotations.Nullable;
import org.openrewrite.trait.Reference;

import java.util.*;

@Incubating(since = "8.39.0")
public interface SourceFileWithReferences extends SourceFile {

References getReferences();

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
class References {
private final SourceFile sourceFile;
private final Set<Reference> references;

public Collection<Reference> findMatches(Reference.Matcher matcher) {
return findMatchesInternal(matcher, null);
}

public Collection<Reference> findMatches(Reference.Matcher matcher, Reference.Kind kind) {
return findMatchesInternal(matcher, kind);
}

private List<Reference> findMatchesInternal(Reference.Matcher matcher, Reference.@Nullable Kind kind) {
List<Reference> list = new ArrayList<>();
for (Reference ref : references) {
if ((kind == null || ref.getKind().equals(kind)) && ref.matches(matcher) ) {
list.add(ref);
}
}
return list;
}

public static References build(SourceFile sourceFile) {
Set<Reference> references = new HashSet<>();
ServiceLoader<Reference.Provider> loader = ServiceLoader.load(Reference.Provider.class);
loader.forEach(provider -> {
if (provider.isAcceptable(sourceFile)) {
references.addAll(provider.getReferences(sourceFile));
}
});
return new References(sourceFile, references);
}
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 3a2b164

Please sign in to comment.