use of com.github.javaparser.resolution.types.ResolvedType 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.resolution.types.ResolvedType in project javaparser by javaparser.
the class Issue84 method variadicIssue.
@Test
public void variadicIssue() {
CompilationUnit cu = parseSample("Issue84");
final MethodCallExpr methodCall = Navigator.findMethodCall(cu, "variadicMethod").get();
final JavaParserFacade javaParserFacade = JavaParserFacade.get(new ReflectionTypeSolver());
final ResolvedType type = javaParserFacade.getType(methodCall);
assertEquals(String.class.getCanonicalName(), type.asReferenceType().getQualifiedName());
}
use of com.github.javaparser.resolution.types.ResolvedType 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.resolution.types.ResolvedType in project javaparser by javaparser.
the class ReferenceTypeImpl method transformTypeParameters.
/**
* Execute a transformation on all the type parameters of this element.
*/
@Override
public ResolvedType transformTypeParameters(ResolvedTypeTransformer transformer) {
ResolvedType result = this;
int i = 0;
for (ResolvedType tp : this.typeParametersValues()) {
ResolvedType transformedTp = transformer.transform(tp);
// Identity comparison on purpose
if (transformedTp != tp) {
List<ResolvedType> typeParametersCorrected = result.asReferenceType().typeParametersValues();
typeParametersCorrected.set(i, transformedTp);
result = create(typeDeclaration, typeParametersCorrected);
}
i++;
}
return result;
}
use of com.github.javaparser.resolution.types.ResolvedType 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