Skip to content

Commit

Permalink
Merge pull request #34 from LoiNguyenCS/fix_errorProne
Browse files Browse the repository at this point in the history
Solve error prone warnings
  • Loading branch information
kelloggm authored Nov 2, 2023
2 parents 3a33207 + 45d87d2 commit 1e93372
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 70 deletions.
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
id 'signing'
id 'com.diffplug.spotless' version '6.18.0'
id 'org.checkerframework' version '0.6.34'
id("net.ltgt.errorprone") version "2.0.2"
id("net.ltgt.errorprone") version "3.1.0"
id 'com.adarshr.test-logger' version '3.1.0'
}

Expand Down Expand Up @@ -80,6 +80,10 @@ spotless {
}
}

compileJava {
options.compilerArgs += ['-Werror']
}

compileJava.dependsOn 'spotlessApply'
check.dependsOn requireJavadoc

Expand Down
27 changes: 16 additions & 11 deletions src/main/java/org/checkerframework/specimin/JavaTypeCorrect.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.checkerframework.specimin;

import com.google.common.base.Splitter;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.checkerframework.checker.signature.qual.ClassGetSimpleName;
Expand Down Expand Up @@ -78,15 +81,17 @@ public void runJavacAndUpdateTypes(String filePath) {
ProcessBuilder processBuilder = new ProcessBuilder(arguments);
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader reader =
new BufferedReader(
new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8));
String line;
while ((line = reader.readLine()) != null) {
if (line.contains("error: incompatible types")) {
updateTypeToChange(line);
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(e);
}
}

