use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.
the class JavassistUtils method parseTypeParameters.
private static List<ResolvedType> parseTypeParameters(String signature, TypeSolver typeSolver, Context invokationContext) {
String originalSignature = signature;
if (signature.contains("<")) {
signature = signature.substring(signature.indexOf('<') + 1);
if (!signature.endsWith(">")) {
throw new IllegalArgumentException();
}
signature = signature.substring(0, signature.length() - 1);
if (signature.contains(",")) {
throw new UnsupportedOperationException();
}
if (signature.startsWith("?")) {
// TODO: check bounds
List<ResolvedType> types = new ArrayList<>();
types.add(ResolvedWildcard.UNBOUNDED);
return types;
}
List<ResolvedType> typeParameters = parseTypeParameters(signature, typeSolver, invokationContext);
if (signature.contains("<")) {
signature = signature.substring(0, signature.indexOf('<'));
}
if (signature.contains(">")) {
throw new UnsupportedOperationException();
}
ResolvedType type = new SymbolSolver(typeSolver).solveTypeUsage(signature, invokationContext);
if (type.isReferenceType() && typeParameters.size() > 0) {
type = type.asReferenceType().transformTypeParameters(tp -> typeParameters.remove(0));
}
List<ResolvedType> types = new ArrayList<>();
types.add(type);
return types;
} else {
return Collections.emptyList();
}
}
use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.
the class JavassistUtils method getMethodUsage.
static Optional<MethodUsage> getMethodUsage(CtClass ctClass, String name, List<ResolvedType> argumentsTypes, TypeSolver typeSolver, Context invokationContext) {
// TODO avoid bridge and synthetic methods
for (CtMethod method : ctClass.getDeclaredMethods()) {
if (method.getName().equals(name)) {
// TODO check typeParametersValues
MethodUsage methodUsage = new MethodUsage(new JavassistMethodDeclaration(method, typeSolver));
if (argumentsTypes.size() < methodUsage.getNoParams()) {
// this method cannot be a good candidate (except if variadic ?)
continue;
}
try {
if (method.getGenericSignature() != null) {
SignatureAttribute.MethodSignature methodSignature = SignatureAttribute.toMethodSignature(method.getGenericSignature());
List<ResolvedType> parametersOfReturnType = parseTypeParameters(methodSignature.getReturnType().toString(), typeSolver, invokationContext);
ResolvedType newReturnType = methodUsage.returnType();
// consume one parametersOfReturnType at the time
if (newReturnType.isReferenceType() && parametersOfReturnType.size() > 0) {
newReturnType = newReturnType.asReferenceType().transformTypeParameters(tp -> parametersOfReturnType.remove(0));
}
methodUsage = methodUsage.replaceReturnType(newReturnType);
}
return Optional.of(methodUsage);
} catch (BadBytecode e) {
throw new RuntimeException(e);
}
}
}
try {
CtClass superClass = ctClass.getSuperclass();
if (superClass != null) {
Optional<MethodUsage> ref = new JavassistClassDeclaration(superClass, typeSolver).solveMethodAsUsage(name, argumentsTypes, typeSolver, invokationContext, null);
if (ref.isPresent()) {
return ref;
}
}
} catch (NotFoundException e) {
throw new RuntimeException(e);
}
try {
for (CtClass interfaze : ctClass.getInterfaces()) {
Optional<MethodUsage> ref = new JavassistInterfaceDeclaration(interfaze, typeSolver).solveMethodAsUsage(name, argumentsTypes, typeSolver, invokationContext, null);
if (ref.isPresent()) {
return ref;
}
}
} catch (NotFoundException e) {
throw new RuntimeException(e);
}
return Optional.empty();
}
use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.
the class MyObjectProvider method byName.
@Override
public ResolvedReferenceType byName(String qualifiedName) {
TypeSolver typeSolver = new ReflectionTypeSolver();
ResolvedReferenceTypeDeclaration typeDeclaration = typeSolver.solveType(qualifiedName);
if (!typeDeclaration.getTypeParameters().isEmpty()) {
throw new UnsupportedOperationException();
}
return new ReferenceTypeImpl(typeDeclaration, typeSolver);
}
use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.
the class Issue116 method arrayTypeIsNotPartOfTheTree.
@Test
public void arrayTypeIsNotPartOfTheTree() {
CompilationUnit cu = parseSample("Issue116");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "JavaTest");
MethodDeclaration methodDeclaration = Navigator.demandMethod(clazz, "foo");
TypeSolver typeSolver = new ReflectionTypeSolver();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
com.github.javaparser.ast.type.Type typeNode = methodDeclaration.getParameters().get(0).getType();
ResolvedType type = javaParserFacade.convert(typeNode, typeNode);
assertEquals("java.lang.String[]", type.describe());
ExpressionStmt expressionStmt = (ExpressionStmt) methodDeclaration.getBody().get().getStatements().get(0);
Expression argRef = expressionStmt.getExpression();
assertEquals("java.lang.String[]", javaParserFacade.getType(argRef).describe());
}
use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.
the class Issue156 method testFieldAccessThroughClassAndThis.
@Test
public void testFieldAccessThroughClassAndThis() {
CompilationUnit cu = parseSample("Issue156");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Issue156");
List<MethodCallExpr> methods = clazz.getChildNodes().get(2).getChildNodes().get(1).findAll(MethodCallExpr.class);
TypeSolver typeSolver = new ReflectionTypeSolver();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
assertEquals("char", javaParserFacade.getType(methods.get(0)).describe());
}
Aggregations