aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-deprecated/src/main/java/org/sonar/api/resources/JavaFile.java
blob: 4bb59509364eccc40c580318cfcf7e8f74b20113 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * SonarQube, open source software quality management tool.
 * Copyright (C) 2008-2013 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * SonarQube 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.
 *
 * SonarQube 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 this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.api.resources;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.api.utils.WildcardPattern;

import java.io.File;
import java.util.List;

/**
 * A class that represents a Java class. This class can either be a Test class or source class
 *
 * @since 1.10
 * @deprecated since 4.2 use {@link org.sonar.api.resources.File}
 */
@Deprecated
public class JavaFile extends Resource {

  private static final String JAVA_SUFFIX = ".java";
  private static final String JAV_SUFFIX = ".jav";
  private String className;
  private String filename;
  private String fullyQualifiedName;
  private String packageFullyQualifiedName;
  private boolean unitTest;
  private JavaPackage parent;

  private JavaFile() {
    // Default constructor
  }

  /**
   * Creates a JavaFile that is not a class of test based on package and file names
   */
  public JavaFile(String packageName, String className) {
    this(packageName, className, false);
  }

  /**
   * Creates a JavaFile that can be of any type based on package and file names
   *
   * @param unitTest whether it is a unit test file or a source file
   */
  public JavaFile(String packageKey, String className, boolean unitTest) {
    if (className == null) {
      throw new IllegalArgumentException("Java filename can not be null");
    }
    this.className = StringUtils.trim(className);
    String deprecatedKey;
    if (StringUtils.isBlank(packageKey)) {
      this.packageFullyQualifiedName = JavaPackage.DEFAULT_PACKAGE_NAME;
      this.fullyQualifiedName = this.className;
      deprecatedKey = new StringBuilder().append(this.packageFullyQualifiedName).append(".").append(this.className).toString();
    } else {
      this.packageFullyQualifiedName = packageKey.trim();
      deprecatedKey = new StringBuilder().append(this.packageFullyQualifiedName).append(".").append(this.className).toString();
      this.fullyQualifiedName = deprecatedKey;
    }
    setDeprecatedKey(deprecatedKey);
    this.unitTest = unitTest;
  }

  /**
   * Creates a source file from its key
   */
  public JavaFile(String deprecatedKey) {
    this(deprecatedKey, false);
  }

