JavaDescriptorResolver.java 23.3 KB
Newer Older
1 2
package org.jetbrains.jet.lang.resolve.java;

3
import com.google.common.collect.Lists;
A
Andrey Breslav 已提交
4
import com.google.common.collect.Maps;
A
Andrey Breslav 已提交
5
import com.google.common.collect.Sets;
6 7 8 9 10
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
A
Andrey Breslav 已提交
11
import org.jetbrains.jet.lang.descriptors.*;
A
Andrey Breslav 已提交
12
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
13
import org.jetbrains.jet.lang.resolve.BindingContext;
14
import org.jetbrains.jet.lang.resolve.BindingTrace;
15
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
16 17 18 19 20 21 22 23
import org.jetbrains.jet.lang.types.*;

import java.util.*;

/**
 * @author abreslav
 */
public class JavaDescriptorResolver {
24 25
    
    public static String JAVA_ROOT = "<java_root>";
26

27 28
    /*package*/ static final DeclarationDescriptor JAVA_METHOD_TYPE_PARAMETER_PARENT = new DeclarationDescriptorImpl(null, Collections.<AnnotationDescriptor>emptyList(), "<java_generic_method>") {

29 30
        @Override
        public DeclarationDescriptor substitute(TypeSubstitutor substitutor) {
31
            throw new UnsupportedOperationException();
32 33
        }

34 35
        @Override
        public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
36
            return visitor.visitDeclarationDescriptor(this, data);
37 38 39
        }
    };

A
Andrey Breslav 已提交
40
    /*package*/ static final DeclarationDescriptor JAVA_CLASS_OBJECT = new DeclarationDescriptorImpl(null, Collections.<AnnotationDescriptor>emptyList(), "<java_class_object_emulation>") {
A
Andrey Breslav 已提交
41 42 43
        @NotNull
        @Override
        public DeclarationDescriptor substitute(TypeSubstitutor substitutor) {
44
            throw new UnsupportedOperationException();
A
Andrey Breslav 已提交
45 46 47 48 49 50 51 52
        }

        @Override
        public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
            return visitor.visitDeclarationDescriptor(this, data);
        }
    };

53
    protected final Map<String, ClassDescriptor> classDescriptorCache = new HashMap<String, ClassDescriptor>();
A
Andrey Breslav 已提交
54
    protected final Map<PsiTypeParameter, TypeParameterDescriptor> typeParameterDescriptorCache = Maps.newHashMap();
A
Andrey Breslav 已提交
55
    protected final Map<PsiMethod, FunctionDescriptor> methodDescriptorCache = Maps.newHashMap();
A
Andrey Breslav 已提交
56
    protected final Map<PsiField, VariableDescriptor> fieldDescriptorCache = Maps.newHashMap();
A
Andrey Breslav 已提交
57
    protected final Map<PsiElement, NamespaceDescriptor> namespaceDescriptorCache = Maps.newHashMap();
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    protected final JavaPsiFacade javaFacade;
    protected final GlobalSearchScope javaSearchScope;
    protected final JavaSemanticServices semanticServices;

    public JavaDescriptorResolver(Project project, JavaSemanticServices semanticServices) {
        this.javaFacade = JavaPsiFacade.getInstance(project);
        this.javaSearchScope = GlobalSearchScope.allScope(project);
        this.semanticServices = semanticServices;
    }

    @NotNull
    public ClassDescriptor resolveClass(@NotNull PsiClass psiClass) {
        String qualifiedName = psiClass.getQualifiedName();
        ClassDescriptor classDescriptor = classDescriptorCache.get(qualifiedName);
        if (classDescriptor == null) {
            classDescriptor = createJavaClassDescriptor(psiClass);
            classDescriptorCache.put(qualifiedName, classDescriptor);
        }
        return classDescriptor;
    }

    @Nullable
    public ClassDescriptor resolveClass(@NotNull String qualifiedName) {
        ClassDescriptor classDescriptor = classDescriptorCache.get(qualifiedName);
        if (classDescriptor == null) {
            PsiClass psiClass = javaFacade.findClass(qualifiedName, javaSearchScope);
            if (psiClass == null) {
                return null;
            }
            classDescriptor = createJavaClassDescriptor(psiClass);
        }
        return classDescriptor;
    }

