Skip to content

Commit

Permalink
IndexOutOfBoundsException is fixed while resolving overloaded methods
Browse files Browse the repository at this point in the history
  • Loading branch information
idegtiarenko committed Dec 15, 2015
1 parent cd45afe commit 05efe14
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 17 deletions.
42 changes: 25 additions & 17 deletions src/org/concordion/plugin/idea/ConcordionPsiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,35 +88,43 @@ public static PsiMethod findMethodInClass(@NotNull PsiClass containingClass, @Nu
}

private static boolean argumentTypesMatchParameterTypes(@NotNull PsiParameter[] parameters, @NotNull List<PsiType> arguments) {
if (parameters.length == 0) {
return arguments.isEmpty();
}

int lastParameterIndex = parameters.length - 1;
for (int p = 0; p < lastParameterIndex; p++) {
if (!validArgumentType(arguments.get(p), parameters[p].getType())) {
if (!isVarArg(parameters)) {
if (parameters.length != arguments.size()) {
return false;
}
}
if (!parameters[lastParameterIndex].isVarArgs()) {
return parameters.length == arguments.size()
&& validArgumentType(arguments.get(lastParameterIndex), parameters[lastParameterIndex].getType());
for (int i = 0; i < parameters.length; i++) {
if (!validArgumentType(arguments.get(i), parameters[i].getType())) {
return false;
}
}
return true;
} else {
//array is passed into vararg
if (parameters.length == arguments.size() && validArgumentType(arguments.get(lastParameterIndex), parameters[lastParameterIndex].getType())) {
int varArgIndex = parameters.length - 1;
for (int i = 0; i < varArgIndex; i++) {
if (!validArgumentType(arguments.get(i), parameters[i].getType())) {
return false;
}
}
//array passed into vararg
if (parameters.length == arguments.size() && validArgumentType(arguments.get(varArgIndex), parameters[varArgIndex].getType())) {
return true;
}
PsiType last = parameters[lastParameterIndex].getType().getDeepComponentType();
for (int a = lastParameterIndex; a < arguments.size(); a++) {
if (!validArgumentType(arguments.get(a), last)) {
PsiType last = parameters[varArgIndex].getType().getDeepComponentType();
for (int i = varArgIndex; i < arguments.size(); i++) {
if (!validArgumentType(arguments.get(i), last)) {
return false;
}
}
return true;
}
}

private static boolean validArgumentType(PsiType argument, PsiType parameter) {
private static boolean isVarArg(@NotNull PsiParameter[] parameters) {
int lastOne = parameters.length - 1;
return lastOne >= 0 && parameters[lastOne].isVarArgs();
}

private static boolean validArgumentType(@Nullable PsiType argument, @NotNull PsiType parameter) {
return argument == null
|| argument == DYNAMIC
|| parameter.isAssignableFrom(argument);
Expand Down
10 changes: 10 additions & 0 deletions testData/reference/OverloadedMethodArgumentsNumber2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html xmlns:c="http://www.concordion.org/2007/concordion">
<head>
<title>Test spec</title>
</head>
<body>

<span c:assertEquals="methodToResolve(1, 2)<caret>">42</span>

</body>
</html>
18 changes: 18 additions & 0 deletions testData/reference/OverloadedMethodArgumentsNumber2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.test;

import org.concordion.integration.junit4.ConcordionRunner;
import org.junit.runner.RunWith;

import java.lang.Object;

@RunWith(ConcordionRunner.class)
public class OverloadedMethodArgumentsNumber2 {

public int methodToResolve(Object arg1, Object arg2, Object arg3, Object arg4) {
return 42;
}

public int methodToResolve(Object arg1, Object arg2) {
return 42;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,21 @@ public void testResolveOverloadedMethodsWithDifferentArgumentsType() {
assertThat(concordionMethod.getParametersCount()).isEqualTo(3);
}

public void testResolveOverloadedMethodsWithDifferentArgumentsType2() {
copyJavaRunnerToConcordionProject("OverloadedMethodArgumentsNumber2.java");
VirtualFile htmlSpec = copyHtmlSpecToConcordionProject("OverloadedMethodArgumentsNumber2.html");

myFixture.configureFromExistingVirtualFile(htmlSpec);

ConcordionMethod concordionMethod = elementUnderCaret();
PsiMethod javaMethod = resolveReferences(concordionMethod);

assertThat(javaMethod).isNotNull();
assertThat(javaMethod.getName()).isEqualTo("methodToResolve");
assertThat(javaMethod.getParameterList().getParameters()).hasSize(2);
assertThat(concordionMethod.getParametersCount()).isEqualTo(2);
}

public void testResolveInheritedMethodReferences() {

copyJavaRunnerToConcordionProject("Parent.java");
Expand Down

0 comments on commit 05efe14

Please sign in to comment.