JavaDescriptorResolver.java 29.0 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
import com.intellij.openapi.project.Project;
7
import com.intellij.openapi.vfs.VirtualFile;
8
import com.intellij.psi.*;
9
import com.intellij.psi.search.DelegatingGlobalSearchScope;
10 11 12
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
A
Andrey Breslav 已提交
13
import org.jetbrains.jet.lang.descriptors.*;
A
Andrey Breslav 已提交
14
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
15
import org.jetbrains.jet.lang.resolve.BindingContext;
16
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
17
import org.jetbrains.jet.lang.types.*;
18
import org.jetbrains.jet.plugin.JetFileType;
19 20 21 22 23 24 25

import java.util.*;

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

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

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

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

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

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

55
    protected final Map<String, ClassDescriptor> classDescriptorCache = Maps.newHashMap();
A
Andrey Breslav 已提交
56
    protected final Map<PsiTypeParameter, TypeParameterDescriptor> typeParameterDescriptorCache = Maps.newHashMap();
A
Andrey Breslav 已提交
57
    protected final Map<PsiMethod, FunctionDescriptor> methodDescriptorCache = Maps.newHashMap();
A
Andrey Breslav 已提交
58
    protected final Map<PsiField, VariableDescriptor> fieldDescriptorCache = Maps.newHashMap();
A
Andrey Breslav 已提交
59
    protected final Map<PsiElement, NamespaceDescriptor> namespaceDescriptorCache = Maps.newHashMap();
60 61 62 63 64 65
    protected final JavaPsiFacade javaFacade;
    protected final GlobalSearchScope javaSearchScope;
    protected final JavaSemanticServices semanticServices;

    public JavaDescriptorResolver(Project project, JavaSemanticServices semanticServices) {
        this.javaFacade = JavaPsiFacade.getInstance(project);
66 67 68 69 70 71
        this.javaSearchScope = new DelegatingGlobalSearchScope(GlobalSearchScope.allScope(project)) {
            @Override
            public boolean contains(VirtualFile file) {
                return myBaseScope.contains(file) && file.getFileType() != JetFileType.INSTANCE;
            }
        };
72 73 74 75 76 77
        this.semanticServices = semanticServices;
    }

    @NotNull
    public ClassDescriptor resolveClass(@NotNull PsiClass psiClass) {
        String qualifiedName = psiClass.getQualifiedName();
78

79
        // First, let's check that this is a real Java class, not a Java's view on a Kotlin class:
80 81 82 83 84
        ClassDescriptor kotlinClassDescriptor = semanticServices.getKotlinClassDescriptor(qualifiedName);
        if (kotlinClassDescriptor != null) {
            return kotlinClassDescriptor;
        }

85
        // Not let's take a descriptor of a Java class
86 87 88 89 90 91 92 93 94 95
        ClassDescriptor classDescriptor = classDescriptorCache.get(qualifiedName);
        if (classDescriptor == null) {
            classDescriptor = createJavaClassDescriptor(psiClass);
            classDescriptorCache.put(qualifiedName, classDescriptor);
        }
        return classDescriptor;
    }

    @Nullable
    public ClassDescriptor resolveClass(@NotNull String qualifiedName) {
96
        // First, let's check that this is a real Java class, not a Java's view on a Kotlin class:
97 98 99 100 101
        ClassDescriptor kotlinClassDescriptor = semanticServices.getKotlinClassDescriptor(qualifiedName);
        if (kotlinClassDescriptor != null) {
            return kotlinClassDescriptor;
        }

102
        // Not let's take a descriptor of a Java class
103 104
        ClassDescriptor classDescriptor = classDescriptorCache.get(qualifiedName);
        if (classDescriptor == null) {
105
            PsiClass psiClass = findClass(qualifiedName);
106 107 108 109 110 111 112 113
            if (psiClass == null) {
                return null;
            }
            classDescriptor = createJavaClassDescriptor(psiClass);
        }
        return classDescriptor;
    }