92
    private ClassDescriptor createJavaClassDescriptor(@NotNull final PsiClass psiClass) {
A
Andrey Breslav 已提交
93 94 95
        assert !classDescriptorCache.containsKey(psiClass.getQualifiedName()) : psiClass.getQualifiedName();
        classDescriptorCache.put(psiClass.getQualifiedName(), null); // TODO

96
        String name = psiClass.getName();
97
        JavaClassDescriptor classDescriptor = new JavaClassDescriptor(
98
                resolveParentDescriptor(psiClass), psiClass.isInterface() ? ClassKind.TRAIT : ClassKind.CLASS
99
        );
A
Andrey Breslav 已提交
100
        classDescriptor.setName(name);
A
Andrey Breslav 已提交
101

A
Andrey Breslav 已提交
102
        List<JetType> supertypes = new ArrayList<JetType>();
A
Andrey Breslav 已提交
103
        List<TypeParameterDescriptor> typeParameters = resolveTypeParameters(classDescriptor, psiClass.getTypeParameters());
A
Andrey Breslav 已提交
104 105
        classDescriptor.setTypeConstructor(new TypeConstructorImpl(
                classDescriptor,
A
Andrey Breslav 已提交
106
                Collections.<AnnotationDescriptor>emptyList(), // TODO
107
                // TODO
108
                psiClass.hasModifierProperty(PsiModifier.FINAL),
A
Andrey Breslav 已提交
109
                name,
A
Andrey Breslav 已提交
110
                typeParameters,
A
Andrey Breslav 已提交
111 112
                supertypes
        ));
113 114 115
        classDescriptor.setModality(Modality.convertFromFlags(
                psiClass.hasModifierProperty(PsiModifier.ABSTRACT) || psiClass.isInterface(),
                !psiClass.hasModifierProperty(PsiModifier.FINAL))
A
Andrey Breslav 已提交
116
        );
117
        classDescriptor.setVisibility(resolveVisibilityFromPsiModifiers(semanticServices.getTrace(), psiClass));
A
Andrey Breslav 已提交
118
        classDescriptorCache.put(psiClass.getQualifiedName(), classDescriptor);
119
        classDescriptor.setUnsubstitutedMemberScope(new JavaClassMembersScope(classDescriptor, psiClass, semanticServices, false));
A
Andrey Breslav 已提交
120
//        classDescriptor.setClassObjectMemberScope(new JavaClassMembersScope(classDescriptor, psiClass, semanticServices, true));
A
Andrey Breslav 已提交
121 122
        // UGLY HACK
        supertypes.addAll(getSupertypes(psiClass));
123 124 125 126 127 128 129 130 131 132 133
        if (psiClass.isInterface()) {
            classDescriptor.setSuperclassType(JetStandardClasses.getAnyType()); // TODO : Make it java.lang.Object
        }
        else {
            PsiClassType[] extendsListTypes = psiClass.getExtendsListTypes();
            assert extendsListTypes.length == 0 || extendsListTypes.length == 1;
            JetType superclassType = extendsListTypes.length == 0
                                            ? JetStandardClasses.getAnyType()
                                            : semanticServices.getTypeTransformer().transformToType(extendsListTypes[0]);
            classDescriptor.setSuperclassType(superclassType);
        }
A
Andrey Breslav 已提交
134 135

        PsiMethod[] psiConstructors = psiClass.getConstructors();
136 137 138 139 140

        if (psiConstructors.length == 0) {
            if (!psiClass.hasModifierProperty(PsiModifier.ABSTRACT) && !psiClass.isInterface()) {
                ConstructorDescriptorImpl constructorDescriptor = new ConstructorDescriptorImpl(
                        classDescriptor,
A
Andrey Breslav 已提交
141
                        Collections.<AnnotationDescriptor>emptyList(),
142
                        false);
143
                constructorDescriptor.initialize(typeParameters, Collections.<ValueParameterDescriptor>emptyList(), Modality.FINAL, classDescriptor.getVisibility());
144
                constructorDescriptor.setReturnType(classDescriptor.getDefaultType());
145
                classDescriptor.addConstructor(constructorDescriptor);
146
                semanticServices.getTrace().record(BindingContext.CONSTRUCTOR, psiClass, constructorDescriptor);
147 148 149 150 151 152
            }
        }
        else {
            for (PsiMethod constructor : psiConstructors) {
                ConstructorDescriptorImpl constructorDescriptor = new ConstructorDescriptorImpl(
                        classDescriptor,
A
Andrey Breslav 已提交
153
                        Collections.<AnnotationDescriptor>emptyList(), // TODO
154
                        false);
155 156
                constructorDescriptor.initialize(typeParameters, resolveParameterDescriptors(constructorDescriptor, constructor.getParameterList().getParameters()), Modality.FINAL,
                                                 resolveVisibilityFromPsiModifiers(semanticServices.getTrace(), constructor));
157
                constructorDescriptor.setReturnType(classDescriptor.getDefaultType());
158
                classDescriptor.addConstructor(constructorDescriptor);
159
                semanticServices.getTrace().record(BindingContext.CONSTRUCTOR, constructor, constructorDescriptor);
160
            }
A
Andrey Breslav 已提交
161
        }
162

163
        semanticServices.getTrace().record(BindingContext.CLASS, psiClass, classDescriptor);
164

165 166 167
        return classDescriptor;
    }

