Skip to content

Commit

Permalink
Merge branch 'main' into feature/3156_Lombok_UtilityClass
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Jun 30, 2023
2 parents 66e9dbf + 093048f commit 15fe027
Show file tree
Hide file tree
Showing 171 changed files with 4,668 additions and 2,337 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,16 @@
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.SourceFile;
import org.openrewrite.maven.MavenExecutionContextView;
import org.openrewrite.maven.MavenParser;
import org.openrewrite.maven.cache.CompositeMavenPomCache;
import org.openrewrite.maven.cache.InMemoryMavenPomCache;
import org.openrewrite.maven.cache.RocksdbMavenPomCache;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.xml.tree.Xml;

import java.nio.file.Paths;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Fork(1)
@Measurement(iterations = 2)
Expand All @@ -61,7 +59,7 @@ public void parse(Blackhole blackhole) {
MavenExecutionContextView ctx = MavenExecutionContextView.view(new InMemoryExecutionContext());
ctx.setPomCache(pomCache);

Optional<Xml.Document> maven = MavenParser.builder().build().parse(ctx,
Optional<SourceFile> maven = MavenParser.builder().build().parse(ctx,
"" +
"<project>" +
" <parent>" +
Expand Down
11 changes: 0 additions & 11 deletions rewrite-core/openrewrite.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,6 @@
"type": "boolean",
"default": false
},
"applicability": {
"type": "object",
"properties": {
"singleSource": {
"$ref": "#/$defs/recipeList"
},
"anySource": {
"$ref": "#/$defs/recipeList"
}
}
},
"recipeList": {
"$ref": "#/$defs/recipeList"
}
Expand Down
7 changes: 7 additions & 0 deletions rewrite-core/src/main/java/org/openrewrite/Cursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,13 @@ public <T> T pollMessage(String key) {
return messages == null ? null : (T) messages.remove(key);
}

public void clearMessages() {
if (messages != null) {
messages.clear();
messages = null;
}
}

/**
* Creates a cursor at the same position, but with its own messages that can't influence
* the messages of the cursor that was forked.
Expand Down
37 changes: 27 additions & 10 deletions rewrite-core/src/main/java/org/openrewrite/FindParseFailures.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@
import org.openrewrite.marker.Markup;
import org.openrewrite.table.ParseFailures;

import static java.util.Objects.requireNonNull;

@Value
@EqualsAndHashCode(callSuper = true)
public class FindParseFailures extends Recipe {
ParseFailures failures = new ParseFailures(this);

@Option(displayName = "Max snippet length",
description = "When the failure occurs on a granular tree element, its source code will be included " +
"as a column in the data table up to this maximum snippet length.",
required = false)
@Nullable
Integer maxSnippetLength;

transient ParseFailures failures = new ParseFailures(this);

@Override
public String getDisplayName() {
Expand All @@ -42,18 +48,29 @@ public String getDescription() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new TreeVisitor<Tree, ExecutionContext>() {

@Override
public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
SourceFile sourceFile = (SourceFile) requireNonNull(tree);
return sourceFile.getMarkers().findFirst(ParseExceptionResult.class)
.<Tree>map(exceptionResult -> {
public Tree postVisit(Tree tree, ExecutionContext ctx) {
return tree.getMarkers().findFirst(ParseExceptionResult.class)
.map(exceptionResult -> {
String snippet = tree instanceof SourceFile ? null : tree.printTrimmed(getCursor());
if(snippet != null && maxSnippetLength != null && snippet.length() > maxSnippetLength) {
snippet = snippet.substring(0, maxSnippetLength);
}

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

return Markup.info(tree, exceptionResult.getMessage());
})
.orElse(sourceFile);
.orElse(tree);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

public class InMemoryExecutionContext implements ExecutionContext {
private final Map<String, Object> messages = new ConcurrentHashMap<>();
private final Consumer<Throwable> onError;
private final Function<Integer, Duration> runTimeout;
private final BiConsumer<Throwable, ExecutionContext> onTimeout;

public InMemoryExecutionContext() {
Expand All @@ -38,19 +36,18 @@ public InMemoryExecutionContext() {
}

public InMemoryExecutionContext(Consumer<Throwable> onError) {
this(onError, n -> Duration.ofHours(2));
this(onError, Duration.ofHours(2));
}

public InMemoryExecutionContext(Consumer<Throwable> onError,
Function<Integer, Duration> runTimeout) {
public InMemoryExecutionContext(Consumer<Throwable> onError, Duration runTimeout) {
this(onError, runTimeout, (throwable, ctx) -> {
});
}

public InMemoryExecutionContext(Consumer<Throwable> onError, Function<Integer, Duration> runTimeout, BiConsumer<Throwable, ExecutionContext> onTimeout) {
public InMemoryExecutionContext(Consumer<Throwable> onError, Duration runTimeout, BiConsumer<Throwable, ExecutionContext> onTimeout) {
this.onError = onError;
this.runTimeout = runTimeout;
this.onTimeout = onTimeout;
putMessage(ExecutionContext.RUN_TIMEOUT, runTimeout);
}

@Override
Expand Down
37 changes: 37 additions & 0 deletions rewrite-core/src/main/java/org/openrewrite/ParseErrorPrinter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2023 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 org.openrewrite.marker.Marker;
import org.openrewrite.tree.ParseError;

public class ParseErrorPrinter<P> extends ParseErrorVisitor<PrintOutputCapture<P>> {
@Override
public ParseError visitParseError(ParseError e, PrintOutputCapture<P> p) {
for (Marker marker : e.getMarkers().getMarkers()) {
p.append(p.getMarkerPrinter().beforePrefix(marker, new Cursor(getCursor(), marker), it -> it));
}
visitMarkers(e.getMarkers(), p);
for (Marker marker : e.getMarkers().getMarkers()) {
p.append(p.getMarkerPrinter().beforeSyntax(marker, new Cursor(getCursor(), marker), it -> it));
}
p.append(e.getText());
for (Marker marker : e.getMarkers().getMarkers()) {
p.append(p.getMarkerPrinter().afterSyntax(marker, new Cursor(getCursor(), marker), it -> it));
}
return e;
}
}
35 changes: 35 additions & 0 deletions rewrite-core/src/main/java/org/openrewrite/ParseErrorVisitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2023 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 org.openrewrite.tree.ParseError;

public class ParseErrorVisitor <P> extends TreeVisitor<Tree, P> {

@Override
public boolean isAcceptable(SourceFile sourceFile, P p) {
return sourceFile instanceof ParseError;
}

@Override
public boolean isAdaptableTo(@SuppressWarnings("rawtypes") Class<? extends TreeVisitor> adaptTo) {
return adaptTo.isAssignableFrom(ParseErrorVisitor.class);
}

public ParseError visitParseError(ParseError e, P p) {
return e.withMarkers(visitMarkers(e.getMarkers(), p));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import lombok.Value;
import lombok.With;
import org.openrewrite.internal.ExceptionUtils;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.marker.Marker;

import java.util.UUID;
Expand All @@ -28,9 +30,28 @@
@With
public class ParseExceptionResult implements Marker {
UUID id;
String parserType;
String exceptionType;
String message;

public static ParseExceptionResult build(Parser<?> parser, Throwable t) {
return new ParseExceptionResult(randomId(), ExceptionUtils.sanitizeStackTrace(t, parser.getClass()));
/**
* The type of tree element that was being parsed when the failure occurred.
*/
@Nullable
String treeType;

public static ParseExceptionResult build(Class<? extends Parser> parserClass, Throwable t) {
String simpleName = t.getClass().getSimpleName();
return new ParseExceptionResult(
randomId(),
parserClass.getSimpleName(),
!StringUtils.isBlank(simpleName) ? simpleName : t.getClass().getName(),
ExceptionUtils.sanitizeStackTrace(t, parserClass),
null
);
}

public static ParseExceptionResult build(Parser parser, Throwable t) {
return build(parser.getClass(), t);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ParseWarning implements Marker {
UUID id;
String message;

public static ParseWarning build(Parser<?> parser, Throwable t) {
public static ParseWarning build(Parser parser, Throwable t) {
return new ParseWarning(randomId(), ExceptionUtils.sanitizeStackTrace(t, parser.getClass()));
}
}
19 changes: 9 additions & 10 deletions rewrite-core/src/main/java/org/openrewrite/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@

import static java.util.stream.Collectors.toList;

public interface Parser<S extends SourceFile> {
default Stream<S> parse(Iterable<Path> sourceFiles, @Nullable Path relativeTo, ExecutionContext ctx) {
public interface Parser {
default Stream<SourceFile> parse(Iterable<Path> sourceFiles, @Nullable Path relativeTo, ExecutionContext ctx) {
return parseInputs(StreamSupport
.stream(sourceFiles.spliterator(), false)
.map(sourceFile -> new Input(sourceFile, () -> {
Expand All @@ -54,11 +54,11 @@ default Stream<S> parse(Iterable<Path> sourceFiles, @Nullable Path relativeTo, E
);
}

default Stream<S> parse(String... sources) {
default Stream<SourceFile> parse(String... sources) {
return parse(new InMemoryExecutionContext(), sources);
}

default Stream<S> parse(ExecutionContext ctx, String... sources) {
default Stream<SourceFile> parse(ExecutionContext ctx, String... sources) {
return parseInputs(
Arrays.stream(sources).map(source ->
new Input(
Expand All @@ -79,21 +79,20 @@ default Stream<S> parse(ExecutionContext ctx, String... sources) {
* @param ctx The execution context
* @return A stream of {@link SourceFile}.
*/
Stream<S> parseInputs(Iterable<Input> sources, @Nullable Path relativeTo, ExecutionContext ctx);
Stream<SourceFile> parseInputs(Iterable<Input> sources, @Nullable Path relativeTo, ExecutionContext ctx);

boolean accept(Path path);

default boolean accept(Input input) {
return input.isSynthetic() || accept(input.getPath());
}

default List<Input> acceptedInputs(Iterable<Input> input) {
default Stream<Input> acceptedInputs(Iterable<Input> input) {
return StreamSupport.stream(input.spliterator(), false)
.filter(this::accept)
.collect(toList());
.filter(this::accept);
}

default Parser<S> reset() {
default Parser reset() {
return this;
}

Expand Down Expand Up @@ -218,7 +217,7 @@ abstract class Builder implements Cloneable {
@Getter
private final Class<? extends SourceFile> sourceFileType;

public abstract Parser<?> build();
public abstract Parser build();

/**
* The name of the domain specific language this parser builder produces a parser for.
Expand Down
11 changes: 2 additions & 9 deletions rewrite-core/src/main/java/org/openrewrite/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,6 @@ public String getJacksonPolymorphicTypeTag() {
return getClass().getName();
}

public static final TreeVisitor<?, ExecutionContext> NOOP = new TreeVisitor<Tree, ExecutionContext>() {
@Override
public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
return tree;
}
};

private transient RecipeDescriptor descriptor;

@Nullable
Expand All @@ -94,7 +87,7 @@ public String getDescription() {

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return NOOP;
return TreeVisitor.noop();
}
}

Expand Down Expand Up @@ -269,7 +262,7 @@ public List<Recipe> getRecipeList() {
* @return A tree visitor that will perform operations associated with the recipe.
*/
public TreeVisitor<?, ExecutionContext> getVisitor() {
return NOOP;
return TreeVisitor.noop();
}

public void addDataTable(DataTable<?> dataTable) {
Expand Down
Loading

0 comments on commit 15fe027

Please sign in to comment.