Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into issue_38
Browse files Browse the repository at this point in the history
  • Loading branch information
Tahiatul Islam committed Nov 29, 2023
2 parents eefedcb + 54b1c2d commit 230749f
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -528,11 +528,7 @@ public Visitable visit(MethodDeclaration node, Void arg) {
try {
nodeType.resolve();
} catch (UnsolvedSymbolException | UnsupportedOperationException e) {
// 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.
String packageName = classAndPackageMap.getOrDefault(nodeTypeSimpleForm, currentPackage);
UnsolvedClass syntheticType = new UnsolvedClass(nodeTypeSimpleForm, packageName);
this.updateMissingClass(syntheticType);
updateUnsolvedClassWithClassName(nodeTypeSimpleForm, false);
}
}

Expand Down Expand Up @@ -699,12 +695,7 @@ public Visitable visit(ObjectCreationExpr newExpr, Void p) {
try {
List<String> argumentsCreation = getArgumentsFromObjectCreation(newExpr);
UnsolvedMethod creationMethod = new UnsolvedMethod("", type, argumentsCreation);
// 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);
this.updateMissingClass(newClass);
updateUnsolvedClassWithClassName(type, false, creationMethod);
} catch (Exception q) {
// can not solve the parameters for this object creation in this current run
}
Expand All @@ -728,15 +719,12 @@ private void handleParameterResolveFailure(@NonNull Parameter parameter) {
} else {
// since it is unsolved, it could not be a primitive type
@ClassGetSimpleName String className = parameter.getType().asClassOrInterfaceType().getName().asString();
String packageName = classAndPackageMap.getOrDefault(className, currentPackage);
UnsolvedClass newClass;
if (parameter.getParentNode().isPresent()
&& parameter.getParentNode().get() instanceof CatchClause) {
newClass = new UnsolvedClass(className, packageName, true);
updateUnsolvedClassWithClassName(className, true);
} else {
newClass = new UnsolvedClass(className, packageName);
updateUnsolvedClassWithClassName(className, false);
}
updateMissingClass(newClass);
}
}

Expand Down Expand Up @@ -841,13 +829,9 @@ public void updateUnsolvedClassWithMethod(
} else {
returnType = desiredReturnType;
}
// 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);
UnsolvedClass missingClass = updateUnsolvedClassWithClassName(className, false, 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 @@ -922,6 +906,32 @@ 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)
* @param isExceptionType if the class is of exceptionType
* @return the newly-created UnsolvedClass method, for further processing. This output may be
* ignored.
*/
public UnsolvedClass updateUnsolvedClassWithClassName(
@ClassGetSimpleName String nameOfClass,
boolean isExceptionType,
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, isExceptionType);
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 230749f

Please sign in to comment.