114
    private ClassDescriptor createJavaClassDescriptor(@NotNull final PsiClass psiClass) {
A
Andrey Breslav 已提交
115 116 117
        assert !classDescriptorCache.containsKey(psiClass.getQualifiedName()) : psiClass.getQualifiedName();
        classDescriptorCache.put(psiClass.getQualifiedName(), null); // TODO

118
        String name = psiClass.getName();
119
        JavaClassDescriptor classDescriptor = new JavaClassDescriptor(
120
                resolveParentDescriptor(psiClass), psiClass.isInterface() ? ClassKind.TRAIT : ClassKind.CLASS
121
        );
A
Andrey Breslav 已提交
122
        classDescriptor.setName(name);
A
Andrey Breslav 已提交
123

A
Andrey Breslav 已提交
124
        List<JetType> supertypes = new ArrayList<JetType>();
125
        List<TypeParameterDescriptor> typeParameters = makeUninitializedTypeParameters(classDescriptor, psiClass.getTypeParameters());
A
Andrey Breslav 已提交
126 127
        classDescriptor.setTypeConstructor(new TypeConstructorImpl(
                classDescriptor,
A
Andrey Breslav 已提交
128
                Collections.<AnnotationDescriptor>emptyList(), // TODO
129
                // TODO
130
                psiClass.hasModifierProperty(PsiModifier.FINAL),
A
Andrey Breslav 已提交
131
                name,
A
Andrey Breslav 已提交
132
                typeParameters,
A
Andrey Breslav 已提交
133 134
                supertypes
        ));
135 136 137
        classDescriptor.setModality(Modality.convertFromFlags(
                psiClass.hasModifierProperty(PsiModifier.ABSTRACT) || psiClass.isInterface(),
                !psiClass.hasModifierProperty(PsiModifier.FINAL))
A
Andrey Breslav 已提交
138
        );
139
        classDescriptor.setVisibility(resolveVisibilityFromPsiModifiers(psiClass));
A
Andrey Breslav 已提交
140
        classDescriptorCache.put(psiClass.getQualifiedName(), classDescriptor);
141
        classDescriptor.setUnsubstitutedMemberScope(new JavaClassMembersScope(classDescriptor, psiClass, semanticServices, false));
142

A
Andrey Breslav 已提交
143
        // UGLY HACK
144 145
        initializeTypeParameters(psiClass);

A
Andrey Breslav 已提交
146
        supertypes.addAll(getSupertypes(psiClass));
147 148 149 150 151 152 153 154 155 156 157
        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 已提交
158 159

        PsiMethod[] psiConstructors = psiClass.getConstructors();
160 161 162 163 164

        if (psiConstructors.length == 0) {
            if (!psiClass.hasModifierProperty(PsiModifier.ABSTRACT) && !psiClass.isInterface()) {
                ConstructorDescriptorImpl constructorDescriptor = new ConstructorDescriptorImpl(
                        classDescriptor,
A
Andrey Breslav 已提交
165
                        Collections.<AnnotationDescriptor>emptyList(),
166
                        false);
167
                constructorDescriptor.initialize(typeParameters, Collections.<ValueParameterDescriptor>emptyList(), Modality.FINAL, classDescriptor.getVisibility());
168
                constructorDescriptor.setReturnType(classDescriptor.getDefaultType());
169
                classDescriptor.addConstructor(constructorDescriptor);
170
                semanticServices.getTrace().record(BindingContext.CONSTRUCTOR, psiClass, constructorDescriptor);
171 172 173 174 175 176
            }
        }
        else {
            for (PsiMethod constructor : psiConstructors) {
                ConstructorDescriptorImpl constructorDescriptor = new ConstructorDescriptorImpl(
                        classDescriptor,
A
Andrey Breslav 已提交
177
                        Collections.<AnnotationDescriptor>emptyList(), // TODO
178
                        false);
179
                constructorDescriptor.initialize(typeParameters, resolveParameterDescriptors(constructorDescriptor, constructor.getParameterList().getParameters()), Modality.FINAL,
180
                                                 resolveVisibilityFromPsiModifiers(constructor));
181
                constructorDescriptor.setReturnType(classDescriptor.getDefaultType());
182
                classDescriptor.addConstructor(constructorDescriptor);
183
                semanticServices.getTrace().record(BindingContext.CONSTRUCTOR, constructor, constructorDescriptor);
184
            }
A
Andrey Breslav 已提交
185
        }
186

187
        semanticServices.getTrace().record(BindingContext.CLASS, psiClass, classDescriptor);
188

189 190 191
        return classDescriptor;
    }

