001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2011 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * Sonar is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.api.resources; 021 022 import org.apache.commons.lang.StringUtils; 023 import org.sonar.api.utils.WildcardPattern; 024 025 import java.io.File; 026 import java.util.List; 027 028 /** 029 * A class that represents a Java class. This class can either be a Test class or source class 030 * 031 * @since 1.10 032 */ 033 public class JavaFile extends Resource<JavaPackage> { 034 035 private String filename; 036 private String longName; 037 private String packageKey; 038 private boolean unitTest; 039 private JavaPackage parent; 040 041 /** 042 * Creates a JavaFile that is not a class of test based on package and file names 043 */ 044 public JavaFile(String packageName, String className) { 045 this(packageName, className, false); 046 } 047 048 /** 049 * Creates a JavaFile that can be of any type based on package and file names 050 * 051 * @param unitTest whether it is a unit test file or a source file 052 */ 053 public JavaFile(String packageKey, String className, boolean unitTest) { 054 if (className == null) { 055 throw new IllegalArgumentException("Java filename can not be null"); 056 } 057 if (className.indexOf('$') >= 0) { 058 throw new IllegalArgumentException("Java inner classes are not supported : " + className); 059 } 060 this.filename = StringUtils.trim(className); 061 String key; 062 if (StringUtils.isBlank(packageKey)) { 063 this.packageKey = JavaPackage.DEFAULT_PACKAGE_NAME; 064 this.longName = this.filename; 065 key = new StringBuilder().append(this.packageKey).append(".").append(this.filename).toString(); 066 } else { 067 this.packageKey = packageKey.trim(); 068 key = new StringBuilder().append(this.packageKey).append(".").append(this.filename).toString(); 069 this.longName = key; 070 } 071 setKey(key); 072 this.unitTest = unitTest; 073 } 074 075 /** 076 * Creates a source file from its key 077 */ 078 public JavaFile(String key) { 079 this(key, false); 080 } 081 082 /** 083 * Creates any JavaFile from its key 084 * 085 * @param unitTest whether it is a unit test file or a source file 086 */ 087 public JavaFile(String key, boolean unitTest) { 088 if (key == null) { 089 throw new IllegalArgumentException("Java filename can not be null"); 090 } 091 if (key.indexOf('$') >= 0) { 092 throw new IllegalArgumentException("Java inner classes are not supported : " + key); 093 } 094 String realKey = StringUtils.trim(key); 095 this.unitTest = unitTest; 096 097 if (realKey.contains(".")) { 098 this.filename = StringUtils.substringAfterLast(realKey, "."); 099 this.packageKey = StringUtils.substringBeforeLast(realKey, "."); 100 this.longName = realKey; 101 102 } else { 103 this.filename = realKey; 104 this.longName = realKey; 105 this.packageKey = JavaPackage.DEFAULT_PACKAGE_NAME; 106 realKey = new StringBuilder().append(JavaPackage.DEFAULT_PACKAGE_NAME).append(".").append(realKey).toString(); 107 } 108 setKey(realKey); 109 } 110 111 /** 112 * {@inheritDoc} 113 */ 114 public JavaPackage getParent() { 115 if (parent == null) { 116 parent = new JavaPackage(packageKey); 117 } 118 return parent; 119 120 } 121 122 /** 123 * @return null 124 */ 125 public String getDescription() { 126 return null; 127 } 128 129 /** 130 * @return Java 131 */ 132 public Language getLanguage() { 133 return Java.INSTANCE; 134 } 135 136 /** 137 * {@inheritDoc} 138 */ 139 public String getName() { 140 return filename; 141 } 142 143 /** 144 * {@inheritDoc} 145 */ 146 public String getLongName() { 147 return longName; 148 } 149 150 /** 151 * @return SCOPE_ENTITY 152 */ 153 public String getScope() { 154 return Scopes.FILE; 155 } 156 157 /** 158 * @return QUALIFIER_UNIT_TEST_CLASS or QUALIFIER_CLASS depending whether it is a unit test class 159 */ 160 public String getQualifier() { 161 return unitTest ? Qualifiers.UNIT_TEST_FILE : Qualifiers.CLASS; 162 } 163 164 /** 165 * @return whether the JavaFile is a unit test class or not 166 */ 167 public boolean isUnitTest() { 168 return unitTest; 169 } 170 171 /** 172 * {@inheritDoc} 173 */ 174 public boolean matchFilePattern(String antPattern) { 175 if (unitTest) { 176 return false; 177 } 178 String fileKey = getKey(); 179 if (!fileKey.endsWith(".java")) { 180 fileKey += ".java"; 181 } 182 if (StringUtils.substringAfterLast(antPattern, "/").indexOf(".") < 0) { 183 antPattern += ".*"; 184 } 185 WildcardPattern matcher = WildcardPattern.create(antPattern, "."); 186 return matcher.match(fileKey); 187 } 188 189 public static JavaFile fromRelativePath(String relativePath, boolean unitTest) { 190 if (relativePath != null) { 191 String pacname = null; 192 String classname = relativePath; 193 194 if (relativePath.indexOf('/') >= 0) { 195 pacname = StringUtils.substringBeforeLast(relativePath, "/"); 196 pacname = StringUtils.replace(pacname, "/", "."); 197 classname = StringUtils.substringAfterLast(relativePath, "/"); 198 } 199 classname = StringUtils.substringBeforeLast(classname, "."); 200 return new JavaFile(pacname, classname, unitTest); 201 } 202 return null; 203 } 204 205 /** 206 * Creates a JavaFile from a file in the source directories 207 * 208 * @return the JavaFile created if exists, null otherwise 209 */ 210 public static JavaFile fromIOFile(File file, List<File> sourceDirs, boolean unitTest) { 211 if (file == null || !StringUtils.endsWithIgnoreCase(file.getName(), ".java")) { 212 return null; 213 } 214 String relativePath = DefaultProjectFileSystem.getRelativePath(file, sourceDirs); 215 return fromRelativePath(relativePath, unitTest); 216 } 217 218 /** 219 * Shortcut to fromIOFile with an abolute path 220 */ 221 public static JavaFile fromAbsolutePath(String path, List<File> sourceDirs, boolean unitTest) { 222 if (path == null) { 223 return null; 224 } 225 return fromIOFile(new File(path), sourceDirs, unitTest); 226 } 227 228 @Override 229 public String toString() { 230 return getKey(); 231 } 232 233 // @Override 234 // public boolean equals(Object o) { 235 // if (this == o) return true; 236 // if (o == null || getClass() != o.getClass()) return false; 237 // if (!super.equals(o)) return false; 238 // 239 // JavaFile javaFile = (JavaFile) o; 240 // if (unitTest != javaFile.unitTest) return false; 241 // if (!getKey().equals(javaFile.getKey())) return false; 242 // 243 // return true; 244 // } 245 // 246 // @Override 247 // public int hashCode() { 248 // int result = super.hashCode(); 249 // result = 31 * result + getKey().hashCode(); 250 // result = 31 * result + (unitTest ? 1 : 0); 251 // return result; 252 // } 253 }