2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.psi
.resolve
;
18 import com
.intellij
.psi
.*;
19 import com
.intellij
.psi
.infos
.CandidateInfo
;
20 import com
.intellij
.psi
.infos
.MethodCandidateInfo
;
21 import com
.intellij
.psi
.scope
.PsiConflictResolver
;
22 import com
.intellij
.psi
.scope
.PsiScopeProcessor
;
23 import com
.intellij
.psi
.scope
.conflictResolvers
.JavaMethodsConflictResolver
;
24 import com
.intellij
.psi
.scope
.conflictResolvers
.DuplicateConflictResolver
;
25 import com
.intellij
.psi
.scope
.processor
.MethodCandidatesProcessor
;
26 import com
.intellij
.psi
.scope
.processor
.MethodResolverProcessor
;
27 import com
.intellij
.psi
.util
.MethodSignature
;
28 import com
.intellij
.util
.Function
;
29 import com
.intellij
.util
.containers
.ContainerUtil
;
30 import gnu
.trove
.THashSet
;
31 import org
.jetbrains
.annotations
.NotNull
;
32 import org
.jetbrains
.annotations
.Nullable
;
34 import java
.util
.Collection
;
40 public class JavaMethodResolveHelper
{
41 private final Set
<MethodSignature
> myDuplicates
= new THashSet
<MethodSignature
>();
43 private final MethodCandidatesProcessor myProcessor
;
44 @Nullable private final PsiType
[] myParameterTypes
;
46 public JavaMethodResolveHelper(final PsiElement argumentList
, @Nullable final PsiType
[] parameterTypes
) {
47 myParameterTypes
= parameterTypes
;
48 final PsiConflictResolver resolver
= parameterTypes
== null ? DuplicateConflictResolver
.INSTANCE
: new JavaMethodsConflictResolver(argumentList
, parameterTypes
);
49 myProcessor
= new MethodResolverProcessor(argumentList
, new PsiConflictResolver
[]{resolver
}) {
50 protected MethodCandidateInfo
createCandidateInfo(final PsiMethod method
, final PsiSubstitutor substitutor
,
51 final boolean staticProblem
,
52 final boolean accessible
) {
53 return new MethodCandidateInfo(method
, substitutor
, !accessible
, staticProblem
, argumentList
, myCurrentFileContext
, parameterTypes
, PsiType
.EMPTY_ARRAY
);
57 protected boolean isAccepted(final PsiMethod candidate
) {
58 return !candidate
.isConstructor();
63 public void addMethod(PsiMethod method
, PsiSubstitutor substitutor
, boolean staticError
) {
64 if (myDuplicates
.add(method
.getSignature(substitutor
))) {
65 myProcessor
.addMethod(method
, substitutor
, staticError
);
70 public ErrorType
getResolveError() {
71 final CandidateInfo
[] candidates
= myProcessor
.getCandidates();
72 if (candidates
.length
!= 1) return ErrorType
.RESOLVE
;
74 if (!candidates
[0].isStaticsScopeCorrect()) return ErrorType
.STATIC
;
76 final MethodCandidateInfo info
= (MethodCandidateInfo
)candidates
[0];
77 if (myParameterTypes
== null) return ErrorType
.NONE
;
79 if (!info
.isApplicable()) {
80 boolean hasNulls
= false;
81 final PsiParameter
[] parameters
= info
.getElement().getParameterList().getParameters();
82 if (myParameterTypes
.length
== parameters
.length
) {
83 for (int i
= 0; i
< myParameterTypes
.length
; i
++) {
84 PsiType type
= myParameterTypes
[i
];
88 else if (!parameters
[i
].getType().isAssignableFrom(type
)) {
89 return ErrorType
.RESOLVE
;
93 return hasNulls ? ErrorType
.NONE
: ErrorType
.RESOLVE
;
95 return ErrorType
.NONE
;
98 public void handleEvent(final PsiScopeProcessor
.Event event
, final Object associated
) {
99 myProcessor
.handleEvent(event
, associated
);
102 public enum ErrorType
{
103 NONE
, STATIC
, RESOLVE
106 public Collection
<JavaMethodCandidateInfo
> getMethods() {
107 return ContainerUtil
.mapNotNull(myProcessor
.getResult(), new Function
<JavaResolveResult
, JavaMethodCandidateInfo
>() {
108 public JavaMethodCandidateInfo
fun(final JavaResolveResult javaResolveResult
) {
109 return new JavaMethodCandidateInfo((PsiMethod
)javaResolveResult
.getElement(), javaResolveResult
.getSubstitutor());