From 9c47fdf7ec3ad48730334c7c460fa47a0f50ff2e Mon Sep 17 00:00:00 2001 From: Anton Leherbauer Date: Wed, 2 Jun 2010 14:16:55 +0000 Subject: [PATCH] Bug 315365 - [templates] ClassCastException when using ${todo} in file template --- .../corext/codemanipulation/StubUtility.java | 15 ++++++++++++--- .../template/c/CodeTemplateContextType.java | 9 +++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/codemanipulation/StubUtility.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/codemanipulation/StubUtility.java index 063dfa711a4..f1d268d9372 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/codemanipulation/StubUtility.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/codemanipulation/StubUtility.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2001, 2008 IBM Corporation and others. + * Copyright (c) 2001, 2010 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -130,10 +130,19 @@ public class StubUtility { } public static String getFileContent(Template template, IFile file, String lineDelimiter) throws CoreException { - FileTemplateContext context= new FileTemplateContext(template.getContextTypeId(), lineDelimiter); + ICProject cproject = null; + final IProject project = file.getProject(); + if (CoreModel.hasCNature(project)) { + cproject = CoreModel.getDefault().create(project); + } + FileTemplateContext context; + if (cproject != null) { + context= new CodeTemplateContext(template.getContextTypeId(), cproject, lineDelimiter); + } else { + context= new FileTemplateContext(template.getContextTypeId(), lineDelimiter); + } String fileComment= getFileComment(file, lineDelimiter); context.setVariable(CodeTemplateContextType.FILE_COMMENT, fileComment != null ? fileComment : ""); //$NON-NLS-1$ - ICProject cproject = CoreModel.getDefault().create(file.getProject()); String includeGuardSymbol= generateIncludeGuardSymbol(file, cproject); context.setVariable(CodeTemplateContextType.INCLUDE_GUARD_SYMBOL, includeGuardSymbol != null ? includeGuardSymbol : ""); //$NON-NLS-1$ context.setResourceVariables(file); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/CodeTemplateContextType.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/CodeTemplateContextType.java index 25c0188b90f..2a45decfbfe 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/CodeTemplateContextType.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/template/c/CodeTemplateContextType.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2008 IBM Corporation and others. + * Copyright (c) 2000, 2010 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -17,6 +17,7 @@ import org.eclipse.jface.text.templates.TemplateException; import org.eclipse.jface.text.templates.TemplateVariableResolver; import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.internal.corext.codemanipulation.StubUtility; @@ -99,7 +100,11 @@ public class CodeTemplateContextType extends FileTemplateContextType { @Override protected String resolve(TemplateContext context) { - String todoTaskTag= StubUtility.getTodoTaskTag(((CodeTemplateContext) context).getCProject()); + ICProject cProject = null; + if (context instanceof CodeTemplateContext) { + cProject = ((CodeTemplateContext) context).getCProject(); + } + String todoTaskTag= StubUtility.getTodoTaskTag(cProject); if (todoTaskTag == null) return "XXX"; //$NON-NLS-1$