diff options
author | Jean-Baptiste Queru <jbq@google.com> | 2013-01-08 11:11:20 -0800 |
---|---|---|
committer | Jean-Baptiste Queru <jbq@google.com> | 2013-01-08 11:11:20 -0800 |
commit | b56ea2a18f232d79481e778085fd64e8ae486fc3 (patch) | |
tree | 44e1f6eb4864a45033f865b74fe783e3d784dd6a /java/java-impl/src/com/intellij/psi/resolve | |
download | idea-b56ea2a18f232d79481e778085fd64e8ae486fc3.tar.gz |
Snapshot of commit d5ec1d5018ed24f1b4f32b1d09df6dbd7e2fc425
from branch master of git://git.jetbrains.org/idea/community.git
Diffstat (limited to 'java/java-impl/src/com/intellij/psi/resolve')
-rw-r--r-- | java/java-impl/src/com/intellij/psi/resolve/JavaMethodCandidateInfo.java | 40 | ||||
-rw-r--r-- | java/java-impl/src/com/intellij/psi/resolve/JavaMethodResolveHelper.java | 135 |
2 files changed, 175 insertions, 0 deletions
diff --git a/java/java-impl/src/com/intellij/psi/resolve/JavaMethodCandidateInfo.java b/java/java-impl/src/com/intellij/psi/resolve/JavaMethodCandidateInfo.java new file mode 100644 index 000000000000..11956574f8e1 --- /dev/null +++ b/java/java-impl/src/com/intellij/psi/resolve/JavaMethodCandidateInfo.java @@ -0,0 +1,40 @@ +/* + * Copyright 2000-2009 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.psi.resolve; + +import com.intellij.psi.PsiMethod; +import com.intellij.psi.PsiSubstitutor; + +/** + * @author peter + */ +public class JavaMethodCandidateInfo { + private final PsiMethod myMethod; + private final PsiSubstitutor mySubstitutor; + + public JavaMethodCandidateInfo(PsiMethod method, PsiSubstitutor substitutor) { + myMethod = method; + mySubstitutor = substitutor; + } + + public PsiMethod getMethod() { + return myMethod; + } + + public PsiSubstitutor getSubstitutor() { + return mySubstitutor; + } +} diff --git a/java/java-impl/src/com/intellij/psi/resolve/JavaMethodResolveHelper.java b/java/java-impl/src/com/intellij/psi/resolve/JavaMethodResolveHelper.java new file mode 100644 index 000000000000..3db0c30e8fb4 --- /dev/null +++ b/java/java-impl/src/com/intellij/psi/resolve/JavaMethodResolveHelper.java @@ -0,0 +1,135 @@ +/* + * Copyright 2000-2009 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.psi.resolve; + +import com.intellij.pom.java.LanguageLevel; +import com.intellij.psi.*; +import com.intellij.psi.infos.CandidateInfo; +import com.intellij.psi.infos.MethodCandidateInfo; +import com.intellij.psi.scope.PsiConflictResolver; +import com.intellij.psi.scope.PsiScopeProcessor; +import com.intellij.psi.scope.conflictResolvers.JavaMethodsConflictResolver; +import com.intellij.psi.scope.conflictResolvers.DuplicateConflictResolver; +import com.intellij.psi.scope.processor.MethodCandidatesProcessor; +import com.intellij.psi.scope.processor.MethodResolverProcessor; +import com.intellij.psi.util.MethodSignature; +import com.intellij.psi.util.PsiUtil; +import com.intellij.util.Function; +import com.intellij.util.containers.ContainerUtil; +import gnu.trove.THashSet; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Collection; +import java.util.Set; + +/** + * @author peter + */ +public class JavaMethodResolveHelper { + private final Set<MethodSignature> myDuplicates = new THashSet<MethodSignature>(); + + private final MethodCandidatesProcessor myProcessor; + @Nullable private final PsiType[] myArgumentTypes; + + public JavaMethodResolveHelper(final PsiElement argumentList, @Nullable final PsiType[] argumentTypes) { + myArgumentTypes = argumentTypes; + final LanguageLevel languageLevel = PsiUtil.getLanguageLevel(argumentList); + final PsiConflictResolver resolver = argumentTypes == null ? DuplicateConflictResolver.INSTANCE : new JavaMethodsConflictResolver(argumentList, argumentTypes); + myProcessor = new MethodResolverProcessor(argumentList, new PsiConflictResolver[]{resolver}) { + @Override + protected MethodCandidateInfo createCandidateInfo(final PsiMethod method, final PsiSubstitutor substitutor, + final boolean staticProblem, + final boolean accessible) { + return JavaMethodResolveHelper.this + .createCandidateInfo(method, substitutor, staticProblem, myCurrentFileContext, !accessible, argumentList, argumentTypes, + languageLevel); + } + + @Override + protected boolean isAccepted(final PsiMethod candidate) { + return !candidate.isConstructor(); + } + }; + } + + protected MethodCandidateInfo createCandidateInfo(PsiMethod method, + PsiSubstitutor substitutor, + boolean staticProblem, + PsiElement currentFileContext, + boolean accessProblem, + PsiElement argumentList, + PsiType[] argumentTypes, LanguageLevel languageLevel) { + return new MethodCandidateInfo(method, substitutor, accessProblem, staticProblem, argumentList, currentFileContext, argumentTypes, + PsiType.EMPTY_ARRAY, languageLevel); + } + + public void addMethod(PsiMethod method, PsiSubstitutor substitutor, boolean staticError) { + if (myDuplicates.add(method.getSignature(substitutor))) { + myProcessor.addMethod(method, substitutor, staticError); + } + } + + @NotNull + public ErrorType getResolveError() { + final CandidateInfo[] candidates = myProcessor.getCandidates(); + if (candidates.length != 1) return ErrorType.RESOLVE; + + if (!candidates[0].isStaticsScopeCorrect()) return ErrorType.STATIC; + + return getResolveError((MethodCandidateInfo)candidates[0]); + } + + protected ErrorType getResolveError(MethodCandidateInfo info) { + if (myArgumentTypes == null) return ErrorType.NONE; + + if (!info.isApplicable()) { + boolean hasNulls = false; + //noinspection ConstantConditions + final PsiParameter[] parameters = info.getElement().getParameterList().getParameters(); + if (myArgumentTypes.length == parameters.length) { + for (int i = 0; i < myArgumentTypes.length; i++) { + PsiType type = myArgumentTypes[i]; + if (type == null) { + hasNulls = true; + } + else if (!parameters[i].getType().isAssignableFrom(type)) { + return ErrorType.RESOLVE; + } + } + } + return hasNulls ? ErrorType.NONE : ErrorType.RESOLVE; + } + return ErrorType.NONE; + } + + public void handleEvent(final PsiScopeProcessor.Event event, final Object associated) { + myProcessor.handleEvent(event, associated); + } + + public enum ErrorType { + NONE, STATIC, RESOLVE + } + + public Collection<JavaMethodCandidateInfo> getMethods() { + return ContainerUtil.mapNotNull(myProcessor.getResult(), new Function<JavaResolveResult, JavaMethodCandidateInfo>() { + @Override + public JavaMethodCandidateInfo fun(final JavaResolveResult javaResolveResult) { + return new JavaMethodCandidateInfo((PsiMethod)javaResolveResult.getElement(), javaResolveResult.getSubstitutor()); + } + }); + } +} |