diff --git a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ImplementMethod.rts b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ImplementMethod.rts index ba804a149a3..7fc4d475cc9 100644 --- a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ImplementMethod.rts +++ b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ImplementMethod.rts @@ -838,3 +838,22 @@ void TestClass::foo() { } +//!Bug 355006 - NPE implementing template function +//#org.eclipse.cdt.ui.tests.refactoring.implementmethod.ImplementMethodRefactoringTest +//@.config +filename=A.h +infos=1 +//@A.h + +/*$*/template void func(T&);/*$$*/ + +//= + +template void func(T&); + +template inline void func(T& ) +{ +} + + + diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/implementmethod/ImplementMethodRefactoring.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/implementmethod/ImplementMethodRefactoring.java index bf101d1f1e6..510c842227f 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/implementmethod/ImplementMethodRefactoring.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/implementmethod/ImplementMethodRefactoring.java @@ -260,15 +260,16 @@ public class ImplementMethodRefactoring extends CRefactoring2 { IASTFunctionDefinition functionDefinition = nodeFactory.newFunctionDefinition(declSpecifier, createdMethodDeclarator, nodeFactory.newCompoundStatement()); functionDefinition.setParent(unit); - if (NodeHelper.isContainedInTemplateDeclaration(declarationParent)) { - ICPPASTTemplateDeclaration templateDeclaration = nodeFactory.newTemplateDeclaration(functionDefinition); - templateDeclaration.setParent(unit); + ICPPASTTemplateDeclaration templateDeclaration = NodeHelper.findContainedTemplateDecalaration(declarationParent); + if (templateDeclaration != null) { + ICPPASTTemplateDeclaration newTemplateDeclaration = nodeFactory.newTemplateDeclaration(functionDefinition); + newTemplateDeclaration.setParent(unit); - for (ICPPASTTemplateParameter templateParameter : ((ICPPASTTemplateDeclaration) declarationParent.getParent().getParent() ).getTemplateParameters()) { - templateDeclaration.addTemplateParameter(templateParameter.copy(CopyStyle.withLocations)); + for (ICPPASTTemplateParameter templateParameter : templateDeclaration.getTemplateParameters()) { + newTemplateDeclaration.addTemplateParameter(templateParameter.copy(CopyStyle.withLocations)); } - return templateDeclaration; + return newTemplateDeclaration; } return functionDefinition; } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/NodeHelper.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/NodeHelper.java index fac66b73716..3aad6f7f999 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/NodeHelper.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/NodeHelper.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2009 Institute for Software, HSR Hochschule fuer Technik + * Copyright (c) 2008, 2011 Institute for Software, HSR Hochschule fuer Technik * Rapperswil, University of applied sciences and others * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -212,11 +212,16 @@ public class NodeHelper { } public static boolean isContainedInTemplateDeclaration(IASTNode node) { - if (node == null) { - return false; - } else if (node instanceof ICPPASTTemplateDeclaration) { - return true; + return findContainedTemplateDecalaration(node) != null; + } + + public static ICPPASTTemplateDeclaration findContainedTemplateDecalaration(IASTNode node) { + while (node != null) { + if (node instanceof ICPPASTTemplateDeclaration) { + return (ICPPASTTemplateDeclaration) node; + } + node = node.getParent(); } - return isContainedInTemplateDeclaration(node.getParent()); + return null; } }