Search in sources :

Example 1 with JavaFieldInfo

use of com.redhat.qute.commons.JavaFieldInfo in project quarkus-ls by redhat-developer.

the class QuteCompletionsForExpression method fillCompletionFields.

/**
 * Fill completion list <code>list</code> with the methods of the given Java
 * type <code>resolvedType</code>.
 *
 * @param resolvedType       the Java type class, interface.
 * @param range              the range.
 * @param projectUri         the project Uri
 * @param existingProperties the existing properties and method, field
 *                           signature.
 * @param list               the completion list.
 */
private void fillCompletionFields(ResolvedJavaTypeInfo resolvedType, Range range, String projectUri, Set<String> existingProperties, CompletionList list) {
    // Fill completion with Java type fields.
    for (JavaFieldInfo field : resolvedType.getFields()) {
        String fieldName = field.getName();
        // class B {String foo;}
        if (!existingProperties.contains(fieldName)) {
            fillCompletionField(field, range, list);
            existingProperties.add(fieldName);
        }
    }
    // Fill completion with extended Java type fields.
    List<String> extendedTypes = resolvedType.getExtendedTypes();
    if (extendedTypes != null) {
        for (String extendedType : extendedTypes) {
            ResolvedJavaTypeInfo resolvedExtendedType = javaCache.resolveJavaType(extendedType, projectUri).getNow(null);
            if (resolvedExtendedType != null) {
                fillCompletionFields(resolvedExtendedType, range, projectUri, existingProperties, list);
            }
        }
    }
}
Also used : JavaFieldInfo(com.redhat.qute.commons.JavaFieldInfo) ResolvedJavaTypeInfo(com.redhat.qute.commons.ResolvedJavaTypeInfo)

Example 2 with JavaFieldInfo

use of com.redhat.qute.commons.JavaFieldInfo 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 3 with JavaFieldInfo

use of com.redhat.qute.commons.JavaFieldInfo in project quarkus-ls by redhat-developer.

the class MockQuteProject method registerField.

protected static JavaMemberInfo registerField(String fieldSignature, ResolvedJavaTypeInfo resolvedType) {
    JavaFieldInfo member = new JavaFieldInfo();
    member.setSignature(fieldSignature);
    resolvedType.getFields().add(member);
    return member;
}
Also used : JavaFieldInfo(com.redhat.qute.commons.JavaFieldInfo)

Example 4 with JavaFieldInfo

use of com.redhat.qute.commons.JavaFieldInfo in project quarkus-ls by redhat-developer.

the class MockQuteProjectRegistry method getJavaDefinition.

@Override
public CompletableFuture<Location> getJavaDefinition(QuteJavaDefinitionParams params) {
    MockQuteProject project = (MockQuteProject) getProject(params.getProjectUri());
    if (project == null) {
        return CompletableFuture.completedFuture(null);
    }
    String className = params.getSourceType();
    ResolvedJavaTypeInfo classInfo = project.getResolvedJavaTypeSync(className);
    if (classInfo != null) {
        Range definitionRange = null;
        String fieldName = params.getSourceField();
        if (fieldName != null) {
            // Definition for field
            JavaFieldInfo fieldInfo = findField(classInfo, fieldName);
            if (fieldInfo != null) {
                definitionRange = JAVA_FIELD_RANGE;
            }
        } else {
            // Definition for method
            String methodName = params.getSourceMethod();
            if (methodName != null) {
                JavaMethodInfo methodInfo = findMethod(classInfo, methodName);
                if (methodInfo != null) {
                    definitionRange = JAVA_METHOD_RANGE;
                }
            } else {
                // Definition for class
                definitionRange = JAVA_CLASS_RANGE;
            }
        }
        if (definitionRange != null) {
            int index = className.indexOf('<');
            if (index != -1) {
                // ex : java.util.List<E>
                // Remove generic
                className = className.substring(0, index);
            }
            // ex : java/util/List.java
            String javeFileUri = className.replaceAll("[.]", "/") + ".java";
            Location location = new Location(javeFileUri, definitionRange);
            return CompletableFuture.completedFuture(location);
        }
    }
    return CompletableFuture.completedFuture(null);
}
Also used : JavaMethodInfo(com.redhat.qute.commons.JavaMethodInfo) JavaFieldInfo(com.redhat.qute.commons.JavaFieldInfo) ResolvedJavaTypeInfo(com.redhat.qute.commons.ResolvedJavaTypeInfo) Range(org.eclipse.lsp4j.Range) Location(org.eclipse.lsp4j.Location)

Aggregations

JavaFieldInfo (com.redhat.qute.commons.JavaFieldInfo)4 ResolvedJavaTypeInfo (com.redhat.qute.commons.ResolvedJavaTypeInfo)3 JavaMethodInfo (com.redhat.qute.commons.JavaMethodInfo)2 InvalidMethodReason (com.redhat.qute.commons.InvalidMethodReason)1 ITypeResolver (com.redhat.qute.jdt.internal.resolver.ITypeResolver)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 IField (org.eclipse.jdt.core.IField)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 IMethod (org.eclipse.jdt.core.IMethod)1 IType (org.eclipse.jdt.core.IType)1 ITypeHierarchy (org.eclipse.jdt.core.ITypeHierarchy)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 Location (org.eclipse.lsp4j.Location)1 Range (org.eclipse.lsp4j.Range)1