-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
TypeReference
trait to identify Java type references in XML (#4587
) * Initial setup * Use XPathMatcher before checking value * Add JavaTypeReferences to Xml.Document similar to TypesInUse on J.CompilationUnit * Update rewrite-xml/src/main/java/org/openrewrite/xml/internal/JavaTypeReferences.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Small naming fix * Make setup more generic * Add license * Implement ServiceLoader pattern to be able to dynamically provide implementations that detect JavaTypeReference's on source files * Round of self review * Apply most of the feedback * Apply most of the feedback * Missed a few refactor spots * Missed another * Impl has to be public * Rename * More feedback * Merge `SpringTypeReferenceProvider` into `SpringTypeReference` (#4643) * Merge `SpringTypeReferenceProvider` into `SprintTypeReference` * Also rename `getJavaTypeReferences()` * Add code to support `TypeReference` in `UsesType` and `FindTypes` * Add missing `FindTypesTest` * Make TypeReferences a nested class of SourceFileWithTypeReferences * Update rewrite-java/src/main/java/org/openrewrite/java/search/FindTypes.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Remove unused `TypeReference` methods * Rename `Xml.Document#javaTypeReferences` field * Add `@Incubating` annotations --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Knut Wannheden <[email protected]>
- Loading branch information
1 parent
11cb9db
commit 47974da
Showing
13 changed files
with
543 additions
and
59 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
rewrite-core/src/main/java/org/openrewrite/SourceFileWithTypeReferences.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* 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.openrewrite.trait.TypeReference; | ||
|
||
import java.util.*; | ||
|
||
@Incubating(since = "8.39.0") | ||
public interface SourceFileWithTypeReferences extends SourceFile { | ||
|
||
TypeReferences getTypeReferences(); | ||
|
||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE) | ||
@Getter | ||
class TypeReferences { | ||
private final SourceFile sourceFile; | ||
private final Set<TypeReference> typeReferences; | ||
|
||
public Collection<TypeReference> findMatches(TypeReference.Matcher matcher) { | ||
List<TypeReference> list = new ArrayList<>(); | ||
for (TypeReference ref : typeReferences) { | ||
if (ref.matches(matcher)) { | ||
list.add(ref); | ||
} | ||
} | ||
return list; | ||
} | ||
|
||
public static TypeReferences build(SourceFile sourceFile) { | ||
Set<TypeReference> typeReferences = new HashSet<>(); | ||
ServiceLoader<TypeReference.Provider> loader = ServiceLoader.load(TypeReference.Provider.class); | ||
loader.forEach(provider -> { | ||
if (provider.isAcceptable(sourceFile)) { | ||
typeReferences.addAll(provider.getTypeReferences(sourceFile)); | ||
} | ||
}); | ||
return new TypeReferences(sourceFile, typeReferences); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
rewrite-core/src/main/java/org/openrewrite/TypeReferenceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* 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 org.openrewrite.trait.TypeReference; | ||
|
||
import java.util.Set; | ||
|
||
public interface TypeReferenceProvider { | ||
|
||
Set<TypeReference> getTypeReferences(SourceFile sourceFile); | ||
|
||
boolean isAcceptable(SourceFile sourceFile); | ||
} |
43 changes: 43 additions & 0 deletions
43
rewrite-core/src/main/java/org/openrewrite/trait/TypeReference.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* 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.trait; | ||
|
||
import org.openrewrite.Incubating; | ||
import org.openrewrite.SourceFile; | ||
import org.openrewrite.Tree; | ||
|
||
import java.util.Set; | ||
|
||
@Incubating(since = "8.39.0") | ||
public interface TypeReference extends Trait<Tree> { | ||
|
||
String getName(); | ||
|
||
default boolean matches(Matcher matcher) { | ||
return matcher.matchesName(getName()); | ||
} | ||
|
||
interface Provider { | ||
|
||
Set<TypeReference> getTypeReferences(SourceFile sourceFile); | ||
|
||
boolean isAcceptable(SourceFile sourceFile); | ||
} | ||
|
||
interface Matcher { | ||
boolean matchesName(String name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.