Java Code Examples for com.intellij.psi.PsiPackage#getQualifiedName()

The following examples show how to use com.intellij.psi.PsiPackage#getQualifiedName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NewClassCommandAction.java    From json2java4idea with Apache License 2.0 6 votes vote down vote up
@Override
protected void run(@NotNull Result<PsiFile> result) throws Throwable {
    final PsiPackage packageElement = directoryService.getPackage(directory);
    if (packageElement == null) {
        throw new InvalidDirectoryException("Target directory does not provide a package");
    }

    final String fileName = Extensions.append(name, StdFileTypes.JAVA);
    final PsiFile found = directory.findFile(fileName);
    if (found != null) {
        throw new ClassAlreadyExistsException("Class '" + name + "'already exists in " + packageElement.getName());
    }

    final String packageName = packageElement.getQualifiedName();
    final String className = Extensions.remove(this.name, StdFileTypes.JAVA);
    try {
        final String java = converter.convert(packageName, className, json);
        final PsiFile classFile = fileFactory.createFileFromText(fileName, JavaFileType.INSTANCE, java);
        CodeStyleManager.getInstance(classFile.getProject()).reformat(classFile);
        JavaCodeStyleManager.getInstance(classFile.getProject()).optimizeImports(classFile);
        final PsiFile created = (PsiFile) directory.add(classFile);
        result.setResult(created);
    } catch (IOException e) {
        throw new ClassCreationException("Failed to create new class from JSON", e);
    }
}
 
Example 2
Source File: ComponentFinder.java    From litho with Apache License 2.0 5 votes vote down vote up
@Override
public PsiClass[] getClasses(PsiPackage psiPackage, GlobalSearchScope scope) {
  if (!scope.contains(dummyFile)) return PsiClass.EMPTY_ARRAY;

  // We don't create own package, but provide additional classes to existing one
  final String packageQN = psiPackage.getQualifiedName();
  return Arrays.stream(ComponentsCacheService.getInstance(project).getAllComponents())
      .filter(cls -> StringUtil.getPackageName(cls.getQualifiedName()).equals(packageQN))
      .toArray(PsiClass[]::new);
}
 
Example 3
Source File: GenerateAction.java    From RIBs with Apache License 2.0 5 votes vote down vote up
/**
 * @return gets the current package name for an executing action.
 */
protected final String getPackageName() {
  /** Preconditions have been validated by {@link GenerateAction#isAvailable(DataContext)}. */
  final Project project = Preconditions.checkNotNull(CommonDataKeys.PROJECT.getData(dataContext));
  final IdeView view = Preconditions.checkNotNull(LangDataKeys.IDE_VIEW.getData(dataContext));
  final PsiDirectory directory = Preconditions.checkNotNull(view.getOrChooseDirectory());
  final PsiPackage psiPackage =
      Preconditions.checkNotNull(JavaDirectoryService.getInstance().getPackage(directory));

  return psiPackage.getQualifiedName();
}
 
Example 4
Source File: GenerateAction.java    From RIBs with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if a Java package exists for a directory.
 *
 * @param directory to check.
 * @return {@code true} when a package exists, {@code false} when it does not.
 */
private boolean checkPackageExists(PsiDirectory directory) {
  PsiPackage pkg = JavaDirectoryService.getInstance().getPackage(directory);
  if (pkg == null) {
    return false;
  }

  String name = pkg.getQualifiedName();
  return StringUtil.isEmpty(name)
      || PsiNameHelper.getInstance(directory.getProject()).isQualifiedName(name);
}
 
Example 5
Source File: BlazeAndroidModuleTemplate.java    From intellij with Apache License 2.0 5 votes vote down vote up
/**
 * The new component wizard uses {@link NamedModuleTemplate#getName()} for the default package
 * name of the new component. If we can figure it out from the target directory here, then we can
 * pass it to the new component wizard.
 */
private static String getPackageName(Project project, VirtualFile targetDirectory) {
  PsiDirectory psiDirectory = PsiManager.getInstance(project).findDirectory(targetDirectory);
  if (psiDirectory == null) {
    return null;
  }
  PsiPackage psiPackage = JavaDirectoryService.getInstance().getPackage(psiDirectory);
  if (psiPackage == null) {
    return null;
  }
  return psiPackage.getQualifiedName();
}