live templates: incorrect adding generic parameter IDEA-134434

This commit is contained in:
Andrey Starovoyt
2014-12-23 18:14:29 +03:00
parent 74eaf5419f
commit b20fc1c936
9 changed files with 108 additions and 21 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2014 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.
@@ -68,27 +68,33 @@ public class JavaTemplateUtil {
PsiElement element = file.findElementAt(segmentStart);
PsiMethod method = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
if (method != null) {
if (((PsiTypeParameter)aClass).getOwner() instanceof PsiMethod || method.hasModifierProperty(PsiModifier.STATIC)) {
PsiTypeParameterList paramList = method.getTypeParameterList();
PsiTypeParameter[] params = paramList != null ? paramList.getTypeParameters() : PsiTypeParameter.EMPTY_ARRAY;
for (PsiTypeParameter param : params) {
if (param.getName().equals(aClass.getName())) return;
}
try {
if (paramList == null) {
final PsiTypeParameterList newList =
JVMElementFactories.getFactory(method.getLanguage(), project).createTypeParameterList();
paramList = (PsiTypeParameterList)method.addAfter(newList, method.getModifierList());
}
paramList.add(aClass.copy());
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document);
}
catch (IncorrectOperationException e) {
LOG.error(e);
if (!method.hasModifierProperty(PsiModifier.STATIC)) {
PsiTypeParameterListOwner owner = ((PsiTypeParameter)aClass).getOwner();
if (PsiTreeUtil.isAncestor(owner, method, false)) {
continue;
}
}
PsiTypeParameterList paramList = method.getTypeParameterList();
PsiTypeParameter[] params = paramList != null ? paramList.getTypeParameters() : PsiTypeParameter.EMPTY_ARRAY;
for (PsiTypeParameter param : params) {
if (param.getName().equals(aClass.getName())) return;
}
try {
if (paramList == null) {
final PsiTypeParameterList newList =
JVMElementFactories.getFactory(method.getLanguage(), project).createTypeParameterList();
paramList = (PsiTypeParameterList)method.addAfter(newList, method.getModifierList());
}
paramList.add(aClass.copy());
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document);
}
catch (IncorrectOperationException e) {
LOG.error(e);
}
}
} else if (!noImport) {
}
else if (!noImport) {
addImportForClass(document, aClass, segmentStart, segmentEnd);
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document);
}

View File

@@ -0,0 +1,15 @@
// "Create method 'f'" "true"
public class CreateMethodTest {
public <T> void aMethod(final T t) {
class Nested {
public T call() {
T result = f(t);
return result;
}
private T f(T t) {
<selection>return null;</selection>
}
}
}
}

View File

@@ -0,0 +1,11 @@
// "Create method 'f'" "true"
public class CreateMethodTest {
public <T> void aMethod(final T t) {
class Nested {
public T call() {
T result = f<caret>(t);
return result;
}
}
}
}

View File

@@ -0,0 +1,13 @@
import java.util.List;
class Foo {
<T> void method1(final T[] val) {
class Inner {
void method2() {
for (T t : <selection>val</selection><caret>) {
}
}
}
}
}

View File

@@ -0,0 +1,11 @@
import java.util.List;
class Foo {
<T> void method1(final T[] val) {
class Inner {
void method2() {
<caret>
}
}
}
}

View File

@@ -0,0 +1,10 @@
class Example {
<T> void test(T[] foo) {
new Runnable() {
@Override
public void run() {
foo.for<caret>
}
};
}
}

View File

@@ -0,0 +1,12 @@
class Example {
<T> void test(T[] foo) {
new Runnable() {
@Override
public void run() {
for (T t : foo) {
<caret>
}
}
};
}
}

View File

@@ -385,6 +385,13 @@ class Foo {
checkResult();
}
public void testIterParameterizedInnerInMethod() {
configure();
startTemplate("iter", "iterations")
stripTrailingSpaces();
checkResult();
}
public void testAsListToar() {
configure();
startTemplate("toar", "other")
@@ -955,6 +962,4 @@ class Foo {
}
""")
}
}

View File

@@ -34,6 +34,10 @@ public class ForeachTemplateTest extends PostfixTemplateTestCase {
doTest();
}
public void testInAnonymousRunnable() {
doTest();
}
public void testFinalLocals() {
CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());
boolean oldGenerateFinalLocals = settings.GENERATE_FINAL_LOCALS;