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

Crash fix for UnionType parameter in Issue #38 #45

Merged
merged 25 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
0314236
[UNCOMPLTE] testcase and visitor method update for unionType parameter
Nov 15, 2023
eeec7ed
test runner add
Nov 15, 2023
d52e8b0
spotless apply
Nov 15, 2023
70e3b75
adding missing imports.
Nov 15, 2023
81d45c8
renaming test classes
Nov 15, 2023
ab2a7f9
handle unsolvable unionType parameter
Nov 17, 2023
a077029
adding test cases
Nov 21, 2023
7fec587
removing unsolved test
Nov 21, 2023
660071e
Merge branch 'main' into issue_38
kelloggm Nov 27, 2023
67c2d61
[UNCOMPLTE] testcase and visitor method update for unionType parameter
Nov 15, 2023
a2daa8c
adding missing imports.
Nov 15, 2023
02a8623
issue fixes in targetvisitor for parameter of union type
Nov 28, 2023
1768703
test fail fix
Nov 28, 2023
75a3b48
adding custom method in custom exception
Nov 28, 2023
8a10d54
using actual type instead of var and removing try catch block where i…
Nov 28, 2023
0c05341
updating constructor of unsolvedClass
Nov 29, 2023
8cbbd49
test case with addition block in catch clauses
Nov 29, 2023
5bfe7e1
Merge remote-tracking branch 'upstream/main' into issue_38
Nov 29, 2023
302cf78
adding testcase for unsolvable exception type
Nov 29, 2023
5e32e2d
checking value presence in optional type
Nov 29, 2023
eefedcb
factoring uniontype resolution to a method, breaking the string build…
Nov 29, 2023
230749f
Merge remote-tracking branch 'upstream/main' into issue_38
Nov 29, 2023
c766008
removing synthetic file mistakenly added in the commit
Nov 29, 2023
8a5bf1f
documenting resolveUnionType method
Nov 29, 2023
34a10d5
add documentation to methods where it is missing
Nov 29, 2023
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 @@ -16,6 +16,7 @@
import com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt;
import com.github.javaparser.ast.type.ReferenceType;
import com.github.javaparser.ast.type.Type;
import com.github.javaparser.ast.type.UnionType;
import com.github.javaparser.ast.visitor.ModifierVisitor;
import com.github.javaparser.ast.visitor.Visitable;
import com.github.javaparser.resolution.UnsolvedSymbolException;
Expand Down Expand Up @@ -223,24 +224,21 @@ public Visitable visit(MethodDeclaration method, Void p) {
public Visitable visit(Parameter para, Void p) {
if (insideTargetMethod) {
Type type = para.getType();
String paraTypeFullName = "";
ResolvedType paramType;
if (type.isUnionType()) {
for (ReferenceType param : para.getType().asUnionType().getElements()) {
paramType = param.resolve();
paraTypeFullName =
paramType.asReferenceType().getTypeDeclaration().get().getQualifiedName();
usedClass.add(paraTypeFullName);
}
resolveUnionType(type.asUnionType());
} else {
// Parameter resolution (para.resolve()) does not work in catch clause.
// However, resolution works on the type of the parameter.
// Bug report: https://github.com/javaparser/javaparser/issues/4240
ResolvedType paramType;
if (para.getParentNode().isPresent() && para.getParentNode().get() instanceof CatchClause) {
tahiat marked this conversation as resolved.
Show resolved Hide resolved
paramType = para.getType().resolve();
} else {
paramType = para.resolve().getType();
}

if (paramType.isReferenceType()) {
paraTypeFullName =
String paraTypeFullName =
paramType.asReferenceType().getTypeDeclaration().get().getQualifiedName();
usedClass.add(paraTypeFullName);
for (ResolvedType typeParameterValue :
Expand Down Expand Up @@ -330,4 +328,16 @@ public Visitable visit(NameExpr expr, Void p) {
}
return super.visit(expr, p);
}

/**
* @param type unionType parameter
tahiat marked this conversation as resolved.
Show resolved Hide resolved
*/
private void resolveUnionType(UnionType type) {
for (ReferenceType param : type.getElements()) {
ResolvedType paramType = param.resolve();
String paraTypeFullName =
paramType.asReferenceType().getTypeDeclaration().get().getQualifiedName();
usedClass.add(paraTypeFullName);
}
}
}
13 changes: 3 additions & 10 deletions src/main/java/org/checkerframework/specimin/UnsolvedClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,11 @@ public void updateFieldByType(String currentType, String correctType) {
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("package ").append(packageName).append(";\n");
sb.append("public class ").append(className).append(getTypeVariablesAsString());
if (isExceptionType) {
sb.append("public class ")
.append(className)
.append(getTypeVariablesAsString())
.append(" extends Exception")
.append(" {\n");
} else {
sb.append("public class ")
.append(className)
.append(getTypeVariablesAsString())
.append(" {\n");
sb.append(" extends Exception");
}
sb.append(" {\n");
for (String variableDeclarations : classFields) {
sb.append(" " + "public " + variableDeclarations + ";\n");
}
Expand Down
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,11 +829,8 @@ 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);
UnsolvedClass missingClass = updateUnsolvedClassWithClassName(className, thisMethod);
UnsolvedClass missingClass = updateUnsolvedClassWithClassName(className, false, thisMethod);
syntheticMethodAndClass.put(methodName, missingClass);

// if the return type is not specified, a synthetic return type will be created. This part of
Expand Down Expand Up @@ -928,15 +913,18 @@ public void updateClassesFromJarSourcesForMethodCall(MethodCallExpr expr) {
* @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, UnsolvedMethod... unsolvedMethods) {
@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);
UnsolvedClass result = new UnsolvedClass(nameOfClass, packageName, isExceptionType);
for (UnsolvedMethod unsolvedMethod : unsolvedMethods) {
result.addMethod(unsolvedMethod);
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.