Skip to content

Commit

Permalink
Merge pull request #57 from LoiNguyenCS/remove-exception
Browse files Browse the repository at this point in the history
assume unfound classes to be in the same package as the current class
  • Loading branch information
LoiNguyenCS authored Nov 29, 2023
2 parents 2bb200e + 2be3a83 commit 54b1c2d
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -526,13 +526,7 @@ 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);
}
updateUnsolvedClassWithClassName(nodeTypeSimpleForm);
}
}

Expand Down Expand Up @@ -677,12 +671,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);
}
updateUnsolvedClassWithClassName(className);
}
}
gotException = true;
Expand All @@ -706,14 +695,7 @@ 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);
}

updateUnsolvedClassWithClassName(type, creationMethod);
} catch (Exception q) {
// can not solve the parameters for this object creation in this current run
}
Expand Down Expand Up @@ -807,14 +789,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));
UnsolvedMethod thisMethod = new UnsolvedMethod(methodName, returnType, listOfParameters);
missingClass.addMethod(thisMethod);
UnsolvedClass missingClass = updateUnsolvedClassWithClassName(className, thisMethod);
syntheticMethodAndClass.put(methodName, missingClass);
this.updateMissingClass(missingClass);

// if the return type is not specified, a synthetic return type will be created. This part of
// codes creates the corresponding class for that synthetic return type
Expand Down Expand Up @@ -889,6 +866,29 @@ 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 and update the list of missing class with that UnsolvedClass instance.
*
* @param nameOfClass the name of an unsolved class
* @param unsolvedMethods unsolved methods to add to the class before updating this visitor's set
* missing classes (optional, may be omitted)
* @return the newly-created UnsolvedClass method, for further processing. This output may be
* ignored.
*/
public UnsolvedClass updateUnsolvedClassWithClassName(
@ClassGetSimpleName String nameOfClass, UnsolvedMethod... unsolvedMethods) {
// 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);
UnsolvedClass result = new UnsolvedClass(nameOfClass, packageName);
for (UnsolvedMethod unsolvedMethod : unsolvedMethods) {
result.addMethod(unsolvedMethod);
}
updateMissingClass(result);
return result;
}

/**
* 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");
}
}

0 comments on commit 54b1c2d

Please sign in to comment.