Skip to content

Commit

Permalink
some more cleaning and clarification
Browse files Browse the repository at this point in the history
  • Loading branch information
LoiNguyenCS committed Nov 19, 2023
1 parent e601238 commit e2188c2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 42 deletions.
11 changes: 6 additions & 5 deletions src/main/java/org/checkerframework/specimin/SpeciminRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ public static void performMinimization(
// correct the types of all related files before adding them to parsedTargetFiles
JavaTypeCorrect typeCorrecter = new JavaTypeCorrect(root, relatedClass);
typeCorrecter.correctTypesForAllFiles();
Map<@ClassGetSimpleName String, @ClassGetSimpleName String> typeToChange =
Map<@ClassGetSimpleName String, @ClassGetSimpleName String> typesToChange =
typeCorrecter.getTypeToChange();
addMissingClass.updateTypes(typeToChange);
addMissingClass.updateTypes(typesToChange);

for (String directory : relatedClass) {
// directories already in parsedTargetFiles are original files in the root directory, we are
Expand All @@ -191,17 +191,18 @@ public static void performMinimization(
// target key will have this form: "path/of/package/ClassName.java"
String classFullyQualfiedName = target.getKey().replace("/", ".");
if (!classFullyQualfiedName.endsWith(".java")) {
throw new RuntimeException("A Java file directory does not end with .java: " + classFullyQualfiedName);
throw new RuntimeException(
"A Java file directory does not end with .java: " + classFullyQualfiedName);
}
classFullyQualfiedName =
classFullyQualfiedName.substring(0, classFullyQualfiedName.length() - 5);
classFullyQualfiedName.substring(0, classFullyQualfiedName.length() - 5);
@SuppressWarnings("signature") // since it's the last element of a fully qualified path
@ClassGetSimpleName String simpleName =
classFullyQualfiedName.substring(classFullyQualfiedName.lastIndexOf(".") + 1);
// If this condition is true, this class is a synthetic class initially created to be a
// return type of some synthetic methods, but later javac has found the correct return type
// for that method.
if (typeToChange.containsKey(simpleName)) {
if (typesToChange.containsKey(simpleName)) {
continue;
}
if (!finder.getUsedClass().contains(classFullyQualfiedName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ public Visitable visit(Parameter para, Void p) {
if (insideTargetMethod) {
ResolvedType paraType = para.resolve().getType();
if (paraType.isReferenceType()) {
String paraTypeFullName = paraType.asReferenceType().getTypeDeclaration().get().getQualifiedName();
String paraTypeFullName =
paraType.asReferenceType().getTypeDeclaration().get().getQualifiedName();
usedClass.add(paraTypeFullName);
for (ResolvedType typeVariable : paraType.asReferenceType().typeParametersValues()) {
String typeVariableFullName = typeVariable.describe();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,31 +264,6 @@ public Visitable visit(PackageDeclaration node, Void arg) {
return super.visit(node, arg);
}

/*
@Override
public Node visit(ImportDeclaration decl, Void arg) {
@SuppressWarnings(
"signature") // since this is the content of an import statement, it will be a qualified
// class path
@FullyQualifiedName String declAsString = decl.getNameAsString();
// if there is no jar paths included, every imported class is unsolved, apart from those
// belonging to the Java language.
if (!classesFromJar.isEmpty() || declAsString.startsWith("java.")) {
return super.visit(decl, arg);
}
UnsolvedClass declaredClass = getSimpleSyntheticClassFromFullyQualifiedName(declAsString);
try {
createMissingClass(declaredClass);
missingClass.add(declaredClass);
} catch (RuntimeException e) {
// The class file already exists in the codebase
return super.visit(decl, arg);
}
return super.visit(decl, arg);
}
*/

@Override
public Visitable visit(ClassOrInterfaceDeclaration node, Void arg) {
SimpleName nodeName = node.getName();
Expand Down Expand Up @@ -542,7 +517,6 @@ public Visitable visit(MethodDeclaration node, Void arg) {
// since this method declaration is inside an anonymous class, its parent will be an
// ObjectCreationExpr
((ObjectCreationExpr) parentNode).resolve();
nodeType.resolve();
} catch (UnsolvedSymbolException | UnsupportedOperationException e) {
SimpleName classNodeSimpleName = ((ObjectCreationExpr) parentNode).getType().getName();
String nameOfClass = classNodeSimpleName.asString();
Expand Down Expand Up @@ -600,9 +574,12 @@ public Visitable visit(MethodCallExpr method, Void p) {

@Override
public Visitable visit(ClassOrInterfaceType typeExpr, Void p) {
// this line is to work around a bug of JavaParser. When a type is called as a class path, such
// as com.example.Dog dog, JavaParser will also consider com and com.example as type name. This
// bug happens even if the source file of class Dog is present in the codebase.
/**
* Workaround for a JavaParser bug: When a type is referenced as a class path, like
* com.example.Dog dog, JavaParser considers its package components (com and com.example) as
* types, too. This issue happens even when the source file of the Dog class is present in the
* codebase.
*/
if (!isCapital(typeExpr.getName().asString())) {
return super.visit(typeExpr, p);
}
Expand Down Expand Up @@ -1289,13 +1266,6 @@ public void createMissingClass(UnsolvedClass missedClass) {
this.rootDirectory + classDirectory + "/" + missedClass.getClassName() + ".java";
Path filePath = Paths.get(filePathStr);

// When class paths are not provide, this visitor will attempt to create synthetic classes for
// every class imported by the import statements. However, some of those classes might already
// exist in the input codebase.
if (Files.exists(filePath)) {
throw new RuntimeException("File already exists: " + filePath);
}

createdClass.add(filePath);
try {
Path parentPath = filePath.getParent();
Expand Down

0 comments on commit e2188c2

Please sign in to comment.