Skip to content

Commit

Permalink
Preparing for upcoming release of NullAway (0.9.9)
Browse files Browse the repository at this point in the history
This PR updates Annotator to work with the upcoming release of NullAway 0.9.9.

Through PR621 and PR622 NullAway had the following changes:

Param Protection Test feature is removed from NullAway and the config file for fix serialization has changed.
NullAway will no longer serialize the actual @nullable annotation, it only suggests the name of the annotation (e.g. nullable) followed by a change in fix serialization config file.
All changes above has been addressed in this PR.
  • Loading branch information
nimakarimipour authored Jul 14, 2022
2 parents aea37b6 + fd81f74 commit 0aaa9e1
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 152 deletions.
8 changes: 5 additions & 3 deletions core/src/main/java/edu/ucr/cs/riple/core/Annotator.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private void preprocess() {
System.out.println("Making the first build...");
Utility.buildProject(config, true);
Set<Fix> uninitializedFields =
Utility.readFixesFromOutputDirectory(config, Fix.factory(null)).stream()
Utility.readFixesFromOutputDirectory(config, Fix.factory(config, null)).stream()
.filter(fix -> fix.reasons.contains("FIELD_NO_INIT") && fix.isOnField())
.collect(Collectors.toSet());
FieldInitializationAnalysis analysis =
Expand All @@ -94,7 +94,8 @@ private void explore() {
while (true) {
Utility.buildProject(config);
Set<Fix> remainingFixes =
Utility.readFixesFromOutputDirectory(config, Fix.factory(fieldDeclarationAnalysis));
Utility.readFixesFromOutputDirectory(
config, Fix.factory(config, fieldDeclarationAnalysis));
if (config.useCache) {
remainingFixes =
remainingFixes.stream()
Expand All @@ -104,7 +105,8 @@ private void explore() {
ImmutableSet<Fix> fixes = ImmutableSet.copyOf(remainingFixes);
Bank<Error> errorBank = new Bank<>(config.dir.resolve("errors.tsv"), Error::new);
Bank<Fix> fixBank =
new Bank<>(config.dir.resolve("fixes.tsv"), Fix.factory(fieldDeclarationAnalysis));
new Bank<>(
config.dir.resolve("fixes.tsv"), Fix.factory(config, fieldDeclarationAnalysis));
MethodInheritanceTree tree =
new MethodInheritanceTree(config.dir.resolve(Serializer.METHOD_INFO_FILE_NAME));
RegionTracker tracker = new CompoundTracker(config.dir, tree);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@

public interface Factory<T extends Hashable> {

T build(String[] infos);
T build(String[] info);
}
12 changes: 8 additions & 4 deletions core/src/main/java/edu/ucr/cs/riple/core/metadata/index/Fix.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package edu.ucr.cs.riple.core.metadata.index;

import com.google.common.collect.Sets;
import edu.ucr.cs.riple.core.Config;
import edu.ucr.cs.riple.core.metadata.field.FieldDeclarationAnalysis;
import edu.ucr.cs.riple.injector.Change;
import edu.ucr.cs.riple.injector.location.Location;
Expand All @@ -50,9 +51,9 @@ public Fix(Change change, String reason, String encClass, String endMethod) {
this.reasons = reason != null ? Sets.newHashSet(reason) : new HashSet<>();
}

public static Factory<Fix> factory(FieldDeclarationAnalysis analysis) {
return infos -> {
Location location = Location.createLocationFromArrayInfo(infos);
public static Factory<Fix> factory(Config config, FieldDeclarationAnalysis analysis) {
return info -> {
Location location = Location.createLocationFromArrayInfo(info);
if (analysis != null) {
location.ifField(
field -> {
Expand All @@ -61,7 +62,10 @@ public static Factory<Fix> factory(FieldDeclarationAnalysis analysis) {
field.variables.addAll(variables);
});
}
return new Fix(new Change(location, infos[7], true), infos[6], infos[8], infos[9]);
// TODO: Uncomment preconditions below once NullAway 0.9.9 is released.
// Preconditions.checkArgument(info[7].equals("nullable"), "unsupported annotation: " +
// info[7]);
return new Fix(new Change(location, config.nullableAnnot, true), info[6], info[8], info[9]);
};
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -38,41 +38,26 @@ public class FixSerializationConfig {

public final boolean fieldInitInfoEnabled;

public final boolean methodParamProtectionTestEnabled;

public final int paramTestIndex;

/** The directory where all files generated/read by Fix Serialization package resides. */
@Nullable public final String outputDirectory;

public final AnnotationConfig annotationConfig;

/** Default Constructor, all features are disabled with this config. */
public FixSerializationConfig() {
suggestEnabled = false;
suggestEnclosing = false;
fieldInitInfoEnabled = false;
methodParamProtectionTestEnabled = false;
paramTestIndex = Integer.MAX_VALUE;
annotationConfig = new AnnotationConfig();
outputDirectory = null;
}

public FixSerializationConfig(
boolean suggestEnabled,
boolean suggestEnclosing,
boolean fieldInitInfoEnabled,
boolean methodParamProtectionTestEnabled,
int paramTestIndex,
AnnotationConfig annotationConfig,
String outputDirectory) {
this.suggestEnabled = suggestEnabled;
this.suggestEnclosing = suggestEnclosing;
this.fieldInitInfoEnabled = fieldInitInfoEnabled;
this.methodParamProtectionTestEnabled = methodParamProtectionTestEnabled;
this.paramTestIndex = paramTestIndex;
this.outputDirectory = outputDirectory;
this.annotationConfig = annotationConfig;
}

/** Builder class for Serialization Config */
Expand All @@ -81,18 +66,12 @@ public static class Builder {
private boolean suggestEnabled;
private boolean suggestEnclosing;
private boolean fieldInitInfo;
private boolean methodParamProtectionTestEnabled;
private int paramIndex;
private String nullable;
private String nonnull;
@Nullable private String outputDir;

public Builder() {
suggestEnabled = false;
suggestEnclosing = false;
fieldInitInfo = false;
nullable = "javax.annotation.Nullable";
nonnull = "javax.annotation.Nonnull";
}

public Builder setSuggest(boolean value, boolean withEnclosing) {
Expand All @@ -101,12 +80,6 @@ public Builder setSuggest(boolean value, boolean withEnclosing) {
return this;
}

public Builder setAnnotations(String nullable, String nonnull) {
this.nullable = nullable;
this.nonnull = nonnull;
return this;
}

public Builder setFieldInitInfo(boolean enabled) {
this.fieldInitInfo = enabled;
return this;
Expand All @@ -117,12 +90,6 @@ public Builder setOutputDirectory(String outputDir) {
return this;
}

public Builder setParamProtectionTest(boolean value, int index) {
this.methodParamProtectionTestEnabled = value;
this.paramIndex = index;
return this;
}

/**
* Builds and writes the config with the state in builder at the given path as XML.
*
Expand All @@ -137,14 +104,7 @@ public FixSerializationConfig build() {
if (outputDir == null) {
throw new IllegalStateException("did not set mandatory output directory");
}
return new FixSerializationConfig(
suggestEnabled,
suggestEnclosing,
fieldInitInfo,
methodParamProtectionTestEnabled,
paramIndex,
new AnnotationConfig(nullable, nonnull),
outputDir);
return new FixSerializationConfig(suggestEnabled, suggestEnclosing, fieldInitInfo, outputDir);
}
}
}
19 changes: 0 additions & 19 deletions core/src/main/java/edu/ucr/cs/riple/core/util/Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
/** Utility class. */
public class Utility {

@SuppressWarnings("StatementWithEmptyBody")
public static void executeCommand(String command, Config config) {
try {
Process p = Runtime.getRuntime().exec(new String[] {"/bin/sh", "-c", command});
Expand Down Expand Up @@ -198,23 +197,6 @@ public static void writeNullAwayConfigInXMLFormat(FixSerializationConfig config,
fieldInitInfoEnabled.setAttribute("active", String.valueOf(config.fieldInitInfoEnabled));
rootElement.appendChild(fieldInitInfoEnabled);

// Method Parameter Protection Test
Element paramTestElement = doc.createElement("paramTest");
paramTestElement.setAttribute(
"active", String.valueOf(config.methodParamProtectionTestEnabled));
paramTestElement.setAttribute("index", String.valueOf(config.paramTestIndex));
rootElement.appendChild(paramTestElement);

// Annotations
Element annots = doc.createElement("annotation");
Element nonnull = doc.createElement("nonnull");
nonnull.setTextContent(config.annotationConfig.getNonNull().getFullName());
Element nullable = doc.createElement("nullable");
nullable.setTextContent(config.annotationConfig.getNullable().getFullName());
annots.appendChild(nullable);
annots.appendChild(nonnull);
rootElement.appendChild(annots);

// Output dir
Element outputDir = doc.createElement("path");
outputDir.setTextContent(config.outputDirectory);
Expand Down Expand Up @@ -288,7 +270,6 @@ public static void buildProject(Config config, boolean initSerializationEnabled)
FixSerializationConfig.Builder nullAwayConfig =
new FixSerializationConfig.Builder()
.setSuggest(true, true)
.setAnnotations(config.nullableAnnot, "UNKNOWN")
.setOutputDirectory(config.dir.toString())
.setFieldInitInfo(initSerializationEnabled);
nullAwayConfig.writeAsXML(config.nullAwayConfigPath.toString());
Expand Down

0 comments on commit 0aaa9e1

Please sign in to comment.