168 169 170
    private DeclarationDescriptor resolveParentDescriptor(PsiClass psiClass) {
        PsiClass containingClass = psiClass.getContainingClass();
        if (containingClass != null) {
171
            return resolveClass(containingClass);
172 173 174 175 176 177 178
        }
        
        PsiJavaFile containingFile = (PsiJavaFile) psiClass.getContainingFile();
        String packageName = containingFile.getPackageName();
        return resolveNamespace(packageName);
    }

179
    private List<TypeParameterDescriptor> resolveTypeParameters(@NotNull DeclarationDescriptor containingDeclaration, @NotNull PsiTypeParameter[] typeParameters) {
180 181
        List<TypeParameterDescriptor> result = Lists.newArrayList();
        for (PsiTypeParameter typeParameter : typeParameters) {
182
            TypeParameterDescriptor typeParameterDescriptor = resolveTypeParameter(containingDeclaration, typeParameter);
A
Andrey Breslav 已提交
183
            result.add(typeParameterDescriptor);
184 185 186 187
        }
        return result;
    }

A
Andrey Breslav 已提交
188
    private TypeParameterDescriptor createJavaTypeParameterDescriptor(@NotNull DeclarationDescriptor owner, @NotNull PsiTypeParameter typeParameter) {
189 190
        TypeParameterDescriptor typeParameterDescriptor = TypeParameterDescriptor.createForFurtherModification(
                owner,
A
Andrey Breslav 已提交
191
                Collections.<AnnotationDescriptor>emptyList(), // TODO
192
                Variance.INVARIANT,
A
Andrey Breslav 已提交
193 194
                typeParameter.getName(),
                typeParameter.getIndex()
195
        );
196
        typeParameterDescriptorCache.put(typeParameter, typeParameterDescriptor);
A
Andrey Breslav 已提交
197 198
        PsiClassType[] referencedTypes = typeParameter.getExtendsList().getReferencedTypes();
        if (referencedTypes.length == 0){
199
            typeParameterDescriptor.addUpperBound(JetStandardClasses.getNullableAnyType());
A
Andrey Breslav 已提交
200 201
        }
        else if (referencedTypes.length == 1) {
202
            typeParameterDescriptor.addUpperBound(semanticServices.getTypeTransformer().transformToType(referencedTypes[0]));
A
Andrey Breslav 已提交
203 204 205
        }
        else {
            for (PsiClassType referencedType : referencedTypes) {
206
                typeParameterDescriptor.addUpperBound(semanticServices.getTypeTransformer().transformToType(referencedType));
A
Andrey Breslav 已提交
207 208
            }
        }
209
        return typeParameterDescriptor;
A
Andrey Breslav 已提交
210 211 212
    }

    @NotNull
