From 4cdb7e40c98b40a048f5cc9ad1ea061ea905ed61 Mon Sep 17 00:00:00 2001 From: Emanuel Graf Date: Fri, 5 Sep 2008 09:33:57 +0000 Subject: [PATCH] FIXED - bug 246062: [Refactoring] NPE extracting a constant from an inlined method https://bugs.eclipse.org/bugs/show_bug.cgi?id=246062 --- .../resources/refactoring/ExtractConstant.rts | 22 +++++++++++++++++++ .../ExtractConstantRefactoring.java | 9 +++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractConstant.rts b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractConstant.rts index 8743b71dcbb..b0b02fd4774 100644 --- a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractConstant.rts +++ b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractConstant.rts @@ -449,3 +449,25 @@ int A::foo() return theAnswer; } +//!Bug 246062 [Refactoring] NPE extracting a constant from an inlined method +//#org.eclipse.cdt.ui.tests.refactoring.extractconstant.ExtractConstantRefactoringTest +//@.config +filename=A.h +//@A.h +class X { + void method() { + int a= /*$*/1/*$$*/; + } +}; + +//= +namespace +{ + const int theAnswer = 1; +} +class X { + void method() { + int a= theAnswer; + } +}; + diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoring.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoring.java index 4b3dec63187..04f1725233d 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoring.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoring.java @@ -170,7 +170,14 @@ public class ExtractConstantRefactoring extends CRefactoring { if (binding instanceof CPPMethod) { CPPMethod methode = (CPPMethod) binding; - IASTNode decl = methode.getDeclarations()[0]; + IASTNode[] declarations = methode.getDeclarations(); + + IASTNode decl; + if(declarations != null) { + decl = declarations[0]; + }else { + decl = methode.getDefinition(); + } IASTNode spec = decl.getParent().getParent(); if (spec instanceof ICPPASTCompositeTypeSpecifier) {