-
Notifications
You must be signed in to change notification settings - Fork 8
Extract separate classes from RefasterTemplateProcessor #159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
931516f
Make methods static where possible
timtebeek 08c73ea
Move methods into RecipeWriter
timtebeek c65b1e7
Extract a method to write imports
timtebeek 16e5683
Remove unused method for boxed primitives
timtebeek 8b903de
Pass around `JavacProcessingEnvironment` instead of `Context`
timtebeek 7666bf7
Use factory method for RuleDescriptor
timtebeek fb36e92
Extract separate classes
timtebeek e27753b
Polish
timtebeek c2b7b3d
Match formatting in escape methods
timtebeek 51f21c4
Extract a dedicated method to escape template code
timtebeek b348798
Simplify assignment to `anySearchRecipe`
timtebeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
659 changes: 659 additions & 0 deletions
659
src/main/java/org/openrewrite/java/template/processor/RecipeWriter.java
Large diffs are not rendered by default.
Oops, something went wrong.
1,028 changes: 15 additions & 1,013 deletions
1,028
src/main/java/org/openrewrite/java/template/processor/RefasterTemplateProcessor.java
Large diffs are not rendered by default.
Oops, something went wrong.
124 changes: 124 additions & 0 deletions
124
src/main/java/org/openrewrite/java/template/processor/RuleDescriptor.java
This file contains hidden or 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,124 @@ | ||
| /* | ||
| * Copyright 2025 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.java.template.processor; | ||
|
|
||
| import com.sun.tools.javac.processing.JavacProcessingEnvironment; | ||
| import com.sun.tools.javac.tree.JCTree; | ||
| import com.sun.tools.javac.util.Name; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| import javax.tools.Diagnostic; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| import static org.openrewrite.java.template.processor.RefasterTemplateProcessor.AFTER_TEMPLATE; | ||
| import static org.openrewrite.java.template.processor.RefasterTemplateProcessor.BEFORE_TEMPLATE; | ||
|
|
||
| class RuleDescriptor { | ||
|
|
||
| public final JCTree.JCClassDecl classDecl; | ||
| public final List<TemplateDescriptor> beforeTemplates; | ||
| public final @Nullable TemplateDescriptor afterTemplate; | ||
|
|
||
| private RuleDescriptor( | ||
| JCTree.JCClassDecl classDecl, | ||
| List<TemplateDescriptor> beforeTemplates, | ||
| @Nullable TemplateDescriptor afterTemplate) { | ||
| this.classDecl = classDecl; | ||
| this.beforeTemplates = beforeTemplates; | ||
| this.afterTemplate = afterTemplate; | ||
| } | ||
|
|
||
| public static @Nullable RuleDescriptor create( | ||
| JavacProcessingEnvironment processingEnv, | ||
| JCTree.JCCompilationUnit cu, JCTree.JCClassDecl classDecl) { | ||
| List<TemplateDescriptor> beforeTemplates = new ArrayList<>(); | ||
| TemplateDescriptor afterTemplate = null; | ||
| for (JCTree member : classDecl.getMembers()) { | ||
| if (member instanceof JCTree.JCMethodDecl) { | ||
| JCTree.JCMethodDecl method = (JCTree.JCMethodDecl) member; | ||
| List<JCTree.JCAnnotation> annotations = RefasterTemplateProcessor.getTemplateAnnotations(method, BEFORE_TEMPLATE::equals); | ||
| if (!annotations.isEmpty()) { | ||
| beforeTemplates.add(new TemplateDescriptor(processingEnv, cu, classDecl, method)); | ||
| } | ||
| annotations = RefasterTemplateProcessor.getTemplateAnnotations(method, AFTER_TEMPLATE::equals); | ||
| if (!annotations.isEmpty()) { | ||
| afterTemplate = new TemplateDescriptor(processingEnv, cu, classDecl, method); | ||
| } | ||
| } | ||
| } | ||
| return new RuleDescriptor(classDecl, beforeTemplates, afterTemplate) | ||
| .validate(processingEnv, classDecl); | ||
| } | ||
|
|
||
| private @Nullable RuleDescriptor validate(JavacProcessingEnvironment processingEnv, JCTree.JCClassDecl classDecl) { | ||
| if (beforeTemplates.isEmpty()) { | ||
| return null; | ||
| } | ||
|
|
||
| for (JCTree member : classDecl.getMembers()) { | ||
| if (member instanceof JCTree.JCMethodDecl && beforeTemplates.stream().noneMatch(t -> t.method == member) && | ||
| (afterTemplate == null || member != afterTemplate.method)) { | ||
| for (JCTree.JCAnnotation annotation : RefasterTemplateProcessor.getTemplateAnnotations(((JCTree.JCMethodDecl) member), RefasterTemplateProcessor.UNSUPPORTED_ANNOTATIONS::contains)) { | ||
| RefasterTemplateProcessor.printNoteOnce(processingEnv, "@" + annotation.annotationType + " is currently not supported", classDecl.sym); | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // resolve so that we can inspect the template body | ||
| boolean valid = resolve(processingEnv); | ||
| if (valid) { | ||
| for (TemplateDescriptor template : beforeTemplates) { | ||
| valid &= template.validate(); | ||
| } | ||
| if (afterTemplate != null) { | ||
| valid &= afterTemplate.validate(); | ||
| } | ||
| } | ||
|
|
||
| if (valid && afterTemplate != null) { | ||
| Set<Name> requiredParameters = RefasterTemplateProcessor.findParameterOrder(afterTemplate.method, 0).keySet(); | ||
| for (TemplateDescriptor beforeTemplate : beforeTemplates) { | ||
| for (int i = 0; i < beforeTemplate.getArity(); i++) { | ||
| Set<Name> providedParameters = RefasterTemplateProcessor.findParameterOrder(beforeTemplate.method, i).keySet(); | ||
| if (!providedParameters.containsAll(requiredParameters)) { | ||
| RefasterTemplateProcessor.printNoteOnce(processingEnv, "@AfterTemplate defines arguments that are not present in all @BeforeTemplate methods", classDecl.sym); | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return valid ? this : null; | ||
| } | ||
|
|
||
| private boolean resolve(JavacProcessingEnvironment processingEnv) { | ||
| boolean valid = true; | ||
| try { | ||
| for (TemplateDescriptor beforeTemplate : beforeTemplates) { | ||
| valid &= beforeTemplate.resolve(); | ||
| } | ||
| if (afterTemplate != null) { | ||
| valid &= afterTemplate.resolve(); | ||
| } | ||
| } catch (Throwable t) { | ||
| processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "Had trouble type attributing the template."); | ||
| valid = false; | ||
| } | ||
| return valid; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.