213
    public TypeParameterDescriptor resolveTypeParameter(@NotNull DeclarationDescriptor containingDeclaration, @NotNull PsiTypeParameter psiTypeParameter) {
A
Andrey Breslav 已提交
214 215
        TypeParameterDescriptor typeParameterDescriptor = typeParameterDescriptorCache.get(psiTypeParameter);
        if (typeParameterDescriptor == null) {
216 217
            typeParameterDescriptor = createJavaTypeParameterDescriptor(containingDeclaration, psiTypeParameter);
//            This is done inside the method: typeParameterDescriptorCache.put(psiTypeParameter, typeParameterDescriptor);
A
Andrey Breslav 已提交
218 219 220 221
        }
        return typeParameterDescriptor;
    }

A
Andrey Breslav 已提交
222 223
    private Collection<? extends JetType> getSupertypes(PsiClass psiClass) {
        List<JetType> result = new ArrayList<JetType>();
224 225 226 227 228 229
        result.add(JetStandardClasses.getAnyType());
        transformSupertypeList(result, psiClass.getExtendsListTypes());
        transformSupertypeList(result, psiClass.getImplementsListTypes());
        return result;
    }

A
Andrey Breslav 已提交
230
    private void transformSupertypeList(List<JetType> result, PsiClassType[] extendsListTypes) {
231
        for (PsiClassType type : extendsListTypes) {
A
Andrey Breslav 已提交
232
            JetType transform = semanticServices.getTypeTransformer().transformToType(type);
233 234 235 236 237

            result.add(TypeUtils.makeNotNullable(transform));
        }
    }

238
    public NamespaceDescriptor resolveNamespace(String qualifiedName) {
239 240
        PsiPackage psiPackage = javaFacade.findPackage(qualifiedName);
        if (psiPackage == null) {
A
Andrey Breslav 已提交
241 242 243
            PsiClass psiClass = javaFacade.findClass(qualifiedName, javaSearchScope);
            if (psiClass == null) return null;
            return resolveNamespace(psiClass);
244 245 246 247 248 249
        }
        return resolveNamespace(psiPackage);
    }

    private NamespaceDescriptor resolveNamespace(@NotNull PsiPackage psiPackage) {
        NamespaceDescriptor namespaceDescriptor = namespaceDescriptorCache.get(psiPackage);
250
        if (namespaceDescriptor == null) {
A
Andrey Breslav 已提交
251
            namespaceDescriptor = createJavaNamespaceDescriptor(psiPackage);
252
            namespaceDescriptorCache.put(psiPackage, namespaceDescriptor);
253 254 255 256
        }
        return namespaceDescriptor;
    }

A
Andrey Breslav 已提交
257 258 259 260 261 262 263 264 265
    private NamespaceDescriptor resolveNamespace(@NotNull PsiClass psiClass) {
        NamespaceDescriptor namespaceDescriptor = namespaceDescriptorCache.get(psiClass);
        if (namespaceDescriptor == null) {
            namespaceDescriptor = createJavaNamespaceDescriptor(psiClass);
            namespaceDescriptorCache.put(psiClass, namespaceDescriptor);
        }
        return namespaceDescriptor;
    }

