aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-check-api/src
diff options
context:
space:
mode:
authorsimonbrandhof <simon.brandhof@gmail.com>2010-09-06 14:08:06 +0000
committersimonbrandhof <simon.brandhof@gmail.com>2010-09-06 14:08:06 +0000
commitaeadc1f9129274949daaa57738c7c4550bdfbc7b (patch)
tree08dadf5ef7474fc41d1d48f74648f1ba8b55f34d /sonar-check-api/src
downloadsonarqube-aeadc1f9129274949daaa57738c7c4550bdfbc7b.tar.gz
sonarqube-aeadc1f9129274949daaa57738c7c4550bdfbc7b.zip
SONAR-236 remove deprecated code from checkstyle plugin + display default value of rule parameters in Q profile console
Diffstat (limited to 'sonar-check-api/src')
-rw-r--r--sonar-check-api/src/main/java/org/sonar/check/AnnotationIntrospector.java71
-rw-r--r--sonar-check-api/src/main/java/org/sonar/check/BelongsToProfile.java38
-rw-r--r--sonar-check-api/src/main/java/org/sonar/check/BelongsToProfiles.java36
-rw-r--r--sonar-check-api/src/main/java/org/sonar/check/Check.java64
-rw-r--r--sonar-check-api/src/main/java/org/sonar/check/CheckProperty.java46
-rw-r--r--sonar-check-api/src/main/java/org/sonar/check/IsoCategory.java26
-rw-r--r--sonar-check-api/src/main/java/org/sonar/check/Message.java34
-rw-r--r--sonar-check-api/src/main/java/org/sonar/check/Priority.java28
-rw-r--r--sonar-check-api/src/test/java/org/sonar/check/AnnotationIntrospectorTest.java88
-rw-r--r--sonar-check-api/src/test/java/org/sonar/check/CheckTest.java44
-rw-r--r--sonar-check-api/src/test/java/org/sonar/check/FakeCheck.java40
11 files changed, 515 insertions, 0 deletions
diff --git a/sonar-check-api/src/main/java/org/sonar/check/AnnotationIntrospector.java b/sonar-check-api/src/main/java/org/sonar/check/AnnotationIntrospector.java
new file mode 100644
index 00000000000..15def7431c3
--- /dev/null
+++ b/sonar-check-api/src/main/java/org/sonar/check/AnnotationIntrospector.java
@@ -0,0 +1,71 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.check;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.List;
+
+public final class AnnotationIntrospector {
+
+ private AnnotationIntrospector() {
+ // only static methods
+ }
+
+ public static String getCheckKey(Class annotatedClass) {
+ Check checkAnnotation = getCheckAnnotation(annotatedClass);
+ if (checkAnnotation == null) {
+ return null;
+ }
+
+ String key = checkAnnotation.key();
+ if (key == null || "".equals(key.trim())) {
+ key = annotatedClass.getCanonicalName();
+ }
+ return key;
+ }
+
+ public static Check getCheckAnnotation(Class annotatedClass) {
+ return (Check) annotatedClass.getAnnotation(Check.class);
+ }
+
+ public static List<Field> getPropertyFields(Class annotatedClass) {
+ List<Field> fields = new ArrayList<Field>();
+ for (Field field : annotatedClass.getDeclaredFields()) {
+ org.sonar.check.CheckProperty propertyAnnotation = field.getAnnotation(org.sonar.check.CheckProperty.class);
+ if (propertyAnnotation != null) {
+ fields.add(field);
+ }
+ }
+ return fields;
+ }
+
+ public static String getPropertyFieldKey(Field field) {
+ String key = null;
+ org.sonar.check.CheckProperty propertyAnnotation = field.getAnnotation(org.sonar.check.CheckProperty.class);
+ if (propertyAnnotation != null) {
+ key = propertyAnnotation.key();
+ if (key == null || "".equals(key)) {
+ key = field.getName();
+ }
+ }
+ return key;
+ }
+}
diff --git a/sonar-check-api/src/main/java/org/sonar/check/BelongsToProfile.java b/sonar-check-api/src/main/java/org/sonar/check/BelongsToProfile.java
new file mode 100644
index 00000000000..a79a64817f8
--- /dev/null
+++ b/sonar-check-api/src/main/java/org/sonar/check/BelongsToProfile.java
@@ -0,0 +1,38 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.check;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * @since 2.1
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
+public @interface BelongsToProfile {
+
+ String title();
+
+ Priority priority();
+
+}
diff --git a/sonar-check-api/src/main/java/org/sonar/check/BelongsToProfiles.java b/sonar-check-api/src/main/java/org/sonar/check/BelongsToProfiles.java
new file mode 100644
index 00000000000..a7d15fdf891
--- /dev/null
+++ b/sonar-check-api/src/main/java/org/sonar/check/BelongsToProfiles.java
@@ -0,0 +1,36 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.check;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * @since 2.1
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
+public @interface BelongsToProfiles {
+
+ BelongsToProfile[] value() default {};
+
+}
diff --git a/sonar-check-api/src/main/java/org/sonar/check/Check.java b/sonar-check-api/src/main/java/org/sonar/check/Check.java
new file mode 100644
index 00000000000..8a8ae8ef1af
--- /dev/null
+++ b/sonar-check-api/src/main/java/org/sonar/check/Check.java
@@ -0,0 +1,64 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.check;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * @since 2.1
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
+public @interface Check {
+
+ /**
+ * The default key is the class name.
+ */
+ String key() default "";
+
+ /**
+ * The path to resource bundles (optional). If not set, then it equals the class name.
+ */
+ String bundle() default "";
+
+ /**
+ * The check title. If not defined, then the title is the key
+ */
+ String title() default "";
+
+ /**
+ * The check description, optional.
+ */
+ String description() default "";
+
+ /**
+ * Default priority.
+ */
+ Priority priority() default Priority.MAJOR;
+
+
+ /**
+ * Will probably be deprecated and replaced by tags in version 2.2
+ */
+ IsoCategory isoCategory();
+}
diff --git a/sonar-check-api/src/main/java/org/sonar/check/CheckProperty.java b/sonar-check-api/src/main/java/org/sonar/check/CheckProperty.java
new file mode 100644
index 00000000000..17ac5801e53
--- /dev/null
+++ b/sonar-check-api/src/main/java/org/sonar/check/CheckProperty.java
@@ -0,0 +1,46 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.check;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * @since 2.1
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+public @interface CheckProperty {
+
+ /**
+ * The default key is the field name, read by reflection. Overriding this key can be useful when
+ * obfuscating the code.
+ */
+ String key() default "";
+
+ String title() default "";
+
+ /**
+ * Optional description
+ */
+ String description() default "";
+}
diff --git a/sonar-check-api/src/main/java/org/sonar/check/IsoCategory.java b/sonar-check-api/src/main/java/org/sonar/check/IsoCategory.java
new file mode 100644
index 00000000000..d85967bf0a2
--- /dev/null
+++ b/sonar-check-api/src/main/java/org/sonar/check/IsoCategory.java
@@ -0,0 +1,26 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.check;
+
+public enum IsoCategory {
+
+ Reliability, Efficiency, Portability, Usability, Maintainability, NONE
+
+}
diff --git a/sonar-check-api/src/main/java/org/sonar/check/Message.java b/sonar-check-api/src/main/java/org/sonar/check/Message.java
new file mode 100644
index 00000000000..0eecb1437c2
--- /dev/null
+++ b/sonar-check-api/src/main/java/org/sonar/check/Message.java
@@ -0,0 +1,34 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.check;
+
+import java.util.Locale;
+
+public interface Message {
+
+ /**
+ * Return the checker which logged the message. The checker class must be annotated with <code>@Check</code>
+ */
+ Object getChecker();
+
+ Integer getLine();
+
+ String getText(Locale locale);
+}
diff --git a/sonar-check-api/src/main/java/org/sonar/check/Priority.java b/sonar-check-api/src/main/java/org/sonar/check/Priority.java
new file mode 100644
index 00000000000..bdeb121b3a5
--- /dev/null
+++ b/sonar-check-api/src/main/java/org/sonar/check/Priority.java
@@ -0,0 +1,28 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.check;
+
+public enum Priority {
+ /**
+ * WARNING : DO NOT CHANGE THE ENUMERATION ORDER
+ * the enum ordinal is used for db persistence
+ */
+ INFO, MINOR, MAJOR, CRITICAL, BLOCKER
+}
diff --git a/sonar-check-api/src/test/java/org/sonar/check/AnnotationIntrospectorTest.java b/sonar-check-api/src/test/java/org/sonar/check/AnnotationIntrospectorTest.java
new file mode 100644
index 00000000000..d0ae45c084e
--- /dev/null
+++ b/sonar-check-api/src/test/java/org/sonar/check/AnnotationIntrospectorTest.java
@@ -0,0 +1,88 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.check;
+
+import org.junit.Test;
+
+import java.lang.reflect.Field;
+import java.util.List;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.*;
+
+public class AnnotationIntrospectorTest {
+
+ @Test
+ public void defaultCheckKeyIsClassname() {
+ assertThat(AnnotationIntrospector.getCheckKey(SimplestCheck.class), is(SimplestCheck.class.getCanonicalName()));
+ }
+
+ @Test
+ public void checkKeyCanBeOverriden() {
+ assertThat(AnnotationIntrospector.getCheckKey(CheckWithOverridenKey.class), is("overridenKey"));
+ }
+
+ @Test
+ public void noProperties() {
+ assertThat(AnnotationIntrospector.getPropertyFields(SimplestCheck.class).size(), is(0));
+ }
+
+ @Test
+ public void getProperties() {
+ List<Field> fields = AnnotationIntrospector.getPropertyFields(CheckWithProperties.class);
+ assertThat(fields.size(), is(2));
+
+ assertThat(fields.get(0).getName(), is("active"));
+ assertThat(AnnotationIntrospector.getPropertyFieldKey(fields.get(0)), is("active"));
+
+ assertThat(fields.get(1).getName(), is("max"));
+ assertThat(AnnotationIntrospector.getPropertyFieldKey(fields.get(1)), is("Maximum"));
+ }
+
+
+ @Test
+ public void getCheckAnnotation() {
+ assertNotNull(AnnotationIntrospector.getCheckAnnotation(SimplestCheck.class));
+ assertNull(AnnotationIntrospector.getCheckAnnotation(String.class));
+ }
+}
+
+
+@Check(isoCategory = IsoCategory.Portability, priority = Priority.CRITICAL)
+class SimplestCheck {
+
+}
+
+@Check(key = "overridenKey", isoCategory = IsoCategory.Portability, priority = Priority.CRITICAL)
+class CheckWithOverridenKey {
+
+}
+
+@Check(isoCategory = IsoCategory.Portability, priority = Priority.CRITICAL)
+class CheckWithProperties {
+
+ @CheckProperty
+ private boolean active = false;
+
+ @CheckProperty(key = "Maximum")
+ private int max = 50;
+
+ private String nonProperty;
+} \ No newline at end of file
diff --git a/sonar-check-api/src/test/java/org/sonar/check/CheckTest.java b/sonar-check-api/src/test/java/org/sonar/check/CheckTest.java
new file mode 100644
index 00000000000..e0758fb789d
--- /dev/null
+++ b/sonar-check-api/src/test/java/org/sonar/check/CheckTest.java
@@ -0,0 +1,44 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.check;
+
+import org.junit.Test;
+
+import java.lang.reflect.Field;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+public class CheckTest {
+ @Test
+ public void getCheckMetadata() {
+ Check check = FakeCheck.class.getAnnotation(Check.class);
+ assertThat(check.title(), is("Detailed Check"));
+ assertThat(check.description(), is("Detailed description"));
+ assertThat(check.isoCategory(), is(IsoCategory.Reliability));
+ }
+
+ @Test
+ public void getCheckProperty() throws NoSuchFieldException {
+ Field field = FakeCheck.class.getDeclaredField("max");
+ CheckProperty propertyAnnotation = field.getAnnotation(CheckProperty.class);
+ assertThat(propertyAnnotation.description(), is("Maximum value"));
+ }
+}
diff --git a/sonar-check-api/src/test/java/org/sonar/check/FakeCheck.java b/sonar-check-api/src/test/java/org/sonar/check/FakeCheck.java
new file mode 100644
index 00000000000..46f8e80d390
--- /dev/null
+++ b/sonar-check-api/src/test/java/org/sonar/check/FakeCheck.java
@@ -0,0 +1,40 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.check;
+
+@Check(title ="Detailed Check", description = "Detailed description", isoCategory = IsoCategory.Reliability)
+public class FakeCheck {
+
+ @CheckProperty(description = "Maximum value")
+ private String max;
+
+ @CheckProperty(description ="Minimum value")
+ protected String min;
+
+ private int nonConfigurableProperty;
+
+ public String getMax() {
+ return max;
+ }
+
+ public void setMax(String max) {
+ this.max = max;
+ }
+}