Java Code Examples for com.intellij.psi.PsiClass#getContainingFile()

The following examples show how to use com.intellij.psi.PsiClass#getContainingFile() . 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: TypeCheckRefactoringType.java    From IntelliJDeodorant with MIT License 6 votes vote down vote up
public AbstractTypeCheckRefactoring(TypeCheckElimination typeCheckElimination) {
    PsiClass sourceTypeDeclaration = typeCheckElimination.getTypeCheckClass();
    PsiFile sourceFile = sourceTypeDeclaration.getContainingFile();
    QuadriFunction<PsiFile, Project, PsiClass, TypeCheckElimination, PolymorphismRefactoring> constructor;
    if (typeCheckElimination.getExistingInheritanceTree() == null) {
        constructor = ReplaceTypeCodeWithStateStrategy::new;
    } else {
        constructor = ReplaceConditionalWithPolymorphism::new;
    }

    refactoring = constructor.apply(
            sourceFile,
            scope.getProject(),
            sourceTypeDeclaration,
            typeCheckElimination
    );
}
 
Example 2
Source File: CodeGeneratorAction.java    From code-generator with Apache License 2.0 6 votes vote down vote up
private Entity buildClassEntity(PsiClass psiClass) {
    PsiFile psiFile = psiClass.getContainingFile();
    String className = psiClass.getName();
    String packageName = ((PsiClassOwner) psiFile).getPackageName();

    List<Field> fields = Arrays.stream(psiClass.getAllFields()).map(field -> {
        String fieldType = field.getType().getPresentableText();
        String fieldName = field.getName();
        return new Field(fieldType, fieldName);
    }).collect(Collectors.toList());

    return Entity.builder()
        .name(className)
        .packageName(packageName)
        .fields(fields).build();
}
 
Example 3
Source File: ConfigDiscovery.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Nullable
private String calculateCanonicalPath(@NotNull PsiClass psiClass) {
  String canonicalPath = null;
  final PsiFile psiFile;
  if (psiClass instanceof LombokLightClassBuilder) {
    // Use containing class for all LombokLightClasses
    final PsiClass containingClass = psiClass.getContainingClass();
    if (null != containingClass) {
      psiFile = containingClass.getContainingFile();
    } else {
      psiFile = null;
    }
  } else {
    psiFile = psiClass.getContainingFile();
  }

  if (null != psiFile) {
    canonicalPath = getDirectoryCanonicalPath(psiFile);
    if (null == canonicalPath) {
      canonicalPath = getDirectoryCanonicalPath(psiFile.getOriginalFile());
    }
  }
  return PathUtil.toSystemIndependentName(canonicalPath);
}
 
Example 4
Source File: TypeStateCheckingTest.java    From IntelliJDeodorant with MIT License 5 votes vote down vote up
private static PolymorphismRefactoring createRefactoring(TypeCheckElimination typeCheckElimination,
                                                         Project project) {
    PsiClass sourceTypeDeclaration = typeCheckElimination.getTypeCheckClass();
    PsiFile sourceFile = sourceTypeDeclaration.getContainingFile();
    QuadriFunction<PsiFile, Project, PsiClass, TypeCheckElimination, PolymorphismRefactoring> constructor;
    if (typeCheckElimination.getExistingInheritanceTree() == null) {
        constructor = ReplaceTypeCodeWithStateStrategy::new;
    } else {
        constructor = ReplaceConditionalWithPolymorphism::new;
    }

    return constructor.apply(sourceFile, project, sourceTypeDeclaration, typeCheckElimination);
}
 
Example 5
Source File: JavaSyncStatusContributor.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public PsiFileAndName toPsiFileAndName(BlazeProjectData projectData, ProjectViewNode<?> node) {
  if (!(node instanceof ClassTreeNode)) {
    return null;
  }
  PsiClass psiClass = ((ClassTreeNode) node).getPsiClass();
  if (psiClass == null) {
    return null;
  }
  PsiFile file = psiClass.getContainingFile();
  return file != null ? new PsiFileAndName(file, psiClass.getName()) : null;
}
 
Example 6
Source File: RunUtil.java    From intellij with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an instance of {@link java.io.File} related to the containing file of the given class.
 * It returns {@code null} if the given class is not contained in a file and only exists in
 * memory.
 */
