-
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 initial support for java references for yaml (#4698)
* Add initial support for java references for yaml scalars * Add incubation annotation * Add extra test * Add rename support in YamlReference * Add NullMarked Package info * Restrict to application files and rename provider * Fix paths in tests * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Knut Wannheden <[email protected]> * Minimize diff between Yaml and Properties type references --------- Co-authored-by: Tim te Beek <[email protected]> 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
4e723d8
commit 104f705
Showing
9 changed files
with
329 additions
and
35 deletions.
There are no files selected for viewing
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
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
105 changes: 105 additions & 0 deletions
105
rewrite-yaml/src/main/java/org/openrewrite/yaml/trait/YamlReference.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,105 @@ | ||
/* | ||
* 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.yaml.trait; | ||
|
||
import lombok.Value; | ||
import org.jspecify.annotations.Nullable; | ||
import org.openrewrite.Cursor; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.SourceFile; | ||
import org.openrewrite.Tree; | ||
import org.openrewrite.trait.Reference; | ||
import org.openrewrite.trait.SimpleTraitMatcher; | ||
import org.openrewrite.yaml.tree.Yaml; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
import java.util.regex.Pattern; | ||
|
||
@Value | ||
public class YamlReference implements Reference { | ||
Cursor cursor; | ||
Kind kind; | ||
|
||
@Override | ||
public Kind getKind() { | ||
return kind; | ||
} | ||
|
||
@Override | ||
public String getValue() { | ||
if (getTree() instanceof Yaml.Scalar) { | ||
return ((Yaml.Scalar) getTree()).getValue(); | ||
} | ||
throw new IllegalArgumentException("getTree() must be an Yaml.Scalar: " + getTree().getClass()); | ||
} | ||
|
||
@Override | ||
public boolean supportsRename() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Tree rename(Renamer renamer, Cursor cursor, ExecutionContext ctx) { | ||
Tree tree = cursor.getValue(); | ||
if (tree instanceof Yaml.Scalar) { | ||
return ((Yaml.Scalar) tree).withValue(renamer.rename(this)); | ||
} | ||
throw new IllegalArgumentException("cursor.getValue() must be an Yaml.Scalar but is: " + tree.getClass()); | ||
} | ||
|
||
private static class Matcher extends SimpleTraitMatcher<YamlReference> { | ||
private static final Predicate<String> javaFullyQualifiedTypePattern = Pattern.compile( | ||
"\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*\\.\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*(?:\\.\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*)*") | ||
.asPredicate(); | ||
|
||
@Override | ||
protected @Nullable YamlReference test(Cursor cursor) { | ||
Object value = cursor.getValue(); | ||
if (value instanceof Yaml.Scalar && | ||
javaFullyQualifiedTypePattern.test(((Yaml.Scalar) value).getValue())) { | ||
return new YamlReference(cursor, determineKind(((Yaml.Scalar) value).getValue())); | ||
} | ||
return null; | ||
} | ||
|
||
private Kind determineKind(String value) { | ||
return Character.isUpperCase(value.charAt(value.lastIndexOf('.') + 1)) ? Kind.TYPE : Kind.PACKAGE; | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class Provider implements Reference.Provider { | ||
|
||
private static final Predicate<String> applicationPropertiesMatcher = Pattern.compile("^application(-\\w+)?\\.(yaml|yml)$").asPredicate(); | ||
|
||
@Override | ||
public boolean isAcceptable(SourceFile sourceFile) { | ||
return sourceFile instanceof Yaml.Documents && applicationPropertiesMatcher.test(sourceFile.getSourcePath().getFileName().toString()); | ||
} | ||
|
||
@Override | ||
public Set<Reference> getReferences(SourceFile sourceFile) { | ||
Set<Reference> references = new HashSet<>(); | ||
new Matcher().asVisitor(reference -> { | ||
references.add(reference); | ||
return reference.getTree(); | ||
}).visit(sourceFile, 0); | ||
return references; | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
rewrite-yaml/src/main/java/org/openrewrite/yaml/trait/package-info.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,21 @@ | ||
/* | ||
* 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. | ||
*/ | ||
@NullMarked | ||
@NonNullFields | ||
package org.openrewrite.yaml.trait; | ||
|
||
import org.jspecify.annotations.NullMarked; | ||
import org.openrewrite.internal.lang.NonNullFields; |
Oops, something went wrong.