266 267
    private NamespaceDescriptor createJavaNamespaceDescriptor(@NotNull PsiPackage psiPackage) {
        String name = psiPackage.getName();
268
        JavaNamespaceDescriptor namespaceDescriptor = new JavaNamespaceDescriptor(
269
                resolveParentDescriptor(psiPackage),
A
Andrey Breslav 已提交
270
                Collections.<AnnotationDescriptor>emptyList(), // TODO
271
                name == null ? JAVA_ROOT : name
272
        );
273

274
        namespaceDescriptor.setMemberScope(new JavaPackageScope(psiPackage.getQualifiedName(), namespaceDescriptor, semanticServices));
275
        semanticServices.getTrace().record(BindingContext.NAMESPACE, psiPackage, namespaceDescriptor);
276 277 278
        return namespaceDescriptor;
    }

279 280 281 282 283 284 285 286
    private DeclarationDescriptor resolveParentDescriptor(@NotNull PsiPackage psiPackage) {
        PsiPackage parentPackage = psiPackage.getParentPackage();
        if (parentPackage == null) {
            return null;
        }
        return resolveNamespace(parentPackage);
    }

287
    private NamespaceDescriptor createJavaNamespaceDescriptor(@NotNull final PsiClass psiClass) {
288
        JavaNamespaceDescriptor namespaceDescriptor = new JavaNamespaceDescriptor(
289
                resolveParentDescriptor(psiClass),
A
Andrey Breslav 已提交
290
                Collections.<AnnotationDescriptor>emptyList(), // TODO
291
                psiClass.getName()
292
        );
293
        namespaceDescriptor.setMemberScope(new JavaClassMembersScope(namespaceDescriptor, psiClass, semanticServices, true));
294
        semanticServices.getTrace().record(BindingContext.NAMESPACE, psiClass, namespaceDescriptor);
295 296 297
        return namespaceDescriptor;
    }

298
    public List<ValueParameterDescriptor> resolveParameterDescriptors(DeclarationDescriptor containingDeclaration, PsiParameter[] parameters) {
299 300 301
        List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>();
        for (int i = 0, parametersLength = parameters.length; i < parametersLength; i++) {
            PsiParameter parameter = parameters[i];
A
Andrey Breslav 已提交
302
            String name = parameter.getName();
303 304 305 306 307 308 309 310 311 312 313
            PsiType psiType = parameter.getType();

            JetType varargElementType;
            if (psiType instanceof PsiEllipsisType) {
                PsiEllipsisType psiEllipsisType = (PsiEllipsisType) psiType;
                varargElementType = semanticServices.getTypeTransformer().transformToType(psiEllipsisType.getComponentType());
            }
            else {
                varargElementType = null;
            }
            JetType outType = semanticServices.getTypeTransformer().transformToType(psiType);
314
            result.add(new ValueParameterDescriptorImpl(
315
                    containingDeclaration,
316
                    i,
A
Andrey Breslav 已提交
317
                    Collections.<AnnotationDescriptor>emptyList(), // TODO
A
Andrey Breslav 已提交
318
                    name == null ? "p" + i : name,
319
                    null, // TODO : review
320
                    outType,
321
                    false,
322
                    varargElementType
323 324 325 326
            ));
        }
        return result;
    }
A
Andrey Breslav 已提交
327

A
Andrey Breslav 已提交
328 329 330 331 332 333 334 335 336
    public VariableDescriptor resolveFieldToVariableDescriptor(DeclarationDescriptor containingDeclaration, PsiField field) {
        VariableDescriptor variableDescriptor = fieldDescriptorCache.get(field);
        if (variableDescriptor != null) {
            return variableDescriptor;
        }
        JetType type = semanticServices.getTypeTransformer().transformToType(field.getType());
        boolean isFinal = field.hasModifierProperty(PsiModifier.FINAL);
        PropertyDescriptor propertyDescriptor = new PropertyDescriptor(
                containingDeclaration,
A
Andrey Breslav 已提交
337
                Collections.<AnnotationDescriptor>emptyList(),
338
                Modality.FINAL,
339
                resolveVisibilityFromPsiModifiers(semanticServices.getTrace(), field),
A
Andrey Breslav 已提交
340 341
                !isFinal,
                null,
342
                DescriptorUtils.getExpectedThisObjectIfNeeded(containingDeclaration),
A
Andrey Breslav 已提交
343 344 345
                field.getName(),
                isFinal ? null : type,
                type);
346
        semanticServices.getTrace().record(BindingContext.VARIABLE, field, propertyDescriptor);
A
Andrey Breslav 已提交
347 348 349 350
        fieldDescriptorCache.put(field, propertyDescriptor);
        return propertyDescriptor;
    }

