Skip to content

Commit

Permalink
Merge pull request mybatis#1093 from harawata/constructor-args-resolu…
Browse files Browse the repository at this point in the history
…tion-bug

When resolving constructor args, @param should precede actual name even when useActualParamName is enabled.
  • Loading branch information
harawata authored Sep 7, 2017
2 parents 0c3d264 + 0debc1d commit 6b3884c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
35 changes: 20 additions & 15 deletions src/main/java/org/apache/ibatis/mapping/ResultMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,24 +185,29 @@ private boolean argTypesMatch(final List<String> constructorArgNames,
}

private List<String> getArgNames(Constructor<?> constructor) {
if (resultMap.configuration.isUseActualParamName() && Jdk.parameterExists) {
return ParamNameUtil.getParamNames(constructor);
} else {
List<String> paramNames = new ArrayList<String>();
final Annotation[][] paramAnnotations = constructor.getParameterAnnotations();
int paramCount = paramAnnotations.length;
for (int paramIndex = 0; paramIndex < paramCount; paramIndex++) {
String name = null;
for (Annotation annotation : paramAnnotations[paramIndex]) {
if (annotation instanceof Param) {
name = ((Param) annotation).value();
break;
}
List<String> paramNames = new ArrayList<String>();
List<String> actualParamNames = null;
final Annotation[][] paramAnnotations = constructor.getParameterAnnotations();
int paramCount = paramAnnotations.length;
for (int paramIndex = 0; paramIndex < paramCount; paramIndex++) {
String name = null;
for (Annotation annotation : paramAnnotations[paramIndex]) {
if (annotation instanceof Param) {
name = ((Param) annotation).value();
break;
}
}
if (name == null && resultMap.configuration.isUseActualParamName() && Jdk.parameterExists) {
if (actualParamNames == null) {
actualParamNames = ParamNameUtil.getParamNames(constructor);
}
if (actualParamNames.size() > paramIndex) {
name = actualParamNames.get(paramIndex);
}
paramNames.add(name != null ? name : "arg" + paramIndex);
}
return paramNames;
paramNames.add(name != null ? name : "arg" + paramIndex);
}
return paramNames;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public User(@Param("id") String id) {
this.id = Integer.valueOf(id);
}

public User(Integer userId, String name) {
public User(Integer userId, @Param("name") String userName) {
super();
this.id = userId;
this.name = name;
this.name = userName;
}

public User(@Param("id") int id, @Param("name") String name, @Param("team") String team) {
Expand Down

0 comments on commit 6b3884c

Please sign in to comment.