diff --git a/injector/src/main/java/edu/ucr/cs/riple/injector/location/Location.java b/injector/src/main/java/edu/ucr/cs/riple/injector/location/Location.java index d449cfd7c..f19fd9ed7 100644 --- a/injector/src/main/java/edu/ucr/cs/riple/injector/location/Location.java +++ b/injector/src/main/java/edu/ucr/cs/riple/injector/location/Location.java @@ -268,6 +268,17 @@ public boolean equals(Object o) { return type == other.type && clazz.equals(other.clazz); } + /** + * Applies a visitor to this location. + * + * @param the return type of the visitor's methods + * @param

the type of the additional parameter to the visitor's methods + * @param v the visitor operating on this type + * @param p additional parameter to the visitor + * @return a visitor-specified result + */ + public abstract R accept(LocationVisitor v, P p); + @Override public int hashCode() { return Objects.hash(type, clazz); diff --git a/injector/src/main/java/edu/ucr/cs/riple/injector/location/LocationVisitor.java b/injector/src/main/java/edu/ucr/cs/riple/injector/location/LocationVisitor.java new file mode 100644 index 000000000..83f82332d --- /dev/null +++ b/injector/src/main/java/edu/ucr/cs/riple/injector/location/LocationVisitor.java @@ -0,0 +1,69 @@ +/* + * MIT License + * + * Copyright (c) 2023 Nima Karimipour + * + * 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.location; + +/** + * A visitor of types, in the style of the visitor design pattern. When a visitor is passed to a + * location's {@link Location#accept accept} method, the visitXyz method + * applicable to that location is invoked. + */ +public interface LocationVisitor { + + /** + * Visits a location for a method. + * + * @param onMethod the location for a method + * @param p a visitor-specified parameter + * @return a visitor-specified result + */ + R visitMethod(OnMethod onMethod, P p); + + /** + * Visits a location for a field. + * + * @param onField the location for a field + * @param p a visitor-specified parameter + * @return a visitor-specified result + */ + R visitField(OnField onField, P p); + + /** + * Visits a location for a parameter. + * + * @param onParameter the location for a parameter + * @param p a visitor-specified parameter + * @return a visitor-specified result + */ + R visitParameter(OnParameter onParameter, P p); + + /** + * Visits a location for a class. + * + * @param onClass the location for a class + * @param p a visitor-specified parameter + * @return a visitor-specified result + */ + R visitClass(OnClass onClass, P p); +} diff --git a/injector/src/main/java/edu/ucr/cs/riple/injector/location/OnClass.java b/injector/src/main/java/edu/ucr/cs/riple/injector/location/OnClass.java index b9270eb39..86af0b459 100644 --- a/injector/src/main/java/edu/ucr/cs/riple/injector/location/OnClass.java +++ b/injector/src/main/java/edu/ucr/cs/riple/injector/location/OnClass.java @@ -81,6 +81,11 @@ protected void fillJsonInformation(JSONObject res) { // no op } + @Override + public R accept(LocationVisitor v, P p) { + return v.visitClass(this, p); + } + @Override public String toString() { return "OnClass{" + "type=" + type + ", clazz='" + clazz + '\'' + ", path=" + path + '}'; diff --git a/injector/src/main/java/edu/ucr/cs/riple/injector/location/OnField.java b/injector/src/main/java/edu/ucr/cs/riple/injector/location/OnField.java index 6381e2a9c..e4de051a0 100644 --- a/injector/src/main/java/edu/ucr/cs/riple/injector/location/OnField.java +++ b/injector/src/main/java/edu/ucr/cs/riple/injector/location/OnField.java @@ -129,6 +129,11 @@ public boolean equals(Object o) { return super.equals(other) && !Collections.disjoint(variables, other.variables); } + @Override + public R accept(LocationVisitor v, P p) { + return v.visitField(this, p); + } + @Override public int hashCode() { return Objects.hash(super.hashCode(), variables); diff --git a/injector/src/main/java/edu/ucr/cs/riple/injector/location/OnMethod.java b/injector/src/main/java/edu/ucr/cs/riple/injector/location/OnMethod.java index 499322998..c57287d4b 100644 --- a/injector/src/main/java/edu/ucr/cs/riple/injector/location/OnMethod.java +++ b/injector/src/main/java/edu/ucr/cs/riple/injector/location/OnMethod.java @@ -117,6 +117,11 @@ public boolean equals(Object o) { return super.equals(other) && method.equals(other.method); } + @Override + public R accept(LocationVisitor v, P p) { + return v.visitMethod(this, p); + } + @Override public int hashCode() { return Objects.hash(super.hashCode(), method); diff --git a/injector/src/main/java/edu/ucr/cs/riple/injector/location/OnParameter.java b/injector/src/main/java/edu/ucr/cs/riple/injector/location/OnParameter.java index db5763a30..b1aaad7c1 100644 --- a/injector/src/main/java/edu/ucr/cs/riple/injector/location/OnParameter.java +++ b/injector/src/main/java/edu/ucr/cs/riple/injector/location/OnParameter.java @@ -125,6 +125,11 @@ public boolean equals(Object o) { return super.equals(other) && method.equals(other.method) && index == other.index; } + @Override + public R accept(LocationVisitor v, P p) { + return v.visitParameter(this, p); + } + @Override public int hashCode() { return Objects.hash(super.hashCode(), method, index);