unused declaration: method patterns conversion to manual entry points (IDEA-160856)

This commit is contained in:
Anna Kozlova
2016-09-07 14:55:16 +03:00
parent f858da5fa5
commit 62b7a6528e
2 changed files with 64 additions and 15 deletions
@@ -255,21 +255,23 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
if (!newEntryPoint.isValid()) return;
if (isPersistent) {
if (newEntryPoint instanceof RefClass || newEntryPoint instanceof RefMethod) {
final ClassPattern classPattern = new ClassPattern();
RefClass refClass = newEntryPoint instanceof RefMethod ? ((RefMethod)newEntryPoint).getOwnerClass()
: (RefClass)newEntryPoint;
classPattern.pattern = new SmartRefElementPointerImpl(refClass, true).getFQName();
if (newEntryPoint instanceof RefMethod && !(newEntryPoint instanceof RefImplicitConstructor)) {
classPattern.method = newEntryPoint.getName();
}
getPatterns().add(classPattern);
if (!refClass.isAnonymous()) {
final ClassPattern classPattern = new ClassPattern();
classPattern.pattern = new SmartRefElementPointerImpl(refClass, true).getFQName();
if (newEntryPoint instanceof RefMethod && !(newEntryPoint instanceof RefImplicitConstructor)) {
classPattern.method = getMethodName(newEntryPoint);
}
getPatterns().add(classPattern);
final EntryPointsManager entryPointsManager = getInstance(newEntryPoint.getRefManager().getProject());
if (this != entryPointsManager) {
entryPointsManager.addEntryPoint(newEntryPoint, true);
}
final EntryPointsManager entryPointsManager = getInstance(newEntryPoint.getRefManager().getProject());
if (this != entryPointsManager) {
entryPointsManager.addEntryPoint(newEntryPoint, true);
}
return;
return;
}
}
}
@@ -313,6 +315,12 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
}
}
private static String getMethodName(@NotNull RefElement newEntryPoint) {
String methodSignature = newEntryPoint.getName();
int indexOf = methodSignature.indexOf("(");
return indexOf > 0 ? methodSignature.substring(0, indexOf) : methodSignature;
}
@Override
public void removeEntryPoint(@NotNull RefElement anEntryPoint) {
myTemporaryEntryPoints.remove(anEntryPoint);
@@ -340,13 +348,24 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
}
}
if (anEntryPoint instanceof RefMethod && ((RefMethod)anEntryPoint).isConstructor() || anEntryPoint instanceof RefClass) {
if (anEntryPoint instanceof RefMethod || anEntryPoint instanceof RefClass) {
final RefClass aClass = anEntryPoint instanceof RefClass ? (RefClass)anEntryPoint : ((RefMethod)anEntryPoint).getOwnerClass();
final String qualifiedName = aClass.getQualifiedName();
for (Iterator<ClassPattern> iterator = getPatterns().iterator(); iterator.hasNext(); ) {
if (Comparing.equal(iterator.next().pattern, qualifiedName)) {
//todo if inheritance or pattern?
iterator.remove();
ClassPattern classPattern = iterator.next();
if (Comparing.equal(classPattern.pattern, qualifiedName)) {
if (anEntryPoint instanceof RefMethod && ((RefMethod)anEntryPoint).isConstructor() || anEntryPoint instanceof RefClass) {
if (classPattern.method.isEmpty()) {
//todo if inheritance or pattern?
iterator.remove();
}
}
else {
String methodName = getMethodName(anEntryPoint);
if (methodName.equals(classPattern.method)) {
iterator.remove();
}
}
}
}
}
@@ -20,6 +20,7 @@ import com.intellij.codeInspection.ex.EntryPointsManagerBase
import com.intellij.codeInspection.ex.InspectionManagerEx
import com.intellij.codeInspection.reference.RefClass
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
import junit.framework.TestCase
class UnusedDeclarationClassPatternsTest : LightCodeInsightFixtureTestCase() {
@@ -118,4 +119,33 @@ class UnusedDeclarationClassPatternsTest : LightCodeInsightFixtureTestCase() {
context.cleanup()
}
}
fun testAddRemoveMethodEntryPoint() {
val aClass = myFixture.addClass("public class Foo {void foo(){}}")
val entryPointsManager = EntryPointsManagerBase.getInstance(project)
val context = (InspectionManager.getInstance(project) as InspectionManagerEx).createNewGlobalContext(false)
try {
val refMethod = context.refManager.getReference(aClass.methods[0])
assertNotNull(refMethod)
val refClass = context.refManager.getReference(aClass)
assertNotNull(refClass)
val patterns = entryPointsManager.patterns
assertEmpty(patterns)
entryPointsManager.addEntryPoint(refMethod!!, true)
assertSize(1, patterns)
val classPattern = patterns.iterator().next()
assertEquals("Foo", classPattern.pattern)
assertEquals("foo", classPattern.method)
assertEmpty(entryPointsManager.entryPoints)
entryPointsManager.removeEntryPoint(refMethod)
assertEmpty(patterns)
assertEmpty(entryPointsManager.entryPoints)
}
finally {
context.cleanup()
}
}
}