use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.
the class ReflectionEnumDeclaration method solveMethodAsUsage.
public Optional<MethodUsage> solveMethodAsUsage(String name, List<ResolvedType> parameterTypes, TypeSolver typeSolver, Context invokationContext, List<ResolvedType> typeParameterValues) {
Optional<MethodUsage> res = ReflectionMethodResolutionLogic.solveMethodAsUsage(name, parameterTypes, typeSolver, invokationContext, typeParameterValues, this, clazz);
if (res.isPresent()) {
// We have to replace method type typeParametersValues here
InferenceContext inferenceContext = new InferenceContext(MyObjectProvider.INSTANCE);
MethodUsage methodUsage = res.get();
int i = 0;
List<ResolvedType> parameters = new LinkedList<>();
for (ResolvedType actualType : parameterTypes) {
ResolvedType formalType = methodUsage.getParamType(i);
// We need to replace the class type typeParametersValues (while we derive the method ones)
parameters.add(inferenceContext.addPair(formalType, actualType));
i++;
}
try {
ResolvedType returnType = inferenceContext.addSingle(methodUsage.returnType());
for (int j = 0; j < parameters.size(); j++) {
methodUsage = methodUsage.replaceParamType(j, inferenceContext.resolve(parameters.get(j)));
}
methodUsage = methodUsage.replaceReturnType(inferenceContext.resolve(returnType));
return Optional.of(methodUsage);
} catch (ConfilictingGenericTypesException e) {
return Optional.empty();
}
} else {
return res;
}
}
use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.
the class JavassistEnumDeclaration method solveMethod.
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);
}
return MethodResolutionLogic.findMostApplicable(candidates, name, argumentsTypes, typeSolver);
}
use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.
the class TypeInference method instantiationInference.
public Optional<InstantiationSet> instantiationInference(List<Expression> argumentExpressions, ResolvedMethodDeclaration methodDeclaration) {
// if (methodCallExpr.getTypeArguments().isPresent()) {
// throw new IllegalArgumentException("Type inference unnecessary as type arguments have been specified");
// }
// Given a method invocation that provides no explicit type arguments, the process to determine whether a
// potentially applicable generic method m is applicable is as follows:
// - Where P1, ..., Pp (p ≥ 1) are the type parameters of m, let α1, ..., αp be inference variables, and
// let θ be the substitution [P1:=α1, ..., Pp:=αp].
List<ResolvedTypeParameterDeclaration> Ps = methodDeclaration.getTypeParameters();
List<InferenceVariable> alphas = InferenceVariable.instantiate(Ps);
Substitution theta = Substitution.empty();
for (int i = 0; i < Ps.size(); i++) {
theta = theta.withPair(Ps.get(0), alphas.get(0));
}
// - An initial bound set, B0, is constructed from the declared bounds of P1, ..., Pp, as described in §18.1.3.
BoundSet B0 = boundSetup(Ps, alphas);
// - For all i (1 ≤ i ≤ p), if Pi appears in the throws clause of m, then the bound throws αi is implied.
// These bounds, if any, are incorporated with B0 to produce a new bound set, B1.
BoundSet B1 = B0;
for (int i = 0; i < Ps.size(); i++) {
ResolvedTypeParameterDeclaration Pi = Ps.get(i);
if (appearInThrowsClause(Pi, methodDeclaration)) {
B1 = B1.withBound(new ThrowsBound(alphas.get(i)));
}
}
// - A set of constraint formulas, C, is constructed as follows.
//
// Let F1, ..., Fn be the formal parameter types of m, and let e1, ..., ek be the actual argument expressions
// of the invocation. Then:
List<ResolvedType> Fs = formalParameterTypes(methodDeclaration);
List<Expression> es = argumentExpressions;
Optional<ConstraintFormulaSet> C = Optional.empty();
if (!C.isPresent()) {
C = testForApplicabilityByStrictInvocation(Fs, es, theta);
}
if (!C.isPresent()) {
C = testForApplicabilityByLooseInvocation(Fs, es, theta);
}
if (!C.isPresent()) {
C = testForApplicabilityByVariableArityInvocation(Fs, es, theta);
}
if (!C.isPresent()) {
return Optional.empty();
}
// - C is reduced (§18.2) and the resulting bounds are incorporated with B1 to produce a new bound set, B2.
BoundSet resultingBounds = C.get().reduce(typeSolver);
BoundSet B2 = B1.incorporate(resultingBounds, typeSolver);
if (B2.containsFalse()) {
return Optional.empty();
}
Optional<InstantiationSet> instantiation = B2.performResolution(alphas, typeSolver);
return instantiation;
}
use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.
the class ContextTest method resolveReferenceToLambdaParam.
@Test
public void resolveReferenceToLambdaParam() throws ParseException, IOException {
CompilationUnit cu = parseSample("Navigator");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Navigator");
MethodDeclaration method = Navigator.demandMethod(clazz, "findType");
MethodCallExpr callToGetName = Navigator.findMethodCall(method, "getName").get();
Expression referenceToT = callToGetName.getScope().get();
String pathToJar = adaptPath("src/test/resources/javaparser-core-2.1.0.jar");
TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JarTypeSolver(pathToJar));
ResolvedType typeOfT = JavaParserFacade.get(typeSolver).getType(referenceToT);
assertEquals("? super com.github.javaparser.ast.body.TypeDeclaration", typeOfT.describe());
}
use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.
the class ContextTest method resolveReferenceToLambdaParamBase.
@Test
public void resolveReferenceToLambdaParamBase() {
CompilationUnit cu = parseSample("NavigatorSimplified");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Navigator");
MethodDeclaration method = Navigator.demandMethod(clazz, "findType");
NameExpr refToT = Navigator.findNameExpression(method, "t").get();
TypeSolver typeSolver = new ReflectionTypeSolver();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
ResolvedType ref = javaParserFacade.getType(refToT);
assertEquals("? super java.lang.String", ref.describe());
}
Aggregations