From 3c2fd7ab95931e4527b67854237535705b9f8cdb Mon Sep 17 00:00:00 2001 From: Alena Laskavaia Date: Sun, 16 May 2010 02:18:02 +0000 Subject: [PATCH] Cleaned up problem preferences interfaces and fixed persistence --- .../NamingConventionFunctionChecker.java | 22 +-- .../internal/checkers/ReturnChecker.java | 8 +- .../checkers/StatementHasNoEffectChecker.java | 8 +- .../cxx/model/AbstractAstFunctionChecker.java | 4 +- .../cxx/model/AbstractIndexAstChecker.java | 31 +-- .../.settings/org.eclipse.jdt.ui.prefs | 55 ++++++ .../param/BasicProblemPreferenceTest.java | 140 ++++++++++++++ .../core/param/ListProblemPreferenceTest.java | 109 +++++++++++ .../core/param/MapProblemPreferenceTest.java | 97 ++++++++++ .../core/test/AutomatedIntegrationSuite.java | 1 + .../codan/core/test/CodanFastTestSuite.java | 44 +++++ .../.settings/org.eclipse.jdt.ui.prefs | 3 +- .../META-INF/MANIFEST.MF | 3 +- ...AbstractCheckerWithProblemPreferences.java | 99 ++++++++++ ...ters.java => ICheckerWithPreferences.java} | 4 +- .../cdt/codan/core/model/IProblem.java | 8 +- .../codan/core/model/IProblemWorkingCopy.java | 19 +- .../param/AbstractProblemParameterInfo.java | 87 --------- .../core/param/AbstractProblemPreference.java | 156 +++++++++++++++ .../core/param/BasicProblemPreference.java | 130 +++++++++++++ .../codan/core/param/IProblemPreference.java | 19 ++ .../param/IProblemPreferenceContainer.java | 22 +++ ...java => IProblemPreferenceDescriptor.java} | 41 ++-- .../core/param/IProblemPreferenceValue.java | 31 +++ .../codan/core/param/ListParameterInfo.java | 87 --------- .../core/param/ListProblemPreference.java | 177 ++++++++++++++++++ .../codan/core/param/MapParameterInfo.java | 83 -------- .../core/param/MapProblemPreference.java | 153 +++++++++++++++ .../codan/core/param/SingleParameterInfo.java | 114 ----------- .../codan/internal/core/CheckersRegisry.java | 15 +- .../internal/core/CodanPreferencesLoader.java | 54 +++++- .../internal/core/model/CodanProblem.java | 23 +-- ...NamingConventionFunctionIIndexChecker.java | 31 ++- .../.settings/org.eclipse.jdt.ui.prefs | 55 ++++++ .../codan/internal/ui/CodanUIMessages.java | 2 +- .../ui/dialogs/ParametersComposite.java | 159 +++++++++------- .../cdt/codan/internal/ui/messages.properties | 8 +- .../ui/preferences/CodanPreferencePage.java | 61 +++--- .../ui/preferences/ProblemsTreeEditor.java | 81 +++++--- 39 files changed, 1617 insertions(+), 627 deletions(-) create mode 100644 codan/org.eclipse.cdt.codan.core.test/.settings/org.eclipse.jdt.ui.prefs create mode 100644 codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/param/BasicProblemPreferenceTest.java create mode 100644 codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/param/ListProblemPreferenceTest.java create mode 100644 codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/param/MapProblemPreferenceTest.java create mode 100644 codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CodanFastTestSuite.java create mode 100644 codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/AbstractCheckerWithProblemPreferences.java rename codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/{ICheckerWithParameters.java => ICheckerWithPreferences.java} (92%) delete mode 100644 codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/AbstractProblemParameterInfo.java create mode 100644 codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/AbstractProblemPreference.java create mode 100644 codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/BasicProblemPreference.java create mode 100644 codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/IProblemPreference.java create mode 100644 codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/IProblemPreferenceContainer.java rename codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/{IProblemParameterInfo.java => IProblemPreferenceDescriptor.java} (77%) create mode 100644 codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/IProblemPreferenceValue.java delete mode 100644 codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/ListParameterInfo.java create mode 100644 codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/ListProblemPreference.java delete mode 100644 codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/MapParameterInfo.java create mode 100644 codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/MapProblemPreference.java delete mode 100644 codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/SingleParameterInfo.java create mode 100644 codan/org.eclipse.cdt.codan.ui/.settings/org.eclipse.jdt.ui.prefs diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/NamingConventionFunctionChecker.java b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/NamingConventionFunctionChecker.java index aa745964d80..ebe2c8551e7 100644 --- a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/NamingConventionFunctionChecker.java +++ b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/NamingConventionFunctionChecker.java @@ -13,10 +13,9 @@ package org.eclipse.cdt.codan.internal.checkers; import java.util.regex.Pattern; import org.eclipse.cdt.codan.core.cxx.model.AbstractIndexAstChecker; -import org.eclipse.cdt.codan.core.model.ICheckerWithParameters; +import org.eclipse.cdt.codan.core.model.ICheckerWithPreferences; import org.eclipse.cdt.codan.core.model.IProblem; import org.eclipse.cdt.codan.core.model.IProblemWorkingCopy; -import org.eclipse.cdt.codan.core.param.SingleParameterInfo; import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.IASTDeclaration; import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition; @@ -29,10 +28,9 @@ import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; * */ public class NamingConventionFunctionChecker extends AbstractIndexAstChecker - implements ICheckerWithParameters { - private static final String DEFAULT_PATTERN = "^[a-z]"; // default pattern name starts with english lowercase letter //$NON-NLS-1$ - public static final String PARAM_KEY = "pattern"; //$NON-NLS-1$ + implements ICheckerWithPreferences { private static final String ER_ID = "org.eclipse.cdt.codan.internal.checkers.NamingConventionFunctionChecker"; //$NON-NLS-1$ + public static final String PARAM_KEY = "pattern"; //$NON-NLS-1$ public void processAst(IASTTranslationUnit ast) { final IProblem pt = getProblemById(ER_ID, getFile()); @@ -44,7 +42,7 @@ public class NamingConventionFunctionChecker extends AbstractIndexAstChecker public int visit(IASTDeclaration element) { if (element instanceof IASTFunctionDefinition) { - String parameter = (String) pt.getParameter(PARAM_KEY); + String parameter = (String) getPreference(pt,PARAM_KEY); Pattern pattern = Pattern.compile(parameter); IASTName astName = ((IASTFunctionDefinition) element) .getDeclarator().getName(); @@ -65,13 +63,15 @@ public class NamingConventionFunctionChecker extends AbstractIndexAstChecker * (non-Javadoc) * * @see - * org.eclipse.cdt.codan.core.model.ICheckerWithParameters#initParameters + * org.eclipse.cdt.codan.core.model.ICheckerWithPreferences#initParameters * (org.eclipse.cdt.codan.core.model.IProblemWorkingCopy) */ - public void initParameters(IProblemWorkingCopy problem) { - problem.setParameterInfo(new SingleParameterInfo(PARAM_KEY, - CheckersMessages.NamingConventionFunctionChecker_LabelNamePattern)); - problem.setParameter(PARAM_KEY, DEFAULT_PATTERN); + public void initPreferences(IProblemWorkingCopy problem) { + addPreference( + problem, + PARAM_KEY, + CheckersMessages.NamingConventionFunctionChecker_LabelNamePattern, + "^[a-z]"); //$NON-NLS-1$ } @Override diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ReturnChecker.java b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ReturnChecker.java index 4b10032e28a..33ff9c5e94d 100644 --- a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ReturnChecker.java +++ b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ReturnChecker.java @@ -98,7 +98,7 @@ public class ReturnChecker extends AbstractAstFunctionChecker { */ protected boolean checkImplicitReturn(String id) { final IProblem pt = getProblemById(id, getFile()); - return (Boolean) pt.getParameter(PARAM_IMPLICIT); + return (Boolean) getPreference(pt,PARAM_IMPLICIT); } /** @@ -158,11 +158,11 @@ public class ReturnChecker extends AbstractAstFunctionChecker { return type; } - /* checker must implement @link ICheckerWithParameters */ - public void initParameters(IProblemWorkingCopy problem) { + /* checker must implement @link ICheckerWithPreferences */ + public void initPreferences(IProblemWorkingCopy problem) { if (problem.getId().equals(RET_NO_VALUE_ID) || problem.getId().equals(RET_NORET_ID)) { - addParam(problem, PARAM_IMPLICIT, + addPreference(problem, PARAM_IMPLICIT, CheckersMessages.ReturnChecker_Param0, Boolean.FALSE); } } diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/StatementHasNoEffectChecker.java b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/StatementHasNoEffectChecker.java index 5ae1cebbf61..2379cd527fd 100644 --- a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/StatementHasNoEffectChecker.java +++ b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/StatementHasNoEffectChecker.java @@ -11,6 +11,7 @@ package org.eclipse.cdt.codan.internal.checkers; import org.eclipse.cdt.codan.core.cxx.model.AbstractIndexAstChecker; +import org.eclipse.cdt.codan.core.model.IProblemWorkingCopy; import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression; import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement; @@ -27,15 +28,14 @@ import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTCompoundStatementExpression; * Checker that detects statements without effect such as * * a+b; - * * or - * * +b; * * */ public class StatementHasNoEffectChecker extends AbstractIndexAstChecker { private static final String ER_ID = "org.eclipse.cdt.codan.internal.checkers.StatementHasNoEffectProblem"; //$NON-NLS-1$ + private static final String PARAM_MACRO_ID = "macro"; public void processAst(IASTTranslationUnit ast) { ast.accept(new CheckStmpVisitor()); @@ -111,4 +111,8 @@ public class StatementHasNoEffectChecker extends AbstractIndexAstChecker { return false; } } + + public void initPreferences(IProblemWorkingCopy problem) { + addPreference(problem, PARAM_MACRO_ID, "Check statements that belong to macro", Boolean.TRUE); + } } diff --git a/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/model/AbstractAstFunctionChecker.java b/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/model/AbstractAstFunctionChecker.java index a68cbede238..9941e15d4f1 100644 --- a/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/model/AbstractAstFunctionChecker.java +++ b/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/model/AbstractAstFunctionChecker.java @@ -10,7 +10,7 @@ *******************************************************************************/ package org.eclipse.cdt.codan.core.cxx.model; -import org.eclipse.cdt.codan.core.model.ICheckerWithParameters; +import org.eclipse.cdt.codan.core.model.ICheckerWithPreferences; import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier; import org.eclipse.cdt.core.dom.ast.IASTDeclaration; @@ -23,7 +23,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier; * Abstract class for checkers that do all the work on function definition level */ public abstract class AbstractAstFunctionChecker extends - AbstractIndexAstChecker implements ICheckerWithParameters { + AbstractIndexAstChecker implements ICheckerWithPreferences { public void processAst(IASTTranslationUnit ast) { // traverse the ast using the visitor pattern. ast.accept(new ASTVisitor() { diff --git a/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/model/AbstractIndexAstChecker.java b/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/model/AbstractIndexAstChecker.java index 13f9b3bcfb5..bc2491583db 100644 --- a/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/model/AbstractIndexAstChecker.java +++ b/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/model/AbstractIndexAstChecker.java @@ -12,15 +12,9 @@ package org.eclipse.cdt.codan.core.cxx.model; import org.eclipse.cdt.codan.core.CodanCorePlugin; import org.eclipse.cdt.codan.core.cxx.Activator; -import org.eclipse.cdt.codan.core.model.AbstractChecker; -import org.eclipse.cdt.codan.core.model.ICheckerWithParameters; +import org.eclipse.cdt.codan.core.model.AbstractCheckerWithProblemPreferences; import org.eclipse.cdt.codan.core.model.IProblemLocation; -import org.eclipse.cdt.codan.core.model.IProblemWorkingCopy; import org.eclipse.cdt.codan.core.model.IRunnableInEditorChecker; -import org.eclipse.cdt.codan.core.param.IProblemParameterInfo; -import org.eclipse.cdt.codan.core.param.MapParameterInfo; -import org.eclipse.cdt.codan.core.param.SingleParameterInfo; -import org.eclipse.cdt.codan.core.param.IProblemParameterInfo.ParameterType; import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; @@ -39,8 +33,8 @@ import org.eclipse.core.runtime.Path; * * Clients may extend this class. */ -public abstract class AbstractIndexAstChecker extends AbstractChecker implements - ICAstChecker, IRunnableInEditorChecker, ICheckerWithParameters { +public abstract class AbstractIndexAstChecker extends AbstractCheckerWithProblemPreferences implements + ICAstChecker, IRunnableInEditorChecker { private IFile file; protected IFile getFile() { @@ -134,23 +128,4 @@ public abstract class AbstractIndexAstChecker extends AbstractChecker implements processAst(ast); } } - - public void initParameters(IProblemWorkingCopy problem) { - // do nothing - } - - public IProblemParameterInfo addParam(IProblemWorkingCopy problem, - String key, String label, Object defaultValue) { - MapParameterInfo map = (MapParameterInfo) problem.getParameterInfo(); - if (map == null) { - map = new MapParameterInfo(); - problem.setParameterInfo(map); - } - SingleParameterInfo info = new SingleParameterInfo(key, - label, - ParameterType.typeOf(defaultValue)); - map.setElement(info); - problem.setParameter(key, defaultValue); - return info; - } } diff --git a/codan/org.eclipse.cdt.codan.core.test/.settings/org.eclipse.jdt.ui.prefs b/codan/org.eclipse.cdt.codan.core.test/.settings/org.eclipse.jdt.ui.prefs new file mode 100644 index 00000000000..bdaa3ce29fb --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core.test/.settings/org.eclipse.jdt.ui.prefs @@ -0,0 +1,55 @@ +#Sat May 15 15:21:20 EDT 2010 +eclipse.preferences.version=1 +editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true +sp_cleanup.add_default_serial_version_id=true +sp_cleanup.add_generated_serial_version_id=false +sp_cleanup.add_missing_annotations=true +sp_cleanup.add_missing_deprecated_annotations=true +sp_cleanup.add_missing_methods=false +sp_cleanup.add_missing_nls_tags=false +sp_cleanup.add_missing_override_annotations=true +sp_cleanup.add_missing_override_annotations_interface_methods=false +sp_cleanup.add_serial_version_id=false +sp_cleanup.always_use_blocks=true +sp_cleanup.always_use_parentheses_in_expressions=false +sp_cleanup.always_use_this_for_non_static_field_access=false +sp_cleanup.always_use_this_for_non_static_method_access=false +sp_cleanup.convert_to_enhanced_for_loop=false +sp_cleanup.correct_indentation=false +sp_cleanup.format_source_code=true +sp_cleanup.format_source_code_changes_only=false +sp_cleanup.make_local_variable_final=false +sp_cleanup.make_parameters_final=false +sp_cleanup.make_private_fields_final=true +sp_cleanup.make_type_abstract_if_missing_method=false +sp_cleanup.make_variable_declarations_final=false +sp_cleanup.never_use_blocks=false +sp_cleanup.never_use_parentheses_in_expressions=true +sp_cleanup.on_save_use_additional_actions=true +sp_cleanup.organize_imports=true +sp_cleanup.qualify_static_field_accesses_with_declaring_class=false +sp_cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true +sp_cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true +sp_cleanup.qualify_static_member_accesses_with_declaring_class=false +sp_cleanup.qualify_static_method_accesses_with_declaring_class=false +sp_cleanup.remove_private_constructors=true +sp_cleanup.remove_trailing_whitespaces=true +sp_cleanup.remove_trailing_whitespaces_all=true +sp_cleanup.remove_trailing_whitespaces_ignore_empty=false +sp_cleanup.remove_unnecessary_casts=true +sp_cleanup.remove_unnecessary_nls_tags=true +sp_cleanup.remove_unused_imports=true +sp_cleanup.remove_unused_local_variables=false +sp_cleanup.remove_unused_private_fields=true +sp_cleanup.remove_unused_private_members=false +sp_cleanup.remove_unused_private_methods=true +sp_cleanup.remove_unused_private_types=true +sp_cleanup.sort_members=false +sp_cleanup.sort_members_all=false +sp_cleanup.use_blocks=false +sp_cleanup.use_blocks_only_for_return_and_throw=false +sp_cleanup.use_parentheses_in_expressions=false +sp_cleanup.use_this_for_non_static_field_access=false +sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true +sp_cleanup.use_this_for_non_static_method_access=false +sp_cleanup.use_this_for_non_static_method_access_only_if_necessary=true diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/param/BasicProblemPreferenceTest.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/param/BasicProblemPreferenceTest.java new file mode 100644 index 00000000000..6cf374ebb26 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/param/BasicProblemPreferenceTest.java @@ -0,0 +1,140 @@ +/******************************************************************************* + * Copyright (c) 2009,2010 Alena Laskavaia + * 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alena Laskavaia - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.codan.core.param; + +import java.io.File; + +import junit.framework.TestCase; + +import org.eclipse.cdt.codan.core.param.IProblemPreferenceDescriptor.PreferenceType; +import org.junit.Test; + +/** + * Test for BasicProblemPreference + */ +public class BasicProblemPreferenceTest extends TestCase { + private static final String TEST_STR = "aaa"; //$NON-NLS-1$ + BasicProblemPreference pref; + String key = "xxx"; //$NON-NLS-1$ + + @Override + protected void setUp() throws Exception { + pref = new BasicProblemPreference(key, "My Value"); //$NON-NLS-1$ + } + + @Test + public void testIntegerExportValue() { + pref.setType(PreferenceType.TYPE_INTEGER); + pref.setValue(22); + String value = pref.exportValue(); + assertEquals(String.valueOf(22), value); + } + + @Test + public void testIntegerImportValue() { + pref.setType(PreferenceType.TYPE_INTEGER); + pref.importValue("22"); //$NON-NLS-1$ + assertEquals(22, pref.getValue()); + } + + @Test + public void testStringExportValue() { + pref.setType(PreferenceType.TYPE_STRING); + pref.setValue(TEST_STR); + String value = pref.exportValue(); + assertEquals(TEST_STR, value); + } + + @Test + public void testStringImportValue() { + pref.setType(PreferenceType.TYPE_STRING); + pref.importValue(TEST_STR); + assertEquals(TEST_STR, pref.getValue()); + } + + @Test + public void testBooleanImportValue() { + pref.setType(PreferenceType.TYPE_BOOLEAN); + pref.setValue(Boolean.TRUE); + String value = pref.exportValue(); + assertEquals("true", value); //$NON-NLS-1$ + pref.importValue(TEST_STR); + assertEquals(Boolean.FALSE, pref.getValue()); + } + + @Test + public void testFileImportValue() { + pref.setType(PreferenceType.TYPE_FILE); + File file = new File("file.c"); //$NON-NLS-1$ + pref.setValue(file); + String value = pref.exportValue(); + assertEquals(file.getName(), value); + pref.importValue(file.getName()); + assertEquals(file, pref.getValue()); + } + + public void testBadKey() { + try { + pref.setKey(null); + fail("Should be exception"); //$NON-NLS-1$ + } catch (Exception e) { + assertTrue(true); + } + } + + public void testBadType() { + try { + pref.setType(null); + fail("Should be exception"); //$NON-NLS-1$ + } catch (Exception e) { + assertTrue(true); + } + } + + @Test + public void testStringImportValueNum() { + pref.setType(PreferenceType.TYPE_STRING); + pref.importValue("42.5"); + assertEquals("42.5", pref.getValue()); + } + + /** + * @param str + */ + protected void checkImportExport(String str) { + pref.setType(PreferenceType.TYPE_STRING); + pref.setValue(str); + pref.importValue(pref.exportValue()); + assertEquals(str, pref.getValue()); + } + + @Test + public void testStringExportSpecial() { + checkImportExport("a=b"); + checkImportExport("\""); + checkImportExport("33"); + checkImportExport("22.4"); + checkImportExport("a,b"); + checkImportExport("{a+b}"); + checkImportExport("\b"); + } + // public void testEscape() { + // String str = "\"a\""; + // String res = pref.escape(str); + // assertEquals("\"\\\"a\\\"\"", res); + // } + // + // public void testUnEscape() { + // String res = "\"a\""; + // String str = "\"\\\"a\\\"\""; + // assertEquals(res, pref.unescape(str)); + // } +} diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/param/ListProblemPreferenceTest.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/param/ListProblemPreferenceTest.java new file mode 100644 index 00000000000..86399218a90 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/param/ListProblemPreferenceTest.java @@ -0,0 +1,109 @@ +/******************************************************************************* + * Copyright (c) 2009,2010 Alena Laskavaia + * 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alena Laskavaia - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.codan.core.param; + +import junit.framework.TestCase; + +import org.eclipse.cdt.codan.core.param.IProblemPreferenceDescriptor.PreferenceType; +import org.junit.Test; + +/** + * Test for BasicProblemPreference + */ +public class ListProblemPreferenceTest extends TestCase { + private static final String PAR1 = "0"; //$NON-NLS-1$ + private static final String PAR2 = "1"; //$NON-NLS-1$ + private ListProblemPreference list; + private String key = "list"; //$NON-NLS-1$ + private ListProblemPreference list2; + + @Override + protected void setUp() throws Exception { + list = new ListProblemPreference(key, "My Value"); //$NON-NLS-1$ + list2 = new ListProblemPreference(key, "My Value2"); //$NON-NLS-1$ + } + + /** + * @param parval + * @return + */ + protected BasicProblemPreference addPar(String key, Object parval) { + BasicProblemPreference str = makePar(key, parval); + list.addChildDescriptor(str); + return str; + } + + /** + * @param parval + * @param parval + * @return + */ + protected BasicProblemPreference makePar(String key, Object parval) { + BasicProblemPreference str = new BasicProblemPreference(key, key); + if (parval != null) { + str.setValue(parval); + str.setType(PreferenceType.typeOf(parval)); + } + return str; + } + + @Test + public void testExportValueStr() { + BasicProblemPreference str = addPar(PAR1, "42.5"); //$NON-NLS-1$ + String value = list.exportValue(); + assertEquals("(42.5)", value); //$NON-NLS-1$ + } + + @Test + public void testImportValue() { + addPar(PAR1, "xxx"); //$NON-NLS-1$ + String value = list.exportValue(); + BasicProblemPreference str2 = new BasicProblemPreference(PAR1, PAR1); + list2.addChildDescriptor(str2); + list2.importValue(value); + assertEquals("xxx", list2.getChildValue(PAR1)); //$NON-NLS-1$ + } + + @Test + public void testImportValueSpec() { + BasicProblemPreference str = addPar(PAR1, "a=b"); //$NON-NLS-1$ + String value = list.exportValue(); + BasicProblemPreference str2 = new BasicProblemPreference(PAR1, PAR1); + list2.addChildDescriptor(str2); + list2.importValue(value); + assertEquals(str.getValue(), list2.getChildValue(PAR1)); + } + + @Test + public void testImportValue2() { + addPar(PAR1, "a=b"); //$NON-NLS-1$ + BasicProblemPreference p2 = addPar(PAR2, "2,\"2"); //$NON-NLS-1$ + String value = list.exportValue(); + list = new ListProblemPreference(key, "My Value"); //$NON-NLS-1$ + addPar(PAR1, null); + addPar(PAR2, null); + list.importValue(value); + assertEquals("a=b", list.getChildValue(PAR1)); //$NON-NLS-1$ + assertEquals(p2.getValue(), list.getChildValue(PAR2)); + } + + @Test + public void testImportValue2_nosec() { + addPar(PAR1, "a=b"); //$NON-NLS-1$ + BasicProblemPreference p2 = addPar(PAR2, "2' 2\""); //$NON-NLS-1$ + String value = list.exportValue(); + list = new ListProblemPreference(key, "My Value"); //$NON-NLS-1$ + addPar(PAR1, null); + list.importValue(value); + assertEquals("a=b", list.getChildValue(PAR1)); //$NON-NLS-1$ + assertEquals(p2.getValue(), list.getChildValue(PAR2)); + } +} diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/param/MapProblemPreferenceTest.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/param/MapProblemPreferenceTest.java new file mode 100644 index 00000000000..79329e77751 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/param/MapProblemPreferenceTest.java @@ -0,0 +1,97 @@ +/******************************************************************************* + * Copyright (c) 2009,2010 Alena Laskavaia + * 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alena Laskavaia - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.codan.core.param; + +import junit.framework.TestCase; + +import org.eclipse.cdt.codan.core.param.IProblemPreferenceDescriptor.PreferenceType; +import org.junit.Test; + +/** + * Test for BasicProblemPreference + */ +public class MapProblemPreferenceTest extends TestCase { + private static final String PAR1 = "aaa"; //$NON-NLS-1$ + private static final String PAR2 = "bbb"; //$NON-NLS-1$ + private MapProblemPreference map; + private String key = "map"; //$NON-NLS-1$ + private MapProblemPreference map2; + + @Override + protected void setUp() throws Exception { + map = new MapProblemPreference(key, "My Value"); //$NON-NLS-1$ + map2 = new MapProblemPreference(key, "My Value2"); //$NON-NLS-1$ + } + + /** + * @param parval + * @return + */ + protected BasicProblemPreference addPar(String key, Object parval) { + BasicProblemPreference str = makePar(key, parval); + map.addChildDescriptor(str); + return str; + } + + /** + * @param parval + * @param parval + * @return + */ + protected BasicProblemPreference makePar(String key, Object parval) { + BasicProblemPreference str = new BasicProblemPreference(key, key); + if (parval != null) { + str.setValue(parval); + str.setType(PreferenceType.typeOf(parval)); + } + return str; + } + + @Test + public void testExportValueStr() { + BasicProblemPreference str = addPar(PAR1, "42.5"); //$NON-NLS-1$ + String value = map.exportValue(); + assertEquals("{" + str.getKey() + "=>42.5}", value); //$NON-NLS-1$ //$NON-NLS-2$ + } + + @Test + public void testImportValue() { + addPar(PAR1, "xxx"); //$NON-NLS-1$ + String value = map.exportValue(); + BasicProblemPreference str2 = new BasicProblemPreference(PAR1, PAR1); + map2.addChildDescriptor(str2); + map2.importValue(value); + assertEquals("xxx", map2.getChildValue(PAR1)); //$NON-NLS-1$ + } + + @Test + public void testImportValueSpec() { + BasicProblemPreference str = addPar(PAR1, "a=b"); //$NON-NLS-1$ + String value = map.exportValue(); + BasicProblemPreference str2 = new BasicProblemPreference(PAR1, PAR1); + map2.addChildDescriptor(str2); + map2.importValue(value); + assertEquals(str.getValue(), map2.getChildValue(PAR1)); + } + + @Test + public void testImportValue2() { + addPar(PAR1, "a=b"); //$NON-NLS-1$ + BasicProblemPreference p2 = addPar(PAR2, "2,\"2"); //$NON-NLS-1$ + String value = map.exportValue(); + map = new MapProblemPreference(key, "My Value"); //$NON-NLS-1$ + addPar(PAR1, null); + addPar(PAR2, null); + map.importValue(value); + assertEquals("a=b", map.getChildValue(PAR1)); //$NON-NLS-1$ + assertEquals(p2.getValue(), map.getChildValue(PAR2)); + } +} diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/AutomatedIntegrationSuite.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/AutomatedIntegrationSuite.java index 623884569b6..cfaa138dd5f 100644 --- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/AutomatedIntegrationSuite.java +++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/AutomatedIntegrationSuite.java @@ -41,6 +41,7 @@ public class AutomatedIntegrationSuite extends TestSuite { suite.addTestSuite(SuggestedParenthesisCheckerTest.class); suite.addTestSuite(ExpressionRequiredInReturnCheckerTest.class); suite.addTestSuite(CatchByReferenceTest.class); + suite.addTest(CodanFastTestSuite.suite()); return suite; } } diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CodanFastTestSuite.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CodanFastTestSuite.java new file mode 100644 index 00000000000..84a42fdda1d --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CodanFastTestSuite.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 QNX Software Systems 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * QNX Software Systems - initial API and implementation + * Markus Schorn (Wind River Systems) + *******************************************************************************/ +package org.eclipse.cdt.codan.core.test; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.eclipse.cdt.codan.core.param.BasicProblemPreferenceTest; +import org.eclipse.cdt.codan.core.param.ListProblemPreferenceTest; +import org.eclipse.cdt.codan.core.param.MapProblemPreferenceTest; + +public class CodanFastTestSuite extends TestSuite { + public CodanFastTestSuite() { + } + + public CodanFastTestSuite(Class theClass, String name) { + super(theClass, name); + } + + public CodanFastTestSuite(Class theClass) { + super(theClass); + } + + public CodanFastTestSuite(String name) { + super(name); + } + + public static Test suite() { + final CodanFastTestSuite suite = new CodanFastTestSuite(); + suite.addTestSuite(BasicProblemPreferenceTest.class); + suite.addTestSuite(ListProblemPreferenceTest.class); + suite.addTestSuite(MapProblemPreferenceTest.class); + return suite; + } +} diff --git a/codan/org.eclipse.cdt.codan.core/.settings/org.eclipse.jdt.ui.prefs b/codan/org.eclipse.cdt.codan.core/.settings/org.eclipse.jdt.ui.prefs index 2117213dd56..17cb00ce3ec 100644 --- a/codan/org.eclipse.cdt.codan.core/.settings/org.eclipse.jdt.ui.prefs +++ b/codan/org.eclipse.cdt.codan.core/.settings/org.eclipse.jdt.ui.prefs @@ -1,4 +1,4 @@ -#Fri Nov 20 13:57:56 EST 2009 +#Sat May 15 15:20:42 EDT 2010 eclipse.preferences.version=1 editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true sp_cleanup.add_default_serial_version_id=true @@ -8,6 +8,7 @@ sp_cleanup.add_missing_deprecated_annotations=true sp_cleanup.add_missing_methods=false sp_cleanup.add_missing_nls_tags=false sp_cleanup.add_missing_override_annotations=true +sp_cleanup.add_missing_override_annotations_interface_methods=false sp_cleanup.add_serial_version_id=false sp_cleanup.always_use_blocks=true sp_cleanup.always_use_parentheses_in_expressions=false diff --git a/codan/org.eclipse.cdt.codan.core/META-INF/MANIFEST.MF b/codan/org.eclipse.cdt.codan.core/META-INF/MANIFEST.MF index fbe282e1feb..5470216a928 100644 --- a/codan/org.eclipse.cdt.codan.core/META-INF/MANIFEST.MF +++ b/codan/org.eclipse.cdt.codan.core/META-INF/MANIFEST.MF @@ -16,7 +16,8 @@ Export-Package: org.eclipse.cdt.codan.core, x-friends:="org.eclipse.cdt.codan.checkers, org.eclipse.cdt.codan.checkers.ui, org.eclipse.cdt.codan.ui, - org.eclipse.cdt.codan.core.cxx", + org.eclipse.cdt.codan.core.cxx, + org.eclipse.cdt.codan.core.test", org.eclipse.cdt.codan.internal.core; x-friends:="org.eclipse.cdt.codan.core, org.eclipse.cdt.codan.core.cxx, diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/AbstractCheckerWithProblemPreferences.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/AbstractCheckerWithProblemPreferences.java new file mode 100644 index 00000000000..26141d36147 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/AbstractCheckerWithProblemPreferences.java @@ -0,0 +1,99 @@ +/******************************************************************************* + * Copyright (c) 2009,2010 Alena Laskavaia + * 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alena Laskavaia - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.codan.core.model; + +import org.eclipse.cdt.codan.core.param.BasicProblemPreference; +import org.eclipse.cdt.codan.core.param.IProblemPreference; +import org.eclipse.cdt.codan.core.param.IProblemPreferenceDescriptor.PreferenceType; +import org.eclipse.cdt.codan.core.param.MapProblemPreference; + +/** + * AbstarctChecker that has extra method to simplify adding parameters + */ +public abstract class AbstractCheckerWithProblemPreferences extends + AbstractChecker implements ICheckerWithPreferences { + /** + * Checker that actually has parameter must override this + */ + public void initPreferences(IProblemWorkingCopy problem) { + // do nothing + } + + /** + * Add a parameter + * + * @param problem + * - problem that has parameter + * @param key + * - parameter key + * @param label + * - parameter label - user visible + * @param defaultValue + * - parameter default value + * @return - parameter info object + */ + public IProblemPreference addPreference(IProblemWorkingCopy problem, + String key, String label, Object defaultValue) { + MapProblemPreference map = getTopLevelPreferenceMap(problem); + BasicProblemPreference info = new BasicProblemPreference(key, label, + PreferenceType.typeOf(defaultValue)); + map.addChildDescriptor(info); + setDefaultPreferenceValue(problem, key, defaultValue); + return info; + } + + public IProblemPreference addPreference(IProblemWorkingCopy problem, + IProblemPreference info, Object defaultValue) { + MapProblemPreference map = getTopLevelPreferenceMap(problem); + map.addChildDescriptor(info); + setDefaultPreferenceValue(problem, info.getKey(), defaultValue); + return info; + } + + /** + * @param problem + * @param key + * @param defaultValue + */ + protected void setDefaultPreferenceValue(IProblemWorkingCopy problem, + String key, Object defaultValue) { + MapProblemPreference map = getTopLevelPreferenceMap(problem); + if (map.getChildValue(key) == null) + map.addChildValue(key, defaultValue); + } + + /** + * @param problem + * @return + */ + protected MapProblemPreference getTopLevelPreferenceMap( + IProblemWorkingCopy problem) { + MapProblemPreference map = (MapProblemPreference) problem + .getPreference(); + if (map == null) { + map = new MapProblemPreference("params", ""); //$NON-NLS-1$ //$NON-NLS-2$ + problem.setPreference(map); + } + return map; + } + + /** + * Return value for the key in the top level preference map + * + * @param problem + * @param key + * @return + */ + public Object getPreference(IProblem problem, String key) { + return ((MapProblemPreference) problem.getPreference()) + .getChildValue(key); + } +} diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/ICheckerWithParameters.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/ICheckerWithPreferences.java similarity index 92% rename from codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/ICheckerWithParameters.java rename to codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/ICheckerWithPreferences.java index cf91aea2328..b8d9f41f2e3 100644 --- a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/ICheckerWithParameters.java +++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/ICheckerWithPreferences.java @@ -22,7 +22,7 @@ package org.eclipse.cdt.codan.core.model; * * @noextend This interface is not intended to be extended by clients. */ -public interface ICheckerWithParameters { +public interface ICheckerWithPreferences { /** * Implement this method to set default parameters for checkers with * parameters. @@ -30,5 +30,5 @@ public interface ICheckerWithParameters { * @param problem * - instance of problem working copy */ - void initParameters(IProblemWorkingCopy problem); + void initPreferences(IProblemWorkingCopy problem); } diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/IProblem.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/IProblem.java index 5e04b40994f..0c7fc0deaec 100644 --- a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/IProblem.java +++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/IProblem.java @@ -10,7 +10,7 @@ *******************************************************************************/ package org.eclipse.cdt.codan.core.model; -import org.eclipse.cdt.codan.core.param.IProblemParameterInfo; +import org.eclipse.cdt.codan.core.param.IProblemPreference; /** * Interface representing code analysis problem type. For example @@ -65,16 +65,14 @@ public interface IProblem extends IProblemElement { */ String getMessagePattern(); - public Object getParameter(Object key); - /** - * Get parameter info root - contains description of types of all the + * Get root preference descriptor - contains description of types of all the * parameters or null if not defined (used by ui to generate user controls * for changing parameters) * * @return */ - public IProblemParameterInfo getParameterInfo(); + public IProblemPreference getPreference(); /** * Get short description of a problem diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/IProblemWorkingCopy.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/IProblemWorkingCopy.java index 81fd5d78dde..df586b764ac 100644 --- a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/IProblemWorkingCopy.java +++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/IProblemWorkingCopy.java @@ -10,7 +10,7 @@ *******************************************************************************/ package org.eclipse.cdt.codan.core.model; -import org.eclipse.cdt.codan.core.param.IProblemParameterInfo; +import org.eclipse.cdt.codan.core.param.IProblemPreference; /** * Modifiable problem. @@ -52,22 +52,11 @@ public interface IProblemWorkingCopy extends IProblem { /** * Set value for the checker parameter, checker may set value during - * initialization only + * initialization only, which would the default. User control this values + * through ui later. * - * @param key - * - key of the parameter - * @param value - * - parameter value */ - public void setParameter(Object key, Object value); - - /** - * Set parameter info object for the given problem, see - * {@link IProblemParameterInfo} - * - * @param info - */ - public void setParameterInfo(IProblemParameterInfo info); + public void setPreference(IProblemPreference pref); /** * Set problem description diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/AbstractProblemParameterInfo.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/AbstractProblemParameterInfo.java deleted file mode 100644 index 1866fc32833..00000000000 --- a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/AbstractProblemParameterInfo.java +++ /dev/null @@ -1,87 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009 Alena Laskavaia - * 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 - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Alena Laskavaia - initial API and implementation - *******************************************************************************/ -package org.eclipse.cdt.codan.core.param; - -import java.util.Iterator; - -/** - * Default implementation for single parameter checker of type string. - * - */ -public abstract class AbstractProblemParameterInfo implements - IProblemParameterInfo { - public static final String PARAM = "param"; //$NON-NLS-1$ - - /* - * (non-Javadoc) - * - * @see org.eclipse.cdt.codan.core.model.IProblemParameterInfo#getKey() - */ - public String getKey() { - return PARAM; - } - - /* - * (non-Javadoc) - * - * @see org.eclipse.cdt.codan.core.model.IProblemParameterInfo#getType() - */ - public ParameterType getType() { - return ParameterType.TYPE_STRING; - } - - /* - * (non-Javadoc) - * - * @see org.eclipse.cdt.codan.core.model.IProblemParameterInfo#getUiInfo() - */ - public String getUiInfo() { - return null; - } - - /* - * (non-Javadoc) - * - * @see org.eclipse.cdt.codan.core.model.IProblemParameterInfo#getLabel() - */ - public String getLabel() { - return getKey(); - } - - /* - * (non-Javadoc) - * - * @see - * org.eclipse.cdt.codan.core.model.IProblemParameterInfo#getElement(java - * .lang.String) - */ - public IProblemParameterInfo getElement(String key) { - return null; - } - - /* - * (non-Javadoc) - * - * @see org.eclipse.cdt.codan.core.model.IProblemParameterInfo#getToolTip() - */ - public String getToolTip() { - return null; - } - - /* - * (non-Javadoc) - * - * @see org.eclipse.cdt.codan.core.model.IProblemParameterInfo#getIterator() - */ - public Iterator getIterator() { - return null; - } -} diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/AbstractProblemPreference.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/AbstractProblemPreference.java new file mode 100644 index 00000000000..5943233b4d0 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/AbstractProblemPreference.java @@ -0,0 +1,156 @@ +/******************************************************************************* + * Copyright (c) 2009 Alena Laskavaia + * 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alena Laskavaia - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.codan.core.param; + +import java.io.ByteArrayInputStream; +import java.io.InputStreamReader; +import java.io.StreamTokenizer; + +/** + * Default implementation for single parameter checker of type string. + * + */ +public abstract class AbstractProblemPreference implements IProblemPreference { + public static final String PARAM = "param"; //$NON-NLS-1$ + protected String key; + protected String label; + protected String toolTip = null; + protected PreferenceType type; + protected String uiInfo; + private IProblemPreference parent; + + public PreferenceType getType() { + return type; + } + + public String getLabel() { + return label; + } + + public String getToolTip() { + return toolTip; + } + + public String getKey() { + return key; + } + + public String getUiInfo() { + return uiInfo; + } + + public void setKey(String key) { + if (isValidIdentifier(key)) + this.key = key; + else + throw new IllegalArgumentException( + "Key must have java identifier syntax or number, i.e no dots and other funky stuff"); //$NON-NLS-1$ + } + + protected boolean isValidIdentifier(String id) { + int n = id.length(); + if (n == 0) + return false; + for (int i = 0; i < n; i++) + if (!Character.isJavaIdentifierPart(id.charAt(i))) + return false; + return true; + } + + public void setLabel(String label) { + if (label == null) + throw new NullPointerException("Label cannot be null"); //$NON-NLS-1$ + this.label = label; + } + + public void setToolTip(String tooltip) { + this.toolTip = tooltip; + } + + public void setType(PreferenceType type) { + if (type == null) + throw new NullPointerException("Type cannot be null"); //$NON-NLS-1$ + this.type = type; + } + + public void setUiInfo(String uiinfo) { + this.uiInfo = uiinfo; + } + + public Object getValue() { + throw new UnsupportedOperationException(); + } + + public void setValue(Object value) { + throw new UnsupportedOperationException(); + } + + public IProblemPreference getChildDescriptor(String key) { + throw new UnsupportedOperationException(); + } + + public IProblemPreference[] getChildDescriptors() { + throw new UnsupportedOperationException(); + } + + public void addChildDescriptor(IProblemPreference info) { + throw new UnsupportedOperationException(); + } + + @Override + public Object clone() { + try { + return super.clone(); + } catch (CloneNotSupportedException e) { + return null; + } + } + + /** + * @param str + * @return + */ + protected StreamTokenizer getImportTokenizer(String str) { + ByteArrayInputStream st = new ByteArrayInputStream(str.getBytes()); + StreamTokenizer tokenizer = new StreamTokenizer(new InputStreamReader( + st)); + tokenizer.resetSyntax(); + tokenizer.quoteChar('"'); + tokenizer.wordChars('_', '_'); + tokenizer.wordChars('-', '-'); + tokenizer.wordChars('.', '.'); + tokenizer.wordChars('0', '9'); + tokenizer.wordChars('a', 'z'); + tokenizer.wordChars('A', 'Z'); + tokenizer.wordChars(128 + 32, 255); + tokenizer.whitespaceChars(0, ' '); + tokenizer.commentChar('/'); + return tokenizer; + } + + public IProblemPreference getParent() { + return parent; + } + + /** + * @param parent + * the parent to set + */ + public void setParent(IProblemPreference parent) { + this.parent = parent; + } + + public String getQualifiedKey() { + if (parent == null) + return getKey(); + return parent.getQualifiedKey() + "." + getKey(); //$NON-NLS-1$ + } +} diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/BasicProblemPreference.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/BasicProblemPreference.java new file mode 100644 index 00000000000..6f1c58a52ec --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/BasicProblemPreference.java @@ -0,0 +1,130 @@ +/******************************************************************************* + * Copyright (c) 2009 Alena Laskavaia + * 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alena Laskavaia - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.codan.core.param; + +import java.io.File; +import java.io.IOException; +import java.io.StreamTokenizer; +import java.util.regex.Pattern; + +/** + * ParameterInfo representing a single checker parameter + * + */ +public class BasicProblemPreference extends AbstractProblemPreference { + Object value; + { + key = PARAM; + type = PreferenceType.TYPE_STRING; + } + + /** + * Generate an info with given key and label + * + * @param key + * - property id (use in actual property hash of a checker) + * @param label + * - label to be shown to user + * @param type + * - parameter type + * @return + */ + public BasicProblemPreference(String key, String label, PreferenceType type) { + if (key == null) + throw new NullPointerException("key"); //$NON-NLS-1$ + if (type == null) + throw new NullPointerException("type"); //$NON-NLS-1$ + setKey(key); + setLabel(label); + setType(type); + } + + /** + * Generate an info with given key and label + * + * @param key + * - property id (use in actual property hash of a checker) + * @param label + * - label to be shown to user + * @return + */ + public BasicProblemPreference(String key, String label) { + setKey(key); + setLabel(label); + } + + @Override + public void setValue(Object value) { + this.value = value; + } + + @Override + public Object getValue() { + return value; + } + + public String exportValue() { + Pattern pat = Pattern.compile("^[A-Za-z0-9._-]+$"); //$NON-NLS-1$ + String x = String.valueOf(getValue()); + if (pat.matcher(x).find() == false) + return escape(x); + return x; + } + + protected String escape(String x) { + x = x.replaceAll("[\"\\\\]", "\\\\$0"); //$NON-NLS-1$//$NON-NLS-2$ + return "\"" + x + "\""; //$NON-NLS-1$//$NON-NLS-2$ + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.cdt.codan.core.param.IProblemPreferenceValue#importValue( + * java.lang.String) + */ + public void importValue(String str) { + if (str.startsWith("\"")) //$NON-NLS-1$ + str = unescape(str); + switch (getType()) { + case TYPE_STRING: + setValue(str); + break; + case TYPE_INTEGER: + setValue(Integer.parseInt(str)); + break; + case TYPE_BOOLEAN: + setValue(Boolean.valueOf(str)); + break; + case TYPE_FILE: + setValue(new File(str)); + break; + default: + throw new IllegalArgumentException(getType() + + " is not supported for basic type"); //$NON-NLS-1$ + } + } + + /** + * @param str + * @return + */ + protected String unescape(String str) { + StreamTokenizer tokenizer = getImportTokenizer(str); + try { + tokenizer.nextToken(); + } catch (IOException e) { + return null; + } + String sval = tokenizer.sval; + return sval; + } +} diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/IProblemPreference.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/IProblemPreference.java new file mode 100644 index 00000000000..0fd8ddb5bd7 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/IProblemPreference.java @@ -0,0 +1,19 @@ +/******************************************************************************* + * Copyright (c) 2009,2010 Alena Laskavaia + * 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alena Laskavaia - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.codan.core.param; + +/** + * Value of the problem preference. If more than one it can be composite, i.e. + * map + */ +public interface IProblemPreference extends Cloneable, IProblemPreferenceValue, + IProblemPreferenceDescriptor { +} diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/IProblemPreferenceContainer.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/IProblemPreferenceContainer.java new file mode 100644 index 00000000000..2a1a2ce0e53 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/IProblemPreferenceContainer.java @@ -0,0 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2009,2010 Alena Laskavaia + * 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alena Laskavaia - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.codan.core.param; + +/** + * Interface for container type preferences + */ +public interface IProblemPreferenceContainer { + Object getChildValue(String key); + + void addChildValue(String key, Object value); + + void removeChildValue(String key); +} diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/IProblemParameterInfo.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/IProblemPreferenceDescriptor.java similarity index 77% rename from codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/IProblemParameterInfo.java rename to codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/IProblemPreferenceDescriptor.java index 82962682a59..ec67ac24d8b 100644 --- a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/IProblemParameterInfo.java +++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/IProblemPreferenceDescriptor.java @@ -11,7 +11,6 @@ package org.eclipse.cdt.codan.core.param; import java.io.File; -import java.util.Iterator; import java.util.List; import java.util.Map; @@ -29,8 +28,8 @@ import java.util.Map; * @noextend This interface is not intended to be extended by clients. * @noimplement This interface is not intended to be implemented by clients. */ -public interface IProblemParameterInfo { - public enum ParameterType { +public interface IProblemPreferenceDescriptor extends Cloneable { + public enum PreferenceType { TYPE_STRING("string"), //$NON-NLS-1$ TYPE_INTEGER("integer"), //$NON-NLS-1$ TYPE_BOOLEAN("boolean"), //$NON-NLS-1$ @@ -39,14 +38,14 @@ public interface IProblemParameterInfo { TYPE_MAP("map"); //$NON-NLS-1$ private String literal; - private ParameterType(String literal) { + private PreferenceType(String literal) { this.literal = literal; } - public static ParameterType valueOfLiteral(String name) { - ParameterType[] values = values(); + public static PreferenceType valueOfLiteral(String name) { + PreferenceType[] values = values(); for (int i = 0; i < values.length; i++) { - ParameterType e = values[i]; + PreferenceType e = values[i]; if (e.literal.equals(name)) return e; } @@ -62,7 +61,7 @@ public interface IProblemParameterInfo { * @param value * @return parameter type corresponding to the value java type */ - public static ParameterType typeOf(Object value) { + public static PreferenceType typeOf(Object value) { if (value instanceof Boolean) return TYPE_BOOLEAN; if (value instanceof String) @@ -90,7 +89,7 @@ public interface IProblemParameterInfo { * * @return string value of the type */ - ParameterType getType(); + PreferenceType getType(); /** * Additional info on how it is represented in the ui, for example boolean @@ -115,20 +114,32 @@ public interface IProblemParameterInfo { String getToolTip(); /** - * Available if type is list or hash. Returns value of subparamer with the + * Available if type is composite. Returns value of subdescriptor with the * name of key. For the "list" type key is the number (index). * * @param key - * - name of the subparameter. + * - name of the subdescriptor. * @return */ - IProblemParameterInfo getElement(String key); + IProblemPreference getChildDescriptor(String key); /** - * Available if type is list or hash. Returns iterator over parameter values - * for list and hash. + * Available if type is list or map. Returns array of children. + * Of size 0 for basic types, keys for map, and arrays of 0 element for + * array type + * (since all elements are the same). * * @return */ - Iterator getIterator(); + IProblemPreference[] getChildDescriptors(); + + void addChildDescriptor(IProblemPreference info); + + Object clone(); + + IProblemPreference getParent(); + + public void setParent(IProblemPreference parent); + + String getQualifiedKey(); } diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/IProblemPreferenceValue.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/IProblemPreferenceValue.java new file mode 100644 index 00000000000..1d546ddf371 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/IProblemPreferenceValue.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * Copyright (c) 2009,2010 Alena Laskavaia + * 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alena Laskavaia - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.codan.core.param; + +/** + * Value of the problem preference. If more than one it can be composite, i.e. + * map + */ +public interface IProblemPreferenceValue extends Cloneable { + /** + * Get value of parameter if it is basic type. + * + * @param key + * @return + */ + Object getValue(); + + void setValue(Object value); + + String exportValue(); + + void importValue(String str); +} diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/ListParameterInfo.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/ListParameterInfo.java deleted file mode 100644 index 90a298bc6fc..00000000000 --- a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/ListParameterInfo.java +++ /dev/null @@ -1,87 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009 Alena Laskavaia - * 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 - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Alena Laskavaia - initial API and implementation - *******************************************************************************/ -package org.eclipse.cdt.codan.core.param; - -import java.util.ArrayList; -import java.util.Iterator; - -/** - * @author Alena - * - */ -public class ListParameterInfo extends SingleParameterInfo { - protected ArrayList list = new ArrayList( - 1); - - /** - * @param key - * @param label - */ - public ListParameterInfo(String key, String label) { - super(key, label); - } - - @Override - public ParameterType getType() { - return ParameterType.TYPE_LIST; - } - - @Override - public void setType(ParameterType type) { - throw new UnsupportedOperationException(); - } - - /** - * Get parameter into for element equal to key's int value, - * - * @throws NumberFormatException - * if key is not number - * @throws ArrayIndexOutOfBoundsException - * is index is out of bound - */ - @Override - public IProblemParameterInfo getElement(String key) - throws NumberFormatException, ArrayIndexOutOfBoundsException { - if (key == null) { - // special case if all element are the same return first, if key is - // null - return list.get(0); - } - Integer iv = Integer.valueOf(key); - if (iv.intValue() >= list.size() && list.size() == 1) { - // special case if all element are the same return first - return list.get(0); - } - return list.get(iv.intValue()); - } - - /** - * Set i'th element of parameter info, if all are the same i is 0 - * - * @param i - * @param info - */ - public void setElement(int i, IProblemParameterInfo info) { - while (i >= list.size()) { - list.add(null); - } - list.set(i, info); - } - - public IProblemParameterInfo getElement(int i) { - return list.get(i); - } - - @Override - public Iterator getIterator() { - return list.iterator(); - } -} diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/ListProblemPreference.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/ListProblemPreference.java new file mode 100644 index 00000000000..c1bca407256 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/ListProblemPreference.java @@ -0,0 +1,177 @@ +/******************************************************************************* + * Copyright (c) 2009 Alena Laskavaia + * 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alena Laskavaia - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.codan.core.param; + +import java.io.IOException; +import java.io.StreamTokenizer; +import java.util.ArrayList; +import java.util.Iterator; + +/** + * @author Alena + * + */ +public class ListProblemPreference extends AbstractProblemPreference implements + IProblemPreferenceContainer { + protected ArrayList list = new ArrayList( + 1); + + /** + * @param key + * @param label + */ + public ListProblemPreference(String key, String label) { + setKey(key); + setLabel(label); + } + + @Override + public PreferenceType getType() { + return PreferenceType.TYPE_LIST; + } + + @Override + public void setType(PreferenceType type) { + throw new UnsupportedOperationException(); + } + + /** + * Get parameter into for element equal to key's int value, + * + * @throws NumberFormatException + * if key is not number + * @throws ArrayIndexOutOfBoundsException + * is index is out of bound + */ + @Override + public IProblemPreference getChildDescriptor(String key) + throws NumberFormatException { + if (key == null) { + // special case if all element are the same return first, if key is + // null + return (IProblemPreference) getChildPreference(0).clone(); + } + Integer iv = Integer.valueOf(key); + if (iv.intValue() >= list.size()) { + // special case if all element are the same return first clone + IProblemPreference childInfo = (IProblemPreference) getChildPreference( + 0).clone(); + return childInfo; + } + return getChildPreference(iv.intValue()); + } + + /** + * Set i'th element of parameter info, if all are the same i is 0 + * + * @param i + * @param info + */ + public void setChildPreference(int i, IProblemPreference info) { + if (info != null) { + while (i >= list.size()) { + list.add(null); + } + list.set(i, info); + } else { + while (i == list.size() - 1) { + list.remove(i); + } + } + } + + /** + * If all list elements have same info it is enough to set only first one + * (index 0) + */ + @Override + public void addChildDescriptor(IProblemPreference info) { + Integer iv = Integer.valueOf(info.getKey()); + IProblemPreference desc = (IProblemPreference) info.clone(); + desc.setParent(this); + setChildPreference(iv, desc); + } + + public IProblemPreference getChildPreference(int i) { + return list.get(i); + } + + @Override + public IProblemPreference[] getChildDescriptors() { + return list.toArray(new IProblemPreference[list.size()]); + } + + public Object getChildValue(String key) { + IProblemPreference childInfo = getChildDescriptor(key); + return childInfo.getValue(); + } + + public void addChildValue(String key, Object value) { + IProblemPreference pref = getChildDescriptor(key); + pref.setValue(value); + // because descriptor can be phantom we have to set preference phisically + setChildPreference(Integer.parseInt(key), pref); + } + + public void removeChildValue(String key) { + int index = Integer.parseInt(key); + list.remove(index); + } + + @SuppressWarnings("unchecked") + @Override + public Object clone() { + ListProblemPreference list1 = (ListProblemPreference) super.clone(); + list1.list = (ArrayList) list.clone(); + return list1; + } + + public String exportValue() { + StringBuffer buf = new StringBuffer("("); //$NON-NLS-1$ + for (Iterator iterator = list.iterator(); iterator + .hasNext();) { + IProblemPreference d = iterator.next(); + buf.append(d.exportValue()); + if (iterator.hasNext()) + buf.append(","); //$NON-NLS-1$ + } + return buf.toString() + ")"; //$NON-NLS-1$ + } + + public void importValue(String str) { + StreamTokenizer tokenizer = getImportTokenizer(str); + int token; + int index = 0; + try { + token = tokenizer.nextToken(); + if (token != '(') + throw new IllegalArgumentException(str); + while (true) { + token = tokenizer.nextToken(); + String val = tokenizer.sval; + String ik = String.valueOf(index); + IProblemPreference desc = getChildDescriptor(ik); + if (desc != null) { + desc.importValue(val); + addChildValue(ik, desc.getValue()); + } + token = tokenizer.nextToken(); + if (token == ')') + break; + if (token != ',') + throw new IllegalArgumentException(str); + index++; + } + } catch (IOException e) { + throw new IllegalArgumentException(str); + } + } +} diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/MapParameterInfo.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/MapParameterInfo.java deleted file mode 100644 index a6d9bda49a4..00000000000 --- a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/MapParameterInfo.java +++ /dev/null @@ -1,83 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009 Alena Laskavaia - * 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 - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Alena Laskavaia - initial API and implementation - *******************************************************************************/ -package org.eclipse.cdt.codan.core.param; - -import java.util.HashMap; -import java.util.Iterator; - -/** - * HashParamterInfo - for checker that needs more than one parameter and they - * all different "named". - * For example checker has 2 optional boolean parameters. For example checker - * for parameter names - * shadowing would have two boolean options: "check contructors" and - * "check setters". In this case you use this type. - * - */ -public class MapParameterInfo extends SingleParameterInfo { - protected HashMap hash = new HashMap(); - - public MapParameterInfo() { - super(""); //$NON-NLS-1$ - } - - /** - * @param label - * - label for this group of parameters - */ - public MapParameterInfo(String label) { - super(label); - } - - /** - * @param key - * - key for itself - * @param label - * - label for this group of parameters - */ - public MapParameterInfo(String key, String label) { - super(key, label); - } - - @Override - public ParameterType getType() { - return ParameterType.TYPE_MAP; - } - - @Override - public void setType(ParameterType type) { - throw new UnsupportedOperationException(); - } - - /** - * Get parameter into for element by key - * - */ - @Override - public IProblemParameterInfo getElement(String key) { - return hash.get(key); - } - - /** - * Set parameter info for element with the key equals to info.getKey() - * - * @param i - * @param info - */ - public void setElement(IProblemParameterInfo info) { - hash.put(info.getKey(), info); - } - - @Override - public Iterator getIterator() { - return hash.values().iterator(); - } -} diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/MapProblemPreference.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/MapProblemPreference.java new file mode 100644 index 00000000000..e0443dcd101 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/MapProblemPreference.java @@ -0,0 +1,153 @@ +/******************************************************************************* + * Copyright (c) 2009 Alena Laskavaia + * 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alena Laskavaia - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.codan.core.param; + +import java.io.IOException; +import java.io.StreamTokenizer; +import java.util.Iterator; +import java.util.LinkedHashMap; + +/** + * HashParamterInfo - for checker that needs more than one parameter and they + * all different "named". + * For example checker has 2 optional boolean parameters. For example checker + * for parameter names + * shadowing would have two boolean options: "check contructors" and + * "check setters". In this case you use this type. + * + */ +public class MapProblemPreference extends AbstractProblemPreference implements + IProblemPreferenceContainer { + protected LinkedHashMap hash = new LinkedHashMap(); + + public MapProblemPreference() { + super(); + } + + /** + * @param key + * - key for itself + * @param label + * - label for this group of parameters + */ + public MapProblemPreference(String key, String label) { + setKey(key); + setLabel(label); + } + + @Override + public PreferenceType getType() { + return PreferenceType.TYPE_MAP; + } + + @Override + public void setType(PreferenceType type) { + throw new UnsupportedOperationException(); + } + + /** + * Get parameter into for element by key + * + */ + @Override + public IProblemPreference getChildDescriptor(String key) { + return hash.get(key); + } + + /** + * Put parameter info into the map for element with the key equals to + * info.getKey() + * + * @param i + * @param info + */ + @Override + public void addChildDescriptor(IProblemPreference info) { + IProblemPreference desc = (IProblemPreference) info.clone(); + desc.setParent(this); + hash.put(info.getKey(), desc); + } + + @Override + public IProblemPreference[] getChildDescriptors() { + return hash.values().toArray( + new IProblemPreference[hash.values().size()]); + } + + public Object getChildValue(String key) { + IProblemPreference childInfo = getChildDescriptor(key); + return childInfo.getValue(); + } + + public void addChildValue(String key, Object value) { + getChildDescriptor(key).setValue(value); + } + + public void removeChildValue(String key) { + hash.remove(key); + } + + @SuppressWarnings("unchecked") + @Override + public Object clone() { + MapProblemPreference map = (MapProblemPreference) super.clone(); + map.hash = (LinkedHashMap) hash.clone(); + return map; + } + + public String exportValue() { + StringBuffer buf = new StringBuffer("{"); //$NON-NLS-1$ + for (Iterator iterator = hash.keySet().iterator(); iterator + .hasNext();) { + String key = iterator.next(); + IProblemPreference d = hash.get(key); + buf.append(key + "=>" + d.exportValue()); //$NON-NLS-1$ + if (iterator.hasNext()) + buf.append(","); //$NON-NLS-1$ + } + return buf.toString() + "}"; //$NON-NLS-1$ + } + + public void importValue(String str) { + StreamTokenizer tokenizer = getImportTokenizer(str); + int token; + try { + token = tokenizer.nextToken(); + if (token != '{') + throw new IllegalArgumentException(str); + while (true) { + token = tokenizer.nextToken(); + String key = tokenizer.sval; + token = tokenizer.nextToken(); + if (token != '=') + throw new IllegalArgumentException(str); + token = tokenizer.nextToken(); + if (token != '>') + throw new IllegalArgumentException(str); + token = tokenizer.nextToken(); + String val = tokenizer.sval; + IProblemPreference desc = getChildDescriptor(key); + if (desc != null) { + desc.importValue(val); + } else { + //putChildValue(key, val); + } + token = tokenizer.nextToken(); + if (token == '}') + break; + if (token != ',') + throw new IllegalArgumentException(str); + } + } catch (IOException e) { + throw new IllegalArgumentException(str); + } + } +} diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/SingleParameterInfo.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/SingleParameterInfo.java deleted file mode 100644 index d2c1047e4b8..00000000000 --- a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/SingleParameterInfo.java +++ /dev/null @@ -1,114 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009 Alena Laskavaia - * 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 - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Alena Laskavaia - initial API and implementation - *******************************************************************************/ -package org.eclipse.cdt.codan.core.param; - -/** - * ParameterInfo representing a single checker parameter - * - */ -public class SingleParameterInfo extends AbstractProblemParameterInfo { - protected String key = PARAM; - protected String label; - protected String toolTip = null; - protected ParameterType type = ParameterType.TYPE_STRING; - protected String uiInfo; - - /** - * Generate an info with given key and label - * - * @param key - * - property id (use in actual property hash of a checker) - * @param label - * - label to be shown to user - * @param type - * - parameter type - * @return - */ - public SingleParameterInfo(String key, String label, ParameterType type) { - if (key == null) - throw new NullPointerException("key"); //$NON-NLS-1$ - if (type == null) - throw new NullPointerException("type"); //$NON-NLS-1$ - setKey(key); - setLabel(label); - setType(type); - } - - /** - * Generate an info with given key and label - * - * @param key - * - property id (use in actual property hash of a checker) - * @param label - * - label to be shown to user - * @return - */ - public SingleParameterInfo(String key, String label) { - setKey(key); - setLabel(label); - } - - /** - * Generate an info with given label, default key PARAM would be as a key - * - * @param label - * - label to be shown to user - * @return - */ - public SingleParameterInfo(String label) { - setLabel(label); - } - - @Override - public ParameterType getType() { - return type; - } - - @Override - public String getLabel() { - return label; - } - - @Override - public String getToolTip() { - return toolTip; - } - - @Override - public String getKey() { - return key; - } - - @Override - public String getUiInfo() { - return uiInfo; - } - - public void setKey(String key) { - this.key = key; - } - - public void setLabel(String label) { - this.label = label; - } - - public void setToolTip(String tooltip) { - this.toolTip = tooltip; - } - - public void setType(ParameterType type) { - this.type = type; - } - - public void setUiInfo(String uiinfo) { - this.uiInfo = uiinfo; - } -} diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/CheckersRegisry.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/CheckersRegisry.java index 108f0c574b0..55d8d648856 100644 --- a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/CheckersRegisry.java +++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/CheckersRegisry.java @@ -19,7 +19,7 @@ import org.eclipse.cdt.codan.core.CodanCorePlugin; import org.eclipse.cdt.codan.core.PreferenceConstants; import org.eclipse.cdt.codan.core.model.CodanSeverity; import org.eclipse.cdt.codan.core.model.IChecker; -import org.eclipse.cdt.codan.core.model.ICheckerWithParameters; +import org.eclipse.cdt.codan.core.model.ICheckerWithPreferences; import org.eclipse.cdt.codan.core.model.ICheckersRegistry; import org.eclipse.cdt.codan.core.model.IProblem; import org.eclipse.cdt.codan.core.model.IProblemCategory; @@ -81,14 +81,19 @@ public class CheckersRegisry implements Iterable, ICheckersRegistry { for (Iterator iterator = problemList.keySet().iterator(); iterator .hasNext();) { IChecker c = iterator.next(); - if (c instanceof ICheckerWithParameters) { + if (c instanceof ICheckerWithPreferences) { Collection list = problemList.get(c); for (Iterator iterator2 = list.iterator(); iterator2 .hasNext();) { IProblem p = iterator2.next(); if (p instanceof IProblemWorkingCopy) { - ((ICheckerWithParameters) c) - .initParameters((IProblemWorkingCopy) p); + try { + ((ICheckerWithPreferences) c) + .initPreferences((IProblemWorkingCopy) p); + } catch (Throwable t) { + t.printStackTrace(); + CodanCorePlugin.log(t); + } } } } @@ -161,7 +166,7 @@ public class CheckersRegisry implements Iterable, ICheckersRegistry { addRefProblem(checkerObj, p); } } - } catch (Exception e) { + } catch (Throwable e) { CodanCorePlugin.log(e); } } diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/CodanPreferencesLoader.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/CodanPreferencesLoader.java index 3f0d24d3478..1cc8b76833a 100644 --- a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/CodanPreferencesLoader.java +++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/CodanPreferencesLoader.java @@ -14,6 +14,8 @@ import org.eclipse.cdt.codan.core.CodanCorePlugin; import org.eclipse.cdt.codan.core.model.CodanSeverity; import org.eclipse.cdt.codan.core.model.IProblem; import org.eclipse.cdt.codan.core.model.IProblemProfile; +import org.eclipse.cdt.codan.core.model.IProblemWorkingCopy; +import org.eclipse.cdt.codan.core.param.IProblemPreference; import org.eclipse.cdt.codan.internal.core.model.CodanProblem; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.ProjectScope; @@ -21,7 +23,7 @@ import org.eclipse.core.runtime.preferences.InstanceScope; import org.osgi.service.prefs.Preferences; /** - * @author Alena + * Helper class to load/save problem profile settings in persistent storage * */ public class CodanPreferencesLoader { @@ -58,7 +60,7 @@ public class CodanPreferencesLoader { */ public void setProperty(String id, String s) { IProblem prob = baseModel.findProblem(id); - if (!(prob instanceof CodanProblem)) + if (!(prob instanceof IProblemWorkingCopy)) return; String sevs = s; boolean enabled = true; @@ -66,14 +68,14 @@ public class CodanPreferencesLoader { sevs = sevs.substring(1); enabled = false; } - ((CodanProblem) prob).setEnabled(enabled); + ((IProblemWorkingCopy) prob).setEnabled(enabled); CodanSeverity sev; try { sev = CodanSeverity.valueOf(sevs); } catch (RuntimeException e) { sev = CodanSeverity.Warning; } - ((CodanProblem) prob).setSeverity(sev); + ((IProblemWorkingCopy) prob).setSeverity(sev); } /* @@ -117,10 +119,28 @@ public class CodanPreferencesLoader { String s = storePreferences.get(id, null); if (s != null) { setProperty(id, s); + setParameterValues(id, storePreferences); } } } + /** + * @param problemId + * @param storePreferences + */ + private void setParameterValues(String problemId, + Preferences storePreferences) { + IProblem prob = baseModel.findProblem(problemId); + String prefKey = getPreferencesKey(problemId); + if (prefKey == null) + return; + String exported = storePreferences.get(prefKey, null); + if (exported != null) { + System.err.println(prefKey + " import " + exported); + prob.getPreference().importValue(exported); + } + } + public static Preferences getProjectNode(IProject project) { if (!project.exists()) return null; @@ -138,4 +158,30 @@ public class CodanPreferencesLoader { return null; return prefNode; } + + /** + * @param id + * @return + */ + public String getPreferencesKey(String id) { + IProblem prob = baseModel.findProblem(id); + IProblemPreference pref = prob.getPreference(); + if (pref == null) + return null; + return id + "." + pref.getKey(); //$NON-NLS-1$ + } + + /** + * @param id + * @return + */ + public String getPreferencesString(String id) { + IProblem prob = baseModel.findProblem(id); + IProblemPreference pref = prob.getPreference(); + if (pref == null) + return null; + String str = pref.exportValue(); + //System.err.println(id + " set " + str); + return str; + } } diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/model/CodanProblem.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/model/CodanProblem.java index 2a0ea72213c..e2187f5c306 100644 --- a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/model/CodanProblem.java +++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/model/CodanProblem.java @@ -10,12 +10,10 @@ *******************************************************************************/ package org.eclipse.cdt.codan.internal.core.model; -import java.util.HashMap; - import org.eclipse.cdt.codan.core.model.CodanSeverity; import org.eclipse.cdt.codan.core.model.IProblemReporter; import org.eclipse.cdt.codan.core.model.IProblemWorkingCopy; -import org.eclipse.cdt.codan.core.param.IProblemParameterInfo; +import org.eclipse.cdt.codan.core.param.IProblemPreference; public class CodanProblem implements IProblemWorkingCopy { private String id; @@ -23,8 +21,7 @@ public class CodanProblem implements IProblemWorkingCopy { private String message; private CodanSeverity severity = CodanSeverity.Warning; private boolean enabled = true; - private HashMap parameters = new HashMap(0); - private IProblemParameterInfo parameterInfo; + private IProblemPreference preference; private boolean frozen; private String description; private String markerType = IProblemReporter.GENERIC_CODE_ANALYSIS_MARKER_TYPE; @@ -76,20 +73,12 @@ public class CodanProblem implements IProblemWorkingCopy { return super.clone(); } - public void setParameter(Object key, Object value) { - parameters.put(key, value); + public void setPreference(IProblemPreference value) { + preference = value; } - public void setParameterInfo(IProblemParameterInfo info) { - parameterInfo = info; - } - - public Object getParameter(Object key) { - return parameters.get(key); - } - - public IProblemParameterInfo getParameterInfo() { - return parameterInfo; + public IProblemPreference getPreference() { + return preference; } /* diff --git a/codan/org.eclipse.cdt.codan.examples/src/org/eclipse/cdt/codan/examples/checkers/NamingConventionFunctionIIndexChecker.java b/codan/org.eclipse.cdt.codan.examples/src/org/eclipse/cdt/codan/examples/checkers/NamingConventionFunctionIIndexChecker.java index 2daaf0d0a3f..7ebb8e217f6 100644 --- a/codan/org.eclipse.cdt.codan.examples/src/org/eclipse/cdt/codan/examples/checkers/NamingConventionFunctionIIndexChecker.java +++ b/codan/org.eclipse.cdt.codan.examples/src/org/eclipse/cdt/codan/examples/checkers/NamingConventionFunctionIIndexChecker.java @@ -11,12 +11,13 @@ package org.eclipse.cdt.codan.examples.checkers; import java.util.regex.Pattern; + import org.eclipse.cdt.codan.core.cxx.model.AbstractCIndexChecker; -import org.eclipse.cdt.codan.core.model.ICheckerWithParameters; +import org.eclipse.cdt.codan.core.model.ICheckerWithPreferences; import org.eclipse.cdt.codan.core.model.IProblem; import org.eclipse.cdt.codan.core.model.IProblemWorkingCopy; -import org.eclipse.cdt.codan.core.param.AbstractProblemParameterInfo; -import org.eclipse.cdt.codan.core.param.IProblemParameterInfo; +import org.eclipse.cdt.codan.core.param.BasicProblemPreference; +import org.eclipse.cdt.codan.core.param.IProblemPreference; import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICElementVisitor; import org.eclipse.cdt.core.model.ITranslationUnit; @@ -27,7 +28,7 @@ import org.eclipse.core.runtime.CoreException; * */ public class NamingConventionFunctionIIndexChecker extends - AbstractCIndexChecker implements ICheckerWithParameters { + AbstractCIndexChecker implements ICheckerWithPreferences { private static final String DEFAULT_PATTERN = "^[a-z]"; // name starts with english lowercase letter //$NON-NLS-1$ public static final String PARAM_KEY = "pattern"; //$NON-NLS-1$ private static final String ER_ID = "org.eclipse.cdt.codan.examples.checkers.NamingConventionFunctionProblem"; //$NON-NLS-1$ @@ -45,7 +46,8 @@ public class NamingConventionFunctionIIndexChecker extends unit.accept(new ICElementVisitor() { public boolean visit(ICElement element) throws CoreException { if (element.getElementType() == ICElement.C_FUNCTION) { - String parameter = (String) pt.getParameter(PARAM_KEY); + String parameter = (String) pt.getPreference() + .getValue(); Pattern pattern = Pattern.compile(parameter); String name = element.getElementName(); if (!pattern.matcher(name).find()) { @@ -67,21 +69,14 @@ public class NamingConventionFunctionIIndexChecker extends * (non-Javadoc) * * @see - * org.eclipse.cdt.codan.core.model.ICheckerWithParameters#initParameters + * org.eclipse.cdt.codan.core.model.ICheckerWithPreferences#initParameters * (org.eclipse.cdt.codan.core.model.IProblemWorkingCopy) */ - public void initParameters(IProblemWorkingCopy problem) { - IProblemParameterInfo info = new AbstractProblemParameterInfo() { - public String getLabel() { - return "Name Pattern"; - } - - public String getKey() { - return PARAM_KEY; - } - }; - problem.setParameterInfo(info); - problem.setParameter(PARAM_KEY, DEFAULT_PATTERN); + public void initPreferences(IProblemWorkingCopy problem) { + IProblemPreference info = new BasicProblemPreference(PARAM_KEY, + "Name Pattern"); + info.setValue(DEFAULT_PATTERN); + problem.setPreference(info); } @Override diff --git a/codan/org.eclipse.cdt.codan.ui/.settings/org.eclipse.jdt.ui.prefs b/codan/org.eclipse.cdt.codan.ui/.settings/org.eclipse.jdt.ui.prefs new file mode 100644 index 00000000000..62d3d7e6edf --- /dev/null +++ b/codan/org.eclipse.cdt.codan.ui/.settings/org.eclipse.jdt.ui.prefs @@ -0,0 +1,55 @@ +#Sat May 15 20:01:30 EDT 2010 +eclipse.preferences.version=1 +editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true +sp_cleanup.add_default_serial_version_id=true +sp_cleanup.add_generated_serial_version_id=false +sp_cleanup.add_missing_annotations=true +sp_cleanup.add_missing_deprecated_annotations=true +sp_cleanup.add_missing_methods=false +sp_cleanup.add_missing_nls_tags=false +sp_cleanup.add_missing_override_annotations=true +sp_cleanup.add_missing_override_annotations_interface_methods=false +sp_cleanup.add_serial_version_id=false +sp_cleanup.always_use_blocks=true +sp_cleanup.always_use_parentheses_in_expressions=false +sp_cleanup.always_use_this_for_non_static_field_access=false +sp_cleanup.always_use_this_for_non_static_method_access=false +sp_cleanup.convert_to_enhanced_for_loop=false +sp_cleanup.correct_indentation=false +sp_cleanup.format_source_code=true +sp_cleanup.format_source_code_changes_only=false +sp_cleanup.make_local_variable_final=false +sp_cleanup.make_parameters_final=false +sp_cleanup.make_private_fields_final=true +sp_cleanup.make_type_abstract_if_missing_method=false +sp_cleanup.make_variable_declarations_final=false +sp_cleanup.never_use_blocks=false +sp_cleanup.never_use_parentheses_in_expressions=true +sp_cleanup.on_save_use_additional_actions=true +sp_cleanup.organize_imports=true +sp_cleanup.qualify_static_field_accesses_with_declaring_class=false +sp_cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true +sp_cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true +sp_cleanup.qualify_static_member_accesses_with_declaring_class=false +sp_cleanup.qualify_static_method_accesses_with_declaring_class=false +sp_cleanup.remove_private_constructors=true +sp_cleanup.remove_trailing_whitespaces=false +sp_cleanup.remove_trailing_whitespaces_all=true +sp_cleanup.remove_trailing_whitespaces_ignore_empty=false +sp_cleanup.remove_unnecessary_casts=true +sp_cleanup.remove_unnecessary_nls_tags=true +sp_cleanup.remove_unused_imports=true +sp_cleanup.remove_unused_local_variables=false +sp_cleanup.remove_unused_private_fields=true +sp_cleanup.remove_unused_private_members=false +sp_cleanup.remove_unused_private_methods=true +sp_cleanup.remove_unused_private_types=true +sp_cleanup.sort_members=false +sp_cleanup.sort_members_all=false +sp_cleanup.use_blocks=false +sp_cleanup.use_blocks_only_for_return_and_throw=false +sp_cleanup.use_parentheses_in_expressions=false +sp_cleanup.use_this_for_non_static_field_access=false +sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true +sp_cleanup.use_this_for_non_static_method_access=false +sp_cleanup.use_this_for_non_static_method_access_only_if_necessary=true diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/CodanUIMessages.java b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/CodanUIMessages.java index 96106445cf3..f2b43fb5e5a 100644 --- a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/CodanUIMessages.java +++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/CodanUIMessages.java @@ -22,7 +22,7 @@ public class CodanUIMessages extends NLS { public static String CheckedTreeEditor_SelectionCannotBeEmpty; public static String CodanPreferencePage_Customize; public static String CodanPreferencePage_Description; - public static String CodanPreferencePage_HasParameters; + public static String CodanPreferencePage_HasPreferences; public static String CodanPreferencePage_Info; public static String CodanPreferencePage_MessageLabel; public static String CodanPreferencePage_NoInfo; diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/dialogs/ParametersComposite.java b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/dialogs/ParametersComposite.java index 168c606c975..cf1f84b9d79 100644 --- a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/dialogs/ParametersComposite.java +++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/dialogs/ParametersComposite.java @@ -10,12 +10,11 @@ *******************************************************************************/ package org.eclipse.cdt.codan.internal.ui.dialogs; -import java.util.Iterator; +import java.io.File; import org.eclipse.cdt.codan.core.model.IProblem; import org.eclipse.cdt.codan.core.model.IProblemWorkingCopy; -import org.eclipse.cdt.codan.core.param.IProblemParameterInfo; -import org.eclipse.cdt.codan.core.param.IProblemParameterInfo.ParameterType; +import org.eclipse.cdt.codan.core.param.IProblemPreference; import org.eclipse.cdt.codan.internal.ui.CodanUIMessages; import org.eclipse.jface.preference.BooleanFieldEditor; import org.eclipse.jface.preference.FieldEditorPreferencePage; @@ -28,13 +27,13 @@ import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; /** - * @author Alena + * Composite to show problem preferences * */ public class ParametersComposite extends Composite { private FieldEditorPreferencePage page; private IProblem problem; - private PreferenceStore pref; + private PreferenceStore prefStore; /** * @param parent @@ -47,44 +46,42 @@ public class ParametersComposite extends Composite { throw new NullPointerException(); this.setLayout(new GridLayout(2, false)); this.problem = problem; - this.pref = new PreferenceStore(); + this.prefStore = new PreferenceStore(); page = new FieldEditorPreferencePage() { @Override protected void createFieldEditors() { noDefaultAndApplyButton(); - IProblemParameterInfo parameterInfo = problem - .getParameterInfo(); - createFieldEditorsForParameters(parameterInfo); + IProblemPreference pref = problem.getPreference(); + createFieldEditorsForParameters(pref); } /** * @param info */ - private void createFieldEditorsForParameters( - IProblemParameterInfo info) { + private void createFieldEditorsForParameters(IProblemPreference info) { if (info == null) return; switch (info.getType()) { case TYPE_STRING: { StringFieldEditor fe = new StringFieldEditor( - info.getKey(), info.getLabel(), + info.getQualifiedKey(), info.getLabel(), getFieldEditorParent()); addField(fe); break; } case TYPE_BOOLEAN: { BooleanFieldEditor fe = new BooleanFieldEditor( - info.getKey(), info.getLabel(), + info.getQualifiedKey(), info.getLabel(), getFieldEditorParent()); addField(fe); break; } case TYPE_MAP: { - Iterator iterator = info - .getIterator(); - while (iterator.hasNext()) { - IProblemParameterInfo info1 = iterator.next(); - createFieldEditorsForParameters(info1); + IProblemPreference[] childrenDescriptor = info + .getChildDescriptors(); + for (int i = 0; i < childrenDescriptor.length; i++) { + IProblemPreference desc = childrenDescriptor[i]; + createFieldEditorsForParameters(desc); } break; } @@ -94,70 +91,92 @@ public class ParametersComposite extends Composite { } } }; - IProblemParameterInfo info = problem.getParameterInfo(); + IProblemPreference info = problem.getPreference(); if (info == null) { Label label = new Label(this, 0); label.setText(CodanUIMessages.ParametersComposite_None); + } else { + initPrefStore(info); } - initPrefStore(info); - page.setPreferenceStore(pref); + page.setPreferenceStore(prefStore); page.createControl(parent); page.getControl().setLayoutData(new GridData(GridData.FILL_BOTH)); } - public void save(IProblemWorkingCopy problemwc) { + public void save(IProblemWorkingCopy problem) { page.performOk(); - IProblemParameterInfo info = problemwc.getParameterInfo(); - savePrefStore(info, problemwc); + savePrefStore(problem.getPreference()); + } + + private void savePrefStore(IProblemPreference desc) { + if (desc == null) + return; + String key = desc.getQualifiedKey(); + switch (desc.getType()) { + case TYPE_STRING: + desc.setValue(prefStore.getString(key)); + break; + case TYPE_BOOLEAN: + desc.setValue(prefStore.getBoolean(key)); + break; + case TYPE_INTEGER: + desc.setValue(prefStore.getInt(key)); + break; + case TYPE_FILE: + desc.setValue(new File(prefStore.getString(key))); + break; + case TYPE_MAP: + case TYPE_LIST: + IProblemPreference[] childrenDescriptor = desc + .getChildDescriptors(); + for (int i = 0; i < childrenDescriptor.length; i++) { + IProblemPreference chi = childrenDescriptor[i]; + savePrefStore(chi); + } + break; + default: + throw new UnsupportedOperationException(desc.getType() + .toString()); + } + } + + private void initPrefStore(IProblemPreference desc) { + if (desc == null) + return; + String key = desc.getQualifiedKey(); + switch (desc.getType()) { + case TYPE_STRING: + prefStore.setValue(key, (String) desc.getValue()); + break; + case TYPE_BOOLEAN: + prefStore.setValue(key, (Boolean) desc.getValue()); + break; + case TYPE_INTEGER: + prefStore.setValue(key, (Integer) desc.getValue()); + break; + case TYPE_FILE: + prefStore.setValue(key, ((File) desc.getValue()).getPath()); + break; + case TYPE_MAP: + case TYPE_LIST: { + IProblemPreference[] childrenDescriptor = desc + .getChildDescriptors(); + for (int i = 0; i < childrenDescriptor.length; i++) { + IProblemPreference chi = childrenDescriptor[i]; + initPrefStore(chi); + } + break; + } + default: + throw new UnsupportedOperationException(desc.getType() + .toString()); + } } /** - * @param info - * @param problemwc + * @return the problem */ - private void savePrefStore(IProblemParameterInfo info, - IProblemWorkingCopy problemwc) { - if (info == null) - return; - String key = info.getKey(); - Object parameter = problem.getParameter(key); - if (info.getType() == ParameterType.TYPE_MAP && parameter == null) { - Iterator iterator = info.getIterator(); - while (iterator.hasNext()) { - IProblemParameterInfo info1 = iterator.next(); - savePrefStore(info1, problemwc); - } - } else if (parameter instanceof String) { - String newValue = pref.getString(key); - problemwc.setParameter(key, newValue); - } else if (parameter instanceof Boolean) { - boolean newValue = pref.getBoolean(key); - problemwc.setParameter(key, newValue); - } else - throw new UnsupportedOperationException(info.getType().toString()); - } - - /** - * @param info - */ - private void initPrefStore(IProblemParameterInfo info) { - if (info == null) - return; - String key = info.getKey(); - Object parameter = problem.getParameter(key); - if (info.getType() == ParameterType.TYPE_MAP && parameter == null) { - Iterator iterator = info.getIterator(); - while (iterator.hasNext()) { - IProblemParameterInfo info1 = iterator.next(); - initPrefStore(info1); - } - } else if (parameter instanceof String) { - pref.setDefault(key, (String) parameter); - pref.setValue(key, (String) parameter); - } else if (parameter instanceof Boolean) { - pref.setDefault(key, (Boolean) parameter); - pref.setValue(key, (Boolean) parameter); - } else - throw new UnsupportedOperationException(info.getType().toString()); + public IProblem getProblem() { + return problem; } } diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/messages.properties b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/messages.properties index 3b58194c83c..a6243257299 100644 --- a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/messages.properties +++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/messages.properties @@ -3,17 +3,17 @@ BuildPropertyPage_RunWithBuild=&Run with build CheckedTreeEditor_SelectionCannotBeEmpty=Selection cannot be empty CodanPreferencePage_Customize=Customize... CodanPreferencePage_Description=Description: -CodanPreferencePage_HasParameters=This problem has parameters +CodanPreferencePage_HasPreferences=This problem has extra preferences CodanPreferencePage_Info=Info CodanPreferencePage_MessageLabel=Message: CodanPreferencePage_NoInfo=Not defined CodanPreferencePage_Parameters=Parameters: -CustomizeProblemComposite_TabParameters=Parameters +CustomizeProblemComposite_TabParameters=Preferences CustomizeProblemComposite_TabScope=Scope -CustomizeProblemDialog_Message=Edit problem parameters, scope and launch options +CustomizeProblemDialog_Message=Edit problem preferences, scope and launch options CustomizeProblemDialog_Title=Customize Problem... Job_TitleRunningAnalysis=Running Code Analysis -ParametersComposite_None=No Parameters +ParametersComposite_None=No extra preferences ProblemsTreeEditor_NameColumn=Name ProblemsTreeEditor_Problems=Problems ProblemsTreeEditor_SeverityColumn=Severity diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/CodanPreferencePage.java b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/CodanPreferencePage.java index 2820176fde9..3a7330065f0 100644 --- a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/CodanPreferencePage.java +++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/CodanPreferencePage.java @@ -17,7 +17,7 @@ import org.eclipse.cdt.codan.core.CodanRuntime; import org.eclipse.cdt.codan.core.model.ICheckersRegistry; import org.eclipse.cdt.codan.core.model.IProblem; import org.eclipse.cdt.codan.core.model.IProblemProfile; -import org.eclipse.cdt.codan.core.param.IProblemParameterInfo; +import org.eclipse.cdt.codan.core.param.IProblemPreference; import org.eclipse.cdt.codan.internal.ui.CodanUIActivator; import org.eclipse.cdt.codan.internal.ui.CodanUIMessages; import org.eclipse.cdt.codan.internal.ui.dialogs.CustomizeProblemDialog; @@ -88,6 +88,7 @@ public class CodanPreferencePage extends FieldEditorOverlayPage implements }; } + @Override protected String getPageId() { return "org.eclipse.cdt.codan.internal.ui.preferences.CodanPreferencePage"; //$NON-NLS-1$ } @@ -97,12 +98,13 @@ public class CodanPreferencePage extends FieldEditorOverlayPage implements * GUI blocks needed to manipulate various types of preferences. Each field * editor knows how to save and restore itself. */ + @Override public void createFieldEditors() { profile = isPropertyPage() ? getRegistry() .getResourceProfileWorkingCopy((IResource) getElement()) : getRegistry().getWorkspaceProfile(); - checkedTreeEditor = new ProblemsTreeEditor( - getFieldEditorParent(), profile); + checkedTreeEditor = new ProblemsTreeEditor(getFieldEditorParent(), + profile); addField(checkedTreeEditor); checkedTreeEditor.getTreeViewer().addSelectionChangedListener( problemSelectionListener); @@ -136,10 +138,10 @@ public class CodanPreferencePage extends FieldEditorOverlayPage implements info.setLayoutData(new GridData(GridData.FILL_BOTH)); info.setLayout(new GridLayout(3, false)); info.setText(CodanUIMessages.CodanPreferencePage_Info); - GridDataFactory gdLab = GridDataFactory.swtDefaults().align( - SWT.BEGINNING, SWT.BEGINNING).grab(false, false); - GridDataFactory gdFact = GridDataFactory.swtDefaults().align( - SWT.BEGINNING, SWT.BEGINNING).grab(true, true); + GridDataFactory gdLab = GridDataFactory.swtDefaults() + .align(SWT.BEGINNING, SWT.BEGINNING).grab(false, false); + GridDataFactory gdFact = GridDataFactory.swtDefaults() + .align(SWT.BEGINNING, SWT.BEGINNING).grab(true, true); // message Label labelMessage = new Label(info, SWT.NONE); labelMessage.setText(CodanUIMessages.CodanPreferencePage_MessageLabel); @@ -152,8 +154,12 @@ public class CodanPreferencePage extends FieldEditorOverlayPage implements labelDesc.setLayoutData(gdLab.create()); infoDesc = new Label(info, SWT.WRAP); PixelConverter pixelConverter = new PixelConverter(comp); - infoDesc.setLayoutData(gdFact.copy().span(2, 1).hint(pixelConverter.convertWidthInCharsToPixels(60), - pixelConverter.convertHeightInCharsToPixels(3)).create()); + infoDesc.setLayoutData(gdFact + .copy() + .span(2, 1) + .hint(pixelConverter.convertWidthInCharsToPixels(60), + pixelConverter.convertHeightInCharsToPixels(3)) + .create()); // params Label labelParams = new Label(info, SWT.NONE); labelParams.setText(CodanUIMessages.CodanPreferencePage_Parameters); @@ -161,8 +167,8 @@ public class CodanPreferencePage extends FieldEditorOverlayPage implements infoParams = new Label(info, SWT.NONE); infoParams.setLayoutData(gdFact.create()); infoButton = new Button(info, SWT.PUSH); - infoButton.setLayoutData(GridDataFactory.swtDefaults().align(SWT.END, - SWT.BEGINNING).create()); + infoButton.setLayoutData(GridDataFactory.swtDefaults() + .align(SWT.END, SWT.BEGINNING).create()); infoButton.setText(CodanUIMessages.CodanPreferencePage_Customize); infoButton.addSelectionListener(new SelectionAdapter() { @Override @@ -170,7 +176,7 @@ public class CodanPreferencePage extends FieldEditorOverlayPage implements openCustomizeDialog(); } }); - restoreWidgetValues(); + restoreWidgetValues(); } /** @@ -205,15 +211,19 @@ public class CodanPreferencePage extends FieldEditorOverlayPage implements * */ private void saveWidgetValues() { - CodanUIActivator.getDefault().getDialogSettings().put(getWidgetId(), - selectedProblem == null ? "" : selectedProblem.getId()); //$NON-NLS-1$ + CodanUIActivator + .getDefault() + .getDialogSettings() + .put(getWidgetId(), + selectedProblem == null ? "" : selectedProblem.getId()); //$NON-NLS-1$ } private void restoreWidgetValues() { - String id = CodanUIActivator.getDefault().getDialogSettings().get( - getWidgetId()); + String id = CodanUIActivator.getDefault().getDialogSettings() + .get(getWidgetId()); if (id != null && id.length() > 0) { - checkedTreeEditor.getTreeViewer().setSelection(new StructuredSelection(profile.findProblem(id)), true); + checkedTreeEditor.getTreeViewer().setSelection( + new StructuredSelection(profile.findProblem(id)), true); } else { setSelectedProblem(null); } @@ -236,21 +246,20 @@ public class CodanPreferencePage extends FieldEditorOverlayPage implements infoParams.setText(""); //$NON-NLS-1$ infoButton.setEnabled(false); } else { - IProblemParameterInfo parameterInfo = selectedProblem - .getParameterInfo(); - String desc = selectedProblem.getDescription(); - if (desc == null) - desc = CodanUIMessages.CodanPreferencePage_NoInfo; + IProblemPreference pref = selectedProblem.getPreference(); + String description = selectedProblem.getDescription(); + if (description == null) + description = CodanUIMessages.CodanPreferencePage_NoInfo; String messagePattern = selectedProblem.getMessagePattern(); String message = CodanUIMessages.CodanPreferencePage_NoInfo; if (messagePattern != null) { - message = MessageFormat.format(messagePattern, "X", "Y", "Z"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ + message = MessageFormat.format(messagePattern, "X", "Y", "Z"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } infoMessage.setText(message); - infoDesc.setText(desc); + infoDesc.setText(description); infoParams - .setText(parameterInfo == null ? CodanUIMessages.CodanPreferencePage_NoInfo - : CodanUIMessages.CodanPreferencePage_HasParameters); + .setText(pref == null ? CodanUIMessages.CodanPreferencePage_NoInfo + : CodanUIMessages.CodanPreferencePage_HasPreferences); infoButton.setEnabled(true); } info.layout(true); diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/ProblemsTreeEditor.java b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/ProblemsTreeEditor.java index b314bbd8d54..d4ed34931ea 100644 --- a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/ProblemsTreeEditor.java +++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/preferences/ProblemsTreeEditor.java @@ -62,7 +62,9 @@ public class ProblemsTreeEditor extends CheckedTreeEditor { Object[] children = p.getChildren(); for (int i = 0; i < children.length; i++) { Object object = children[i]; - if (isChecked(object)) { return true; } + if (isChecked(object)) { + return true; + } } } return false; @@ -76,7 +78,9 @@ public class ProblemsTreeEditor extends CheckedTreeEditor { * Object) */ public boolean isGrayed(Object element) { - if (element instanceof IProblem) { return false; } + if (element instanceof IProblem) { + return false; + } if (element instanceof IProblemCategory) { // checked if at least one is checked (buy grayed) IProblemCategory p = (IProblemCategory) element; @@ -91,14 +95,16 @@ public class ProblemsTreeEditor extends CheckedTreeEditor { all_checked = false; } } - if (all_checked || all_unchecked) return false; + if (all_checked || all_unchecked) + return false; return true; } return false; } } - class ProblemsContentProvider implements IContentProvider, ITreeContentProvider { + class ProblemsContentProvider implements IContentProvider, + ITreeContentProvider { public void dispose() { // TODO Auto-generated method stub } @@ -108,10 +114,15 @@ public class ProblemsTreeEditor extends CheckedTreeEditor { } public Object[] getChildren(Object parentElement) { - if (parentElement instanceof Object[]) return (Object[]) parentElement; - if (parentElement instanceof IProblemCategory) { return ((IProblemCategory) parentElement).getChildren(); } - if (parentElement instanceof IProblemProfile) { return ((IProblemProfile) parentElement).getRoot() - .getChildren(); } + if (parentElement instanceof Object[]) + return (Object[]) parentElement; + if (parentElement instanceof IProblemCategory) { + return ((IProblemCategory) parentElement).getChildren(); + } + if (parentElement instanceof IProblemProfile) { + return ((IProblemProfile) parentElement).getRoot() + .getChildren(); + } return new Object[0]; } @@ -128,35 +139,39 @@ public class ProblemsTreeEditor extends CheckedTreeEditor { } } + @Override public void checkStateChanged(CheckStateChangedEvent event) { Object element = event.getElement(); if (element instanceof IProblemWorkingCopy) { ((IProblemWorkingCopy) element).setEnabled(event.getChecked()); } else if (element instanceof IProblemCategory) { - IProblemCategory cat = (IProblemCategory)element; + IProblemCategory cat = (IProblemCategory) element; IProblemElement[] children = cat.getChildren(); for (int i = 0; i < children.length; i++) { IProblemElement pe = children[i]; - checkStateChanged(new CheckStateChangedEvent(getTreeViewer(), pe, event.getChecked())); + checkStateChanged(new CheckStateChangedEvent(getTreeViewer(), + pe, event.getChecked())); } } getTreeViewer().refresh(); } - - public ProblemsTreeEditor(Composite parent, IProblemProfile profile) { - super(PreferenceConstants.P_PROBLEMS, CodanUIMessages.ProblemsTreeEditor_Problems, parent); + super(PreferenceConstants.P_PROBLEMS, + CodanUIMessages.ProblemsTreeEditor_Problems, parent); setEmptySelectionAllowed(true); getTreeViewer().getTree().setHeaderVisible(true); // getTreeViewer().getTree(). getTreeViewer().setContentProvider(new ProblemsContentProvider()); getTreeViewer().setCheckStateProvider(new ProblemsCheckStateProvider()); // column Name - TreeViewerColumn column1 = new TreeViewerColumn(getTreeViewer(), SWT.NONE); + TreeViewerColumn column1 = new TreeViewerColumn(getTreeViewer(), + SWT.NONE); column1.getColumn().setWidth(300); - column1.getColumn().setText(CodanUIMessages.ProblemsTreeEditor_NameColumn); + column1.getColumn().setText( + CodanUIMessages.ProblemsTreeEditor_NameColumn); column1.setLabelProvider(new ColumnLabelProvider() { + @Override public String getText(Object element) { if (element instanceof IProblem) { IProblem p = (IProblem) element; @@ -170,27 +185,34 @@ public class ProblemsTreeEditor extends CheckedTreeEditor { } }); // column Severity - TreeViewerColumn column2 = new TreeViewerColumn(getTreeViewer(), SWT.NONE); + TreeViewerColumn column2 = new TreeViewerColumn(getTreeViewer(), + SWT.NONE); column2.getColumn().setWidth(100); - column2.getColumn().setText(CodanUIMessages.ProblemsTreeEditor_SeverityColumn); + column2.getColumn().setText( + CodanUIMessages.ProblemsTreeEditor_SeverityColumn); column2.setLabelProvider(new ColumnLabelProvider() { @Override public Image getImage(Object element) { - final ISharedImages images = PlatformUI.getWorkbench().getSharedImages(); + final ISharedImages images = PlatformUI.getWorkbench() + .getSharedImages(); if (element instanceof IProblem) { IProblem p = (IProblem) element; switch (p.getSeverity().intValue()) { - case IMarker.SEVERITY_INFO: - return images.getImage(ISharedImages.IMG_OBJS_INFO_TSK); - case IMarker.SEVERITY_WARNING: - return images.getImage(ISharedImages.IMG_OBJS_WARN_TSK); - case IMarker.SEVERITY_ERROR: - return images.getImage(ISharedImages.IMG_OBJS_ERROR_TSK); + case IMarker.SEVERITY_INFO: + return images + .getImage(ISharedImages.IMG_OBJS_INFO_TSK); + case IMarker.SEVERITY_WARNING: + return images + .getImage(ISharedImages.IMG_OBJS_WARN_TSK); + case IMarker.SEVERITY_ERROR: + return images + .getImage(ISharedImages.IMG_OBJS_ERROR_TSK); } } return null; } + @Override public String getText(Object element) { if (element instanceof IProblem) { IProblem p = (IProblem) element; @@ -200,18 +222,23 @@ public class ProblemsTreeEditor extends CheckedTreeEditor { } }); column2.setEditingSupport(new EditingSupport(getTreeViewer()) { + @Override protected boolean canEdit(Object element) { return element instanceof IProblem; } + @Override protected CellEditor getCellEditor(Object element) { - return new ComboBoxCellEditor(getTreeViewer().getTree(), CodanSeverity.stringValues()); + return new ComboBoxCellEditor(getTreeViewer().getTree(), + CodanSeverity.stringValues()); } + @Override protected Object getValue(Object element) { return ((IProblem) element).getSeverity().intValue(); } + @Override protected void setValue(Object element, Object value) { int index = ((Integer) value).intValue(); CodanSeverity val = CodanSeverity.values()[index]; @@ -276,6 +303,10 @@ public class ProblemsTreeEditor extends CheckedTreeEditor { String id = probs[i].getId(); String s = codanPreferencesLoader.getProperty(id); getPreferenceStore().setValue(id, s); + String params = codanPreferencesLoader.getPreferencesString(id); + if (params != null) + getPreferenceStore().setValue( + codanPreferencesLoader.getPreferencesKey(id), params); } }