use of com.github.javaparser.resolution.types.ResolvedReferenceType in project javaparser by javaparser.
the class ReflectionClassDeclaration method solveMethod.
@Deprecated
public SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> argumentsTypes, boolean staticOnly) {
List<ResolvedMethodDeclaration> methods = new ArrayList<>();
Predicate<Method> staticFilter = m -> !staticOnly || (staticOnly && Modifier.isStatic(m.getModifiers()));
for (Method method : Arrays.stream(clazz.getDeclaredMethods()).filter((m) -> m.getName().equals(name)).filter(staticFilter).sorted(new MethodComparator()).collect(Collectors.toList())) {
if (method.isBridge() || method.isSynthetic())
continue;
ResolvedMethodDeclaration methodDeclaration = new ReflectionMethodDeclaration(method, typeSolver);
methods.add(methodDeclaration);
}
if (getSuperClass() != null) {
ResolvedClassDeclaration superClass = (ResolvedClassDeclaration) getSuperClass().getTypeDeclaration();
SymbolReference<ResolvedMethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(superClass, name, argumentsTypes, staticOnly, typeSolver);
if (ref.isSolved()) {
methods.add(ref.getCorrespondingDeclaration());
}
}
for (ResolvedReferenceType interfaceDeclaration : getInterfaces()) {
SymbolReference<ResolvedMethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(interfaceDeclaration.getTypeDeclaration(), name, argumentsTypes, staticOnly, typeSolver);
if (ref.isSolved()) {
methods.add(ref.getCorrespondingDeclaration());
}
}
return MethodResolutionLogic.findMostApplicable(methods, name, argumentsTypes, typeSolver);
}
use of com.github.javaparser.resolution.types.ResolvedReferenceType in project javaparser by javaparser.
the class ReflectionClassDeclaration method solveMethodAsUsage.
public Optional<MethodUsage> solveMethodAsUsage(String name, List<ResolvedType> argumentsTypes, TypeSolver typeSolver, Context invokationContext, List<ResolvedType> typeParameterValues) {
List<MethodUsage> methods = new ArrayList<>();
for (Method method : Arrays.stream(clazz.getDeclaredMethods()).filter((m) -> m.getName().equals(name)).sorted(new MethodComparator()).collect(Collectors.toList())) {
if (method.isBridge() || method.isSynthetic())
continue;
ResolvedMethodDeclaration methodDeclaration = new ReflectionMethodDeclaration(method, typeSolver);
MethodUsage methodUsage = new MethodUsage(methodDeclaration);
for (int i = 0; i < getTypeParameters().size() && i < typeParameterValues.size(); i++) {
ResolvedTypeParameterDeclaration tpToReplace = getTypeParameters().get(i);
ResolvedType newValue = typeParameterValues.get(i);
methodUsage = methodUsage.replaceTypeParameter(tpToReplace, newValue);
}
methods.add(methodUsage);
}
if (getSuperClass() != null) {
ResolvedClassDeclaration superClass = (ResolvedClassDeclaration) getSuperClass().getTypeDeclaration();
Optional<MethodUsage> ref = ContextHelper.solveMethodAsUsage(superClass, name, argumentsTypes, typeSolver, invokationContext, typeParameterValues);
if (ref.isPresent()) {
methods.add(ref.get());
}
}
for (ResolvedReferenceType interfaceDeclaration : getInterfaces()) {
Optional<MethodUsage> ref = ContextHelper.solveMethodAsUsage(interfaceDeclaration.getTypeDeclaration(), name, argumentsTypes, typeSolver, invokationContext, typeParameterValues);
if (ref.isPresent()) {
methods.add(ref.get());
}
}
Optional<MethodUsage> ref = MethodResolutionLogic.findMostApplicableUsage(methods, name, argumentsTypes, typeSolver);
return ref;
}
use of com.github.javaparser.resolution.types.ResolvedReferenceType in project javaparser by javaparser.
the class ReflectionFactory method typeUsageFor.
public static ResolvedType typeUsageFor(java.lang.reflect.Type type, TypeSolver typeSolver) {
if (type instanceof java.lang.reflect.TypeVariable) {
java.lang.reflect.TypeVariable<?> tv = (java.lang.reflect.TypeVariable<?>) type;
boolean declaredOnClass = tv.getGenericDeclaration() instanceof java.lang.reflect.Type;
ResolvedTypeParameterDeclaration typeParameter = new ReflectionTypeParameter(tv, declaredOnClass, typeSolver);
return new ResolvedTypeVariable(typeParameter);
} else if (type instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) type;
ResolvedReferenceType rawType = typeUsageFor(pt.getRawType(), typeSolver).asReferenceType();
List<java.lang.reflect.Type> actualTypes = new ArrayList<>();
actualTypes.addAll(Arrays.asList(pt.getActualTypeArguments()));
// we consume the actual types
rawType = rawType.transformTypeParameters(tp -> typeUsageFor(actualTypes.remove(0), typeSolver)).asReferenceType();
return rawType;
} else if (type instanceof Class) {
Class<?> c = (Class<?>) type;
if (c.isPrimitive()) {
if (c.getName().equals(Void.TYPE.getName())) {
return ResolvedVoidType.INSTANCE;
} else {
return ResolvedPrimitiveType.byName(c.getName());
}
} else if (c.isArray()) {
return new ResolvedArrayType(typeUsageFor(c.getComponentType(), typeSolver));
} else {
return new ReferenceTypeImpl(typeDeclarationFor(c, typeSolver), typeSolver);
}
} else if (type instanceof GenericArrayType) {
GenericArrayType genericArrayType = (GenericArrayType) type;
return new ResolvedArrayType(typeUsageFor(genericArrayType.getGenericComponentType(), typeSolver));
} else if (type instanceof WildcardType) {
WildcardType wildcardType = (WildcardType) type;
if (wildcardType.getLowerBounds().length > 0 && wildcardType.getUpperBounds().length > 0) {
if (wildcardType.getUpperBounds().length == 1 && wildcardType.getUpperBounds()[0].getTypeName().equals("java.lang.Object")) {
// ok, it does not matter
}
}
if (wildcardType.getLowerBounds().length > 0) {
if (wildcardType.getLowerBounds().length > 1) {
throw new UnsupportedOperationException();
}
return ResolvedWildcard.superBound(typeUsageFor(wildcardType.getLowerBounds()[0], typeSolver));
}
if (wildcardType.getUpperBounds().length > 0) {
if (wildcardType.getUpperBounds().length > 1) {
throw new UnsupportedOperationException();
}
return ResolvedWildcard.extendsBound(typeUsageFor(wildcardType.getUpperBounds()[0], typeSolver));
}
return ResolvedWildcard.UNBOUNDED;
} else {
throw new UnsupportedOperationException(type.getClass().getCanonicalName() + " " + type);
}
}
use of com.github.javaparser.resolution.types.ResolvedReferenceType 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.types.ResolvedReferenceType in project javaparser by javaparser.
the class ReflectionClassAdapter method getInterfaces.
public List<ResolvedReferenceType> getInterfaces() {
List<ResolvedReferenceType> interfaces = new ArrayList<>();
for (java.lang.reflect.Type superInterface : clazz.getGenericInterfaces()) {
if (superInterface instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) superInterface;
List<ResolvedType> typeParameters = Arrays.stream(parameterizedType.getActualTypeArguments()).map((t) -> ReflectionFactory.typeUsageFor(t, typeSolver)).collect(Collectors.toList());
interfaces.add(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration((Class<?>) ((ParameterizedType) superInterface).getRawType(), typeSolver), typeParameters, typeSolver));
} else {
interfaces.add(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration((Class<?>) superInterface, typeSolver), typeSolver));
}
}
return interfaces;
}
Aggregations