From 5b4a5f39b3081312c37e9cf436aee6b04f8995d0 Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Wed, 19 Jan 2011 22:36:38 +0100 Subject: create the module sonar-java-api dedicated to Java support --- .../main/java/org/sonar/java/api/JavaClass.java | 96 ++++++++++++++++++++++ .../main/java/org/sonar/java/api/JavaUtils.java | 43 ++++++++++ .../java/org/sonar/java/api/JavaClassTest.java | 51 ++++++++++++ .../java/org/sonar/java/api/JavaUtilsTest.java | 39 +++++++++ 4 files changed, 229 insertions(+) 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 (limited to 'sonar-java-api/src') 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