下面列出了怎么用com.intellij.psi.PsiImportStatement的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public PsiElement[] getGotoDeclarationTargets(
@Nullable PsiElement sourceElement, int offset, Editor editor) {
// Exclusions
if (sourceElement == null
|| PsiTreeUtil.getParentOfType(sourceElement, PsiImportStatement.class) != null) {
return PsiElement.EMPTY_ARRAY;
}
final Project project = sourceElement.getProject();
return BaseLithoComponentsDeclarationHandler.resolve(sourceElement)
// Filter Methods
.filter(PsiMethod.class::isInstance)
.map(PsiMethod.class::cast)
.map(method -> findSpecMethods(method, project))
.findFirst()
.map(
result -> {
final Map<String, String> data = new HashMap<>();
data.put(EventLogger.KEY_TARGET, "method");
data.put(EventLogger.KEY_TYPE, EventLogger.VALUE_NAVIGATION_TYPE_GOTO);
LOGGER.log(EventLogger.EVENT_NAVIGATION, data);
return result;
})
.orElse(PsiMethod.EMPTY_ARRAY);
}
private Map<String, Set<String>> getFullyQualifiedImportsWithMethods(
final PsiJavaFile javaFile, final Map<String, Set<String>> importVsMethods) {
Map<String, Set<String>> fullyQualifiedImportsWithMethods = new HashMap<>();
PsiImportList importList = javaFile.getImportList();
Collection<PsiImportStatement> importStatements =
PsiTreeUtil.findChildrenOfType(importList, PsiImportStatement.class);
for (PsiImportStatement importStatement : importStatements) {
if (!importStatement.isOnDemand()) {
String qualifiedName = importStatement.getQualifiedName();
if (importVsMethods.containsKey(qualifiedName)) {
fullyQualifiedImportsWithMethods.put(qualifiedName,
importVsMethods.get(qualifiedName));
}
}
}
return fullyQualifiedImportsWithMethods;
}
public final boolean value( PsiFile psiFile )
{
final boolean[] hasPolygeneImportPackage = new boolean[]{ false };
psiFile.accept( new JavaElementVisitor()
{
@Override
public final void visitImportStatement( PsiImportStatement statement )
{
String packageName = statement.getQualifiedName();
if( packageName != null && packageName.startsWith( "org.apache.polygene" ) )
{
hasPolygeneImportPackage[ 0 ] = true;
}
}
@Override
public void visitReferenceExpression( PsiReferenceExpression expression )
{
// Ignore
}
} );
return hasPolygeneImportPackage[ 0 ];
}
/**
* @param sourceElement component to find declaration for
* @param isComponentClass filters component type
* @param hasComponentSpecAnnotation filters resolved componentSpec type
* @param tag event tag for logging
* @return declaration target for the provided sourceElement or null if it wasn't found
*/
@Nullable
static PsiElement getGotoDeclarationTarget(
@Nullable PsiElement sourceElement,
Predicate<PsiClass> isComponentClass,
Predicate<PsiClass> hasComponentSpecAnnotation,
String tag) {
// Exclusions
if (sourceElement == null
|| PsiTreeUtil.getParentOfType(sourceElement, PsiImportStatement.class) != null) {
return null;
}
final Project project = sourceElement.getProject();
return resolve(sourceElement)
// Filter Component classes
.filter(PsiClass.class::isInstance)
.map(PsiClass.class::cast)
.filter(isComponentClass)
// Find Spec classes by name
.map(PsiClass::getQualifiedName)
.filter(Objects::nonNull)
.map(LithoPluginUtils::getLithoComponentSpecNameFromComponent)
.map(specName -> PsiSearchUtils.findOriginalClass(project, specName))
.filter(Objects::nonNull)
// Filter Spec classes by implementation
.filter(hasComponentSpecAnnotation)
.limit(1)
.peek(
psiClass -> {
final Map<String, String> data = new HashMap<>();
data.put(EventLogger.KEY_TARGET, "class");
data.put(EventLogger.KEY_CLASS, tag);
data.put(EventLogger.KEY_TYPE, EventLogger.VALUE_NAVIGATION_TYPE_GOTO);
logger.log(EventLogger.EVENT_NAVIGATION, data);
})
.findFirst()
.orElse(null);
}