Skip to content

Commit

Permalink
adding tests for parameter names obtained through reflection #1624
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomassetti committed Jun 22, 2018
1 parent d4fd8a4 commit e5c2c1f
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 31 deletions.
Expand Up @@ -36,7 +36,7 @@ default boolean hasName() {
}

/**
* Should return the name or throw a RuntimeException if the name is not available.
* Should return the name or return null if the name is not available.
*/
String getName();

Expand Down
Expand Up @@ -47,13 +47,13 @@ public ReflectionParameterDeclaration(Class<?> type, java.lang.reflect.Type gene
this.name = name;
}

/**
*
* @return the name, which can be potentially null
*/
@Override
public String getName() {
if (hasName()) {
return this.name;
} else {
throw new IllegalStateException("Name of parameters obtained through reflection can be not present");
}
return name;
}

@Override
Expand Down
Expand Up @@ -31,48 +31,39 @@
public class ReflectionMethodDeclarationTest {

@Test
public void testGetSignature() {
public void testParameterNameOnClassesFromTheStdLibrary() {
TypeSolver typeResolver = new ReflectionTypeSolver();

ResolvedClassDeclaration object = new ReflectionClassDeclaration(Object.class, typeResolver);
ResolvedInterfaceDeclaration list = new ReflectionInterfaceDeclaration(List.class, typeResolver);

ResolvedMethodDeclaration hashCode = object.getAllMethods().stream().filter(m -> m.getName().equals("hashCode")).findFirst().get().getDeclaration();
ResolvedMethodDeclaration equals = object.getAllMethods().stream().filter(m -> m.getName().equals("equals")).findFirst().get().getDeclaration();
ResolvedMethodDeclaration containsAll = list.getAllMethods().stream().filter(m -> m.getName().equals("containsAll")).findFirst().get().getDeclaration();
ResolvedMethodDeclaration subList = list.getAllMethods().stream().filter(m -> m.getName().equals("subList")).findFirst().get().getDeclaration();

assertEquals("hashCode()", hashCode.getSignature());
assertEquals("equals(java.lang.Object)", equals.getSignature());
assertEquals("containsAll(java.util.Collection<? extends java.lang.Object>)", containsAll.getSignature());
assertEquals("subList(int, int)", subList.getSignature());
assertEquals("arg0", equals.getParam(0).getName());
assertEquals("arg0", containsAll.getParam(0).getName());
assertEquals("arg1", containsAll.getParam(1).getName());
assertEquals("arg0", subList.getParam(0).getName());
assertEquals("arg1", subList.getParam(1).getName());
}

@Test
public void testGetGenericReturnType() {
TypeSolver typeResolver = new ReflectionTypeSolver();

ResolvedInterfaceDeclaration map = new ReflectionInterfaceDeclaration(Map.class, typeResolver);
class Foo {
void myMethod(int a, char c) {

ResolvedMethodDeclaration put = map.getAllMethods().stream().filter(m -> m.getName().equals("put")).findFirst().get().getDeclaration();
assertEquals(true, put.getReturnType().isTypeVariable());
assertEquals(true, put.getReturnType().asTypeParameter().declaredOnType());
assertEquals("java.util.Map.V", put.getReturnType().asTypeParameter().getQualifiedName());
}
}

@Test
public void testGetGenericParameters() {
TypeSolver typeResolver = new ReflectionTypeSolver();
public void testParameterNameOnClassesFromThisProject() {
TypeSolver typeResolver = new ReflectionTypeSolver(false);

ResolvedInterfaceDeclaration map = new ReflectionInterfaceDeclaration(Map.class, typeResolver);
ResolvedClassDeclaration foo = new ReflectionClassDeclaration(Foo.class, typeResolver);

ResolvedMethodDeclaration put = map.getAllMethods().stream().filter(m -> m.getName().equals("put")).findFirst().get().getDeclaration();
assertEquals(true, put.getParam(0).getType().isTypeVariable());
assertEquals(true, put.getParam(0).getType().asTypeParameter().declaredOnType());
assertEquals("java.util.Map.K", put.getParam(0).getType().asTypeParameter().getQualifiedName());
ResolvedMethodDeclaration myMethod = foo.getAllMethods().stream().filter(m -> m.getName().equals("myMethod")).findFirst().get().getDeclaration();

assertEquals(true, put.getParam(1).getType().isTypeVariable());
assertEquals(true, put.getParam(1).getType().asTypeParameter().declaredOnType());
assertEquals("java.util.Map.V", put.getParam(1).getType().asTypeParameter().getQualifiedName());
assertEquals("arg0", myMethod.getParam(0).getName());
assertEquals("arg1", myMethod.getParam(1).getName());
}

}
@@ -0,0 +1,78 @@
/*
* Copyright 2016 Federico Tomassetti
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.github.javaparser.symbolsolver.reflectionmodel;

import com.github.javaparser.resolution.declarations.ResolvedClassDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedInterfaceDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.Test;

import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class ReflectionParameterDeclarationTest {

@Test
public void testGetSignature() {
TypeSolver typeResolver = new ReflectionTypeSolver();

ResolvedClassDeclaration object = new ReflectionClassDeclaration(Object.class, typeResolver);
ResolvedInterfaceDeclaration list = new ReflectionInterfaceDeclaration(List.class, typeResolver);

ResolvedMethodDeclaration hashCode = object.getAllMethods().stream().filter(m -> m.getName().equals("hashCode")).findFirst().get().getDeclaration();
ResolvedMethodDeclaration equals = object.getAllMethods().stream().filter(m -> m.getName().equals("equals")).findFirst().get().getDeclaration();
ResolvedMethodDeclaration containsAll = list.getAllMethods().stream().filter(m -> m.getName().equals("containsAll")).findFirst().get().getDeclaration();
ResolvedMethodDeclaration subList = list.getAllMethods().stream().filter(m -> m.getName().equals("subList")).findFirst().get().getDeclaration();

assertEquals("hashCode()", hashCode.getSignature());
assertEquals("equals(java.lang.Object)", equals.getSignature());
assertEquals("containsAll(java.util.Collection<? extends java.lang.Object>)", containsAll.getSignature());
assertEquals("subList(int, int)", subList.getSignature());
}

@Test
public void testGetGenericReturnType() {
TypeSolver typeResolver = new ReflectionTypeSolver();

ResolvedInterfaceDeclaration map = new ReflectionInterfaceDeclaration(Map.class, typeResolver);

ResolvedMethodDeclaration put = map.getAllMethods().stream().filter(m -> m.getName().equals("put")).findFirst().get().getDeclaration();
assertEquals(true, put.getReturnType().isTypeVariable());
assertEquals(true, put.getReturnType().asTypeParameter().declaredOnType());
assertEquals("java.util.Map.V", put.getReturnType().asTypeParameter().getQualifiedName());
}

@Test
public void testGetGenericParameters() {
TypeSolver typeResolver = new ReflectionTypeSolver();

ResolvedInterfaceDeclaration map = new ReflectionInterfaceDeclaration(Map.class, typeResolver);

ResolvedMethodDeclaration put = map.getAllMethods().stream().filter(m -> m.getName().equals("put")).findFirst().get().getDeclaration();
assertEquals(true, put.getParam(0).getType().isTypeVariable());
assertEquals(true, put.getParam(0).getType().asTypeParameter().declaredOnType());
assertEquals("java.util.Map.K", put.getParam(0).getType().asTypeParameter().getQualifiedName());

assertEquals(true, put.getParam(1).getType().isTypeVariable());
assertEquals(true, put.getParam(1).getType().asTypeParameter().declaredOnType());
assertEquals("java.util.Map.V", put.getParam(1).getType().asTypeParameter().getQualifiedName());
}
}

0 comments on commit e5c2c1f

Please sign in to comment.