JavaDescriptorResolver.java 23.4 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
A
Andrey Breslav 已提交
192
                false,
193
                Variance.INVARIANT,
A
Andrey Breslav 已提交
194 195
                typeParameter.getName(),
                typeParameter.getIndex()
196
        );
197
        typeParameterDescriptorCache.put(typeParameter, typeParameterDescriptor);
A
Andrey Breslav 已提交
198 199
        PsiClassType[] referencedTypes = typeParameter.getExtendsList().getReferencedTypes();
        if (referencedTypes.length == 0){
200
            typeParameterDescriptor.addUpperBound(JetStandardClasses.getNullableAnyType());
A
Andrey Breslav 已提交
201 202
        }
        else if (referencedTypes.length == 1) {
203
            typeParameterDescriptor.addUpperBound(semanticServices.getTypeTransformer().transformToType(referencedTypes[0]));
A
Andrey Breslav 已提交
204 205 206
        }
        else {
            for (PsiClassType referencedType : referencedTypes) {
207
                typeParameterDescriptor.addUpperBound(semanticServices.getTypeTransformer().transformToType(referencedType));
A
Andrey Breslav 已提交
208 209
            }
        }
210
        return typeParameterDescriptor;
A
Andrey Breslav 已提交
211 212 213
    }

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

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

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

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

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

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

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

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

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

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

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

299
    public List<ValueParameterDescriptor> resolveParameterDescriptors(DeclarationDescriptor containingDeclaration, PsiParameter[] parameters) {
300 301 302
        List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>();
        for (int i = 0, parametersLength = parameters.length; i < parametersLength; i++) {
            PsiParameter parameter = parameters[i];
A
Andrey Breslav 已提交
303
            String name = parameter.getName();
304 305 306 307 308 309 310 311 312 313 314
            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);
315
            result.add(new ValueParameterDescriptorImpl(
316
                    containingDeclaration,
317
                    i,
A
Andrey Breslav 已提交
318
                    Collections.<AnnotationDescriptor>emptyList(), // TODO
A
Andrey Breslav 已提交
319
                    name == null ? "p" + i : name,
320
                    null, // TODO : review
321
                    outType,
322
                    false,
323
                    varargElementType
324 325 326 327
            ));
        }
        return result;
    }
A
Andrey Breslav 已提交
328

A
Andrey Breslav 已提交
329 330 331 332 333 334 335 336 337
    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 已提交
338
                Collections.<AnnotationDescriptor>emptyList(),
339
                Modality.FINAL,
340
                resolveVisibilityFromPsiModifiers(semanticServices.getTrace(), field),
A
Andrey Breslav 已提交
341 342
                !isFinal,
                null,
343
                DescriptorUtils.getExpectedThisObjectIfNeeded(containingDeclaration),
A
Andrey Breslav 已提交
344 345 346
                field.getName(),
                isFinal ? null : type,
                type);
347
        semanticServices.getTrace().record(BindingContext.VARIABLE, field, propertyDescriptor);
A
Andrey Breslav 已提交
348 349 350 351
        fieldDescriptorCache.put(field, propertyDescriptor);
        return propertyDescriptor;
    }

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

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

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
    @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 已提交
385 386 387 388 389 390 391 392 393 394 395 396
    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 已提交
397
    public FunctionDescriptor resolveMethodToFunctionDescriptor(DeclarationDescriptor owner, PsiClass psiClass, TypeSubstitutor typeSubstitutorForGenericSuperclasses, PsiMethod method) {
A
Andrey Breslav 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410
        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 已提交
411 412
                owner,
                Collections.<AnnotationDescriptor>emptyList(), // TODO
A
Andrey Breslav 已提交
413 414 415 416 417
                method.getName()
        );
        methodDescriptorCache.put(method, functionDescriptorImpl);
        functionDescriptorImpl.initialize(
                null,
418
                DescriptorUtils.getExpectedThisObjectIfNeeded(owner),
A
Andrey Breslav 已提交
419 420
                resolveTypeParameters(functionDescriptorImpl, method.getTypeParameters()),
                semanticServices.getDescriptorResolver().resolveParameterDescriptors(functionDescriptorImpl, parameters),
421
                semanticServices.getTypeTransformer().transformToType(returnType),
422 423
                Modality.convertFromFlags(method.hasModifierProperty(PsiModifier.ABSTRACT), !method.hasModifierProperty(PsiModifier.FINAL)),
                resolveVisibilityFromPsiModifiers(semanticServices.getTrace(), method)
A
Andrey Breslav 已提交
424
        );
425
        semanticServices.getTrace().record(BindingContext.FUNCTION, method, functionDescriptorImpl);
A
Andrey Breslav 已提交
426 427 428 429 430 431
        FunctionDescriptor substitutedFunctionDescriptor = functionDescriptorImpl;
        if (method.getContainingClass() != psiClass) {
            substitutedFunctionDescriptor = functionDescriptorImpl.substitute(typeSubstitutorForGenericSuperclasses);
        }
        return substitutedFunctionDescriptor;
    }
432 433 434 435 436 437 438 439

    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));
    }

440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
    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);
    }
465
}