Skip to content

Commit

Permalink
WIP keep the used JavaParser in Marker
Browse files Browse the repository at this point in the history
  • Loading branch information
fabapp2 committed Oct 16, 2023
1 parent 7dc1a6a commit 9ca1d25
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2021 - 2023 the original author or authors.
*
* 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
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* 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.springframework.sbm.parsers;

import lombok.Value;
import lombok.With;
import org.openrewrite.java.JavaParser;
import org.openrewrite.marker.Marker;

import java.util.UUID;


/**
* Used to keep the stateful {@link JavaParser} for later parsing.
*
* This is required when (re-)parsing a submodule somewhere (non-leaf) in a reactor build tree.
* Then this module requires the JavaParser with types from the last parse where types from lower modules are cached.
*
* @author Fabian Krüger
*/
@Value
@With
public class JavaParserMarker implements Marker {
private final UUID id;
private final JavaParser javaParser;
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,14 @@ public List<SourceFile> processMainSources(
.map(r -> new Parser.Input(ResourceUtil.getPath(r), () -> ResourceUtil.getInputStream(r)))
.toList();

Stream<? extends SourceFile> cus = Stream.of(javaParserBuilder)
.map(JavaParser.Builder::build)
.flatMap(parser -> parser.parseInputs(inputs, baseDir, executionContext))
JavaParser javaParser = javaParserBuilder.build();
Stream<? extends SourceFile> cus = javaParser.parseInputs(inputs, baseDir, executionContext)
.peek(s -> alreadyParsed.add(baseDir.resolve(s.getSourcePath())));


List<Marker> mainProjectProvenance = new ArrayList<>(provenanceMarkers);
mainProjectProvenance.add(sourceSet("main", dependencies, typeCache));
mainProjectProvenance.add(new JavaParserMarker(UUID.randomUUID(), javaParser));

// FIXME: 945 Why target and not all main?
List<Path> parsedJavaPaths = mainJavaSources.stream().map(ResourceUtil::getPath).toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.openrewrite.SourceFile;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.internal.JavaTypeCache;
import org.openrewrite.java.internal.TypesInUse;
import org.openrewrite.java.marker.JavaSourceSet;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
Expand Down Expand Up @@ -154,6 +155,10 @@ void differentParsersWithSharedCacheAndSeparateCallsShouldNotResolveTypes() {
// -> Should resolve type A
// -> Same parser, separate calls, same type cache
// --> WORKS! That's what we need.
// BUT! It'd require keeping and parsing the parser -> Can a parser parse the same resource twice?
// Yes, but it needs to be reset
// Type A can also be resolved from B then.
// So keeping and passing the used parser (per source set) would be an option.
@Test
@DisplayName("Same parser with shared cache in separate calls should resolve types")
void sameParserWithSharedCacheInSeparateCallsShouldResolveTypes() {
Expand All @@ -164,14 +169,23 @@ void sameParserWithSharedCacheInSeparateCallsShouldResolveTypes() {

// Same parser two calls
SourceFile a = javaParser.parse(aSource).toList().get(0);
SourceFile b1= javaParser.parse(bSource).toList().get(0);
// fails! need to call reset()
javaParser.reset();
SourceFile b = javaParser.parse(bSource).toList().get(0);


J.CompilationUnit compilationUnitB = (J.CompilationUnit) b;
String fullyQualifiedName2 = ((JavaType.FullyQualified) compilationUnitB.getClasses().get(0).getExtends().getType()).getFullyQualifiedName();
// A can be resolved
assertThat(fullyQualifiedName2).isEqualTo("com.foo.A");

List<String> bTypesInUseFqNames = compilationUnitB.getTypesInUse().getTypesInUse().stream().map(fq -> ((JavaType.FullyQualified) fq).getFullyQualifiedName()).toList();

TypesInUse.FindTypesInUse findTypesInUse = new TypesInUse.FindTypesInUse();
findTypesInUse.visit(compilationUnitB, 0);
System.out.println(findTypesInUse.getTypes());

// A is in typesInUse of B
assertThat(bTypesInUseFqNames).contains("com.foo.A");
//No markers were set
Expand Down Expand Up @@ -270,7 +284,6 @@ void someTest() {
List<Xml.Document> parsedBuildFiles = buildFileParser.parseBuildFiles(baseDir, List.of(resources.get(0)), List.of(), new RewriteExecutionContext(), false, provenanceMarkersMap);
Xml.Document document = parsedBuildFiles.get(0);


Collection<Path> classpath = new ArrayList<>();
Collection<byte[]> classBytesClasspath = new ArrayList<>();

Expand Down

0 comments on commit 9ca1d25

Please sign in to comment.