192 193 194
    private DeclarationDescriptor resolveParentDescriptor(PsiClass psiClass) {
        PsiClass containingClass = psiClass.getContainingClass();
        if (containingClass != null) {
195
            return resolveClass(containingClass);
196 197 198 199 200 201 202
        }
        
        PsiJavaFile containingFile = (PsiJavaFile) psiClass.getContainingFile();
        String packageName = containingFile.getPackageName();
        return resolveNamespace(packageName);
    }

203
    private List<TypeParameterDescriptor> makeUninitializedTypeParameters(@NotNull DeclarationDescriptor containingDeclaration, @NotNull PsiTypeParameter[] typeParameters) {
204 205
        List<TypeParameterDescriptor> result = Lists.newArrayList();
        for (PsiTypeParameter typeParameter : typeParameters) {
206
            TypeParameterDescriptor typeParameterDescriptor = makeUninitializedTypeParameter(containingDeclaration, typeParameter);
A
Andrey Breslav 已提交
207
            result.add(typeParameterDescriptor);
208 209 210 211
        }
        return result;
    }

212 213 214
    @NotNull
    private TypeParameterDescriptor makeUninitializedTypeParameter(@NotNull DeclarationDescriptor containingDeclaration, @NotNull PsiTypeParameter psiTypeParameter) {
        assert typeParameterDescriptorCache.get(psiTypeParameter) == null : psiTypeParameter.getText();
215
        TypeParameterDescriptor typeParameterDescriptor = TypeParameterDescriptor.createForFurtherModification(
216
                containingDeclaration,
A
Andrey Breslav 已提交
217
                Collections.<AnnotationDescriptor>emptyList(), // TODO
A
Andrey Breslav 已提交
218
                false,
219
                Variance.INVARIANT,
220 221
                psiTypeParameter.getName(),
                psiTypeParameter.getIndex()
222
        );
223 224 225 226 227
        typeParameterDescriptorCache.put(psiTypeParameter, typeParameterDescriptor);
        return typeParameterDescriptor;
    }

    private void initializeTypeParameter(PsiTypeParameter typeParameter, TypeParameterDescriptor typeParameterDescriptor) {
A
Andrey Breslav 已提交
228 229
        PsiClassType[] referencedTypes = typeParameter.getExtendsList().getReferencedTypes();
        if (referencedTypes.length == 0){
230
            typeParameterDescriptor.addUpperBound(JetStandardClasses.getNullableAnyType());
A
Andrey Breslav 已提交
231 232
        }
        else if (referencedTypes.length == 1) {
233
            typeParameterDescriptor.addUpperBound(semanticServices.getTypeTransformer().transformToType(referencedTypes[0]));
A
Andrey Breslav 已提交
234 235 236
        }
        else {
            for (PsiClassType referencedType : referencedTypes) {
237
                typeParameterDescriptor.addUpperBound(semanticServices.getTypeTransformer().transformToType(referencedType));
A
Andrey Breslav 已提交
238 239
            }
        }
240 241 242 243 244 245
    }

    private void initializeTypeParameters(PsiTypeParameterListOwner typeParameterListOwner) {
        for (PsiTypeParameter psiTypeParameter : typeParameterListOwner.getTypeParameters()) {
            initializeTypeParameter(psiTypeParameter, resolveTypeParameter(psiTypeParameter));
        }
A
Andrey Breslav 已提交
246 247 248
    }

    @NotNull
