@Override public ResolvedClassDeclaration declaringType() { return new ReflectionClassDeclaration(constructor.getDeclaringClass(), typeSolver); }
public Optional<MethodUsage> solveMethodAsUsage(String name, List<ResolvedType> argumentsTypes, Context invokationContext, List<ResolvedType> typeParameterValues) { List<MethodUsage> methods = new ArrayList<>(); for (Method method : Arrays.stream(clazz.getDeclaredMethods()).filter((m) -> m.getName().equals(name)).sorted(new MethodComparator()).collect(Collectors.toList())) { if (method.isBridge() || method.isSynthetic()) continue; ResolvedMethodDeclaration methodDeclaration = new ReflectionMethodDeclaration(method, typeSolver); MethodUsage methodUsage = new MethodUsage(methodDeclaration); for (int i = 0; i < getTypeParameters().size() && i < typeParameterValues.size(); i++) { ResolvedTypeParameterDeclaration tpToReplace = getTypeParameters().get(i); ResolvedType newValue = typeParameterValues.get(i); methodUsage = methodUsage.replaceTypeParameter(tpToReplace, newValue); } methods.add(methodUsage); } if (getSuperClass() != null) { ResolvedClassDeclaration superClass = (ResolvedClassDeclaration) getSuperClass().getTypeDeclaration(); Optional<MethodUsage> ref = ContextHelper.solveMethodAsUsage(superClass, name, argumentsTypes, invokationContext, typeParameterValues); if (ref.isPresent()) { methods.add(ref.get()); } } for (ResolvedReferenceType interfaceDeclaration : getInterfaces()) { Optional<MethodUsage> ref = ContextHelper.solveMethodAsUsage(interfaceDeclaration.getTypeDeclaration(), name, argumentsTypes, invokationContext, typeParameterValues); if (ref.isPresent()) { methods.add(ref.get()); } } Optional<MethodUsage> ref = MethodResolutionLogic.findMostApplicableUsage(methods, name, argumentsTypes, typeSolver); return ref; }
@Override public boolean canBeAssignedTo(ResolvedReferenceTypeDeclaration other) { if (other instanceof LambdaArgumentTypePlaceholder) { return isFunctionalInterface(); } if (other.getQualifiedName().equals(getQualifiedName())) { return true; } if (this.clazz.getSuperclass() != null && new ReflectionClassDeclaration(clazz.getSuperclass(), typeSolver).canBeAssignedTo(other)) { return true; } for (Class<?> interfaze : clazz.getInterfaces()) { if (new ReflectionInterfaceDeclaration(interfaze, typeSolver).canBeAssignedTo(other)) { return true; } } return false; }
@Deprecated public SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> argumentsTypes, boolean staticOnly) { List<ResolvedMethodDeclaration> methods = new ArrayList<>(); Predicate<Method> staticFilter = m -> !staticOnly || (staticOnly && Modifier.isStatic(m.getModifiers())); for (Method method : Arrays.stream(clazz.getDeclaredMethods()).filter((m) -> m.getName().equals(name)).filter(staticFilter) .sorted(new MethodComparator()).collect(Collectors.toList())) { if (method.isBridge() || method.isSynthetic()) continue; ResolvedMethodDeclaration methodDeclaration = new ReflectionMethodDeclaration(method, typeSolver); methods.add(methodDeclaration); } if (getSuperClass() != null) { ResolvedClassDeclaration superClass = (ResolvedClassDeclaration) getSuperClass().getTypeDeclaration(); SymbolReference<ResolvedMethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(superClass, name, argumentsTypes, staticOnly, typeSolver); if (ref.isSolved()) { methods.add(ref.getCorrespondingDeclaration()); } } for (ResolvedReferenceType interfaceDeclaration : getInterfaces()) { SymbolReference<ResolvedMethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(interfaceDeclaration.getTypeDeclaration(), name, argumentsTypes, staticOnly, typeSolver); if (ref.isSolved()) { methods.add(ref.getCorrespondingDeclaration()); } } return MethodResolutionLogic.findMostApplicable(methods, name, argumentsTypes, typeSolver); }
return Arrays.asList(typeOfScope.asWildcard().getBoundedType().asReferenceType().getTypeDeclaration()); } else { return Arrays.asList(new ReflectionClassDeclaration(Object.class, typeSolver).asReferenceType()); return Arrays.asList(new ReflectionClassDeclaration(Object.class, typeSolver).asReferenceType()); } else if (typeOfScope.isTypeVariable()) { Collection<ResolvedReferenceTypeDeclaration> result = new ArrayList<>();
@Test public void testGetSuperclassWithoutTypeParameters() { ReflectionClassDeclaration compilationUnit = (ReflectionClassDeclaration) typeResolver.solveType("com.github.javaparser.ast.CompilationUnit"); assertEquals("com.github.javaparser.ast.Node", compilationUnit.getSuperClass().getQualifiedName()); }
@Test public void testGetInterfacesWithoutParameters() { ReflectionClassDeclaration compilationUnit = (ReflectionClassDeclaration) typeResolver.solveType("com.github.javaparser.ast.CompilationUnit"); assertEquals(ImmutableSet.of(), compilationUnit.getInterfaces().stream().map(i -> i.getQualifiedName()).collect(Collectors.toSet())); ReflectionClassDeclaration coid = (ReflectionClassDeclaration) typeResolver.solveType("com.github.javaparser.ast.body.ClassOrInterfaceDeclaration"); assertEquals(ImmutableSet.of("com.github.javaparser.ast.nodeTypes.NodeWithExtends", "com.github.javaparser.ast.nodeTypes.modifiers.NodeWithFinalModifier", "com.github.javaparser.ast.nodeTypes.NodeWithConstructors", "com.github.javaparser.ast.nodeTypes.NodeWithImplements", "com.github.javaparser.ast.nodeTypes.modifiers.NodeWithAbstractModifier", "com.github.javaparser.ast.nodeTypes.NodeWithTypeParameters", "com.github.javaparser.resolution.Resolvable"), coid.getInterfaces().stream().map(i -> i.getQualifiedName()).collect(Collectors.toSet())); }
@Override public String getClassName() { String canonicalName = clazz.getCanonicalName(); if (canonicalName != null && getPackageName() != null) { return canonicalName.substring(getPackageName().length() + 1, canonicalName.length()); } return null; }
@Override public String toString() { return "ReflectionClassDeclaration{" + "clazz=" + getId() + '}'; }
@Override public boolean isAssignableBy(ResolvedReferenceTypeDeclaration other) { return isAssignableBy(new ReferenceTypeImpl(other, typeSolver)); }
@Deprecated public SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> argumentsTypes, boolean staticOnly) { List<ResolvedMethodDeclaration> methods = new ArrayList<>(); Predicate<Method> staticFilter = m -> !staticOnly || (staticOnly && Modifier.isStatic(m.getModifiers())); for (Method method : Arrays.stream(clazz.getDeclaredMethods()).filter((m) -> m.getName().equals(name)).filter(staticFilter) .sorted(new MethodComparator()).collect(Collectors.toList())) { if (method.isBridge() || method.isSynthetic()) continue; ResolvedMethodDeclaration methodDeclaration = new ReflectionMethodDeclaration(method, typeSolver); methods.add(methodDeclaration); } if (getSuperClass() != null) { ResolvedClassDeclaration superClass = (ResolvedClassDeclaration) getSuperClass().getTypeDeclaration(); SymbolReference<ResolvedMethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(superClass, name, argumentsTypes, staticOnly, typeSolver); if (ref.isSolved()) { methods.add(ref.getCorrespondingDeclaration()); } } for (ResolvedReferenceType interfaceDeclaration : getInterfaces()) { SymbolReference<ResolvedMethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(interfaceDeclaration.getTypeDeclaration(), name, argumentsTypes, staticOnly, typeSolver); if (ref.isSolved()) { methods.add(ref.getCorrespondingDeclaration()); } } return MethodResolutionLogic.findMostApplicable(methods, name, argumentsTypes, typeSolver); }
@Test public void testGetSuperclassWithTypeParameters() { ReflectionClassDeclaration compilationUnit = (ReflectionClassDeclaration) typeResolver.solveType("com.github.javaparser.ast.body.ConstructorDeclaration"); assertEquals("com.github.javaparser.ast.body.CallableDeclaration", compilationUnit.getSuperClass().getQualifiedName()); assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration", compilationUnit.getSuperClass().typeParametersMap().getValueBySignature("com.github.javaparser.ast.body.CallableDeclaration.T").get().asReferenceType().getQualifiedName()); }
@Test public void testGetInterfacesWithParameters() { ReflectionClassDeclaration constructorDeclaration = (ReflectionClassDeclaration) typeResolver.solveType("com.github.javaparser.ast.body.ConstructorDeclaration"); System.out.println(constructorDeclaration.getInterfaces().stream().map(t -> t.getQualifiedName()).collect(Collectors.toList())); assertEquals(9, constructorDeclaration.getInterfaces().size()); interfaze = constructorDeclaration.getInterfaces().get(0); assertEquals("com.github.javaparser.ast.nodeTypes.NodeWithBlockStmt", interfaze.getQualifiedName()); assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration", interfaze.typeParametersMap().getValueBySignature("com.github.javaparser.ast.nodeTypes.NodeWithBlockStmt.N").get().asReferenceType().getQualifiedName()); interfaze = constructorDeclaration.getInterfaces().get(1); assertEquals("com.github.javaparser.ast.nodeTypes.modifiers.NodeWithAccessModifiers", interfaze.getQualifiedName()); assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration", interfaze.typeParametersMap().getValueBySignature("com.github.javaparser.ast.nodeTypes.modifiers.NodeWithAccessModifiers.N").get().asReferenceType().getQualifiedName()); interfaze = constructorDeclaration.getInterfaces().get(2); assertEquals("com.github.javaparser.ast.nodeTypes.NodeWithJavadoc", interfaze.getQualifiedName()); assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration", interfaze.typeParametersMap().getValueBySignature("com.github.javaparser.ast.nodeTypes.NodeWithJavadoc.N").get().asReferenceType().getQualifiedName()); interfaze = constructorDeclaration.getInterfaces().get(3); assertEquals("com.github.javaparser.ast.nodeTypes.NodeWithDeclaration", interfaze.getQualifiedName()); interfaze = constructorDeclaration.getInterfaces().get(4); assertEquals("com.github.javaparser.ast.nodeTypes.NodeWithSimpleName", interfaze.getQualifiedName()); assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration", interfaze.typeParametersMap().getValueBySignature("com.github.javaparser.ast.nodeTypes.NodeWithSimpleName.N").get().asReferenceType().getQualifiedName()); interfaze = constructorDeclaration.getInterfaces().get(5); assertEquals("com.github.javaparser.ast.nodeTypes.NodeWithParameters", interfaze.getQualifiedName()); assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration", interfaze.typeParametersMap().getValueBySignature("com.github.javaparser.ast.nodeTypes.NodeWithParameters.N").get().asReferenceType().getQualifiedName()); interfaze = constructorDeclaration.getInterfaces().get(6);
@Override public String getClassName() { String canonicalName = clazz.getCanonicalName(); if (canonicalName != null && getPackageName() != null) { return canonicalName.substring(getPackageName().length() + 1, canonicalName.length()); } return null; }
@Override public String toString() { return "ReflectionClassDeclaration{" + "clazz=" + getId() + '}'; }
@Override public boolean isAssignableBy(ResolvedReferenceTypeDeclaration other) { return isAssignableBy(new ReferenceTypeImpl(other, typeSolver)); }
@Override public ResolvedClassDeclaration declaringType() { return new ReflectionClassDeclaration(constructor.getDeclaringClass(), typeSolver); }
@Override public boolean canBeAssignedTo(ResolvedReferenceTypeDeclaration other) { if (other instanceof LambdaArgumentTypePlaceholder) { return isFunctionalInterface(); } if (other.getQualifiedName().equals(getQualifiedName())) { return true; } if (this.clazz.getSuperclass() != null && new ReflectionClassDeclaration(clazz.getSuperclass(), typeSolver).canBeAssignedTo(other)) { return true; } for (Class<?> interfaze : clazz.getInterfaces()) { if (new ReflectionInterfaceDeclaration(interfaze, typeSolver).canBeAssignedTo(other)) { return true; } } return false; }
public Optional<MethodUsage> solveMethodAsUsage(String name, List<ResolvedType> argumentsTypes, TypeSolver typeSolver, Context invokationContext, List<ResolvedType> typeParameterValues) { List<MethodUsage> methods = new ArrayList<>(); for (Method method : Arrays.stream(clazz.getDeclaredMethods()).filter((m) -> m.getName().equals(name)).sorted(new MethodComparator()).collect(Collectors.toList())) { if (method.isBridge() || method.isSynthetic()) continue; ResolvedMethodDeclaration methodDeclaration = new ReflectionMethodDeclaration(method, typeSolver); MethodUsage methodUsage = new MethodUsage(methodDeclaration); for (int i = 0; i < getTypeParameters().size() && i < typeParameterValues.size(); i++) { ResolvedTypeParameterDeclaration tpToReplace = getTypeParameters().get(i); ResolvedType newValue = typeParameterValues.get(i); methodUsage = methodUsage.replaceTypeParameter(tpToReplace, newValue); } methods.add(methodUsage); } if (getSuperClass() != null) { ResolvedClassDeclaration superClass = (ResolvedClassDeclaration) getSuperClass().getTypeDeclaration(); Optional<MethodUsage> ref = ContextHelper.solveMethodAsUsage(superClass, name, argumentsTypes, typeSolver, invokationContext, typeParameterValues); if (ref.isPresent()) { methods.add(ref.get()); } } for (ResolvedReferenceType interfaceDeclaration : getInterfaces()) { Optional<MethodUsage> ref = ContextHelper.solveMethodAsUsage(interfaceDeclaration.getTypeDeclaration(), name, argumentsTypes, typeSolver, invokationContext, typeParameterValues); if (ref.isPresent()) { methods.add(ref.get()); } } Optional<MethodUsage> ref = MethodResolutionLogic.findMostApplicableUsage(methods, name, argumentsTypes, typeSolver); return ref; }
methods.add(methodDeclaration); if (getSuperClass() != null) { ResolvedClassDeclaration superClass = (ResolvedClassDeclaration) getSuperClass().getTypeDeclaration(); SymbolReference<ResolvedMethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(superClass, name, argumentsTypes, staticOnly); if (ref.isSolved()) { for (ResolvedReferenceType interfaceDeclaration : getInterfaces()) { SymbolReference<ResolvedMethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(interfaceDeclaration.getTypeDeclaration(), name, argumentsTypes, staticOnly); if (ref.isSolved()) {