Skip to content

Commit

Permalink
Merge branch 'main' into feature/artifact-with-property
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd authored Oct 11, 2024
2 parents aaba2d8 + 17ce402 commit e70903b
Show file tree
Hide file tree
Showing 98 changed files with 3,467 additions and 1,073 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@State(Scope.Benchmark)
Expand All @@ -38,10 +39,13 @@ public void setup() throws URISyntaxException, IOException {
throw new RuntimeException("Unable to create directory");
}

sourceFiles = new ArrayList<>(1_000);
for (int i = 0; i < 1_000; i++) {
Files.writeString(test.resolve("Test" + i + ".java"),
"package test; class Test" + i + " {}");
sourceFiles = new ArrayList<>(100);
// to add some "meat to the bones"
String whitespace = String.join("", Collections.nCopies(1_000, " "));
for (int i = 0; i < 100; i++) {
Path path = test.resolve("Test" + i + ".java");
Files.writeString(path, "package test; class Test%d {%s}".formatted(i, whitespace));
sourceFiles.add(path);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,64 +16,53 @@
package org.openrewrite.benchmarks.java;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Parser;
import org.openrewrite.java.JavaParser;
import org.openrewrite.tree.ParsingExecutionContextView;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

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

@Fork(1)
@Measurement(iterations = 2)
@Warmup(iterations = 2)
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
@Threads(4)
public class ParserInputBenchmark {

@Benchmark
public void readFromDisk(JavaFiles state) {
//language=java
public void detectCharset(JavaFiles state, Blackhole bh) {
JavaParser.fromJavaVersion().build()
.parseInputs(state.getSourceFiles().stream()
.map(sourceFile -> new Parser.Input(sourceFile, () -> {
try {
return Files.newInputStream(sourceFile);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
})
)
.map(Parser.Input::fromFile)
.collect(toList()),
null,
new InMemoryExecutionContext());
new InMemoryExecutionContext()
)
.forEach(bh::consume);
}

@Benchmark
public void readFromDiskWithBufferedInputStream(JavaFiles state) {
//language=java
public void knownCharset(JavaFiles state, Blackhole bh) {
ParsingExecutionContextView ctx = ParsingExecutionContextView.view(new InMemoryExecutionContext())
.setCharset(StandardCharsets.UTF_8);
JavaParser.fromJavaVersion().build()
.parseInputs(state.getSourceFiles().stream()
.map(sourceFile -> new Parser.Input(sourceFile, () -> {
try {
return new BufferedInputStream(Files.newInputStream(sourceFile));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
})
)
.map(Parser.Input::fromFile)
.collect(toList()),
null,
new InMemoryExecutionContext());
ctx
)
.forEach(bh::consume);
}

public static void main(String[] args) throws RunnerException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public Tree postVisit(Tree tree, ExecutionContext ctx) {
return tree;
}

String snippet = tree instanceof SourceFile ? null : tree.printTrimmed(getCursor());
String snippet = tree instanceof SourceFile ? null : tree.printTrimmed(getCursor().getParentTreeCursor());
if (snippet != null && maxSnippetLength != null && snippet.length() > maxSnippetLength) {
snippet = snippet.substring(0, maxSnippetLength);
}
Expand Down
29 changes: 17 additions & 12 deletions rewrite-core/src/main/java/org/openrewrite/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,11 @@ default Stream<SourceFile> parse(String... sources) {

default Stream<SourceFile> parse(ExecutionContext ctx, String... sources) {
return parseInputs(
Arrays.stream(sources).map(source ->
new Input(
sourcePathFromSourceText(Paths.get(Long.toString(System.nanoTime())), source), null,
() -> new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8)),
true
)
).collect(toList()),
Arrays.stream(sources)
.map(source ->
Input.fromString(
sourcePathFromSourceText(Paths.get(Long.toString(System.nanoTime())), source), source)
).collect(toList()),
null,
ctx
);
Expand Down Expand Up @@ -177,6 +175,16 @@ public static Input fromString(Path sourcePath, String source, Charset charset)
return new Input(sourcePath, null, () -> new ByteArrayInputStream(source.getBytes(charset)), true);
}

public static Input fromFile(Path sourcePath) {
return new Input(sourcePath, FileAttributes.fromPath(sourcePath), () -> {
try {
return Files.newInputStream(sourcePath);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}, false);
}

@SuppressWarnings("unused")
public static Input fromResource(String resource) {
return new Input(
Expand All @@ -194,11 +202,8 @@ public static List<Input> fromResource(String resource, String delimiter) {
public static List<Input> fromResource(String resource, String delimiter, @Nullable Charset charset) {
Charset resourceCharset = charset == null ? StandardCharsets.UTF_8 : charset;
return Arrays.stream(StringUtils.readFully(Objects.requireNonNull(Input.class.getResourceAsStream(resource)), resourceCharset).split(delimiter))
.map(source -> new Parser.Input(
Paths.get(Long.toString(System.nanoTime())), null,
() -> new ByteArrayInputStream(source.getBytes(resourceCharset)),
true
))
.map(source -> Parser.Input.fromString(
Paths.get(Long.toString(System.nanoTime())), source))
.collect(toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.dataformat.smile.SmileFactory;
import com.fasterxml.jackson.dataformat.smile.SmileGenerator;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;

import java.io.IOException;
Expand All @@ -47,7 +48,7 @@ public RecipeSerializer() {
// see https://cowtowncoder.medium.com/jackson-2-12-most-wanted-3-5-246624e2d3d0
.constructorDetector(ConstructorDetector.USE_PROPERTIES_BASED)
.build()
.registerModule(new ParameterNamesModule())
.registerModules(new ParameterNamesModule(), new JavaTimeModule())
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
maybeAddKotlinModule(m);
Expand Down
12 changes: 1 addition & 11 deletions rewrite-core/src/main/java/org/openrewrite/Tree.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,15 @@
*/
package org.openrewrite;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.jspecify.annotations.Nullable;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.marker.Markers;

import java.util.UUID;

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "@c")
@JsonPropertyOrder({"@c"}) // serialize type info first
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "@c", include = JsonTypeInfo.As.PROPERTY)
public interface Tree {
@SuppressWarnings("unused")
@JsonProperty("@c")
default String getJacksonPolymorphicTypeTag() {
return getClass().getName();
}

static UUID randomId() {
//noinspection ConstantConditions
Expand Down
36 changes: 19 additions & 17 deletions rewrite-core/src/main/java/org/openrewrite/TreeVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.Timer;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.RecipeRunException;
Expand All @@ -35,6 +36,7 @@
import java.util.function.Function;

import static java.util.Collections.emptyList;
import static java.util.Objects.requireNonNull;

/**
* Abstract {@link TreeVisitor} for processing {@link Tree elements}
Expand All @@ -48,7 +50,7 @@
* @param <T> The type of tree.
* @param <P> An input object that is passed to every visit method.
*/
public abstract class TreeVisitor<T extends Tree, P> {
public abstract class TreeVisitor<T extends @Nullable Tree, P> {
private static final String STOP_AFTER_PRE_VISIT = "__org.openrewrite.stopVisitor__";

Cursor cursor = new Cursor(null, Cursor.ROOT_VALUE);
Expand All @@ -69,7 +71,7 @@ public static <T extends Tree, P> TreeVisitor<T, P> noop() {
};
}

private List<TreeVisitor<?, P>> afterVisit;
private @Nullable List<TreeVisitor<?, P>> afterVisit;

private int visitCount;
private final DistributionSummary visitCountSummary = DistributionSummary.builder("rewrite.visitor.visit.method.count").description("Visit methods called per source file visited.").tag("visitor.class", getClass().getName()).register(Metrics.globalRegistry);
Expand All @@ -79,6 +81,7 @@ public boolean isAcceptable(SourceFile sourceFile, P p) {
}

public void setCursor(@Nullable Cursor cursor) {
assert cursor != null;
this.cursor = cursor;
}

Expand Down Expand Up @@ -126,23 +129,29 @@ public final Cursor updateCursor(T currentValue) {
if (!(old instanceof Tree)) {
throw new IllegalArgumentException("To update the cursor, it must currently be positioned at a Tree instance");
}
if (!((Tree) old).getId().equals(currentValue.getId())) {
if (!((Tree) old).getId().equals(requireNonNull(currentValue).getId())) {
throw new IllegalArgumentException("Updating the cursor in place is only supported for mutations on a Tree instance " +
"that maintain the same ID after the mutation.");
}
cursor = new Cursor(cursor.getParentOrThrow(), currentValue);
return cursor;
}

public @Nullable T preVisit(T tree, P p) {
public @Nullable T preVisit(@NonNull T tree, P p) {
return defaultValue(tree, p);
}

public @Nullable T postVisit(T tree, P p) {
public @Nullable T postVisit(@NonNull T tree, P p) {
return defaultValue(tree, p);
}

public @Nullable T visit(@Nullable Tree tree, P p, Cursor parent) {
if (parent.getValue() instanceof Tree && ((Tree) parent.getValue()).isScope(tree)) {
throw new IllegalArgumentException(
"The `parent` cursor must not point to the same `tree` as the tree to be visited. " +
"This usually indicates that you have used getCursor() where getCursor().getParent() is appropriate."
);
}
this.cursor = parent;
return visit(tree, p);
}
Expand All @@ -156,18 +165,13 @@ public final Cursor updateCursor(T currentValue) {
* @param p A state object that passes through the visitor.
* @return A non-null tree.
*/
public T visitNonNull(Tree tree, P p) {
public @NonNull T visitNonNull(Tree tree, P p) {
T t = visit(tree, p);
assert t != null;
return t;
}

public T visitNonNull(Tree tree, P p, Cursor parent) {
if (parent.getValue() instanceof Tree && ((Tree) parent.getValue()).isScope(tree)) {
throw new IllegalArgumentException(
"The `parent` cursor must not point to the same `tree` as the tree to be visited"
);
}
public @NonNull T visitNonNull(Tree tree, P p, Cursor parent) {
T t = visit(tree, p, parent);
assert t != null;
return t;
Expand Down Expand Up @@ -266,11 +270,9 @@ public P reduce(Tree tree, P p, Cursor parent) {

if (t != null && afterVisit != null) {
for (TreeVisitor<?, P> v : afterVisit) {
if (v != null) {
v.setCursor(getCursor());
//noinspection unchecked
t = (T) v.visit(t, p);
}
v.setCursor(getCursor());
//noinspection unchecked
t = (T) v.visit(t, p);
}
}

Expand Down
Loading

0 comments on commit e70903b

Please sign in to comment.