A
Andrey Breslav 已提交
351
    @NotNull
A
Andrey Breslav 已提交
352 353
    public Set<FunctionDescriptor> resolveFunctionGroup(@NotNull DeclarationDescriptor owner, @NotNull PsiClass psiClass, @Nullable ClassDescriptor classDescriptor, @NotNull String methodName, boolean staticMembers) {
        Set<FunctionDescriptor> writableFunctionGroup = Sets.newLinkedHashSet();
354
        final Collection<HierarchicalMethodSignature> signatures = psiClass.getVisibleSignatures();
A
Andrey Breslav 已提交
355
        TypeSubstitutor typeSubstitutor = createSubstitutorForGenericSupertypes(classDescriptor);
356
        for (HierarchicalMethodSignature signature: signatures) {
357
            if (!methodName.equals(signature.getName())) {
A
Andrey Breslav 已提交
358 359 360
                 continue;
            }

361
            FunctionDescriptor substitutedFunctionDescriptor = resolveHierarchicalSignatureToFunction(owner, psiClass, staticMembers, typeSubstitutor, signature);
362
            if (substitutedFunctionDescriptor != null) {
A
Andrey Breslav 已提交
363
                writableFunctionGroup.add(substitutedFunctionDescriptor);
364
            }
A
Andrey Breslav 已提交
365 366 367
        }
        return writableFunctionGroup;
    }
A
Andrey Breslav 已提交
368

369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
    @Nullable
    private FunctionDescriptor resolveHierarchicalSignatureToFunction(DeclarationDescriptor owner, PsiClass psiClass, boolean staticMembers, TypeSubstitutor typeSubstitutor, HierarchicalMethodSignature signature) {
        PsiMethod method = signature.getMethod();
        if (method.hasModifierProperty(PsiModifier.STATIC) != staticMembers) {
                return null;
        }
        FunctionDescriptor functionDescriptor = resolveMethodToFunctionDescriptor(owner, psiClass, typeSubstitutor, method);
//        if (functionDescriptor != null && !staticMembers) {
//            for (HierarchicalMethodSignature superSignature : signature.getSuperSignatures()) {
//                ((FunctionDescriptorImpl) functionDescriptor).addOverriddenFunction(resolveHierarchicalSignatureToFunction(owner, superSignature.getMethod().getContainingClass(), false, typeSubstitutor, superSignature));
//            }
//        }
        return functionDescriptor;
    }

A
Andrey Breslav 已提交
384 385 386 387 388 389 390 391 392 393 394 395
    public TypeSubstitutor createSubstitutorForGenericSupertypes(ClassDescriptor classDescriptor) {
        TypeSubstitutor typeSubstitutor;
        if (classDescriptor != null) {
            typeSubstitutor = TypeUtils.buildDeepSubstitutor(classDescriptor.getDefaultType());
        }
        else {
            typeSubstitutor = TypeSubstitutor.EMPTY;
        }
        return typeSubstitutor;
    }

    @Nullable
