-
Notifications
You must be signed in to change notification settings - Fork 13
Use visitor for applying changes with locations #171
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
Merged
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ae85f9b
add visitor architecture
nimakarimipour ed7bda6
update visitibility to public
nimakarimipour 2a5b987
initial commit
nimakarimipour 34848b0
Merge branch 'nimak/add-visitor-loc' into nimak/visitor-for-change
nimakarimipour ee5e7a4
use visitor for applying changes on the locations
nimakarimipour e30d077
add javadoc
nimakarimipour a018ff3
remvoe translate method
nimakarimipour 3d4cd7a
Merge branch 'master' into nimak/add-visitor-loc
nimakarimipour 70fdb8a
Merge branch 'nimak/add-visitor-loc' into nimak/visitor-for-change
nimakarimipour 15ff010
rename visit to computeModification
nimakarimipour 2e2dd6b
reword javadoc for ChangeVisitor
nimakarimipour 74e1bf7
Merge branch 'master' into nimak/visitor-for-change
nimakarimipour a2dcdd9
merged master into this
nimakarimipour 66edb7e
update javadoc
nimakarimipour 065bda0
merged master into this
nimakarimipour 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
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
186 changes: 186 additions & 0 deletions
186
injector/src/main/java/edu/ucr/cs/riple/injector/changes/ChangeVisitor.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,186 @@ | ||
| /* | ||
| * Copyright (c) 2023 University of California, Riverside. | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
|
|
||
| package edu.ucr.cs.riple.injector.changes; | ||
|
|
||
| import static edu.ucr.cs.riple.injector.location.OnClass.isAnonymousClassFlatName; | ||
|
|
||
| import com.github.javaparser.ast.CompilationUnit; | ||
| import com.github.javaparser.ast.Node; | ||
| import com.github.javaparser.ast.NodeList; | ||
| import com.github.javaparser.ast.body.BodyDeclaration; | ||
| import com.github.javaparser.ast.body.Parameter; | ||
| import com.github.javaparser.ast.body.VariableDeclarator; | ||
| import com.github.javaparser.utils.Pair; | ||
| import edu.ucr.cs.riple.injector.Helper; | ||
| import edu.ucr.cs.riple.injector.exceptions.TargetClassNotFound; | ||
| import edu.ucr.cs.riple.injector.location.LocationVisitor; | ||
| import edu.ucr.cs.riple.injector.location.OnClass; | ||
| import edu.ucr.cs.riple.injector.location.OnField; | ||
| import edu.ucr.cs.riple.injector.location.OnMethod; | ||
| import edu.ucr.cs.riple.injector.location.OnParameter; | ||
| import edu.ucr.cs.riple.injector.modifications.Modification; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * A visitor for computing the required {@link Modification} to a compilation unit on a specified | ||
| * location for the requested change. | ||
| */ | ||
| public class ChangeVisitor | ||
| implements LocationVisitor<Modification, Pair<NodeList<BodyDeclaration<?>>, Change>> { | ||
|
|
||
| /** Compilation unit which the changes will be applied. */ | ||
| private final CompilationUnit cu; | ||
|
|
||
| public ChangeVisitor(CompilationUnit cu) { | ||
| this.cu = cu; | ||
| } | ||
|
|
||
| @Override | ||
| @Nullable | ||
| public Modification visitMethod( | ||
| OnMethod onMethod, Pair<NodeList<BodyDeclaration<?>>, Change> pair) { | ||
| final AtomicReference<Modification> ans = new AtomicReference<>(); | ||
| final NodeList<BodyDeclaration<?>> members = pair.a; | ||
| final Change change = pair.b; | ||
| members.forEach( | ||
| bodyDeclaration -> | ||
| bodyDeclaration.ifCallableDeclaration( | ||
| callableDeclaration -> { | ||
| if (ans.get() != null) { | ||
| // already found the member. | ||
| return; | ||
| } | ||
| if (onMethod.matchesCallableDeclaration(callableDeclaration)) { | ||
| ans.set(change.visit(callableDeclaration)); | ||
| } | ||
| })); | ||
| if (ans.get() == null) { | ||
| members.forEach( | ||
| bodyDeclaration -> | ||
| bodyDeclaration.ifAnnotationMemberDeclaration( | ||
| annotationMemberDeclaration -> { | ||
| if (annotationMemberDeclaration | ||
| .getNameAsString() | ||
| .equals(Helper.extractCallableName(onMethod.method))) { | ||
| ans.set(change.visit(annotationMemberDeclaration)); | ||
| } | ||
| })); | ||
| } | ||
| return ans.get(); | ||
| } | ||
|
|
||
| @Override | ||
| @Nullable | ||
| public Modification visitField(OnField onField, Pair<NodeList<BodyDeclaration<?>>, Change> pair) { | ||
| final AtomicReference<Modification> ans = new AtomicReference<>(); | ||
| final NodeList<BodyDeclaration<?>> members = pair.a; | ||
| final Change change = pair.b; | ||
| members.forEach( | ||
| bodyDeclaration -> | ||
| bodyDeclaration.ifFieldDeclaration( | ||
| fieldDeclaration -> { | ||
| if (ans.get() != null) { | ||
| // already found the member. | ||
| return; | ||
| } | ||
| NodeList<VariableDeclarator> vars = | ||
| fieldDeclaration.asFieldDeclaration().getVariables(); | ||
| for (VariableDeclarator v : vars) { | ||
| if (onField.variables.contains(v.getName().toString())) { | ||
| ans.set(change.visit(fieldDeclaration)); | ||
| break; | ||
| } | ||
| } | ||
| })); | ||
| return ans.get(); | ||
| } | ||
|
|
||
| @Override | ||
| @Nullable | ||
| public Modification visitParameter( | ||
| OnParameter onParameter, Pair<NodeList<BodyDeclaration<?>>, Change> pair) { | ||
| final AtomicReference<Modification> ans = new AtomicReference<>(); | ||
| final NodeList<BodyDeclaration<?>> members = pair.a; | ||
| final Change change = pair.b; | ||
| members.forEach( | ||
| bodyDeclaration -> | ||
| bodyDeclaration.ifCallableDeclaration( | ||
| callableDeclaration -> { | ||
| if (ans.get() != null) { | ||
| // already found the member. | ||
| return; | ||
| } | ||
| if (onParameter.matchesCallableDeclaration(callableDeclaration)) { | ||
| NodeList<?> params = callableDeclaration.getParameters(); | ||
| if (onParameter.index < params.size()) { | ||
| if (params.get(onParameter.index) != null) { | ||
| Node param = params.get(onParameter.index); | ||
| if (param instanceof Parameter) { | ||
| ans.set(change.visit((Parameter) param)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| })); | ||
| return ans.get(); | ||
| } | ||
|
|
||
| @Override | ||
| @Nullable | ||
| public Modification visitClass(OnClass onClass, Pair<NodeList<BodyDeclaration<?>>, Change> pair) { | ||
| final NodeList<BodyDeclaration<?>> members = pair.a; | ||
| final Change change = pair.b; | ||
| if (isAnonymousClassFlatName(change.location.clazz)) { | ||
| return null; | ||
| } | ||
| // Get the enclosing class of the members | ||
| Optional<Node> optionalClass = members.getParentNode(); | ||
| if (optionalClass.isEmpty() || !(optionalClass.get() instanceof BodyDeclaration<?>)) { | ||
| return null; | ||
| } | ||
| return change.visit(((BodyDeclaration<?>) optionalClass.get())); | ||
| } | ||
|
|
||
| /** | ||
| * Applies the change to the compilation unit. | ||
| * | ||
| * @param change the change to apply. | ||
| * @return the modification that should be applied. | ||
| */ | ||
| @Nullable | ||
| public Modification computeModification(Change change) { | ||
| NodeList<BodyDeclaration<?>> members; | ||
| try { | ||
| members = Helper.getTypeDeclarationMembersByFlatName(cu, change.location.clazz); | ||
| if (members == null) { | ||
| return null; | ||
| } | ||
| return change.location.accept(this, new Pair<>(members, change)); | ||
| } catch (TargetClassNotFound notFound) { | ||
| System.err.println(notFound.getMessage()); | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't actually apply any change, right? It just computes the change (
Modification) to be applied?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes exactly. I reworded the java doc to mention this, please let me know if I should update it. 2e2dd6b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hrm, I don't see the javadoc changes here; maybe the commit got lost somehow?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for that, the change was on the javadoc for the class. Update the method javadoc as well 66edb7e