use of com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration in project javaparser by javaparser.
the class Issue1364 method setup.
@Before
public void setup() {
ClassOrInterfaceDeclaration fakeObject = new ClassOrInterfaceDeclaration();
fakeObject.setName(new SimpleName("java.lang.Object"));
TypeSolver typeSolver = new TypeSolver() {
@Override
public TypeSolver getParent() {
return null;
}
@Override
public void setParent(TypeSolver parent) {
}
@Override
public SymbolReference<ResolvedReferenceTypeDeclaration> tryToSolveType(String name) {
if ("java.lang.Object".equals(name)) {
// custom handling
return SymbolReference.solved(new JavaParserClassDeclaration(fakeObject, this));
}
return SymbolReference.unsolved(ResolvedReferenceTypeDeclaration.class);
}
};
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(typeSolver));
javaParser = new JavaParser(config);
}
use of com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration in project javaparser by javaparser.
the class ReflectionMethodResolutionLogic method solveMethod.
static SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> parameterTypes, boolean staticOnly, TypeSolver typeSolver, ResolvedReferenceTypeDeclaration scopeType, Class clazz) {
List<ResolvedMethodDeclaration> methods = new ArrayList<>();
Predicate<Method> staticOnlyCheck = m -> !staticOnly || (staticOnly && Modifier.isStatic(m.getModifiers()));
for (Method method : clazz.getMethods()) {
if (method.isBridge() || method.isSynthetic() || !method.getName().equals(name) || !staticOnlyCheck.test(method))
continue;
ResolvedMethodDeclaration methodDeclaration = new ReflectionMethodDeclaration(method, typeSolver);
methods.add(methodDeclaration);
}
for (ResolvedReferenceType ancestor : scopeType.getAncestors()) {
SymbolReference<ResolvedMethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(ancestor.getTypeDeclaration(), name, parameterTypes, staticOnly, typeSolver);
if (ref.isSolved()) {
methods.add(ref.getCorrespondingDeclaration());
}
}
if (scopeType.getAncestors().isEmpty()) {
ReferenceTypeImpl objectClass = new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeSolver), typeSolver);
SymbolReference<ResolvedMethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(objectClass.getTypeDeclaration(), name, parameterTypes, staticOnly, typeSolver);
if (ref.isSolved()) {
methods.add(ref.getCorrespondingDeclaration());
}
}
return MethodResolutionLogic.findMostApplicable(methods, name, parameterTypes, typeSolver);
}
use of com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration in project javaparser by javaparser.
the class SymbolSolver method solveTypeUsage.
public ResolvedType solveTypeUsage(String name, Context context) {
Optional<ResolvedType> genericType = context.solveGenericType(name, typeSolver);
if (genericType.isPresent()) {
return genericType.get();
}
ResolvedReferenceTypeDeclaration typeDeclaration = typeSolver.solveType(name);
return new ReferenceTypeImpl(typeDeclaration, typeSolver);
}
use of com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration in project javaparser by javaparser.
the class ReferenceTypeImpl method getAllAncestors.
public List<ResolvedReferenceType> getAllAncestors() {
// We need to go through the inheritance line and propagate the type parametes
List<ResolvedReferenceType> ancestors = typeDeclaration.getAllAncestors();
ancestors = ancestors.stream().map(a -> typeParametersMap().replaceAll(a).asReferenceType()).collect(Collectors.toList());
// Avoid repetitions of Object
ancestors.removeIf(a -> a.getQualifiedName().equals(Object.class.getCanonicalName()));
ResolvedReferenceTypeDeclaration objectType = typeSolver.solveType(Object.class.getCanonicalName());
ResolvedReferenceType objectRef = create(objectType);
ancestors.add(objectRef);
return ancestors;
}
use of com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration in project javaparser by javaparser.
the class AbstractJavaParserContext method findTypeDeclarations.
protected Collection<ResolvedReferenceTypeDeclaration> findTypeDeclarations(Optional<Expression> optScope, TypeSolver typeSolver) {
if (optScope.isPresent()) {
Expression scope = optScope.get();
// consider static methods
if (scope instanceof NameExpr) {
NameExpr scopeAsName = (NameExpr) scope;
SymbolReference<ResolvedTypeDeclaration> symbolReference = this.solveType(scopeAsName.getName().getId(), typeSolver);
if (symbolReference.isSolved() && symbolReference.getCorrespondingDeclaration().isType()) {
return singletonList(symbolReference.getCorrespondingDeclaration().asReferenceType());
}
}
ResolvedType typeOfScope;
try {
typeOfScope = JavaParserFacade.get(typeSolver).getType(scope);
} catch (Exception e) {
throw new RuntimeException("Issue calculating the type of the scope of " + this, e);
}
if (typeOfScope.isWildcard()) {
if (typeOfScope.asWildcard().isExtends() || typeOfScope.asWildcard().isSuper()) {
return singletonList(typeOfScope.asWildcard().getBoundedType().asReferenceType().getTypeDeclaration());
} else {
return singletonList(new ReflectionClassDeclaration(Object.class, typeSolver).asReferenceType());
}
} else if (typeOfScope.isArray()) {
// method call on array are Object methods
return singletonList(new ReflectionClassDeclaration(Object.class, typeSolver).asReferenceType());
} else if (typeOfScope.isTypeVariable()) {
Collection<ResolvedReferenceTypeDeclaration> result = new ArrayList<>();
for (ResolvedTypeParameterDeclaration.Bound bound : typeOfScope.asTypeParameter().getBounds()) {
result.add(bound.getType().asReferenceType().getTypeDeclaration());
}
return result;
} else if (typeOfScope.isConstraint()) {
return singletonList(typeOfScope.asConstraintType().getBound().asReferenceType().getTypeDeclaration());
}
return singletonList(typeOfScope.asReferenceType().getTypeDeclaration());
}
ResolvedType typeOfScope = JavaParserFacade.get(typeSolver).getTypeOfThisIn(wrappedNode);
return singletonList(typeOfScope.asReferenceType().getTypeDeclaration());
}
Aggregations