249
    private TypeParameterDescriptor resolveTypeParameter(@NotNull DeclarationDescriptor containingDeclaration, @NotNull PsiTypeParameter psiTypeParameter) {
A
Andrey Breslav 已提交
250
        TypeParameterDescriptor typeParameterDescriptor = typeParameterDescriptorCache.get(psiTypeParameter);
251
        assert typeParameterDescriptor != null : psiTypeParameter.getText();
A
Andrey Breslav 已提交
252 253 254
        return typeParameterDescriptor;
    }

A
Andrey Breslav 已提交
255 256
    private Collection<? extends JetType> getSupertypes(PsiClass psiClass) {
        List<JetType> result = new ArrayList<JetType>();
257 258 259 260 261 262
        result.add(JetStandardClasses.getAnyType());
        transformSupertypeList(result, psiClass.getExtendsListTypes());
        transformSupertypeList(result, psiClass.getImplementsListTypes());
        return result;
    }

A
Andrey Breslav 已提交
263
    private void transformSupertypeList(List<JetType> result, PsiClassType[] extendsListTypes) {
264
        for (PsiClassType type : extendsListTypes) {
A
Andrey Breslav 已提交
265
            JetType transform = semanticServices.getTypeTransformer().transformToType(type);
266 267 268 269 270

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

271
    public NamespaceDescriptor resolveNamespace(String qualifiedName) {
272
        PsiPackage psiPackage = findPackage(qualifiedName);
273
        if (psiPackage == null) {
274
            PsiClass psiClass = findClass(qualifiedName);
A
Andrey Breslav 已提交
275 276
            if (psiClass == null) return null;
            return resolveNamespace(psiClass);
277 278 279 280
        }
        return resolveNamespace(psiPackage);
    }

281 282 283 284
    private PsiClass findClass(String qualifiedName) {
        return javaFacade.findClass(qualifiedName, javaSearchScope);
    }

285
    /*package*/ PsiPackage findPackage(String qualifiedName) {
286 287 288
        return javaFacade.findPackage(qualifiedName);
    }

289 290
    private NamespaceDescriptor resolveNamespace(@NotNull PsiPackage psiPackage) {
        NamespaceDescriptor namespaceDescriptor = namespaceDescriptorCache.get(psiPackage);
291
        if (namespaceDescriptor == null) {
A
Andrey Breslav 已提交
292
            namespaceDescriptor = createJavaNamespaceDescriptor(psiPackage);
293
            namespaceDescriptorCache.put(psiPackage, namespaceDescriptor);
294 295 296 297
        }
        return namespaceDescriptor;
    }

A
Andrey Breslav 已提交
298 299 300 301 302 303 304 305 306
    private NamespaceDescriptor resolveNamespace(@NotNull PsiClass psiClass) {
        NamespaceDescriptor namespaceDescriptor = namespaceDescriptorCache.get(psiClass);
        if (namespaceDescriptor == null) {
            namespaceDescriptor = createJavaNamespaceDescriptor(psiClass);
            namespaceDescriptorCache.put(psiClass, namespaceDescriptor);
        }
        return namespaceDescriptor;
    }

307 308
    private NamespaceDescriptor createJavaNamespaceDescriptor(@NotNull PsiPackage psiPackage) {
        String name = psiPackage.getName();
309
        JavaNamespaceDescriptor namespaceDescriptor = new JavaNamespaceDescriptor(
310
                resolveParentDescriptor(psiPackage),
A
Andrey Breslav 已提交
311
                Collections.<AnnotationDescriptor>emptyList(), // TODO
312
                name == null ? JAVA_ROOT : name
313
        );
314

315
        namespaceDescriptor.setMemberScope(new JavaPackageScope(psiPackage.getQualifiedName(), namespaceDescriptor, semanticServices));
316
        semanticServices.getTrace().record(BindingContext.NAMESPACE, psiPackage, namespaceDescriptor);
317 318 319
        return namespaceDescriptor;
    }

320 321 322 323 324 325 326 327
    private DeclarationDescriptor resolveParentDescriptor(@NotNull PsiPackage psiPackage) {
        PsiPackage parentPackage = psiPackage.getParentPackage();
        if (parentPackage == null) {
            return null;
        }
        return resolveNamespace(parentPackage);
    }

328
    private NamespaceDescriptor createJavaNamespaceDescriptor(@NotNull final PsiClass psiClass) {
329
        JavaNamespaceDescriptor namespaceDescriptor = new JavaNamespaceDescriptor(
330
                resolveParentDescriptor(psiClass),
A
Andrey Breslav 已提交
331
                Collections.<AnnotationDescriptor>emptyList(), // TODO
332
                psiClass.getName()
333
        );
334
        namespaceDescriptor.setMemberScope(new JavaClassMembersScope(namespaceDescriptor, psiClass, semanticServices, true));
335
        semanticServices.getTrace().record(BindingContext.NAMESPACE, psiClass, namespaceDescriptor);
336 337 338
        return namespaceDescriptor;
    }

339
    public List<ValueParameterDescriptor> resolveParameterDescriptors(DeclarationDescriptor containingDeclaration, PsiParameter[] parameters) {
340 341 342
        List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>();
        for (int i = 0, parametersLength = parameters.length; i < parametersLength; i++) {
            PsiParameter parameter = parameters[i];
343
            ValueParameterDescriptor valueParameterDescriptor = resolveParameterDescriptor(containingDeclaration, i, parameter);
S
Stepan Koltsov 已提交
344 345 346
            if (valueParameterDescriptor != null) {
                result.add(valueParameterDescriptor);
            }
347 348 349
        }
        return result;
    }
A
Andrey Breslav 已提交
350

S
Stepan Koltsov 已提交
351
    @Nullable
352 353 354 355 356 357 358 359 360 361 362
    private ValueParameterDescriptor resolveParameterDescriptor(DeclarationDescriptor containingDeclaration, int i, PsiParameter parameter) {
        PsiType psiType = parameter.getType();

        JetType varargElementType;
        if (psiType instanceof PsiEllipsisType) {
            PsiEllipsisType psiEllipsisType = (PsiEllipsisType) psiType;
            varargElementType = semanticServices.getTypeTransformer().transformToType(psiEllipsisType.getComponentType());
        }
        else {
            varargElementType = null;
        }
363 364 365

        boolean changeNullable = false;
        boolean nullable = true;
S
Stepan Koltsov 已提交
366
        String typeFromAnnotation = null;
367 368 369 370 371 372 373 374 375
        
        // TODO: must be very slow, make it lazy?
        String name = parameter.getName() != null ? parameter.getName() : "p" + i;
        for (PsiAnnotation annotation : parameter.getModifierList().getAnnotations()) {
            // TODO: softcode annotation name

            PsiNameValuePair[] attributes = annotation.getParameterList().getAttributes();
            attributes.toString();

376 377
            if (annotation.getQualifiedName().equals(StdlibNames.JET_VALUE_PARAMETER_CLASS)) {
                PsiLiteralExpression nameExpression = (PsiLiteralExpression) annotation.findAttributeValue(StdlibNames.JET_VALUE_PARAMETER_NAME_FIELD);
378 379 380 381
                if (nameExpression != null) {
                    name = (String) nameExpression.getValue();
                }
                
382
                PsiLiteralExpression nullableExpression = (PsiLiteralExpression) annotation.findAttributeValue(StdlibNames.JET_VALUE_PARAMETER_NULLABLE_FIELD);
383 384 385 386 387 388 389
                if (nullableExpression != null) {
                    nullable = (Boolean) nullableExpression.getValue();
                } else {
                    // default value of parameter
                    nullable = false;
                    changeNullable = true;
                }
S
Stepan Koltsov 已提交
390 391 392 393 394
                
                PsiLiteralExpression signatureExpression = (PsiLiteralExpression) annotation.findAttributeValue(StdlibNames.JET_VALUE_PARAMETER_TYPE_FIELD);
                if (signatureExpression != null) {
                    typeFromAnnotation = (String) signatureExpression.getValue();
                }
S
Stepan Koltsov 已提交
395 396
            } else if (annotation.getQualifiedName().equals(StdlibNames.JET_TYPE_PARAMETER_CLASS)) {
                return null;
397 398 399
            }
        }
        
S
Stepan Koltsov 已提交
400 401 402 403 404 405
        JetType outType;
        if (typeFromAnnotation != null) {
            outType = semanticServices.getTypeTransformer().transformToType(typeFromAnnotation);
        } else {
            outType = semanticServices.getTypeTransformer().transformToType(psiType);
        }
406 407 408 409
        return new ValueParameterDescriptorImpl(
                containingDeclaration,
                i,
                Collections.<AnnotationDescriptor>emptyList(), // TODO
410
                name,
411
                null, // TODO : review
412
                changeNullable ? TypeUtils.makeNullableAsSpecified(outType, nullable) : outType,
413 414 415 416 417
                false,
                varargElementType
        );
    }

A
Andrey Breslav 已提交
418 419 420 421 422 423 424 425 426
    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 已提交
427
                Collections.<AnnotationDescriptor>emptyList(),
428
                Modality.FINAL,
429
                resolveVisibilityFromPsiModifiers(field),
A
Andrey Breslav 已提交
430 431
                !isFinal,
                null,
432
                DescriptorUtils.getExpectedThisObjectIfNeeded(containingDeclaration),
A
Andrey Breslav 已提交
433 434 435
                field.getName(),
                isFinal ? null : type,
                type);
436
        semanticServices.getTrace().record(BindingContext.VARIABLE, field, propertyDescriptor);
A
Andrey Breslav 已提交
437 438 439 440
        fieldDescriptorCache.put(field, propertyDescriptor);
        return propertyDescriptor;
    }

