use of com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration in project javaparser by javaparser.
the class JavaParserTypeParameterResolutionTest method declaredOnMethodPositiveCase.
@Test
public void declaredOnMethodPositiveCase() {
CompilationUnit cu = parseSample("MethodTypeParameter");
TypeSolver typeSolver = new ReflectionTypeSolver();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
ClassOrInterfaceDeclaration classDecl = Navigator.demandClass(cu, "Foo");
MethodDeclaration methodDecl = Navigator.demandMethod(classDecl, "usage");
MethodCallExpr callToFoo = (MethodCallExpr) Navigator.findReturnStmt(methodDecl).getExpression().get();
ResolvedMethodDeclaration methodDeclaration = javaParserFacade.solve(callToFoo).getCorrespondingDeclaration();
for (ResolvedTypeParameterDeclaration tp : methodDeclaration.getTypeParameters()) {
assertTrue(tp instanceof JavaParserTypeParameter);
assertEquals("C", tp.getName());
assertEquals(true, tp.declaredOnMethod());
assertEquals(false, tp.declaredOnType());
}
}
use of com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration in project javaparser by javaparser.
the class ConstraintFormulaTest method testExpressionCompatibleWithTypeReduce1.
/**
* From JLS 18.1.2
*
* From Collections.singleton("hi"), we have the constraint formula ‹"hi" → α›.
* Through reduction, this will become the constraint formula: ‹String <: α›.
*/
@Test
public void testExpressionCompatibleWithTypeReduce1() {
ResolvedTypeParameterDeclaration tp = mock(ResolvedTypeParameterDeclaration.class);
Expression e = new StringLiteralExpr("hi");
InferenceVariable inferenceVariable = new InferenceVariable("α", tp);
ExpressionCompatibleWithType formula = new ExpressionCompatibleWithType(typeSolver, e, inferenceVariable);
ConstraintFormula.ReductionResult res1 = formula.reduce(BoundSet.empty());
assertEquals(ConstraintFormula.ReductionResult.empty().withConstraint(new TypeCompatibleWithType(typeSolver, stringType, inferenceVariable)), res1);
assertEquals(ConstraintFormula.ReductionResult.empty().withConstraint(new TypeSubtypeOfType(typeSolver, stringType, inferenceVariable)), res1.getConstraint(0).reduce(BoundSet.empty()));
}
use of com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration in project javaparser by javaparser.
the class SubtypeOfBoundTest method recognizeProperUpperBound2.
@Test
public void recognizeProperUpperBound2() {
ResolvedTypeParameterDeclaration typeParameterDeclaration1 = mock(ResolvedTypeParameterDeclaration.class);
ResolvedTypeParameterDeclaration typeParameterDeclaration2 = mock(ResolvedTypeParameterDeclaration.class);
// { α <: Iterable<?>, β <: Object, α <: List<β> } describes a proper upper bound for each of α and β, along with a dependency between them.
InferenceVariable alpha = new InferenceVariable("α", typeParameterDeclaration1);
InferenceVariable beta = new InferenceVariable("β", typeParameterDeclaration2);
ResolvedType iterableOfWildcard = new ReferenceTypeImpl(iterableType.getTypeDeclaration(), Arrays.asList(ResolvedWildcard.UNBOUNDED), typeSolver);
ResolvedType listOfBeta = new ReferenceTypeImpl(listType.getTypeDeclaration(), Arrays.asList(beta), typeSolver);
Bound bound1 = new SubtypeOfBound(alpha, iterableOfWildcard);
Bound bound2 = new SubtypeOfBound(beta, objectType);
Bound bound3 = new SubtypeOfBound(alpha, listOfBeta);
assertEquals(false, isProperType(listOfBeta));
assertEquals(Optional.of(new ProperUpperBound(alpha, iterableOfWildcard)), bound1.isProperUpperBound());
assertEquals(Optional.of(new ProperUpperBound(beta, objectType)), bound2.isProperUpperBound());
assertEquals(true, bound3.isADependency());
}
use of com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration in project javaparser by javaparser.
the class MethodUsage method replaceTypeParameter.
public MethodUsage replaceTypeParameter(ResolvedTypeParameterDeclaration typeParameter, ResolvedType type) {
if (type == null) {
throw new IllegalArgumentException();
}
// TODO if the method declaration has a type param with that name ignore this call
MethodUsage res = new MethodUsage(declaration, paramTypes, returnType, exceptionTypes, typeParametersMap.toBuilder().setValue(typeParameter, type).build());
Map<ResolvedTypeParameterDeclaration, ResolvedType> inferredTypes = new HashMap<>();
for (int i = 0; i < paramTypes.size(); i++) {
ResolvedType originalParamType = paramTypes.get(i);
ResolvedType newParamType = originalParamType.replaceTypeVariables(typeParameter, type, inferredTypes);
res = res.replaceParamType(i, newParamType);
}
for (int i = 0; i < exceptionTypes.size(); i++) {
ResolvedType originalType = exceptionTypes.get(i);
ResolvedType newType = originalType.replaceTypeVariables(typeParameter, type, inferredTypes);
res = res.replaceExceptionType(i, newType);
}
ResolvedType oldReturnType = res.returnType;
ResolvedType newReturnType = oldReturnType.replaceTypeVariables(typeParameter, type, inferredTypes);
res = res.replaceReturnType(newReturnType);
return res;
}
use of com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration in project javaparser by javaparser.
the class ReflectionMethodResolutionLogic method replaceParams.
private static MethodUsage replaceParams(List<ResolvedType> typeParameterValues, ResolvedReferenceTypeDeclaration typeParametrizable, ResolvedMethodDeclaration methodDeclaration) {
MethodUsage methodUsage = new MethodUsage(methodDeclaration);
int i = 0;
// Only replace if we have enough values provided
if (typeParameterValues.size() == typeParametrizable.getTypeParameters().size()) {
for (ResolvedTypeParameterDeclaration tp : typeParametrizable.getTypeParameters()) {
methodUsage = methodUsage.replaceTypeParameter(tp, typeParameterValues.get(i));
i++;
}
}
for (ResolvedTypeParameterDeclaration methodTypeParameter : methodDeclaration.getTypeParameters()) {
methodUsage = methodUsage.replaceTypeParameter(methodTypeParameter, new ResolvedTypeVariable(methodTypeParameter));
}
return methodUsage;
}
Aggregations