本文整理汇总了Java中com.intellij.psi.util.ClassUtil.extractPackageName方法的典型用法代码示例。如果您正苦于以下问题:Java ClassUtil.extractPackageName方法的具体用法?Java ClassUtil.extractPackageName怎么用?Java ClassUtil.extractPackageName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.intellij.psi.util.ClassUtil
的用法示例。
在下文中一共展示了ClassUtil.extractPackageName方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: visitReferenceElement
import com.intellij.psi.util.ClassUtil; //导入方法依赖的package包/类
@Override
public void visitReferenceElement(
PsiJavaCodeReferenceElement reference) {
super.visitReferenceElement(reference);
if (reference.isQualified()) {
return;
}
final PsiElement element = reference.resolve();
if (!(element instanceof PsiClass)) {
return;
}
final PsiClass aClass = (PsiClass)element;
final String qualifiedName = aClass.getQualifiedName();
final String packageName =
ClassUtil.extractPackageName(qualifiedName);
if (!importedPackageName.equals(packageName)) {
return;
}
importedClasses.add(aClass);
}
示例2: hasDefaultImportConflict
import com.intellij.psi.util.ClassUtil; //导入方法依赖的package包/类
public static boolean hasDefaultImportConflict(String fqName, PsiJavaFile file) {
final String shortName = ClassUtil.extractClassName(fqName);
final String packageName = ClassUtil.extractPackageName(fqName);
final String filePackageName = file.getPackageName();
if (filePackageName.equals(packageName)) {
return false;
}
final Project project = file.getProject();
final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
final PsiJavaPackage filePackage = psiFacade.findPackage(filePackageName);
if (filePackage == null) {
return false;
}
final PsiClass[] classes = filePackage.getClasses();
for (PsiClass aClass : classes) {
final String className = aClass.getName();
if (shortName.equals(className)) {
return true;
}
}
return false;
}
示例3: hasJavaLangImportConflict
import com.intellij.psi.util.ClassUtil; //导入方法依赖的package包/类
public static boolean hasJavaLangImportConflict(String fqName, PsiJavaFile file) {
final String shortName = ClassUtil.extractClassName(fqName);
@NonNls final String packageName = ClassUtil.extractPackageName(fqName);
if ("java.lang".equals(packageName)) {
return false;
}
final Project project = file.getProject();
final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
final PsiJavaPackage javaLangPackage = psiFacade.findPackage("java.lang");
if (javaLangPackage == null) {
return false;
}
final PsiClass[] classes = javaLangPackage.getClasses();
for (final PsiClass aClass : classes) {
final String className = aClass.getName();
if (shortName.equals(className)) {
return true;
}
}
return false;
}
示例4: hasJavaLangImportConflict
import com.intellij.psi.util.ClassUtil; //导入方法依赖的package包/类
public static boolean hasJavaLangImportConflict(String fqName, PsiJavaFile file) {
final String shortName = ClassUtil.extractClassName(fqName);
final String packageName = ClassUtil.extractPackageName(fqName);
if (HardcodedMethodConstants.JAVA_LANG.equals(packageName)) {
return false;
}
final Project project = file.getProject();
final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
final PsiJavaPackage javaLangPackage = psiFacade.findPackage(HardcodedMethodConstants.JAVA_LANG);
if (javaLangPackage == null) {
return false;
}
final PsiClass[] classes = javaLangPackage.getClasses();
for (final PsiClass aClass : classes) {
final String className = aClass.getName();
if (shortName.equals(className)) {
return true;
}
}
return false;
}
示例5: configure
import com.intellij.psi.util.ClassUtil; //导入方法依赖的package包/类
@Override
public void configure(JUnitConfiguration jUnitConfiguration, PitestConfiguration configuration) {
JUnitConfiguration.Data data = jUnitConfiguration.getPersistentData();
String targetTests = data.getMainClassName();
String targetClasses = null;
if (StringUtils.endsWith(targetTests, "Test")) {
targetClasses = targetTests.substring(0, targetTests.lastIndexOf("Test"));
}
if (targetClasses == null || FileUtil.findFirstThatExist(targetClasses) == null) {
targetClasses = ClassUtil.extractPackageName(targetTests) + ANY_IN_PACKAGE_PATTERN;
}
configure(configuration, targetTests, targetClasses);
}
示例6: hasOnDemandImportConflict
import com.intellij.psi.util.ClassUtil; //导入方法依赖的package包/类
/**
* @param strict if strict is true this method checks if the conflicting
* class which is imported is actually used in the file. If it isn't the
* on demand import can be overridden with an exact import for the fqName
* without breaking stuff.
*/
private static boolean hasOnDemandImportConflict(@NotNull String fqName, @NotNull PsiJavaFile file, boolean strict) {
final PsiImportList imports = file.getImportList();
if (imports == null) {
return false;
}
final PsiImportStatement[] importStatements = imports.getImportStatements();
final String shortName = ClassUtil.extractClassName(fqName);
final String packageName = ClassUtil.extractPackageName(fqName);
for (final PsiImportStatement importStatement : importStatements) {
if (!importStatement.isOnDemand()) {
continue;
}
final PsiJavaCodeReferenceElement importReference = importStatement.getImportReference();
if (importReference == null) {
continue;
}
final String packageText = importReference.getText();
if (packageText.equals(packageName)) {
continue;
}
final PsiElement element = importReference.resolve();
if (element == null || !(element instanceof PsiPackage)) {
continue;
}
final PsiPackage aPackage = (PsiPackage)element;
final PsiClass[] classes = aPackage.findClassByShortName(shortName, file.getResolveScope());
for (final PsiClass aClass : classes) {
if (!strict) {
return true;
}
final String qualifiedClassName = aClass.getQualifiedName();
if (qualifiedClassName == null || fqName.equals(qualifiedClassName)) {
continue;
}
return containsConflictingReference(file, qualifiedClassName);
}
}
return hasJavaLangImportConflict(fqName, file);
}
示例7: hasDefaultImportConflict
import com.intellij.psi.util.ClassUtil; //导入方法依赖的package包/类
public static boolean hasDefaultImportConflict(String fqName, PsiJavaFile file) {
final String shortName = ClassUtil.extractClassName(fqName);
final String packageName = ClassUtil.extractPackageName(fqName);
final String filePackageName = file.getPackageName();
if (filePackageName.equals(packageName)) {
return false;
}
final Project project = file.getProject();
final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
final PsiPackage filePackage = psiFacade.findPackage(filePackageName);
if (filePackage == null) {
return false;
}
return filePackage.containsClassNamed(shortName);
}
示例8: hasJavaLangImportConflict
import com.intellij.psi.util.ClassUtil; //导入方法依赖的package包/类
public static boolean hasJavaLangImportConflict(String fqName, PsiJavaFile file) {
final String shortName = ClassUtil.extractClassName(fqName);
final String packageName = ClassUtil.extractPackageName(fqName);
if (HardcodedMethodConstants.JAVA_LANG.equals(packageName)) {
return false;
}
final Project project = file.getProject();
final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
final PsiPackage javaLangPackage = psiFacade.findPackage(HardcodedMethodConstants.JAVA_LANG);
if (javaLangPackage == null) {
return false;
}
return javaLangPackage.containsClassNamed(shortName);
}
示例9: getNamespace
import com.intellij.psi.util.ClassUtil; //导入方法依赖的package包/类
public String getNamespace()
{
return ClassUtil.extractPackageName( _fqn );
}
示例10: actionPerformed
import com.intellij.psi.util.ClassUtil; //导入方法依赖的package包/类
public void actionPerformed(AnActionEvent event) {
final Project project = ActionEventUtils.getTargetProject(event);
final PsiFile selectedFile = ActionEventUtils.getTargetFile(event);
final PsiClass selectedClass = PsiUtils.getSelectedClass(event)
.orElseThrow(() -> new IllegalStateException("Cannot perform action without a selected class"));
// Note that module may be null if the selected class is in a library rather than the source tree
final Module module = event.getData(LangDataKeys.MODULE);
final ConfigurationProperties configurationProperties = new ConfigurationProperties(project);
// Guess what we are going to call the target matcher class and what its package will be
String preferredClassName = configurationProperties.getMatcherClassNamePrefix() +
selectedClass.getName() +
configurationProperties.getMatcherClassNameSuffix();
String preferredPackageName = ClassUtil.extractPackageName(selectedClass.getQualifiedName());
// Get the list of available source roots
List<VirtualFile> candidateSourceRoots = SourceRootUtils.getSourceAndTestSourceRoots(project);
if (candidateSourceRoots.isEmpty()) {
Messages.showErrorDialog(project, "No source roots have been configured. A source root is required as the target location for the generated class.", "No Source Root");
return;
}
// Guess what the target source root might be
VirtualFile preferredSourceRoot = SourceRootUtils.getPreferredSourceRootForTestClass(project, module, selectedFile.getVirtualFile())
.orElseThrow(IllegalStateException::new);
final MatcherGeneratorProperties generatorProperties = new MatcherGeneratorProperties()
.setProject(project)
.setClassName(preferredClassName)
.setPackageName(preferredPackageName)
.setSourceRoot(preferredSourceRoot)
.setSourceClass(selectedClass)
.setFactoryMethodSuffix(configurationProperties.getFactoryMethodSuffix())
.setTemplateFactoryMethodSuffix(configurationProperties.getTemplateFactoryMethodSuffix())
.setSetterPrefix(configurationProperties.getSetterPrefix())
.setSetterSuffix(configurationProperties.getSetterSuffix())
.setExtensible(configurationProperties.isMakeExtensible())
.setBaseClassName(configurationProperties.getBaseClass())
.setGenerateTemplateFactoryMethod(configurationProperties.isGenerateTemplateFactoryMethod())
.setMakeMethodParametersFinal(configurationProperties.isMakeMethodParametersFinal())
.setUseReflectingPropertyMatcher(configurationProperties.isUseReflectingPropertyMatcher());
MatcherGeneratorOptionsDialog matcherGeneratorOptionsDialog = new MatcherGeneratorOptionsDialog(
project, selectedClass, candidateSourceRoots, generatorProperties);
matcherGeneratorOptionsDialog.show();
if (matcherGeneratorOptionsDialog.isOK()) {
new MatcherGenerator(generatorProperties).generate();
}
}
示例11: addImportIfNeeded
import com.intellij.psi.util.ClassUtil; //导入方法依赖的package包/类
public static void addImportIfNeeded(@NotNull PsiClass aClass, @NotNull PsiElement context) {
final PsiFile file = context.getContainingFile();
if (!(file instanceof PsiJavaFile)) {
return;
}
final PsiJavaFile javaFile = (PsiJavaFile)file;
final PsiClass outerClass = aClass.getContainingClass();
if (outerClass == null) {
if (PsiTreeUtil.isAncestor(javaFile, aClass, true)) {
return;
}
}
else {
if (PsiTreeUtil.isAncestor(outerClass, context, true) &&
!PsiTreeUtil.isAncestor(outerClass.getModifierList(), context, true)) {
return;
}
}
final String qualifiedName = aClass.getQualifiedName();
if (qualifiedName == null) {
return;
}
final PsiImportList importList = javaFile.getImportList();
if (importList == null) {
return;
}
final String containingPackageName = javaFile.getPackageName();
@NonNls final String packageName = ClassUtil.extractPackageName(qualifiedName);
if (containingPackageName.equals(packageName) || importList.findSingleClassImportStatement(qualifiedName) != null) {
return;
}
if (importList.findOnDemandImportStatement(packageName) != null &&
!hasDefaultImportConflict(qualifiedName, javaFile) && !hasOnDemandImportConflict(qualifiedName, javaFile)) {
return;
}
final Project project = importList.getProject();
final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
final PsiElementFactory elementFactory = psiFacade.getElementFactory();
final PsiImportStatement importStatement = elementFactory.createImportStatement(aClass);
importList.add(importStatement);
}
示例12: hasOnDemandImportConflict
import com.intellij.psi.util.ClassUtil; //导入方法依赖的package包/类
/**
* @param strict if strict is true this method checks if the conflicting
* class which is imported is actually used in the file. If it isn't the
* on demand import can be overridden with an exact import for the fqName
* without breaking stuff.
*/
private static boolean hasOnDemandImportConflict(@NotNull String fqName, @NotNull PsiJavaFile file, boolean strict) {
final PsiImportList imports = file.getImportList();
if (imports == null) {
return false;
}
final PsiImportStatement[] importStatements = imports.getImportStatements();
final String shortName = ClassUtil.extractClassName(fqName);
final String packageName = ClassUtil.extractPackageName(fqName);
for (final PsiImportStatement importStatement : importStatements) {
if (!importStatement.isOnDemand()) {
continue;
}
final PsiJavaCodeReferenceElement importReference = importStatement.getImportReference();
if (importReference == null) {
continue;
}
final String packageText = importReference.getText();
if (packageText.equals(packageName)) {
continue;
}
final PsiElement element = importReference.resolve();
if (element == null || !(element instanceof PsiPackage)) {
continue;
}
final PsiJavaPackage aPackage = (PsiJavaPackage)element;
final PsiClass[] classes = aPackage.getClasses();
for (final PsiClass aClass : classes) {
final String className = aClass.getName();
if (!shortName.equals(className)) {
continue;
}
if (!strict) {
return true;
}
final String qualifiedClassName = aClass.getQualifiedName();
if (qualifiedClassName == null || fqName.equals(qualifiedClassName)) {
continue;
}
return containsReferenceToConflictingClass(file, qualifiedClassName);
}
}
return hasJavaLangImportConflict(fqName, file);
}
示例13: hasOnDemandImportConflict
import com.intellij.psi.util.ClassUtil; //导入方法依赖的package包/类
/**
* @param strict if strict is true this method checks if the conflicting
* class which is imported is actually used in the file. If it isn't the
* on demand import can be overridden with an exact import for the fqName
* without breaking stuff.
*/
private static boolean hasOnDemandImportConflict(@NotNull String fqName, @NotNull PsiJavaFile file, boolean strict) {
final PsiImportList imports = file.getImportList();
if (imports == null) {
return false;
}
final PsiImportStatement[] importStatements = imports.getImportStatements();
final String shortName = ClassUtil.extractClassName(fqName);
final String packageName = ClassUtil.extractPackageName(fqName);
for (final PsiImportStatement importStatement : importStatements) {
if (!importStatement.isOnDemand()) {
continue;
}
final PsiJavaCodeReferenceElement importReference = importStatement.getImportReference();
if (importReference == null) {
continue;
}
final String packageText = importReference.getText();
if (packageText.equals(packageName)) {
continue;
}
final PsiElement element = importReference.resolve();
if (element == null || !(element instanceof PsiJavaPackage)) {
continue;
}
final PsiJavaPackage aPackage = (PsiJavaPackage)element;
final PsiClass[] classes = aPackage.getClasses();
for (final PsiClass aClass : classes) {
final String className = aClass.getName();
if (!shortName.equals(className)) {
continue;
}
if (!strict) {
return true;
}
final String qualifiedClassName = aClass.getQualifiedName();
if (qualifiedClassName == null || fqName.equals(qualifiedClassName)) {
continue;
}
return containsConflictingReference(file, qualifiedClassName);
}
}
return hasJavaLangImportConflict(fqName, file);
}