A
Andrey Breslav 已提交
441
    @NotNull
A
Andrey Breslav 已提交
442 443
    public Set<FunctionDescriptor> resolveFunctionGroup(@NotNull DeclarationDescriptor owner, @NotNull PsiClass psiClass, @Nullable ClassDescriptor classDescriptor, @NotNull String methodName, boolean staticMembers) {
        Set<FunctionDescriptor> writableFunctionGroup = Sets.newLinkedHashSet();
444
        final Collection<HierarchicalMethodSignature> signatures = psiClass.getVisibleSignatures();
A
Andrey Breslav 已提交
445
        TypeSubstitutor typeSubstitutor = createSubstitutorForGenericSupertypes(classDescriptor);
446
        for (HierarchicalMethodSignature signature: signatures) {
447
            if (!methodName.equals(signature.getName())) {
A
Andrey Breslav 已提交
448 449 450
                 continue;
            }

451
            FunctionDescriptor substitutedFunctionDescriptor = resolveHierarchicalSignatureToFunction(owner, psiClass, staticMembers, typeSubstitutor, signature);
452
            if (substitutedFunctionDescriptor != null) {
A
Andrey Breslav 已提交
453
                writableFunctionGroup.add(substitutedFunctionDescriptor);
454
            }
A
Andrey Breslav 已提交
455 456 457
        }
        return writableFunctionGroup;
    }
