From 5b4a5f39b3081312c37e9cf436aee6b04f8995d0 Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Wed, 19 Jan 2011 22:36:38 +0100 Subject: [PATCH] create the module sonar-java-api dedicated to Java support --- plugins/sonar-squid-java-plugin/pom.xml | 5 + pom.xml | 35 +++---- sonar-java-api/pom.xml | 29 ++++++ .../java/org/sonar/java/api/JavaClass.java | 96 +++++++++++++++++++ .../java/org/sonar/java/api/JavaUtils.java | 43 +++++++++ .../org/sonar/java/api/JavaClassTest.java | 51 ++++++++++ .../org/sonar/java/api/JavaUtilsTest.java | 39 ++++++++ 7 files changed, 274 insertions(+), 24 deletions(-) create mode 100644 sonar-java-api/pom.xml create mode 100644 sonar-java-api/src/main/java/org/sonar/java/api/JavaClass.java create mode 100644 sonar-java-api/src/main/java/org/sonar/java/api/JavaUtils.java create mode 100644 sonar-java-api/src/test/java/org/sonar/java/api/JavaClassTest.java create mode 100644 sonar-java-api/src/test/java/org/sonar/java/api/JavaUtilsTest.java diff --git a/plugins/sonar-squid-java-plugin/pom.xml b/plugins/sonar-squid-java-plugin/pom.xml index c5054b67cb1..70cb1d91ee2 100644 --- a/plugins/sonar-squid-java-plugin/pom.xml +++ b/plugins/sonar-squid-java-plugin/pom.xml @@ -14,6 +14,11 @@ Squid analyzer for Java. + + org.codehaus.sonar + sonar-java-api + provided + org.codehaus.sonar sonar-plugin-api diff --git a/pom.xml b/pom.xml index d7dbbdf2d68..f582f85f35d 100644 --- a/pom.xml +++ b/pom.xml @@ -13,20 +13,21 @@ archetypes/sonar-basic-plugin archetypes/sonar-gwt-plugin - sonar-core - sonar-deprecated sonar-batch - sonar-maven-plugin - sonar-maven3-plugin sonar-channel sonar-check-api sonar-colorizer + sonar-core + sonar-deprecated sonar-duplications sonar-graph sonar-gwt-api + sonar-java-api + sonar-maven-plugin + sonar-maven3-plugin sonar-plugin-api - sonar-testing-harness sonar-squid + sonar-testing-harness sonar-ws-client plugins/sonar-core-gwt plugins/sonar-core-plugin @@ -314,22 +315,6 @@ - org.apache.maven.plugins maven-javadoc-plugin @@ -342,8 +327,6 @@ mmmm ${project.reporting.outputDirectory}/${project.version}/apidocs - - org.apache.maven.plugins maven-jxr-plugin @@ -353,7 +336,6 @@ mmmm ${project.reporting.outputDirectory}/${project.version}/apidocs - org.apache.maven.plugins maven-source-plugin @@ -413,6 +395,11 @@ mmmm sonar-gwt-api ${project.version} + + org.codehaus.sonar + sonar-java-api + ${project.version} + org.codehaus.sonar sonar-plugin-api diff --git a/sonar-java-api/pom.xml b/sonar-java-api/pom.xml new file mode 100644 index 00000000000..26577b644b9 --- /dev/null +++ b/sonar-java-api/pom.xml @@ -0,0 +1,29 @@ + + + 4.0.0 + + org.codehaus.sonar + sonar + 2.6-SNAPSHOT + + sonar-java-api + Sonar :: Java API + + + org.codehaus.sonar + sonar-plugin-api + + + + + junit + junit + test + + + org.hamcrest + hamcrest-all + test + + + diff --git a/sonar-java-api/src/main/java/org/sonar/java/api/JavaClass.java b/sonar-java-api/src/main/java/org/sonar/java/api/JavaClass.java new file mode 100644 index 00000000000..4a2b9d7086b --- /dev/null +++ b/sonar-java-api/src/main/java/org/sonar/java/api/JavaClass.java @@ -0,0 +1,96 @@ +/* + * 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.java.api; + +import org.apache.commons.lang.StringUtils; +import org.sonar.api.resources.*; + +/** + * @since 2.6 + */ +public final class JavaClass extends Resource { + + private String name; + + private JavaClass(String name) { + this.name = name; + setKey(name); + } + + public String getPackageName() { + return StringUtils.substringBeforeLast(name, JavaUtils.PACKAGE_SEPARATOR); + } + + public String getClassName() { + return StringUtils.substringAfterLast(name, JavaUtils.PACKAGE_SEPARATOR); + } + + @Override + public String getName() { + return getClassName(); + } + + @Override + public String getLongName() { + return name; + } + + @Override + public String getDescription() { + return null; + } + + @Override + public Language getLanguage() { + return Java.INSTANCE; + } + + @Override + public String getScope() { + return ResourceScopes.TYPE; + } + + @Override + public String getQualifier() { + return ResourceQualifiers.CLASS; + } + + @Override + public Resource getParent() { + return null; + } + + @Override + public boolean matchFilePattern(String antPattern) { + return false; + } + + public static JavaClass create(String name) { + return new JavaClass(name); + } + + public static JavaClass create(String packageName, String className) { + if (StringUtils.isBlank(packageName)) { + return new JavaClass(className); + } + String name = new StringBuilder().append(packageName).append(JavaUtils.PACKAGE_SEPARATOR).append(className).toString(); + return new JavaClass(name); + } +} diff --git a/sonar-java-api/src/main/java/org/sonar/java/api/JavaUtils.java b/sonar-java-api/src/main/java/org/sonar/java/api/JavaUtils.java new file mode 100644 index 00000000000..a1925501b5d --- /dev/null +++ b/sonar-java-api/src/main/java/org/sonar/java/api/JavaUtils.java @@ -0,0 +1,43 @@ +/* + * 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.java.api; + +import org.apache.commons.lang.StringUtils; + +public final class JavaUtils { + + public static final String PACKAGE_SEPARATOR = "."; + + private JavaUtils() { + // only static methods + } + + public static String abbreviatePackage(String packageName) { + String[] parts = StringUtils.split(packageName, PACKAGE_SEPARATOR); + StringBuilder sb = new StringBuilder(); + if (parts.length>=1) { + sb.append(parts[0]); + } + for (int index=1 ; index