  /**
   * Creates any JavaFile from its key
   *
   * @param unitTest whether it is a unit test file or a source file
   */
  public JavaFile(String deprecatedKey, boolean unitTest) {
    if (deprecatedKey == null) {
      throw new IllegalArgumentException("Java filename can not be null");
    }
    String realKey = StringUtils.trim(deprecatedKey);
    this.unitTest = unitTest;

    if (realKey.contains(".")) {
      this.className = StringUtils.substringAfterLast(realKey, ".");
      this.packageFullyQualifiedName = StringUtils.substringBeforeLast(realKey, ".");
      this.fullyQualifiedName = realKey;

    } else {
      this.className = realKey;
      this.fullyQualifiedName = realKey;
      this.packageFullyQualifiedName = JavaPackage.DEFAULT_PACKAGE_NAME;
      realKey = new StringBuilder().append(JavaPackage.DEFAULT_PACKAGE_NAME).append(".").append(realKey).toString();
    }
    setDeprecatedKey(realKey);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public JavaPackage getParent() {
    if (parent == null) {
      parent = new JavaPackage(packageFullyQualifiedName);
    }
    return parent;
  }

  /**
   * @return null
   */
  @Override
  public String getDescription() {
    return null;
  }

  /**
   * @return Java
   */
  @Override
  public Language getLanguage() {
    return Java.INSTANCE;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public String getName() {
    return StringUtils.isNotBlank(filename) ? filename : (className + JAVA_SUFFIX);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public String getLongName() {
    return fullyQualifiedName;
  }

  /**
   * @return SCOPE_ENTITY
   */
  @Override
  public String getScope() {
    return Scopes.FILE;
  }

  /**
   * @return QUALIFIER_UNIT_TEST_CLASS or QUALIFIER_FILE depending whether it is a unit test class
   */
  @Override
  public String getQualifier() {
    return unitTest ? Qualifiers.UNIT_TEST_FILE : Qualifiers.FILE;
  }

  /**
   * @return whether the JavaFile is a unit test class or not
   */
  public boolean isUnitTest() {
    return unitTest;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public boolean matchFilePattern(String antPattern) {
    WildcardPattern matcher = WildcardPattern.create(antPattern, Directory.SEPARATOR);
    return matcher.match(getKey());
  }

  public static JavaFile fromIOFile(File file, Project module, boolean unitTest) {
    if (file == null || !StringUtils.endsWithIgnoreCase(file.getName(), JAVA_SUFFIX)) {
      return null;
    }
    PathResolver.RelativePath relativePath = new PathResolver().relativePath(
      unitTest ? module.getFileSystem().getTestDirs() : module.getFileSystem().getSourceDirs(),
      file);
    if (relativePath != null) {
      JavaFile sonarFile = fromRelativePath(relativePath.path(), unitTest);
      sonarFile.setPath(new PathResolver().relativePath(module.getFileSystem().getBasedir(), file));
      return sonarFile;
    }
    return null;
  }

  /**
   * For internal use only.
   */
  public static JavaFile create(String relativePathFromBasedir) {
    JavaFile javaFile = new JavaFile();
    String normalizedPath = normalize(relativePathFromBasedir);
    javaFile.setKey(normalizedPath);
    javaFile.setPath(normalizedPath);
    javaFile.parent = new JavaPackage();
    String directoryPath;
    if (normalizedPath.contains(Directory.SEPARATOR)) {
      directoryPath = StringUtils.substringBeforeLast(normalizedPath, Directory.SEPARATOR);
    } else {
      directoryPath = Directory.SEPARATOR;
    }
    String normalizedParentPath = normalize(directoryPath);
    javaFile.parent.setKey(normalizedParentPath);
    javaFile.parent.setPath(normalizedParentPath);
    return javaFile;
  }

  /**
   * For internal use only.
   */
  public static JavaFile create(String relativePathFromBasedir, String relativePathFromSourceDir, boolean unitTest) {
    JavaFile javaFile = JavaFile.create(relativePathFromBasedir);
    if (relativePathFromSourceDir.contains(Directory.SEPARATOR)) {
      javaFile.packageFullyQualifiedName = StringUtils.substringBeforeLast(relativePathFromSourceDir, Directory.SEPARATOR);
      javaFile.packageFullyQualifiedName = StringUtils.replace(javaFile.packageFullyQualifiedName, Directory.SEPARATOR, ".");
      javaFile.filename = StringUtils.substringAfterLast(relativePathFromSourceDir, Directory.SEPARATOR);
      if (javaFile.filename.endsWith(JAVA_SUFFIX)) {
        javaFile.className = StringUtils.removeEndIgnoreCase(javaFile.filename, JAVA_SUFFIX);
      } else if (javaFile.filename.endsWith(JAV_SUFFIX)) {
        javaFile.className = StringUtils.removeEndIgnoreCase(javaFile.filename, JAV_SUFFIX);
      }
      javaFile.fullyQualifiedName = javaFile.packageFullyQualifiedName + "." + javaFile.className;
      javaFile.setDeprecatedKey(javaFile.fullyQualifiedName);
      javaFile.parent.setDeprecatedKey(Directory.parseKey(StringUtils.substringBeforeLast(relativePathFromSourceDir, Directory.SEPARATOR)));
    } else {
      javaFile.packageFullyQualifiedName = JavaPackage.DEFAULT_PACKAGE_NAME;
      javaFile.className = StringUtils.removeEndIgnoreCase(relativePathFromSourceDir, JAVA_SUFFIX);
      javaFile.fullyQualifiedName = javaFile.className;
      javaFile.setDeprecatedKey(JavaPackage.DEFAULT_PACKAGE_NAME + "." + javaFile.className);
      javaFile.parent.setDeprecatedKey(Directory.ROOT);
    }
    javaFile.unitTest = unitTest;
    return javaFile;
  }

  /**
   * @deprecated since 4.2 use {@link #create(String, String, boolean)}
   */
  @Deprecated
  public static JavaFile fromRelativePath(String relativePath, boolean unitTest) {
    if (relativePath != null) {
      String pacname = null;
      String classname = relativePath;

      if (relativePath.indexOf('/') >= 0) {
        pacname = StringUtils.substringBeforeLast(relativePath, "/");
        pacname = StringUtils.replace(pacname, "/", ".");
        classname = StringUtils.substringAfterLast(relativePath, "/");
      }
      classname = StringUtils.substringBeforeLast(classname, ".");
      return new JavaFile(pacname, classname, unitTest);
    }
    return null;
  }

  /**
   * Creates a JavaFile from a file in the source directories
   *
   * @return the JavaFile created if exists, null otherwise
   * @deprecated since 4.2 use {@link #create(String, String, boolean)}
   */
  @Deprecated
  public static JavaFile fromIOFile(File file, List<File> sourceDirs, boolean unitTest) {
    if (file == null || !StringUtils.endsWithIgnoreCase(file.getName(), JAVA_SUFFIX)) {
      return null;
    }
    PathResolver.RelativePath relativePath = new PathResolver().relativePath(sourceDirs, file);
    if (relativePath != null) {
      return fromRelativePath(relativePath.path(), unitTest);
    }
    return null;
  }

  /**
   * Shortcut to fromIOFile with an abolute path
   * @deprecated since 4.2 use {@link #create(String, String, boolean)}
   */
  @Deprecated
  public static JavaFile fromAbsolutePath(String path, List<File> sourceDirs, boolean unitTest) {
    if (path == null) {
      return null;
    }
    return fromIOFile(new File(path), sourceDirs, unitTest);
  }

  @Override
  public String toString() {
    return new ToStringBuilder(this)
      .append("key", getKey())
      .append("deprecatedKey", getDeprecatedKey())
      .append("path", getPath())
      .append("filename", className)
      .toString();
  }

}