use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.
the class ConstructorResolutionLogic method findMostApplicable.
public static SymbolReference<ResolvedConstructorDeclaration> findMostApplicable(List<ResolvedConstructorDeclaration> constructors, List<ResolvedType> argumentsTypes, TypeSolver typeSolver, boolean wildcardTolerance) {
List<ResolvedConstructorDeclaration> applicableConstructors = constructors.stream().filter((m) -> isApplicable(m, argumentsTypes, typeSolver, wildcardTolerance)).collect(Collectors.toList());
if (applicableConstructors.isEmpty()) {
return SymbolReference.unsolved(ResolvedConstructorDeclaration.class);
}
if (applicableConstructors.size() == 1) {
return SymbolReference.solved(applicableConstructors.get(0));
} else {
ResolvedConstructorDeclaration winningCandidate = applicableConstructors.get(0);
ResolvedConstructorDeclaration other = null;
boolean possibleAmbiguity = false;
for (int i = 1; i < applicableConstructors.size(); i++) {
other = applicableConstructors.get(i);
if (isMoreSpecific(winningCandidate, other, typeSolver)) {
possibleAmbiguity = false;
} else if (isMoreSpecific(other, winningCandidate, typeSolver)) {
possibleAmbiguity = false;
winningCandidate = other;
} else {
if (winningCandidate.declaringType().getQualifiedName().equals(other.declaringType().getQualifiedName())) {
possibleAmbiguity = true;
} else {
// we expect the methods to be ordered such that inherited methods are later in the list
}
}
}
if (possibleAmbiguity) {
// pick the first exact match if it exists
if (!MethodResolutionLogic.isExactMatch(winningCandidate, argumentsTypes)) {
if (MethodResolutionLogic.isExactMatch(other, argumentsTypes)) {
winningCandidate = other;
} else {
throw new MethodAmbiguityException("Ambiguous constructor call: cannot find a most applicable constructor: " + winningCandidate + ", " + other);
}
}
}
return SymbolReference.solved(winningCandidate);
}
}
use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.
the class Issue232 method issue232.
@Test
public void issue232() {
CompilationUnit cu = parseSample("Issue232");
ClassOrInterfaceDeclaration cls = Navigator.demandClassOrInterface(cu, "OfDouble");
TypeSolver typeSolver = new ReflectionTypeSolver();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
Context context = JavaParserFactory.getContext(cls, typeSolver);
SymbolReference<ResolvedTypeDeclaration> reference = context.solveType("OfPrimitive<Double, DoubleConsumer, OfDouble>", typeSolver);
}
use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.
the class Issue251 method testSolveStaticallyImportedMemberType.
@Test
public void testSolveStaticallyImportedMemberType() {
CompilationUnit cu = parseSample("Issue251");
ClassOrInterfaceDeclaration cls = Navigator.demandClassOrInterface(cu, "Main");
TypeSolver typeSolver = new ReflectionTypeSolver();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
MethodDeclaration m = Navigator.demandMethod(cls, "bar");
ExpressionStmt stmt = (ExpressionStmt) m.getBody().get().getStatements().get(1);
MethodCallExpr expression = (MethodCallExpr) stmt.getExpression();
Assert.assertNotNull(javaParserFacade.solve(expression));
}
use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.
the class JavassistInterfaceDeclaration method solveMethod.
@Deprecated
public SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> argumentsTypes, boolean staticOnly) {
List<ResolvedMethodDeclaration> candidates = new ArrayList<>();
Predicate<CtMethod> staticOnlyCheck = m -> !staticOnly || (staticOnly && Modifier.isStatic(m.getModifiers()));
for (CtMethod method : ctClass.getDeclaredMethods()) {
boolean isSynthetic = method.getMethodInfo().getAttribute(SyntheticAttribute.tag) != null;
boolean isNotBridge = (method.getMethodInfo().getAccessFlags() & AccessFlag.BRIDGE) == 0;
if (method.getName().equals(name) && !isSynthetic && isNotBridge && staticOnlyCheck.test(method)) {
candidates.add(new JavassistMethodDeclaration(method, typeSolver));
}
}
try {
CtClass superClass = ctClass.getSuperclass();
if (superClass != null) {
SymbolReference<ResolvedMethodDeclaration> ref = new JavassistClassDeclaration(superClass, typeSolver).solveMethod(name, argumentsTypes, staticOnly);
if (ref.isSolved()) {
candidates.add(ref.getCorrespondingDeclaration());
}
}
} catch (NotFoundException e) {
throw new RuntimeException(e);
}
try {
for (CtClass interfaze : ctClass.getInterfaces()) {
SymbolReference<ResolvedMethodDeclaration> ref = new JavassistInterfaceDeclaration(interfaze, typeSolver).solveMethod(name, argumentsTypes, staticOnly);
if (ref.isSolved()) {
candidates.add(ref.getCorrespondingDeclaration());
}
}
} catch (NotFoundException e) {
throw new RuntimeException(e);
}
return MethodResolutionLogic.findMostApplicable(candidates, name, argumentsTypes, typeSolver);
}
use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.
the class ReflectionClassAdapter method getSuperClass.
public ReferenceTypeImpl getSuperClass() {
if (clazz.getGenericSuperclass() == null) {
return null;
}
java.lang.reflect.Type superType = clazz.getGenericSuperclass();
if (superType instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) superType;
List<ResolvedType> typeParameters = Arrays.stream(parameterizedType.getActualTypeArguments()).map((t) -> ReflectionFactory.typeUsageFor(t, typeSolver)).collect(Collectors.toList());
return new ReferenceTypeImpl(new ReflectionClassDeclaration(clazz.getSuperclass(), typeSolver), typeParameters, typeSolver);
}
return new ReferenceTypeImpl(new ReflectionClassDeclaration(clazz.getSuperclass(), typeSolver), typeSolver);
}
Aggregations