Skip to content

Commit 29acf19

Browse files
authored
Merge pull request #352 from domaframework/fix/primitive-type-validation-issue-351
Fix primitive type validation for DAO method return types
2 parents fea91a5 + 1a7985a commit 29acf19

File tree

5 files changed

+62
-4
lines changed

5 files changed

+62
-4
lines changed

.claude/setting.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

src/main/kotlin/org/domaframework/doma/intellij/common/psi/PsiTypeChecker.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ object PsiTypeChecker {
6060
fun isBaseClassType(psiType: PsiType?): Boolean {
6161
if (psiType == null) return false
6262
// Check if the type is a primitive type
63-
if (psiType is PsiPrimitiveType && psiType.canonicalText == "char") {
64-
return false
63+
if (psiType is PsiPrimitiveType) {
64+
// char is not supported, but other primitive types are
65+
return psiType.canonicalText != "char"
6566
}
6667

6768
// Check if the type is a wrapper class

src/main/kotlin/org/domaframework/doma/intellij/inspection/dao/processor/returntype/SelectReturnTypeCheckProcessor.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package org.domaframework.doma.intellij.inspection.dao.processor.returntype
1717

1818
import com.intellij.psi.PsiClassType
1919
import com.intellij.psi.PsiType
20+
import com.intellij.psi.PsiTypes
2021
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
2122
import org.domaframework.doma.intellij.common.util.DomaClassName
2223
import org.domaframework.doma.intellij.common.util.TypeUtil
@@ -77,7 +78,7 @@ class SelectReturnTypeCheckProcessor(
7778
shortName,
7879
checkTypeCanonicalText,
7980
)
80-
if (checkType == null) return result
81+
if (checkType == null || checkType == PsiTypes.voidType()) return result
8182

8283
if (TypeUtil.isBaseOrOptionalWrapper(checkType)) {
8384
return null

src/test/testData/src/main/java/doma/example/dao/inspection/returntype/FunctionReturnTypeTestDao.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,32 @@
1616
public interface FunctionReturnTypeTestDao {
1717

1818
@Function
19-
int <error descr="The return type int is invalid">primitiveFunction</error>(@In int id);
19+
int primitiveFunction(@In int id);
2020

2121
@Function
2222
void voidFunction(@In int id);
2323

24+
@Function
25+
long primitiveLongFunction(@In long id);
26+
27+
@Function
28+
boolean primitiveBooleanFunction(@In boolean flag);
29+
30+
@Function
31+
double primitiveDoubleFunction(@In double value);
32+
33+
@Function
34+
float primitiveFloatFunction(@In float value);
35+
36+
@Function
37+
byte primitiveByteFunction(@In byte value);
38+
39+
@Function
40+
short primitiveShortFunction(@In short value);
41+
42+
@Function
43+
char <error descr="The return type char is invalid">primitiveCharFunction</error>(@In char value);
44+
2445
@Function
2546
String executeFunction(
2647
@In Integer arg1, @InOut Reference<Integer> arg2, @Out Reference<Integer> arg3);

src/test/testData/src/main/java/doma/example/dao/inspection/returntype/SelectReturnTypeTestDao.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,38 @@ public interface SelectReturnTypeTestDao {
139139
@Sql("select * from emp where salary > /* salary */0")
140140
String selectHogeFunction(BigDecimal salary, HogeFunction function);
141141

142+
// Test cases for primitive return types - these should be valid (except char)
143+
@Select
144+
@Sql("select count(*) from EMPLOYEE")
145+
int selectCountAsInt();
146+
147+
@Select
148+
@Sql("select count(*) from EMPLOYEE")
149+
long selectCountAsLong();
150+
151+
@Select
152+
@Sql("select exists(select 1 from EMPLOYEE where EMPLOYEE_ID = /* id */1)")
153+
boolean selectExistsAsBoolean(Integer id);
154+
155+
@Select
156+
@Sql("select avg(SALARY) from EMPLOYEE")
157+
double selectAverageAsDouble();
158+
159+
@Select
160+
@Sql("select avg(SALARY) from EMPLOYEE")
161+
float selectAverageAsFloat();
162+
163+
@Select
164+
@Sql("select age from EMPLOYEE where EMPLOYEE_ID = /* id */1")
165+
byte selectAgeAsByte(Integer id);
166+
167+
@Select
168+
@Sql("select department_id from EMPLOYEE where EMPLOYEE_ID = /* id */1")
169+
short selectDepartmentIdAsShort(Integer id);
170+
171+
// This should show an error - char is not supported
172+
@Select
173+
@Sql("select initial from EMPLOYEE where EMPLOYEE_ID = /* id */1")
174+
char <error descr="The return type char is invalid">selectInitialAsChar</error>(Integer id);
175+
142176
}

0 commit comments

Comments
 (0)