下面列出了怎么用com.intellij.psi.PsiTypeElement的API类实例代码及写法,或者点击链接到github查看源代码。
private boolean navigateToConstructorIfProvider(PsiParameter psiParameter) {
PsiTypeElement declaringTypeElement = psiParameter.getTypeElement();
PsiClass classElement = JavaPsiFacade.getInstance(psiParameter.getProject()).findClass(
declaringTypeElement.getType().getCanonicalText(),
declaringTypeElement.getResolveScope());
if (classElement == null) {
return false;
}
for (PsiMethod method : classElement.getConstructors()) {
if (PsiConsultantImpl.hasAnnotation(method, CLASS_INJECT) && navigateToElement(method)) {
return true;
}
}
return false;
}
/**
* @return a {@link com.intellij.codeInsight.daemon.GutterIconNavigationHandler} if the element
* is a PsiMethod annotated with @Provides.
*/
@Nullable @Override
public LineMarkerInfo getLineMarkerInfo(@NotNull final PsiElement element) {
// Check methods first (includes constructors).
if (element instanceof PsiMethod) {
PsiMethod methodElement = (PsiMethod) element;
// Does it have an @Provides?
if (hasAnnotation(element, CLASS_PROVIDES)) {
PsiTypeElement returnTypeElement = methodElement.getReturnTypeElement();
if (returnTypeElement != null) {
return new LineMarkerInfo<PsiElement>(element, returnTypeElement.getTextRange(), ICON,
UPDATE_ALL, null, new ProvidesToInjectHandler(), LEFT);
}
}
// Is it an @Inject-able constructor?
if (methodElement.isConstructor() && hasAnnotation(element, CLASS_INJECT)) {
return new LineMarkerInfo<PsiElement>(element, element.getTextRange(), ICON,
UPDATE_ALL, null, new ConstructorInjectToInjectionPlaceHandler(), LEFT);
}
}
return null;
}
private void maybeAddSubscriberMethod(PsiMethod element) {
PsiTypeElement methodParameter = OttoLineMarkerProvider.getMethodParameter(element);
if (methodParameter != null) {
String canonicalText = methodParameter.getType().getCanonicalText();
PsiFile containingFile = methodParameter.getContainingFile();
if (containingFile != null) {
VirtualFile virtualFile = containingFile.getVirtualFile();
if (virtualFile != null) {
synchronized (fileToEventClasses) {
Set<String> eventClasses = getEventClasses(virtualFile);
eventClasses.add(canonicalText);
}
}
}
}
}
@Override public void navigate(final MouseEvent mouseEvent, final PsiElement psiElement) {
PsiMethod subscribeMethod = (PsiMethod) psiElement;
final PsiTypeElement parameterTypeElement = getMethodParameter(subscribeMethod);
final SubscriberMetadata subscriberMetadata = SubscriberMetadata.getSubscriberMetadata(subscribeMethod);
if ((parameterTypeElement.getType() instanceof PsiClassType) && (subscriberMetadata != null)) {
final PsiClass eventClass = ((PsiClassType) parameterTypeElement.getType()).resolve();
PickAction.startPicker(subscriberMetadata.displayedTypesOnSubscriberMethods(),
new RelativePoint(mouseEvent), new PickAction.Callback() {
@Override public void onTypeChose(PickAction.Type type) {
if (type.equals(PickAction.Type.PRODUCER)) {
new ShowUsagesAction(PRODUCERS).startFindUsages(eventClass,
new RelativePoint(mouseEvent), PsiUtilBase.findEditor(psiElement),
MAX_USAGES);
} else if (type.equals(PickAction.Type.EVENT_POST)) {
PsiMethod ottoBusMethod = subscriberMetadata.getBusPostMethod(psiElement.getProject());
new ShowUsagesAction(new BusPostDecider(eventClass)).startFindUsages(
ottoBusMethod, new RelativePoint(mouseEvent),
PsiUtilBase.findEditor(psiElement), MAX_USAGES);
}
}
});
}
}
private static CompletionProvider<CompletionParameters> typeCompletionProvider() {
return new CompletionProvider<CompletionParameters>() {
@Override
protected void addCompletions(
@NotNull CompletionParameters completionParameters,
ProcessingContext processingContext,
@NotNull CompletionResultSet completionResultSet) {
PsiElement element = completionParameters.getPosition();
// Method parameter type in the Spec class
// PsiIdentifier -> PsiJavaCodeReferenceElement -> PsiTypeElement -> PsiMethod -> PsiClass
PsiElement typeElement = PsiTreeUtil.getParentOfType(element, PsiTypeElement.class);
if (typeElement == null) {
return;
}
PsiMethod containingMethod = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
if (containingMethod == null) {
return;
}
PsiClass cls = containingMethod.getContainingClass();
if (!LithoPluginUtils.isLithoSpec(cls)) {
return;
}
// @Prop or @State annotation
PsiModifierList parameterModifiers =
PsiTreeUtil.getPrevSiblingOfType(typeElement, PsiModifierList.class);
if (parameterModifiers == null) {
return;
}
if (parameterModifiers.findAnnotation(Prop.class.getName()) != null) {
addCompletionResult(
completionResultSet, containingMethod, cls.getMethods(), LithoPluginUtils::isProp);
} else if (parameterModifiers.findAnnotation(State.class.getName()) != null) {
addCompletionResult(
completionResultSet, containingMethod, cls.getMethods(), LithoPluginUtils::isState);
}
}
};
}
public static PsiType getReturnPsiType(@Nullable PsiClass eventClass) {
return Optional.ofNullable(eventClass)
.map(cls -> AnnotationUtil.findAnnotation(eventClass, Event.class.getTypeName()))
.map(psiAnnotation -> psiAnnotation.findAttributeValue("returnType"))
.filter(PsiClassObjectAccessExpression.class::isInstance)
.map(PsiClassObjectAccessExpression.class::cast)
.map(PsiClassObjectAccessExpression::getOperand)
.map(PsiTypeElement::getType)
.orElse(PsiType.VOID);
}
private void visitPsiDeclarationStatement(final PsiDeclarationStatement
declarationStatement) {
Collection<PsiTypeElement> typeElements =
PsiTreeUtil.findChildrenOfType(declarationStatement, PsiTypeElement.class);
for (PsiTypeElement element : typeElements) {
String type = removeSpecialSymbols(element.getType().getCanonicalText());
addInMap(type, emptySet);
}
}
@Nullable
@Override
protected PsiType inferType(@NotNull PsiTypeElement typeElement) {
if (!valProcessor.isEnabled(typeElement.getProject())) {
return null;
}
return valProcessor.inferType(typeElement);
}
@Override public boolean shouldShow(Usage usage) {
PsiElement element = ((UsageInfo2UsageAdapter) usage).getElement();
if (element instanceof PsiJavaCodeReferenceElement) {
if ((element = element.getContext()) instanceof PsiTypeElement) {
if ((element = element.getContext()) instanceof PsiParameter) {
if ((element = element.getContext()) instanceof PsiParameterList) {
if ((element = element.getContext()) instanceof PsiMethod) {
return SubscriberMetadata.isAnnotatedWithSubscriber((PsiMethod) element);
}
}
}
}
}
return false;
}
@Override public void navigate(MouseEvent mouseEvent, PsiElement psiElement) {
if (psiElement instanceof PsiMethod) {
PsiMethod psiMethod = (PsiMethod) psiElement;
PsiTypeElement returnTypeElement = psiMethod.getReturnTypeElement();
PsiClass eventClass = ((PsiClassType) returnTypeElement.getType()).resolve();
new ShowUsagesAction(SUBSCRIBERS).startFindUsages(eventClass,
new RelativePoint(mouseEvent), PsiUtilBase.findEditor(returnTypeElement),
MAX_USAGES);
}
}
public static @Nullable PsiTypeElement getMethodParameter(PsiMethod subscribeMethod) {
PsiParameterList parameterList = subscribeMethod.getParameterList();
if (parameterList.getParametersCount() != 1) {
return null;
} else {
PsiParameter subscribeMethodParam = parameterList.getParameters()[0];
return subscribeMethodParam.getTypeElement();
}
}
@Override
public void visitTypeElement(PsiTypeElement type) {
super.visitTypeElement(type);
}
@NotNull
NavigationMarker build() {
final PsiTypeElement typeElement = source instanceof PsiField ? ((PsiField) source).getTypeElement() : null;
final TextRange textRange = typeElement != null ? typeElement.getTextRange() : source.getTextRange();
return new NavigationMarker(source, destination, textRange);
}