use of com.redhat.qute.jdt.internal.resolver.ITypeResolver in project quarkus-ls by redhat-developer.
the class QuteSupportForTemplate method getResolvedJavaType.
/**
* Returns the resolved type (fields and methods) for the given Java type.
*
* @param params the Java type to resolve.
* @param utils the JDT LS utility.
* @param monitor the progress monitor.
*
* @return the resolved type (fields and methods) for the given Java type.
*
* @throws CoreException
*/
public ResolvedJavaTypeInfo getResolvedJavaType(QuteResolvedJavaTypeParams params, IJDTUtils utils, IProgressMonitor monitor) throws CoreException {
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
String projectUri = params.getProjectUri();
IJavaProject javaProject = getJavaProjectFromProjectUri(projectUri);
if (javaProject == null) {
return null;
}
String typeName = params.getClassName();
int index = typeName.indexOf('<');
if (index != -1) {
// ex : java.util.List<org.acme.Item>
String iterableClassName = typeName.substring(0, index);
IType iterableType = findType(iterableClassName, javaProject, monitor);
if (iterableType == null) {
return null;
}
boolean iterable = isIterable(iterableType, monitor);
if (!iterable) {
return null;
}
String iterableOf = typeName.substring(index + 1, typeName.length() - 1);
iterableOf = getFullQualifiedName(monitor, javaProject, iterableOf);
iterableClassName = iterableType.getFullyQualifiedName('.');
typeName = iterableClassName + "<" + iterableOf + ">";
return createIterableType(typeName, iterableClassName, iterableOf);
} else if (typeName.endsWith("[]")) {
// ex : org.acme.Item[]
String iterableOf = typeName.substring(0, typeName.length() - 2);
IType iterableOfType = findType(iterableOf, javaProject, monitor);
if (iterableOfType == null) {
return null;
}
iterableOf = getFullQualifiedName(monitor, javaProject, iterableOf);
typeName = iterableOf + "[]";
return createIterableType(typeName, null, iterableOf);
}
// ex : org.acme.Item, java.util.List, ...
IType type = findType(typeName, javaProject, monitor);
if (type == null) {
return null;
}
ITypeResolver typeResolver = createTypeResolver(type);
// 1) Collect fields
List<JavaFieldInfo> fieldsInfo = new ArrayList<>();
// Standard fields
IField[] fields = type.getFields();
for (IField field : fields) {
if (isValidField(field)) {
// Only public fields are available
JavaFieldInfo info = new JavaFieldInfo();
info.setSignature(typeResolver.resolveFieldSignature(field));
fieldsInfo.add(info);
}
}
// Record fields
if (type.isRecord()) {
for (IField field : type.getRecordComponents()) {
// All record components are valid
JavaFieldInfo info = new JavaFieldInfo();
info.setSignature(typeResolver.resolveFieldSignature(field));
fieldsInfo.add(info);
}
}
// 2) Collect methods
List<JavaMethodInfo> methodsInfo = new ArrayList<>();
Map<String, InvalidMethodReason> invalidMethods = new HashMap<>();
IMethod[] methods = type.getMethods();
for (IMethod method : methods) {
if (isValidMethod(method, type.isInterface())) {
try {
InvalidMethodReason invalid = getValidMethodForQute(method, typeName);
if (invalid != null) {
invalidMethods.put(method.getElementName(), invalid);
} else {
JavaMethodInfo info = new JavaMethodInfo();
info.setSignature(typeResolver.resolveMethodSignature(method));
methodsInfo.add(info);
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error while getting method signature of '" + method.getElementName() + "'.", e);
}
}
}
// Collect type extensions
List<String> extendedTypes = null;
if (type.isInterface()) {
IType[] interfaces = findImplementedInterfaces(type, monitor);
if (interfaces != null && interfaces.length > 0) {
extendedTypes = //
Stream.of(interfaces).map(//
interfaceType -> interfaceType.getFullyQualifiedName()).collect(Collectors.toList());
}
} else {
// ex : String implements CharSequence, ....
ITypeHierarchy typeHierarchy = type.newSupertypeHierarchy(monitor);
IType[] allSuperTypes = typeHierarchy.getSupertypes(type);
extendedTypes = //
Stream.of(allSuperTypes).map(//
superType -> superType.getFullyQualifiedName()).collect(Collectors.toList());
}
if (extendedTypes != null) {
extendedTypes.remove(typeName);
}
ResolvedJavaTypeInfo resolvedType = new ResolvedJavaTypeInfo();
String typeSignature = AbstractTypeResolver.resolveJavaTypeSignature(type);
resolvedType.setSignature(typeSignature);
resolvedType.setFields(fieldsInfo);
resolvedType.setMethods(methodsInfo);
resolvedType.setInvalidMethods(invalidMethods);
resolvedType.setExtendedTypes(extendedTypes);
return resolvedType;
}
use of com.redhat.qute.jdt.internal.resolver.ITypeResolver in project quarkus-ls by redhat-developer.
the class TemplateExtensionSupport method collectResolversForTemplateExtension.
public static void collectResolversForTemplateExtension(IMethod method, IAnnotation templateExtension, List<ValueResolverInfo> resolvers, IProgressMonitor monitor) {
if (isTemplateExtensionMethod(method)) {
ITypeResolver typeResolver = QuteSupportForTemplate.createTypeResolver(method);
collectResolversForTemplateExtension(method, templateExtension, resolvers, typeResolver);
}
}
use of com.redhat.qute.jdt.internal.resolver.ITypeResolver in project quarkus-ls by redhat-developer.
the class TemplateExtensionSupport method collectResolversForTemplateExtension.
private static void collectResolversForTemplateExtension(IType type, IAnnotation templateExtension, List<ValueResolverInfo> resolvers, IProgressMonitor monitor) {
try {
ITypeResolver typeResolver = QuteSupportForTemplate.createTypeResolver(type);
IMethod[] methods = type.getMethods();
for (IMethod method : methods) {
if (isTemplateExtensionMethod(method)) {
IAnnotation methodTemplateExtension = AnnotationUtils.getAnnotation(method, TEMPLATE_EXTENSION_ANNOTATION);
collectResolversForTemplateExtension(method, methodTemplateExtension != null ? methodTemplateExtension : templateExtension, resolvers, typeResolver);
}
}
} catch (JavaModelException e) {
LOGGER.log(Level.SEVERE, "Error while getting methods of '" + type.getElementName() + "'.", e);
}
}
Aggregations