A
Andrey Breslav 已提交
396
    public FunctionDescriptor resolveMethodToFunctionDescriptor(DeclarationDescriptor owner, PsiClass psiClass, TypeSubstitutor typeSubstitutorForGenericSuperclasses, PsiMethod method) {
A
Andrey Breslav 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409
        PsiType returnType = method.getReturnType();
        if (returnType == null) {
            return null;
        }
        FunctionDescriptor functionDescriptor = methodDescriptorCache.get(method);
        if (functionDescriptor != null) {
            if (method.getContainingClass() != psiClass) {
                functionDescriptor = functionDescriptor.substitute(typeSubstitutorForGenericSuperclasses);
            }
            return functionDescriptor;
        }
        PsiParameter[] parameters = method.getParameterList().getParameters();
        FunctionDescriptorImpl functionDescriptorImpl = new FunctionDescriptorImpl(
A
Andrey Breslav 已提交
410 411
                owner,
                Collections.<AnnotationDescriptor>emptyList(), // TODO
A
Andrey Breslav 已提交
412 413 414 415 416
                method.getName()
        );
        methodDescriptorCache.put(method, functionDescriptorImpl);
        functionDescriptorImpl.initialize(
                null,
417
                DescriptorUtils.getExpectedThisObjectIfNeeded(owner),
A
Andrey Breslav 已提交
418 419
                resolveTypeParameters(functionDescriptorImpl, method.getTypeParameters()),
                semanticServices.getDescriptorResolver().resolveParameterDescriptors(functionDescriptorImpl, parameters),
420
                semanticServices.getTypeTransformer().transformToType(returnType),
421 422
                Modality.convertFromFlags(method.hasModifierProperty(PsiModifier.ABSTRACT), !method.hasModifierProperty(PsiModifier.FINAL)),
                resolveVisibilityFromPsiModifiers(semanticServices.getTrace(), method)
A
Andrey Breslav 已提交
423
        );
424
        semanticServices.getTrace().record(BindingContext.FUNCTION, method, functionDescriptorImpl);
A
Andrey Breslav 已提交
425 426 427 428 429 430
        FunctionDescriptor substitutedFunctionDescriptor = functionDescriptorImpl;
        if (method.getContainingClass() != psiClass) {
            substitutedFunctionDescriptor = functionDescriptorImpl.substitute(typeSubstitutorForGenericSuperclasses);
        }
        return substitutedFunctionDescriptor;
    }
431 432 433 434 435 436 437 438

    private static Visibility resolveVisibilityFromPsiModifiers(BindingTrace trace, PsiModifierListOwner modifierListOwner) {
        //TODO report error
        return modifierListOwner.hasModifierProperty(PsiModifier.PUBLIC) ? Visibility.PUBLIC :
                                        (modifierListOwner.hasModifierProperty(PsiModifier.PRIVATE) ? Visibility.PRIVATE :
                                        (modifierListOwner.hasModifierProperty(PsiModifier.PROTECTED) ? Visibility.PROTECTED : Visibility.INTERNAL));
    }

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
    public TypeParameterDescriptor resolveTypeParameter(PsiTypeParameter typeParameter) {
        PsiTypeParameterListOwner owner = typeParameter.getOwner();
        if (owner instanceof PsiClass) {
            PsiClass psiClass = (PsiClass) owner;
            return resolveTypeParameter(resolveClass(psiClass), typeParameter);
        }
        if (owner instanceof PsiMethod) {
            PsiMethod psiMethod = (PsiMethod) owner;
            PsiClass containingClass = psiMethod.getContainingClass();
            DeclarationDescriptor ownerOwner;
            TypeSubstitutor substitutorForGenericSupertypes;
            if (psiMethod.hasModifierProperty(PsiModifier.STATIC)) {
                substitutorForGenericSupertypes = TypeSubstitutor.EMPTY;
                return resolveTypeParameter(JAVA_METHOD_TYPE_PARAMETER_PARENT, typeParameter);
            }
            else {
                ClassDescriptor classDescriptor = resolveClass(containingClass);
                ownerOwner = classDescriptor;
                substitutorForGenericSupertypes = semanticServices.getDescriptorResolver().createSubstitutorForGenericSupertypes(classDescriptor);
            }
            FunctionDescriptor functionDescriptor = resolveMethodToFunctionDescriptor(ownerOwner, containingClass, substitutorForGenericSupertypes, psiMethod);
            return resolveTypeParameter(functionDescriptor, typeParameter);
        }
        throw new IllegalStateException("Unknown parent type: " + owner);
    }
464
}