@Nullable
public static File getFileForClass(PsiClass aClass) {
  PsiFile containingFile = aClass.getContainingFile();
  if (containingFile == null) {
    return null;
  }

  VirtualFile virtualFile = containingFile.getVirtualFile();
  if (virtualFile == null) {
    return null;
  }

  return new File(virtualFile.getPath());
}
 
Example 7
Source File: ScalaSyncStatusContributor.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public PsiFileAndName toPsiFileAndName(BlazeProjectData projectData, ProjectViewNode<?> node) {
  if (!(node instanceof ClassTreeNode)) {
    return null;
  }
  PsiClass psiClass = ((ClassTreeNode) node).getPsiClass();
  if (psiClass == null) {
    return null;
  }
  PsiFile file = psiClass.getContainingFile();
  return file != null ? new PsiFileAndName(file, psiClass.getName()) : null;
}
 
Example 8
Source File: PantsIntegrationTestCase.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
protected void modify(@NonNls @NotNull String qualifiedName) {
  final PsiClass psiClass = findClassAndAssert(qualifiedName);
  final PsiFile psiFile = psiClass.getContainingFile();
  final PsiParserFacade parserFacade = PsiParserFacade.SERVICE.getInstance(myProject);
  final PsiComment comment = parserFacade.createBlockCommentFromText(psiFile.getLanguage(), "Foo");
  WriteCommandAction.runWriteCommandAction(
    myProject,
    ((Runnable) () -> psiFile.add(comment))
  );
  FileDocumentManager manager = FileDocumentManager.getInstance();
  manager.saveAllDocuments();
}
 
Example 9
Source File: PositionUtils.java    From intellij-quarkus with Eclipse Public License 2.0 4 votes vote down vote up
public static Range toNameRange(PsiClass type, IPsiUtils utils) {
	PsiFile openable = type.getContainingFile();
	TextRange sourceRange = type.getNameIdentifier().getTextRange();
	return utils.toRange(openable, sourceRange.getStartOffset(), sourceRange.getLength());
}
 
Example 10
Source File: GenerateComponentActionTest.java    From litho with Apache License 2.0 4 votes vote down vote up
private static AnActionEvent createEvent(PsiClass content) {
  PsiFile containingFile = content.getContainingFile();
  AnActionEvent mock = mock(AnActionEvent.class);
  Mockito.when(mock.getData(CommonDataKeys.PSI_FILE)).thenReturn(containingFile);
  return mock;
}
 
Example 11
Source File: OnEventCreateFixTest.java    From litho with Apache License 2.0 4 votes vote down vote up
private static LightVirtualFile createVirtualFile(PsiClass cls) {
  final PsiFile psiFile = cls.getContainingFile();
  return new LightVirtualFile(psiFile.getName(), psiFile.getFileType(), psiFile.getText());
}
 
Example 12
Source File: PsiBasedClassFileFinder.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public VirtualFile findClassFile(String fqcn) {
  GlobalSearchScope searchScope = module.getModuleRuntimeScope(false);

  PsiClass[] psiClasses =
      ReadAction.compute(
          () ->
              JavaPsiFacade.getInstance(project)
                  .findClasses(getContainingClassName(fqcn), searchScope));
  if (psiClasses.length == 0) {
    return null;
  }

  BlazeProjectData projectData =
      BlazeProjectDataManager.getInstance(project).getBlazeProjectData();
  if (projectData == null) {
    return null;
  }

  // It's possible that there's more than one source file in the project corresponding to
  // the same fully-qualified class name, with Blaze choosing the appropriate source to use
  // according to some configuration flags. Here we check each of them until we find the one
  // that was chosen during Blaze sync.
  for (PsiClass psiClass : psiClasses) {
    PsiFile psiFile = psiClass.getContainingFile();
    if (psiFile == null) {
      continue;
    }

    VirtualFile virtualFile = psiFile.getVirtualFile();
    if (virtualFile == null) {
      continue;
    }

    FileType fileType = psiFile.getFileType();
    VirtualFile classFile = null;

    if (fileType == StdFileTypes.JAVA) {
      classFile =
          findClassFileForSourceAndRegisterResourcePackage(projectData, virtualFile, fqcn);
    } else if (fileType == StdFileTypes.CLASS) {
      classFile = findClassFileForIJarClass(projectData, virtualFile, fqcn);
    }

    if (classFile != null) {
      return classFile;
    }
  }

  return null;
}