A
Andrey Breslav 已提交
458

459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
    @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 已提交
474 475 476 477 478 479 480 481 482 483 484 485
    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 已提交
486
    public FunctionDescriptor resolveMethodToFunctionDescriptor(DeclarationDescriptor owner, PsiClass psiClass, TypeSubstitutor typeSubstitutorForGenericSuperclasses, PsiMethod method) {
A
Andrey Breslav 已提交
487 488 489 490 491 492 493 494 495 496 497
        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;
        }
A
Andrey Breslav 已提交
498
        DeclarationDescriptor classDescriptor = method.hasModifierProperty(PsiModifier.STATIC) ? resolveNamespace(method.getContainingClass()) : resolveClass(method.getContainingClass());
A
Andrey Breslav 已提交
499 500
        PsiParameter[] parameters = method.getParameterList().getParameters();
        FunctionDescriptorImpl functionDescriptorImpl = new FunctionDescriptorImpl(
A
Andrey Breslav 已提交
501 502
                owner,
                Collections.<AnnotationDescriptor>emptyList(), // TODO
A
Andrey Breslav 已提交
503 504 505
                method.getName()
        );
        methodDescriptorCache.put(method, functionDescriptorImpl);
506 507
        List<TypeParameterDescriptor> typeParameters = makeUninitializedTypeParameters(functionDescriptorImpl, method.getTypeParameters());
        initializeTypeParameters(method);
