Search in sources :

Example 1 with ITypeResolver

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;
}
Also used : JavaFieldInfo(com.redhat.qute.commons.JavaFieldInfo) HashMap(java.util.HashMap) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) ResolvedJavaTypeInfo(com.redhat.qute.commons.ResolvedJavaTypeInfo) ITypeResolver(com.redhat.qute.jdt.internal.resolver.ITypeResolver) IField(org.eclipse.jdt.core.IField) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) JavaModelException(org.eclipse.jdt.core.JavaModelException) IType(org.eclipse.jdt.core.IType) JavaMethodInfo(com.redhat.qute.commons.JavaMethodInfo) ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) IJavaProject(org.eclipse.jdt.core.IJavaProject) InvalidMethodReason(com.redhat.qute.commons.InvalidMethodReason) IMethod(org.eclipse.jdt.core.IMethod)

Example 2 with ITypeResolver

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);
    }
}
Also used : ITypeResolver(com.redhat.qute.jdt.internal.resolver.ITypeResolver)

Example 3 with ITypeResolver

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);
    }
}
Also used : IAnnotation(org.eclipse.jdt.core.IAnnotation) JavaModelException(org.eclipse.jdt.core.JavaModelException) ITypeResolver(com.redhat.qute.jdt.internal.resolver.ITypeResolver) IMethod(org.eclipse.jdt.core.IMethod)

Aggregations

ITypeResolver (com.redhat.qute.jdt.internal.resolver.ITypeResolver)3 IMethod (org.eclipse.jdt.core.IMethod)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 InvalidMethodReason (com.redhat.qute.commons.InvalidMethodReason)1 JavaFieldInfo (com.redhat.qute.commons.JavaFieldInfo)1 JavaMethodInfo (com.redhat.qute.commons.JavaMethodInfo)1 ResolvedJavaTypeInfo (com.redhat.qute.commons.ResolvedJavaTypeInfo)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 CoreException (org.eclipse.core.runtime.CoreException)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 IAnnotation (org.eclipse.jdt.core.IAnnotation)1 IField (org.eclipse.jdt.core.IField)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 IType (org.eclipse.jdt.core.IType)1 ITypeHierarchy (org.eclipse.jdt.core.ITypeHierarchy)1