All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.api.resources.JavaFile Maven / Gradle / Ivy

There is a newer version: 5.1
Show newest version
/*
 * 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.api.resources;

import org.apache.commons.lang.StringUtils;
import org.sonar.api.utils.WildcardPattern;

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

/**
 * @since 1.10
 */
public class JavaFile extends AbstractResource {

  private String packageName;

  public JavaFile(String packageName, String className) {
    this(packageName, className, false);
  }

  public JavaFile(String packageName, String className, boolean unitTest) {
    super(Resource.SCOPE_ENTITY, (unitTest ? Resource.QUALIFIER_UNIT_TEST_CLASS : Resource.QUALIFIER_CLASS));
    this.packageName = StringUtils.isBlank(packageName) ? JavaPackage.DEFAULT_PACKAGE_NAME : packageName;
    setLanguage(Java.INSTANCE);
    setName(className);
    setKey(new StringBuilder().append(this.packageName).append(".").append(getName()).toString());
  }

  public JavaFile(String key) {
    this(key, false);
  }

  public JavaFile(String key, boolean unitTest) {
    super(Resource.SCOPE_ENTITY, (unitTest ? Resource.QUALIFIER_UNIT_TEST_CLASS : Resource.QUALIFIER_CLASS));
    setLanguage(Java.INSTANCE);
    setKey(key);
    if (getKey().contains(".")) {
      setName(StringUtils.substringAfterLast(getKey(), "."));
      packageName = StringUtils.substringBeforeLast(getKey(), ".");
    } else {
      setName(getKey());
      setKey(JavaPackage.DEFAULT_PACKAGE_NAME.concat(".").concat(getKey()));
    }
  }

  @Override
  public JavaFile setKey(String key) {
    if (key != null && key.indexOf('$') > 0) {
      throw new IllegalArgumentException("Java inner classes are not supported yet : " + key);
    }
    return (JavaFile) super.setKey(key);
  }

  @Override
  public JavaPackage getParent() {
    return new JavaPackage(packageName);
  }

  public boolean matchFilePattern(String antPattern) {
    String patternWithoutFileSuffix = StringUtils.substringBeforeLast(antPattern, ".");
    WildcardPattern matcher = WildcardPattern.create(patternWithoutFileSuffix, ".");
    return matcher.match(getKey());
  }


  public static JavaFile fromIOFile(File file, List sourceDirs, boolean unitTest) {
    if (file == null || !StringUtils.endsWithIgnoreCase(file.getName(), ".java")) {
      return null;
    }
    String relativePath = DefaultProjectFileSystem.getRelativePath(file, sourceDirs);
    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;
  }

  public static JavaFile fromAbsolutePath(String path, List sourceDirs, boolean unitTest) {
    if (path == null) {
      return null;
    }
    return fromIOFile(new File(path), sourceDirs, unitTest);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy