summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCaroline Rieder2013-06-12 07:31:18 (EDT)
committerSergey Prigogin2013-06-12 12:32:35 (EDT)
commite320522e706e522c0b29f2b7d8f771cf8b0933ac (patch)
treebffdbf97a2840b2b5e987c65f14c5ac6b04c9427
parentee32c3355e9b3c46fe450011d4deb976db99565b (diff)
downloadorg.eclipse.cdt-e320522e706e522c0b29f2b7d8f771cf8b0933ac.zip
org.eclipse.cdt-e320522e706e522c0b29f2b7d8f771cf8b0933ac.tar.gz
org.eclipse.cdt-e320522e706e522c0b29f2b7d8f771cf8b0933ac.tar.bz2
V2: Bug 410074 - Refactoring throws NullPointerExceptionrefs/changes/40/13740/3
Change-Id: Ic2b69a8954a3b9a437a1e8dafaff2f92b10caa6f Reviewed-on: https://git.eclipse.org/r/13740 Reviewed-by: Sergey Prigogin <eclipse.sprigogin@gmail.com> IP-Clean: Sergey Prigogin <eclipse.sprigogin@gmail.com> Tested-by: Sergey Prigogin <eclipse.sprigogin@gmail.com>
-rw-r--r--core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/implementmethod/ImplementMethodRefactoringTest.java3
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/MethodContext.java15
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java7
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/Messages.java1
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/Messages.properties1
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/DefinitionFinder.java37
6 files changed, 42 insertions, 22 deletions
diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/implementmethod/ImplementMethodRefactoringTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/implementmethod/ImplementMethodRefactoringTest.java
index 9071317..2176f0c 100644
--- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/implementmethod/ImplementMethodRefactoringTest.java
+++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/implementmethod/ImplementMethodRefactoringTest.java
@@ -271,9 +271,12 @@ public class ImplementMethodRefactoringTest extends RefactoringTestBase {
//void function_with_impl();
//A.cpp
+ //#include "A.h"
//void function_with_impl() {
//}
//====================
+ //#include "A.h"
+ //
//void function() {
//}
//
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/MethodContext.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/MethodContext.java
index ecc7578..c79e7ec 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/MethodContext.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/MethodContext.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2008, 2010 Institute for Software, HSR Hochschule fuer Technik
+ * Copyright (c) 2008, 2013 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
@@ -8,6 +8,7 @@
*
* Contributors:
* Institute for Software - initial API and implementation
+ * Sergey Prigogin (Google)
*******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring;
@@ -57,17 +58,15 @@ public class MethodContext {
}
public IASTDeclaration getMethodDeclaration() {
- IASTNode parent = declarationName.getParent().getParent();
- if (parent instanceof IASTDeclaration) {
- return (IASTDeclaration) parent;
+ if (declarationName != null) {
+ IASTNode parent = declarationName.getParent().getParent();
+ if (parent instanceof IASTDeclaration) {
+ return (IASTDeclaration) parent;
+ }
}
return null;
}
- public Visibility getMethodDeclarationVisibility() {
- return Visibility.getVisibility(declarationName);
- }
-
public void setMethodQName(ICPPASTQualifiedName qname) {
this.qname = qname;
}
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java
index 7b1b8a0..e3e44f5 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java
@@ -215,6 +215,10 @@ public class ExtractFunctionRefactoring extends CRefactoring {
info.setDeclarator(getDeclaration(container.getNodesToWrite().get(0)));
MethodContext context = NodeHelper.findMethodContext(container.getNodesToWrite().get(0),
refactoringContext, sm.newChild(1));
+ if (context.getType() == ContextType.METHOD && context.getMethodDeclarationName() == null) {
+ initStatus.addFatalError(Messages.ExtractFunctionRefactoring_no_declaration_of_surrounding_method);
+ return initStatus;
+ }
info.setMethodContext(context);
return initStatus;
} finally {
@@ -290,8 +294,9 @@ public class ExtractFunctionRefactoring extends CRefactoring {
MethodContext context = info.getMethodContext();
if (context.getType() == ContextType.METHOD && !context.isInline()) {
+ IASTDeclaration contextDeclaration = context.getMethodDeclaration();
ICPPASTCompositeTypeSpecifier classDeclaration =
- (ICPPASTCompositeTypeSpecifier) context.getMethodDeclaration().getParent();
+ (ICPPASTCompositeTypeSpecifier) contextDeclaration.getParent();
IASTSimpleDeclaration methodDeclaration = getDeclaration(methodName);
if (isMethodAllreadyDefined(methodDeclaration, classDeclaration, getIndex())) {
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/Messages.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/Messages.java
index 74fa495..b30d36f 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/Messages.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/Messages.java
@@ -18,6 +18,7 @@ public final class Messages extends NLS {
public static String ExtractFunctionRefactoring_ExtractFunction;
public static String ExtractFunctionRefactoring_NoStmtSelected;
public static String ExtractFunctionRefactoring_TooManySelected;
+ public static String ExtractFunctionRefactoring_no_declaration_of_surrounding_method;
public static String ExtractFunctionRefactoring_name_in_use;
public static String ExtractFunctionRefactoring_parameter_name_in_use;
public static String ExtractFunctionRefactoring_duplicate_parameter;
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/Messages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/Messages.properties
index 6942af2..ed09621 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/Messages.properties
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/Messages.properties
@@ -13,6 +13,7 @@
ExtractFunctionRefactoring_ExtractFunction=Extract Function
ExtractFunctionRefactoring_NoStmtSelected=No statement selected
ExtractFunctionRefactoring_TooManySelected=Too many declarations in selection.
+ExtractFunctionRefactoring_no_declaration_of_surrounding_method=Unable to find declaration of the surrounding method.
ExtractFunctionRefactoring_name_in_use=Name already in use.
ExtractFunctionRefactoring_parameter_name_in_use=''{0}'' is already used as a name in the selected code
ExtractFunctionRefactoring_duplicate_parameter=A parameter ''{0}'' already exists
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/DefinitionFinder.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/DefinitionFinder.java
index d32d2c1..af2328c 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/DefinitionFinder.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/DefinitionFinder.java
@@ -31,6 +31,7 @@ import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMember;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexBinding;
+import org.eclipse.cdt.core.index.IIndexFile;
import org.eclipse.cdt.core.index.IIndexName;
import org.eclipse.cdt.core.model.CoreModelUtil;
import org.eclipse.cdt.core.model.ITranslationUnit;
@@ -63,7 +64,7 @@ public class DefinitionFinder {
if (binding == null) {
return null;
}
- return getDefinition(binding, context, pm);
+ return getDefinition(binding, name.getTranslationUnit(), context, pm);
}
/**
@@ -71,14 +72,17 @@ public class DefinitionFinder {
* of dirty editors.
*
* @param binding the binding to find the definition for
+ * @param contextTu the translation unit that determines the set of files to search for
+ * the definition. Only the files directly or indirectly included by the translation unit
+ * are considered.
* @param context the refactoring context
* @param pm the progress monitor
* @return the definition name, or {@code null} if there is no definition or if it is
* not unique.
* @throws CoreException thrown in case of errors
*/
- public static IASTName getDefinition(IBinding binding, CRefactoringContext context,
- IProgressMonitor pm) throws CoreException {
+ public static IASTName getDefinition(IBinding binding, IASTTranslationUnit contextTu,
+ CRefactoringContext context, IProgressMonitor pm) throws CoreException {
SubMonitor sm = SubMonitor.convert(pm, 10);
IIndex index = context.getIndex();
if (index == null) {
@@ -97,12 +101,16 @@ public class DefinitionFinder {
if (sm.isCanceled()) {
throw new OperationCanceledException();
}
- ITranslationUnit tu = CoreModelUtil.findTranslationUnitForLocation(
- name.getFile().getLocation(), null);
- if (searchedFiles.add(tu.getLocation().toOSString())) {
- findDefinitionsInTranslationUnit(indexBinding, tu, context, definitions, pm);
- if (definitions.size() > 1)
- return null;
+ IIndexFile indexFile = name.getFile();
+ if (contextTu.getASTFileSet().contains(indexFile) ||
+ contextTu.getIndexFileSet().contains(indexFile)) {
+ ITranslationUnit tu = CoreModelUtil.findTranslationUnitForLocation(
+ indexFile.getLocation(), null);
+ if (searchedFiles.add(tu.getLocation().toOSString())) {
+ findDefinitionsInTranslationUnit(indexBinding, tu, context, definitions, pm);
+ if (definitions.size() > 1)
+ return null;
+ }
}
loopProgress.setWorkRemaining(--remainingCount);
}
@@ -230,7 +238,7 @@ public class DefinitionFinder {
IBinding binding = memberName.resolveBinding();
if (!(binding instanceof ICPPMember))
return null;
- return getMemberDeclaration((ICPPMember) binding, context, pm);
+ return getMemberDeclaration((ICPPMember) binding, memberName.getTranslationUnit(), context, pm);
}
/**
@@ -239,15 +247,18 @@ public class DefinitionFinder {
* editors.
*
* @param member the class member binding to find the declaration for
+ * @param contextTu the translation unit that determines the set of files to search for
+ * the declaration. Only the files directly or indirectly included by the translation unit
+ * are considered.
* @param context the refactoring context
* @param pm the progress monitor
* @return the declaration name, or {@code null} if there is no declaration or if it is
* not unique.
* @throws CoreException thrown in case of errors
*/
- public static IASTName getMemberDeclaration(ICPPMember member, CRefactoringContext context,
- IProgressMonitor pm) throws CoreException, OperationCanceledException {
- IASTName classDefintionName = getDefinition(member.getClassOwner(), context, pm);
+ public static IASTName getMemberDeclaration(ICPPMember member, IASTTranslationUnit contextTu,
+ CRefactoringContext context, IProgressMonitor pm) throws CoreException, OperationCanceledException {
+ IASTName classDefintionName = getDefinition(member.getClassOwner(), contextTu, context, pm);
if (classDefintionName == null)
return null;
IASTCompositeTypeSpecifier compositeTypeSpecifier =