Skip to content

Commit

Permalink
JavassistMethodDeclaration returns correct type of param if param has…
Browse files Browse the repository at this point in the history
… raw type.
  • Loading branch information
Kathrin Geilmann committed Feb 14, 2019
1 parent ff33441 commit a7a5c9a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 9 deletions.
Expand Up @@ -132,15 +132,12 @@ public ResolvedParameterDeclaration getParam(int i) {
variadic = i == (ctMethod.getParameterTypes().length - 1);
}
Optional<String> paramName = JavassistUtils.extractParameterName(ctMethod, i);
if (ctMethod.getGenericSignature() != null) {
SignatureAttribute.MethodSignature methodSignature = SignatureAttribute.toMethodSignature(ctMethod.getGenericSignature());
SignatureAttribute.Type signatureType = methodSignature.getParameterTypes()[i];
return new JavassistParameterDeclaration(JavassistUtils.signatureTypeToType(signatureType,
typeSolver, this), typeSolver, variadic, paramName.orElse(null));
} else {
return new JavassistParameterDeclaration(ctMethod.getParameterTypes()[i], typeSolver, variadic,
paramName.orElse(null));
}
String signature = ctMethod.getGenericSignature() == null ? ctMethod.getSignature() : ctMethod.getGenericSignature();
SignatureAttribute.MethodSignature methodSignature = SignatureAttribute.toMethodSignature(signature);
SignatureAttribute.Type signatureType = methodSignature.getParameterTypes()[i];
return new JavassistParameterDeclaration(JavassistUtils.signatureTypeToType(signatureType,
typeSolver, this), typeSolver, variadic, paramName.orElse(null));

} catch (NotFoundException | BadBytecode e) {
throw new RuntimeException(e);
}
Expand Down
@@ -0,0 +1,52 @@
package com.github.javaparser.symbolsolver.javassistmodel;

import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedParameterDeclaration;
import com.github.javaparser.symbolsolver.AbstractSymbolResolutionTest;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Path;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class JavassistMethodDeclarationTest extends AbstractSymbolResolutionTest {

private TypeSolver typeSolver;

@BeforeEach
void setup() throws IOException {
Path pathToJar = adaptPath("src/test/resources/javassistmethoddecl/javassistmethoddecl.jar");
typeSolver = new CombinedTypeSolver(new JarTypeSolver(pathToJar), new ReflectionTypeSolver());
}

@Test
void getParameterDeclaration_forMethodParameterWithRawType() {
JavassistClassDeclaration classDecl = (JavassistClassDeclaration) typeSolver.solveType("C");
ResolvedMethodDeclaration methodWithRawParameter = findMethodWithNAme(classDecl, "methodWithRawParameter");

ResolvedParameterDeclaration rawParam = methodWithRawParameter.getParam(0);

assertThat(rawParam.describeType(), is("java.util.List"));
}

@Test
void getParameterDeclaration_forMethodParameterWithGenericType() {
JavassistClassDeclaration classDecl = (JavassistClassDeclaration) typeSolver.solveType("C");
ResolvedMethodDeclaration methodWithRawParameter = findMethodWithNAme(classDecl, "methodWithGenericParameter");

ResolvedParameterDeclaration genericParam = methodWithRawParameter.getParam(0);

assertThat(genericParam.describeType(), is("java.util.List<java.lang.String>"));
}

private ResolvedMethodDeclaration findMethodWithNAme(JavassistClassDeclaration classDecl, String name) {
return classDecl.getDeclaredMethods().stream().filter(methodDecl -> methodDecl.getName().equals(name)).findAny().get();
}
}
@@ -0,0 +1,11 @@
import java.util.List;

public class C {

public void methodWithRawParameter(List l) {
}

public void methodWithGenericParameter(List<String> l) {
}

}
Binary file not shown.

0 comments on commit a7a5c9a

Please sign in to comment.