This commit is contained in:
Konstantin Bulenkov
2012-03-26 12:25:39 +02:00
parent 957a0bbfb0
commit d84822f977
11 changed files with 272 additions and 8 deletions

View File

@@ -20,11 +20,11 @@ import com.intellij.codeInsight.completion.InsertionContext;
import com.intellij.codeInsight.completion.JavaLookupElementBuilder;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiTypesUtil;
import com.intellij.psi.util.PsiUtilCore;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -35,9 +35,9 @@ import java.util.List;
* @author Konstantin Bulenkov
*/
public class JavaLangClassMemberReference extends PsiReferenceBase<PsiLiteralExpression> implements InsertHandler<LookupElement> {
private final PsiClassObjectAccessExpression myContext;
private final PsiExpression myContext;
public JavaLangClassMemberReference(PsiLiteralExpression literal, PsiClassObjectAccessExpression context) {
public JavaLangClassMemberReference(PsiLiteralExpression literal, PsiExpression context) {
super(literal);
myContext = context;
}
@@ -67,7 +67,26 @@ public class JavaLangClassMemberReference extends PsiReferenceBase<PsiLiteralExp
@Nullable
private PsiClass getPsiClass() {
return PsiTypesUtil.getPsiClass(myContext.getOperand().getType());
if (myContext instanceof PsiClassObjectAccessExpression) {
return PsiTypesUtil.getPsiClass(((PsiClassObjectAccessExpression)myContext).getOperand().getType());
} else if (myContext instanceof PsiMethodCallExpression) {
final PsiMethod method = ((PsiMethodCallExpression)myContext).resolveMethod();
if (method != null && "forName".equals(method.getName()) && isClass(method.getContainingClass())) {
final PsiExpression[] expressions = ((PsiMethodCallExpression)myContext).getArgumentList().getExpressions();
if (expressions.length == 1 && expressions[0] instanceof PsiLiteralExpression) {
final Object value = ((PsiLiteralExpression)expressions[0]).getValue();
if (value instanceof String) {
final Project project = myContext.getProject();
return JavaPsiFacade.getInstance(project).findClass(String.valueOf(value), GlobalSearchScope.allScope(project));
}
}
}
}
return null;
}
private static boolean isClass(PsiClass aClass) {
return aClass != null && CommonClassNames.JAVA_LANG_CLASS.equals(aClass.getQualifiedName());
}
@Nullable

View File

@@ -33,8 +33,8 @@ public class JavaReflectionReferenceProvider extends PsiReferenceProvider {
final PsiElement expressionList;
if (value != null && (expressionList = element.getParent()) instanceof PsiExpressionList) {
final PsiElement methodCall = expressionList.getParent();
final PsiClassObjectAccessExpression classAccess;
if (methodCall != null && (classAccess = PsiTreeUtil.findChildOfType(methodCall, PsiClassObjectAccessExpression.class)) != null) {
final PsiExpression classAccess;
if (methodCall != null && (classAccess = getContext(methodCall)) != null) {
return new PsiReference[]{new JavaLangClassMemberReference((PsiLiteralExpression)element, classAccess)};
}
}
@@ -42,6 +42,12 @@ public class JavaReflectionReferenceProvider extends PsiReferenceProvider {
return PsiReference.EMPTY_ARRAY;
}
@Nullable
private static PsiExpression getContext(PsiElement methodCall) {
final PsiClassObjectAccessExpression expression = PsiTreeUtil.findChildOfType(methodCall, PsiClassObjectAccessExpression.class);
return expression == null ? PsiTreeUtil.findChildOfType(methodCall, PsiMethodCallExpression.class) : expression;
}
@Nullable
private static String getValue(PsiLiteralExpression element) {
final Object value = element.getValue();

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2000-2012 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.
*/
class ForNameDeclaredField {
void foo() {
Class.forName("Test").getDeclaredField("<caret>");
}
}
class Test {
public int num;
public int num2;
int num3;
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2000-2012 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.
*/
class ForNameDeclaredField {
void foo() {
Class.forName("Test").getDeclaredField("num3");
}
}
class Test {
public int num;
public int num2;
int num3;
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2000-2012 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.
*/
class ForNameDeclaredMethod {
void foo() {
Class.forName("Test").getDeclaredMethod("<caret>");
}
}
class Test {
public void method(){}
public void method2(A a, B b){}
void method3(){}
}
class A {}
class B {}
class C {}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2000-2012 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.
*/
class ForNameDeclaredMethod {
void foo() {
Class.forName("Test").getDeclaredMethod("method2", A.class, B.class);
}
}
class Test {
public void method(){}
public void method2(A a, B b){}
void method3(){}
}
class A {}
class B {}
class C {}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2000-2012 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.
*/
class ForNameField {
void foo() {
Class.forName("Test").getField("<caret>");
}
}
class Test {
public int num;
public int num2;
int num3;
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2000-2012 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.
*/
class ForNameField {
void foo() {
Class.forName("Test").getField("num2");
}
}
class Test {
public int num;
public int num2;
int num3;
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2000-2012 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.
*/
class ForNameMethod {
void foo() {
Class.forName("Test").getMethod("<caret>");
}
}
class Test {
public void method(){}
public void method2(A a, B b){}
void method3(){}
}
class A {}
class B {}
class C {}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2000-2012 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.
*/
class ForNameMethod {
void foo() {
Class.forName("Test").getMethod("method2", A.class, B.class);
}
}
class Test {
public void method(){}
public void method2(A a, B b){}
void method3(){}
}
class A {}
class B {}
class C {}

View File

@@ -52,6 +52,21 @@ public class JavaReflectionCompletionTest extends LightFixtureCompletionTestCase
doTest(1, "method", "method2");
}
public void testForNameDeclaredMethod() throws Exception {
doTest(1, "method", "method2", "method3");
}
public void testForNameMethod() throws Exception {
doTest(1, "method", "method2");
}
public void testForNameField() throws Exception {
doTest(1, "num", "num2");
}
public void testForNameDeclaredField() throws Exception {
doTest(2, "num", "num2", "num3");
}
private void doTest(int index, String... expected) {
configureByFile(getTestName(false) + ".java");