From ae85f9bf86252952ee8cb89d5c29cb4f1b524f05 Mon Sep 17 00:00:00 2001 From: nimakarimipour Date: Tue, 4 Apr 2023 10:57:27 -0700 Subject: [PATCH 1/2] add visitor architecture --- .../cs/riple/injector/location/Location.java | 11 +++ .../injector/location/LocationVisitor.java | 69 +++++++++++++++++++ .../cs/riple/injector/location/OnClass.java | 5 ++ .../cs/riple/injector/location/OnField.java | 5 ++ .../cs/riple/injector/location/OnMethod.java | 5 ++ .../riple/injector/location/OnParameter.java | 5 ++ 6 files changed, 100 insertions(+) create mode 100644 injector/src/main/java/edu/ucr/cs/riple/injector/location/LocationVisitor.java 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..50dcaeac9 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 + */ + 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..1ac715c93 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 + 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..0261f6ab2 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 + 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..85b03a78a 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 + 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..7279c6b0f 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 + R accept(LocationVisitor v, P p) { + return v.visitParameter(this, p); + } + @Override public int hashCode() { return Objects.hash(super.hashCode(), method, index); From ed7bda6541f69b02f78cb1a32aca241764d37e76 Mon Sep 17 00:00:00 2001 From: nimakarimipour Date: Tue, 4 Apr 2023 11:50:56 -0700 Subject: [PATCH 2/2] update visitibility to public --- .../main/java/edu/ucr/cs/riple/injector/location/Location.java | 2 +- .../main/java/edu/ucr/cs/riple/injector/location/OnClass.java | 2 +- .../main/java/edu/ucr/cs/riple/injector/location/OnField.java | 2 +- .../main/java/edu/ucr/cs/riple/injector/location/OnMethod.java | 2 +- .../java/edu/ucr/cs/riple/injector/location/OnParameter.java | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) 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 50dcaeac9..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 @@ -277,7 +277,7 @@ public boolean equals(Object o) { * @param p additional parameter to the visitor * @return a visitor-specified result */ - abstract R accept(LocationVisitor v, P p); + public abstract R accept(LocationVisitor v, P p); @Override public int hashCode() { 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 1ac715c93..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 @@ -82,7 +82,7 @@ protected void fillJsonInformation(JSONObject res) { } @Override - R accept(LocationVisitor v, P p) { + public R accept(LocationVisitor v, P p) { return v.visitClass(this, p); } 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 0261f6ab2..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 @@ -130,7 +130,7 @@ public boolean equals(Object o) { } @Override - R accept(LocationVisitor v, P p) { + public R accept(LocationVisitor v, P p) { return v.visitField(this, p); } 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 85b03a78a..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 @@ -118,7 +118,7 @@ public boolean equals(Object o) { } @Override - R accept(LocationVisitor v, P p) { + public R accept(LocationVisitor v, P p) { return v.visitMethod(this, p); } 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 7279c6b0f..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 @@ -126,7 +126,7 @@ public boolean equals(Object o) { } @Override - R accept(LocationVisitor v, P p) { + public R accept(LocationVisitor v, P p) { return v.visitParameter(this, p); }