use of com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration in project javaparser by javaparser.
the class ReferenceTypeTest method testReplaceTypeVariables.
@Test
public void testReplaceTypeVariables() {
TypeSolver typeResolver = new ReflectionTypeSolver();
ResolvedInterfaceDeclaration streamInterface = new ReflectionInterfaceDeclaration(Stream.class, typeResolver);
ResolvedReferenceType stream = new ReferenceTypeImpl(streamInterface, typeResolver);
ResolvedMethodDeclaration streamMap = streamInterface.getDeclaredMethods().stream().filter(m -> m.getName().equals("map")).findFirst().get();
ResolvedTypeParameterDeclaration streamMapR = streamMap.findTypeParameter("T").get();
ResolvedTypeVariable typeVariable = new ResolvedTypeVariable(streamMapR);
stream = stream.deriveTypeParameters(stream.typeParametersMap().toBuilder().setValue(stream.getTypeDeclaration().getTypeParameters().get(0), typeVariable).build());
ResolvedTypeParameterDeclaration tpToReplace = streamInterface.getTypeParameters().get(0);
ResolvedType replaced = new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeResolver), typeResolver);
ResolvedType streamReplaced = stream.replaceTypeVariables(tpToReplace, replaced);
assertEquals("java.util.stream.Stream<java.lang.String>", streamReplaced.describe());
}
use of com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration in project javaparser by javaparser.
the class JavaParserClassDeclarationTest method testSolveMethodNotExistingBecauseOfTypeParameters.
@Test
public void testSolveMethodNotExistingBecauseOfTypeParameters() {
JavaParserClassDeclaration constructorDeclaration = (JavaParserClassDeclaration) typeSolverNewCode.solveType("com.github.javaparser.ast.body.ConstructorDeclaration");
SymbolReference<ResolvedMethodDeclaration> res = null;
ResolvedReferenceType stringType = (ResolvedReferenceType) ReflectionFactory.typeUsageFor(String.class, typeSolverNewCode);
ResolvedReferenceType rawClassType = (ResolvedReferenceType) ReflectionFactory.typeUsageFor(Class.class, typeSolverNewCode);
ResolvedReferenceType classOfStringType = (ResolvedReferenceType) rawClassType.replaceTypeVariables(rawClassType.getTypeDeclaration().getTypeParameters().get(0), stringType);
res = constructorDeclaration.solveMethod("isThrows", ImmutableList.of(classOfStringType));
assertEquals(false, res.isSolved());
}
use of com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration in project javaparser by javaparser.
the class ReflectionMethodResolutionLogic method solveMethod.
static SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> parameterTypes, boolean staticOnly, TypeSolver typeSolver, ResolvedReferenceTypeDeclaration scopeType, Class clazz) {
List<ResolvedMethodDeclaration> methods = new ArrayList<>();
Predicate<Method> staticOnlyCheck = m -> !staticOnly || (staticOnly && Modifier.isStatic(m.getModifiers()));
for (Method method : clazz.getMethods()) {
if (method.isBridge() || method.isSynthetic() || !method.getName().equals(name) || !staticOnlyCheck.test(method))
continue;
ResolvedMethodDeclaration methodDeclaration = new ReflectionMethodDeclaration(method, typeSolver);
methods.add(methodDeclaration);
}
for (ResolvedReferenceType ancestor : scopeType.getAncestors()) {
SymbolReference<ResolvedMethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(ancestor.getTypeDeclaration(), name, parameterTypes, staticOnly, typeSolver);
if (ref.isSolved()) {
methods.add(ref.getCorrespondingDeclaration());
}
}
if (scopeType.getAncestors().isEmpty()) {
ReferenceTypeImpl objectClass = new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeSolver), typeSolver);
SymbolReference<ResolvedMethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(objectClass.getTypeDeclaration(), name, parameterTypes, staticOnly, typeSolver);
if (ref.isSolved()) {
methods.add(ref.getCorrespondingDeclaration());
}
}
return MethodResolutionLogic.findMostApplicable(methods, name, parameterTypes, typeSolver);
}
use of com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration in project javaparser by javaparser.
the class CompilationUnitContext method solveMethod.
@Override
public SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> argumentsTypes, boolean staticOnly, TypeSolver typeSolver) {
for (ImportDeclaration importDecl : wrappedNode.getImports()) {
if (importDecl.isStatic()) {
if (importDecl.isAsterisk()) {
String importString = importDecl.getNameAsString();
if (this.wrappedNode.getPackageDeclaration().isPresent() && this.wrappedNode.getPackageDeclaration().get().getName().getIdentifier().equals(packageName(importString)) && this.wrappedNode.getTypes().stream().anyMatch(it -> it.getName().getIdentifier().equals(toSimpleName(importString)))) {
// a lower level so this will fail
return SymbolReference.unsolved(ResolvedMethodDeclaration.class);
}
ResolvedTypeDeclaration ref = typeSolver.solveType(importString);
SymbolReference<ResolvedMethodDeclaration> method = MethodResolutionLogic.solveMethodInType(ref, name, argumentsTypes, true, typeSolver);
if (method.isSolved()) {
return method;
}
} else {
String qName = importDecl.getNameAsString();
if (qName.equals(name) || qName.endsWith("." + name)) {
String typeName = getType(qName);
ResolvedTypeDeclaration ref = typeSolver.solveType(typeName);
SymbolReference<ResolvedMethodDeclaration> method = MethodResolutionLogic.solveMethodInType(ref, name, argumentsTypes, true, typeSolver);
if (method.isSolved()) {
return method;
}
}
}
}
}
return SymbolReference.unsolved(ResolvedMethodDeclaration.class);
}
use of com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration in project javaparser by javaparser.
the class ReflectionMethodDeclarationTest method testGetSignature.
@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());
}
Aggregations