Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assume unfound classes to be in the same package as the current class #57

Merged
merged 6 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -526,13 +526,9 @@ public Visitable visit(MethodDeclaration node, Void arg) {
try {
nodeType.resolve();
} catch (UnsolvedSymbolException | UnsupportedOperationException e) {
if (classAndPackageMap.containsKey(nodeTypeSimpleForm)) {
UnsolvedClass syntheticType =
new UnsolvedClass(nodeTypeSimpleForm, classAndPackageMap.get(nodeTypeSimpleForm));
this.updateMissingClass(syntheticType);
} else {
throw new RuntimeException("Unexpected class: " + nodeTypeSimpleForm);
}
// if the class could not be found among import statements, we assume that the class must be
// in the same package as the current class.
this.updateMissingClass(createUnsolvedClass(nodeTypeSimpleForm));
}
}

Expand Down Expand Up @@ -677,12 +673,7 @@ public Visitable visit(Parameter parameter, Void p) {
} else {
// since it is unsolved, it could not be a primitive type
@ClassGetSimpleName String className = parameter.getType().asClassOrInterfaceType().getName().asString();
if (classAndPackageMap.containsKey(className)) {
UnsolvedClass newClass = new UnsolvedClass(className, classAndPackageMap.get(className));
updateMissingClass(newClass);
} else {
throw new RuntimeException("Unexpected class: " + className);
}
updateMissingClass(createUnsolvedClass(className));
LoiNguyenCS marked this conversation as resolved.
Show resolved Hide resolved
}
}
gotException = true;
Expand All @@ -706,14 +697,12 @@ public Visitable visit(ObjectCreationExpr newExpr, Void p) {
try {
List<String> argumentsCreation = getArgumentsFromObjectCreation(newExpr);
UnsolvedMethod creationMethod = new UnsolvedMethod("", type, argumentsCreation);
if (classAndPackageMap.containsKey(type)) {
UnsolvedClass newClass = new UnsolvedClass(type, classAndPackageMap.get(type));
newClass.addMethod(creationMethod);
this.updateMissingClass(newClass);
} else {
throw new RuntimeException("Unexpected class: " + type);
}

// we assume that an unsolved class not found among import statements should be in the same
// package as the current class
String packageName = classAndPackageMap.getOrDefault(type, currentPackage);
UnsolvedClass newClass = new UnsolvedClass(type, packageName);
newClass.addMethod(creationMethod);
LoiNguyenCS marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code should also call createUnsolvedClass (and then call addMethod on the result)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as we've added updateMissingClass to the updateUnsolvedClass method, I think we can't call updateUnsolvedClass here because we need to add the method to the UnsolvedClass instance before updating the list of missing classes.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to add the method to the UnsolvedClass instance before updating the list of missing classes

Why? The pointer is still valid, and I don't think there's any reason to believe that any computation will be done with the list of missing classes before the call to addMethod is executed (assuming a sequential Java semantics, but I'm okay with that assumption here given the way the JVM works).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I am wrong: updateMissingClass does more computation than I expected. I was incorrectly thinking that it just updates a data structure; it also does computation over the list of methods.

Can you modify updateUnsolvedClass to take a varargs array of methods and add them all to the class before it calls updateMissingClass?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Professor, I'm afraid that might make updateUnsolvedClass more complicated than it should be. The reason is that it will require an additional array as an argument whenever it is called, even if there is no methods added (such as in line 529 and 674). And even at line 704, while there is a method added, there is only one method added. So modifying updateUnsolvedClass to take an array of methods as an argument seems to be unnecessary.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will require an additional array as an argument whenever it is called

It will not, if you use the varargs feature as I suggested. It is designed for exactly this situation, and the resulting code should be simpler than what you currently have.

this.updateMissingClass(newClass);
} catch (Exception q) {
// can not solve the parameters for this object creation in this current run
}
Expand Down Expand Up @@ -807,10 +796,9 @@ public void updateUnsolvedClassWithMethod(
} else {
returnType = desiredReturnType;
}
if (!classAndPackageMap.containsKey(className)) {
throw new RuntimeException("Unexpected class: " + className);
}
UnsolvedClass missingClass = new UnsolvedClass(className, classAndPackageMap.get(className));
// a class not found among import statements should be in the same package as the current class
UnsolvedClass missingClass =
new UnsolvedClass(className, classAndPackageMap.getOrDefault(className, currentPackage));
UnsolvedMethod thisMethod = new UnsolvedMethod(methodName, returnType, listOfParameters);
missingClass.addMethod(thisMethod);
syntheticMethodAndClass.put(methodName, missingClass);
Expand Down Expand Up @@ -889,6 +877,20 @@ public void updateClassesFromJarSourcesForMethodCall(MethodCallExpr expr) {
this.updateMissingClass(missingClass);
}

/**
* Given the simple name of an unsolved class, this method will create an UnsolvedClass instance
* to represent that class.
*
* @param nameOfClass the name of an unsolved class
* @return an UnsolvedClass instance
*/
public UnsolvedClass createUnsolvedClass(@ClassGetSimpleName String nameOfClass) {
// if the name of the class is not present among import statements, we assume that this unsolved
// class is in the same directory as the current class
String packageName = classAndPackageMap.getOrDefault(nameOfClass, currentPackage);
return new UnsolvedClass(nameOfClass, packageName);
}

/**
* This method updates a synthetic file based on a solvable expression. The input expression is
* solvable because its data is in the jar files that Specimin taks as input.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.checkerframework.specimin;

import java.io.IOException;
import org.junit.Test;

/**
* This test checks that if there is an unsolved class not present among import statements, Specimin
* will assume that the class is in the same directory as the current class.
*/
public class UnsolvedClassInSamePackageTest {
@Test
public void runTest() throws IOException {
SpeciminTestExecutor.runTestWithoutJarPaths(
"unsolvedclassinsamepackage",
new String[] {"com/example/Simple.java"},
new String[] {"com.example.Simple#bar()"});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example;

public class Baz {

public Baz(java.lang.String parameter0) {
throw new Error();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example;

class Simple {

void bar() {
Baz obj = new Baz("hello");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example;

class Simple {
void bar() {
Baz obj = new Baz("hello");
}
}