A
Andrey Breslav 已提交
508 509
        functionDescriptorImpl.initialize(
                null,
A
Andrey Breslav 已提交
510
                DescriptorUtils.getExpectedThisObjectIfNeeded(classDescriptor),
511
                typeParameters,
A
Andrey Breslav 已提交
512
                semanticServices.getDescriptorResolver().resolveParameterDescriptors(functionDescriptorImpl, parameters),
513
                semanticServices.getDescriptorResolver().makeReturnType(returnType, method),
514
                Modality.convertFromFlags(method.hasModifierProperty(PsiModifier.ABSTRACT), !method.hasModifierProperty(PsiModifier.FINAL)),
515
                resolveVisibilityFromPsiModifiers(method)
A
Andrey Breslav 已提交
516
        );
517
        semanticServices.getTrace().record(BindingContext.FUNCTION, method, functionDescriptorImpl);
A
Andrey Breslav 已提交
518 519 520 521 522 523
        FunctionDescriptor substitutedFunctionDescriptor = functionDescriptorImpl;
        if (method.getContainingClass() != psiClass) {
            substitutedFunctionDescriptor = functionDescriptorImpl.substitute(typeSubstitutorForGenericSuperclasses);
        }
        return substitutedFunctionDescriptor;
    }
524

525 526 527
    private JetType makeReturnType(PsiType returnType, PsiMethod method) {
        boolean changeNullable = false;
        boolean nullable = true;
S
Stepan Koltsov 已提交
528 529
        
        String returnTypeFromAnnotation = null;
530 531

        for (PsiAnnotation annotation : method.getModifierList().getAnnotations()) {
S
Stepan Koltsov 已提交
532 533
            if (annotation.getQualifiedName().equals(StdlibNames.JET_METHOD_CLASS)) {
                PsiLiteralExpression nullableExpression = (PsiLiteralExpression) annotation.findAttributeValue(StdlibNames.JET_METHOD_NULLABLE_RETURN_TYPE_FIELD);
534 535 536 537 538 539 540
                if (nullableExpression != null) {
                    nullable = (Boolean) nullableExpression.getValue();
                } else {
                    // default value of parameter
                    nullable = false;
                    changeNullable = true;
                }
S
Stepan Koltsov 已提交
541 542 543 544 545
                
                PsiLiteralExpression returnTypeExpression = (PsiLiteralExpression) annotation.findAttributeValue(StdlibNames.JET_METHOD_RETURN_TYPE_FIELD);
                if (returnTypeExpression != null) {
                    returnTypeFromAnnotation = (String) returnTypeExpression.getValue();
                }
546 547
            }
        }
S
Stepan Koltsov 已提交
548 549 550 551 552 553
        JetType transformedType;
        if (returnTypeFromAnnotation != null) {
            transformedType = semanticServices.getTypeTransformer().transformToType(returnTypeFromAnnotation);
        } else {
            transformedType = semanticServices.getTypeTransformer().transformToType(returnType);
        }
554 555 556 557 558 559 560
        if (changeNullable) {
            return TypeUtils.makeNullableAsSpecified(transformedType, nullable);
        } else {
            return transformedType;
        }
    }

561
    private static Visibility resolveVisibilityFromPsiModifiers(PsiModifierListOwner modifierListOwner) {
562 563 564 565 566 567
        //TODO report error
        return modifierListOwner.hasModifierProperty(PsiModifier.PUBLIC) ? Visibility.PUBLIC :
                                        (modifierListOwner.hasModifierProperty(PsiModifier.PRIVATE) ? Visibility.PRIVATE :
                                        (modifierListOwner.hasModifierProperty(PsiModifier.PROTECTED) ? Visibility.PROTECTED : Visibility.INTERNAL));
    }

568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
    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);
    }
593
}