Expand All @@ -96,8 +101,8 @@ public void runJavacAndUpdateTypes(String filePath) {
* @param errorMessage the error message to be analyzed
*/
private void updateTypeToChange(String errorMessage) {
String[] splitErrorMessage = errorMessage.split("\\s+");
if (splitErrorMessage.length < 7) {
List<String> splitErrorMessage = Splitter.onPattern("\\s+").splitToList(errorMessage);
if (splitErrorMessage.size() < 7) {
throw new RuntimeException("Unexpected type error messages: " + errorMessage);
}
/* There are two possible forms of error messages here:
Expand All @@ -107,15 +112,15 @@ private void updateTypeToChange(String errorMessage) {
if (errorMessage.contains("cannot be converted to")) {
// since this is from javac, we know that these will be dot-separated identifiers.
@SuppressWarnings("signature")
@DotSeparatedIdentifiers String incorrectType = splitErrorMessage[4];
@DotSeparatedIdentifiers String incorrectType = splitErrorMessage.get(4);
@SuppressWarnings("signature")
@DotSeparatedIdentifiers String correctType = splitErrorMessage[splitErrorMessage.length - 1];
@DotSeparatedIdentifiers String correctType = splitErrorMessage.get(splitErrorMessage.size() - 1);
typeToChange.put(toSimpleName(incorrectType), toSimpleName(correctType));
} else {
@SuppressWarnings("signature")
@DotSeparatedIdentifiers String incorrectType = splitErrorMessage[5];
@DotSeparatedIdentifiers String incorrectType = splitErrorMessage.get(5);
@SuppressWarnings("signature")
@DotSeparatedIdentifiers String correctType = splitErrorMessage[splitErrorMessage.length - 1];
@DotSeparatedIdentifiers String correctType = splitErrorMessage.get(splitErrorMessage.size() - 1);
typeToChange.put(toSimpleName(incorrectType), toSimpleName(correctType));
}
}
Expand All @@ -133,10 +138,10 @@ private void updateTypeToChange(String errorMessage) {
// className will be a dot-separated identifier.
@SuppressWarnings("signature")
public static @ClassGetSimpleName String toSimpleName(@DotSeparatedIdentifiers String className) {
String[] classNameParts = className.split("[.]");
if (classNameParts.length < 2) {
List<String> classNameParts = Splitter.onPattern("[.]").splitToList(className);
if (classNameParts.size() < 2) {
return className;
}
return classNameParts[classNameParts.length - 1];
return classNameParts.get(classNameParts.size() - 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
Expand All @@ -33,6 +34,7 @@ public class SpeciminRunner {
* The main entry point for Specimin.
*
* @param args the arguments to Specimin
* @throws IOException if there is an exception
*/
public static void main(String... args) throws IOException {
OptionParser optionParser = new OptionParser();
Expand All @@ -56,7 +58,6 @@ public static void main(String... args) throws IOException {
optionParser.accepts("outputDirectory").withRequiredArg();

OptionSet options = optionParser.parse(args);
String output = options.valueOf(outputDirectoryOption);
performMinimization(
options.valueOf(rootOption),
options.valuesOf(targetFilesOption),
Expand All @@ -75,6 +76,7 @@ public static void main(String... args) throws IOException {
* @param jarPaths Paths to relevant JAR files.
* @param targetMethodNames A set of target method names to be preserved.
* @param outputDirectory The directory for the output.
* @throws IOException if there is an exception
*/
public static void performMinimization(
String root,
Expand Down Expand Up @@ -201,7 +203,8 @@ public static void performMinimization(
}
// Write the string representation of CompilationUnit to the file
try {
PrintWriter writer = new PrintWriter(targetOutputPath.toFile());
PrintWriter writer =
new PrintWriter(targetOutputPath.toFile(), StandardCharsets.UTF_8.name());
writer.print(target.getValue());
writer.close();
} catch (IOException e) {
Expand Down
25 changes: 17 additions & 8 deletions src/main/java/org/checkerframework/specimin/UnsolvedClass.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.checkerframework.specimin;

import com.google.common.base.Splitter;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.checkerframework.checker.signature.qual.ClassGetSimpleName;

Expand Down Expand Up @@ -96,8 +99,8 @@ public void addFields(String variableExpression) {
* Update the return type of a method. Note: this method is supposed to be used to update
* synthetic methods, where the return type of each method is distinct.
*
* @param currentReturnType
* @param desiredReturnType
* @param currentReturnType the current return type of this method
* @param desiredReturnType the new return type
*/
public void updateMethodByReturnType(
@ClassGetSimpleName String currentReturnType, @ClassGetSimpleName String desiredReturnType) {
Expand All @@ -115,20 +118,26 @@ public void updateMethodByReturnType(
* @param correctType the desired type
*/
public void updateFieldByType(String currentType, String correctType) {
for (String fieldExpression : classFields) {
String[] elements = fieldExpression.split(" ");
Iterator<String> iterator = classFields.iterator();
Set<String> newFields = new HashSet<>();
while (iterator.hasNext()) {
List<String> elements = Splitter.on(' ').splitToList(iterator.next());
// fieldExpression is guaranteed to have the form "TYPE FIELD_NAME". Since this field
// expression is from a synthetic class, there is no annotation involved, so TYPE has no
// space.
String fieldType = elements[0];
String fieldName = elements[1];
String fieldType = elements.get(0);
String fieldName = elements.get(1);
if (fieldType.equals(currentType)) {
classFields.remove(fieldExpression);
classFields.add(
iterator.remove();
newFields.add(
UnsolvedSymbolVisitor.setInitialValueForVariableDeclaration(
correctType, correctType + " " + fieldName));
}
}

for (String field : newFields) {
classFields.add(field);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class UnsolvedMethod {
*
* @param name the name of the method
* @param returnType the return type of the method
* @param parameterList the list of parameters for this method
*/
public UnsolvedMethod(
String name, @ClassGetSimpleName String returnType, List<String> parameterList) {
Expand All @@ -40,7 +41,7 @@ public UnsolvedMethod(
* Set the value of returnType. This method is used when javac tells us that UnsolvedSymbolVisitor
* get the return types wrong.
*
* @param returnType
* @param returnType the return type to bet set for this method
*/
public void setReturnType(@ClassGetSimpleName String returnType) {
this.returnType = returnType;
Expand Down Expand Up @@ -69,6 +70,7 @@ public String getName() {
*
* @return the content of the method with the body stubbed out
*/
@Override
public String toString() {
String arguments = "";
for (int i = 0; i < parameterList.size(); i++) {
Expand Down
Loading

0 comments on commit 1e93372

Please sign in to comment.