Skip to content

Commit

Permalink
Merge pull request #39 from LoiNguyenCS/add-resolve
Browse files Browse the repository at this point in the history
Fix bugs with array type
  • Loading branch information
kelloggm authored Nov 13, 2023
2 parents 1e93372 + 347aff0 commit 0173cab
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
import com.github.javaparser.ast.expr.ObjectCreationExpr;
import com.github.javaparser.ast.expr.SuperExpr;
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.visitor.ModifierVisitor;
import com.github.javaparser.ast.visitor.Visitable;
import com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedFieldDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedValueDeclaration;
import com.github.javaparser.resolution.types.ResolvedReferenceType;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -195,7 +195,9 @@ public Visitable visit(MethodDeclaration method, Void p) {
targetMethods.add(method.resolve().getQualifiedSignature());
unfoundMethods.remove(methodName);
Type returnType = method.getType();
if (returnType instanceof ReferenceType) {
// JavaParser may misinterpret unresolved array types as reference types.
// To ensure accuracy, we resolve the type before proceeding with the check.
if (returnType.resolve() instanceof ResolvedReferenceType) {
usedClass.add(returnType.resolve().asReferenceType().getQualifiedName());
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/org/checkerframework/specimin/ArrayTypeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.checkerframework.specimin;

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

/**
* This test checks if Specimin will work for array types
*/
public class ArrayTypeTest {
@Test
public void runTest() throws IOException {
SpeciminTestExecutor.runTestWithoutJarPaths(
"arrayType",
new String[] {"com/example/Simple.java"},
new String[] {"com.example.Simple#sortArray(int[])"});
}
}
11 changes: 11 additions & 0 deletions src/test/resources/arrayType/expected/com/example/Simple.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example;

import java.util.Arrays;

class Simple {

public static int[] sortArray(int[] array) {
Arrays.sort(array);
return array;
}
}
11 changes: 11 additions & 0 deletions src/test/resources/arrayType/input/com/example/Simple.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example;

import java.util.Arrays;

class Simple {
// Target method.
public static int[] sortArray(int[] array) {
Arrays.sort(array);
return array;
}
}

0 comments on commit 0173cab

Please sign in to comment.