reflection fix + tests

This commit is contained in:
Konstantin Bulenkov
2012-03-23 18:04:43 +01:00
parent 3f8e4814ae
commit ab3a58a664
12 changed files with 362 additions and 2 deletions

View File

@@ -96,10 +96,20 @@ public class JavaLangClassMemberReference extends PsiReferenceBase<PsiLiteralExp
if (psiClass != null && type != null) {
if (type == Type.DECLARED_FIELD) {
return psiClass.getFields();
} else if (type == Type.DECLARED_METHOD) {
} else if (type == Type.FIELD) {
final List<PsiField> fields = new ArrayList<PsiField>();
for (PsiField field : psiClass.getFields()) {
if (isPublic(field)) {
fields.add(field);
}
}
return fields.toArray();
} else if (type == Type.DECLARED_METHOD || type == Type.METHOD) {
final List<LookupElementBuilder> elements = new ArrayList<LookupElementBuilder>();
for (PsiMethod method : psiClass.getMethods()) {
elements.add(JavaLookupElementBuilder.forMethod(method, PsiSubstitutor.EMPTY).setInsertHandler(this));
if (type == Type.DECLARED_METHOD || isPublic(method)) {
elements.add(JavaLookupElementBuilder.forMethod(method, PsiSubstitutor.EMPTY).setInsertHandler(this));
}
}
return elements.toArray();
}
@@ -126,6 +136,10 @@ public class JavaLangClassMemberReference extends PsiReferenceBase<PsiLiteralExp
}
}
private static boolean isPublic(final PsiMember psiField) {
return psiField.hasModifierProperty(PsiModifier.PUBLIC);
}
private static String getMethodTypes(PsiMethod method) {
final StringBuilder buf = new StringBuilder();
for (PsiParameter parameter : method.getParameterList().getParameters()) {

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 DeclaredField {
void foo() {
Test.class.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 DeclaredField {
void foo() {
Test.class.getDeclaredField("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 DecalredMethod {
void foo() {
Test.class.getDeclaredMethod("<caret>");
}
}
class Test {
public void method(){}
public void method2(A a, B b){}
public 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 DecalredMethod2 {
void foo() {
Test.class.getDeclaredMethod("m<caret>", A.class, B.class);
}
}
class Test {
void method(){}
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 DecalredMethod2 {
void foo() {
Test.class.getDeclaredMethod("method3");
}
}
class Test {
void method(){}
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 DecalredMethod {
void foo() {
Test.class.getDeclaredMethod("method2", A.class, B.class);
}
}
class Test {
public void method(){}
public void method2(A a, B b){}
public 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 Field {
void foo() {
Test.class.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 Field {
void foo() {
Test.class.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 DecalredMethod {
void foo() {
Test.class.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 DecalredMethod {
void foo() {
Test.class.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

@@ -0,0 +1,62 @@
/*
* 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.
*/
package com.intellij.codeInsight.completion;
import com.intellij.JavaTestUtil;
/**
* @author Konstantin Bulenkov
*/
public class JavaReflectionCompletionTest extends LightFixtureCompletionTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
}
@Override
protected String getBasePath() {
return JavaTestUtil.getRelativeJavaTestDataPath() + "/codeInsight/completion/reflection/";
}
public void testField() throws Exception {
doTest(1, "num", "num2");
}
public void testDeclaredField() throws Exception {
doTest(1, "num", "num2", "num3");
}
public void testDeclaredMethod() throws Exception {
doTest(1, "method", "method2", "method3");
}
public void testDeclaredMethod2() throws Exception {
doTest(2, "method", "method2", "method3");
}
public void testMethod() throws Exception {
doTest(1, "method", "method2");
}
private void doTest(int index, String... expected) {
configureByFile(getTestName(false) + ".java");
assertStringItems(expected);
selectItem(getLookup().getItems().get(index));
myFixture.checkResultByFile(getTestName(false) + "_after.java");
}
}