mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-21 22:11:40 +07:00
live templates: incorrect adding generic parameter IDEA-134434
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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>) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import java.util.List;
|
||||
|
||||
class Foo {
|
||||
<T> void method1(final T[] val) {
|
||||
class Inner {
|
||||
void method2() {
|
||||
<caret>
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
class Example {
|
||||
<T> void test(T[] foo) {
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
foo.for<caret>
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
class Example {
|
||||
<T> void test(T[] foo) {
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
for (T t : foo) {
|
||||
<caret>
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
}
|
||||
""")
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user