Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
nimakarimipour committed Oct 3, 2024
1 parent ee3e218 commit bf4f438
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 47 deletions.
20 changes: 6 additions & 14 deletions annotator-core/src/main/java/edu/ucr/cs/riple/core/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Container class to store information regarding effectiveness of a fix, its associated fix tree
Expand Down Expand Up @@ -183,28 +181,22 @@ public boolean testEquals(Config config, Report found) {
this.tree.add(this.root);
found.tree.add(found.root);
Set<Location> thisTree =
this.tree.stream()
.flatMap((Function<Fix, Stream<Location>>) fix -> fix.toLocations().stream())
.collect(Collectors.toSet());
this.tree.stream().flatMap(fix -> fix.toLocations().stream()).collect(Collectors.toSet());

Set<Location> otherTree =
found.tree.stream()
.flatMap((Function<Fix, Stream<Location>>) fix -> fix.toLocations().stream())
.collect(Collectors.toSet());
found.tree.stream().flatMap(fix -> fix.toLocations().stream()).collect(Collectors.toSet());
if (!thisTree.equals(otherTree)) {
return false;
}
Set<Location> thisTriggered =
this.triggeredErrors.stream()
.map(Error::getFix)
.filter(Objects::nonNull)
.flatMap((Function<Fix, Stream<Location>>) fix -> fix.toLocations().stream())
.flatMap(error -> error.getFixes().stream())
.flatMap(fix -> fix.toLocations().stream())
.collect(Collectors.toSet());
Set<Location> otherTriggered =
found.triggeredErrors.stream()
.map(Error::getFix)
.filter(Objects::nonNull)
.flatMap((Function<Fix, Stream<Location>>) fix -> fix.toLocations().stream())
.flatMap(error -> error.getFixes().stream())
.flatMap(fix -> fix.toLocations().stream())
.collect(Collectors.toSet());
return otherTriggered.equals(thisTriggered);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,7 @@ private NullAwayError createError(
annot ->
!module.getNonnullStore().hasExplicitNonnullAnnotation(annot.getLocation()))
.collect(ImmutableSet.toImmutableSet());
Fix fix =
cleanedAnnotations.isEmpty()
? null
: new Fix(cleanedAnnotations, ImmutableSet.of(errorType), true);
return new NullAwayError(errorType, errorMessage, region, offset, fix);
return new NullAwayError(errorType, errorMessage, region, offset, cleanedAnnotations);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
import edu.ucr.cs.riple.core.registries.index.Error;
import edu.ucr.cs.riple.core.registries.index.Fix;
import edu.ucr.cs.riple.core.registries.region.Region;
import edu.ucr.cs.riple.injector.changes.AddAnnotation;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/** Represents an error reported by {@link NullAway}. */
public class NullAwayError extends Error {
Expand All @@ -37,8 +40,20 @@ public class NullAwayError extends Error {
/** Error type for field initialization errors from NullAway in {@code String}. */
public static final String FIELD_INITIALIZER_ERROR = "FIELD_NO_INIT";

public NullAwayError(String messageType, String message, Region region, int offset, Fix fix) {
super(messageType, message, region, offset, fix);
public NullAwayError(
String messageType,
String message,
Region region,
int offset,
Set<AddAnnotation> annotations) {
super(messageType, message, region, offset, annotations);
}

@Override
protected Set<Fix> computeFixesFromAnnotations(Set<AddAnnotation> annotations) {
return annotations.stream()
.map(annot -> new Fix(annot, messageType, true))
.collect(Collectors.toSet());
}

@Override
Expand All @@ -61,7 +76,7 @@ public boolean equals(Object o) {
// message and should not be treated as a separate error.
return true;
}
return message.equals(error.message) && fix.equals(error.fix) && offset == error.offset;
return message.equals(error.message) && fixes.equals(error.fixes) && offset == error.offset;
}

@Override
Expand All @@ -71,7 +86,7 @@ public int hashCode() {
// to make sure equal objects will produce the same hashcode.
messageType.equals(METHOD_INITIALIZER_ERROR) ? METHOD_INITIALIZER_ERROR : message,
region,
fix,
fixes,
offset);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void setOrigins(ErrorStore errorStore) {
this.origins =
ImmutableSet.copyOf(
errorStore.getRegionsForElements(
error -> error.getFix() != null && error.getFix().equals(root)));
error -> error.getFixes() != null && error.getFixes().equals(root)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@
import com.google.common.collect.ImmutableSet;
import edu.ucr.cs.riple.core.Context;
import edu.ucr.cs.riple.core.registries.region.Region;
import edu.ucr.cs.riple.injector.changes.AddAnnotation;
import edu.ucr.cs.riple.injector.location.Location;
import edu.ucr.cs.riple.injector.location.OnParameter;
import java.util.Collection;
import java.util.Objects;
import javax.annotation.Nullable;
import java.util.Set;

/** Represents an error reported by NullAway. */
@SuppressWarnings("JavaLangClash")
Expand All @@ -41,31 +42,43 @@ public abstract class Error {
public final String messageType;
/** Error message. */
public final String message;
/** Annotations */
public final Set<AddAnnotation> annotations;
/** The fixes which can resolve this error (possibly null). */
@Nullable protected final Fix fix;
protected final Set<Fix> fixes;
/** Offset of program point in original version where error is reported. */
protected final int offset;
/** Containing region. */
protected final Region region;

/** Error type for method initialization errors from NullAway in {@code String}. */
public Error(String messageType, String message, Region region, int offset, @Nullable Fix fix) {
public Error(
String messageType,
String message,
Region region,
int offset,
Set<AddAnnotation> annotations) {
this.region = region;
this.messageType = messageType;
this.message = message;
this.offset = offset;
this.fix = fix;
this.annotations = annotations;
this.fixes = computeFixesFromAnnotations(annotations);
}

protected abstract Set<Fix> computeFixesFromAnnotations(Set<AddAnnotation> annotations);

/**
* Checks if error is resolvable.
*
* @return true if error is resolvable and false otherwise.
*/
public boolean hasFix() {
return this.fix != null;
return this.fixes != null;
}

public Fix getFix() {
return this.fix;
public Set<Fix> getFixes() {
return this.fixes;
}

/**
Expand All @@ -74,7 +87,7 @@ public Fix getFix() {
* @return true if error is resolvable with only one annotation and false otherwise.
*/
public boolean isSingleAnnotationFix() {
return this.fix != null && fix.changes.size() == 1;
return !fixes.isEmpty() && fixes.iterator().next().changes.size() == 1;
}

/**
Expand All @@ -83,10 +96,9 @@ public boolean isSingleAnnotationFix() {
* @return Location of the fix resolving this error.
*/
public Location toResolvingLocation() {
Preconditions.checkArgument(fix != null && fix.changes.size() == 1);
Preconditions.checkArgument(fix.toLocations().size() == 1);
Preconditions.checkArgument(!fixes.isEmpty() && fixes.iterator().next().changes.size() == 1);
// no get() method, have to use iterator.
return fix.toLocations().iterator().next();
return fixes.iterator().next().changes.iterator().next().getLocation();
}

/**
Expand Down Expand Up @@ -139,7 +151,7 @@ public boolean equals(Object o) {
return messageType.equals(other.messageType)
&& region.equals(other.region)
&& message.equals(other.message)
&& fix.equals(other.fix)
&& fixes.equals(other.fixes)
&& offset == other.offset;
}

Expand All @@ -151,14 +163,14 @@ public boolean equals(Object o) {
* @return true, if error is resolvable via fixes on target module.
*/
public boolean isFixableOnTarget(Context context) {
return fix != null
&& this.fix.changes.stream()
.allMatch(change -> context.targetModuleInfo.declaredInModule(change.getLocation()));
return this.fixes.stream()
.flatMap(fix -> fix.changes.stream())
.allMatch(change -> context.targetModuleInfo.declaredInModule(change.getLocation()));
}

@Override
public int hashCode() {
return Objects.hash(messageType, message, region, fix, offset);
return Objects.hash(messageType, message, region, fixes, offset);
}

@Override
Expand All @@ -182,10 +194,7 @@ public String toString() {
*/
public static <T extends Error> ImmutableSet<Fix> getResolvingFixesOfErrors(
Collection<T> errors) {
return errors.stream()
.filter(Error::hasFix)
.map(Error::getFix)
.collect(ImmutableSet.toImmutableSet());
return errors.stream().flatMap(t -> t.fixes.stream()).collect(ImmutableSet.toImmutableSet());
}

/**
Expand All @@ -195,9 +204,9 @@ public static <T extends Error> ImmutableSet<Fix> getResolvingFixesOfErrors(
* @return true, if this error is resolvable.
*/
public boolean isResolvableWith(Collection<Fix> fixes) {
if (fix == null) {
if (this.fixes.isEmpty()) {
return false;
}
return fixes.contains(this.fix);
return fixes.containsAll(this.fixes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@

package edu.ucr.cs.riple.core.tools;

import com.google.common.collect.ImmutableSet;
import edu.ucr.cs.riple.core.registries.index.Error;
import edu.ucr.cs.riple.core.registries.index.Fix;
import edu.ucr.cs.riple.core.registries.region.Region;
import edu.ucr.cs.riple.injector.changes.AddAnnotation;
import edu.ucr.cs.riple.injector.location.Location;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Wrapper class for {@link Error} used to create dummy errors (with default values) as part of
Expand All @@ -36,6 +41,13 @@
public class TError extends Error {

public TError(Location location) {
super("null", "null", new Region("null", "null"), 0, new TFix(location));
super("null", "null", new Region("null", "null"), 0, Set.of(new DefaultAnnotation(location)));
}

@Override
protected Set<Fix> computeFixesFromAnnotations(Set<AddAnnotation> annotations) {
return annotations.stream()
.map(addAnnotation -> new Fix(addAnnotation, ImmutableSet.of(messageType), true))
.collect(Collectors.toSet());
}
}

0 comments on commit bf4f438

Please sign in to comment.