下面列出了com.intellij.psi.PsiElement#getClass ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@NotNull
@Override
public String getDescriptiveName(@NotNull PsiElement element) {
String elementString = null;
if (element instanceof PsiNamedElement) {
elementString = ((PsiNamedElement) element).getName();
} else {
Class<?> elementClass = element.getClass();
System.out.println("[getDescriptiveName] elementClass="+elementClass);
}
if (elementString == null) {
elementString = "";
}
return elementString;
}
@NotNull
@Override
public String getNodeText(@NotNull PsiElement element, boolean useFullName) {
String elementString = null;
if (element instanceof PsiNamedElement) {
elementString = ((PsiNamedElement) element).getName();
} else {
Class<?> elementClass = element.getClass();
System.out.println("[getNodeText] elementClass="+elementClass);
}
if (elementString == null) {
elementString = "";
}
return elementString;
}
@Nonnull
List<ProviderBinding.ProviderInfo<PsiReferenceProvider,ProcessingContext>> getPairsByElement(@Nonnull PsiElement element,
@Nonnull PsiReferenceService.Hints hints) {
final Class<? extends PsiElement> clazz = element.getClass();
List<ProviderBinding.ProviderInfo<PsiReferenceProvider, ProcessingContext>> ret = null;
for (Class aClass : myKnownSupers.get(clazz)) {
SimpleProviderBinding<PsiReferenceProvider> simpleBinding = myBindingsMap.get(aClass);
NamedObjectProviderBinding<PsiReferenceProvider> namedBinding = myNamedBindingsMap.get(aClass);
if (simpleBinding == null && namedBinding == null) continue;
if (ret == null) ret = new SmartList<ProviderBinding.ProviderInfo<PsiReferenceProvider, ProcessingContext>>();
if (simpleBinding != null) {
simpleBinding.addAcceptableReferenceProviders(element, ret, hints);
}
if (namedBinding != null) {
namedBinding.addAcceptableReferenceProviders(element, ret, hints);
}
}
return ret == null ? Collections.<ProviderBinding.ProviderInfo<PsiReferenceProvider, ProcessingContext>>emptyList() : ret;
}
private static PsiElement2UsageTargetAdapter convertToUsageTarget(@NotNull PsiElement elementToSearch, @NotNull FindUsagesOptions findUsagesOptions) {
if(elementToSearch instanceof NavigationItem) {
return new PsiElement2UsageTargetAdapter(elementToSearch, findUsagesOptions);
} else {
throw new IllegalArgumentException("Wrong usage target:" + elementToSearch + "; " + elementToSearch.getClass());
}
}
public static TextRange getTextRange(PsiElement element, String str) {
Class elementClass = element.getClass();
try {
Method method = elementClass.getMethod("getValueRange");
Object obj = method.invoke(element);
return (TextRange) obj;
} catch (Exception e) {
return null;
}
}
@SuppressWarnings({"NonConstantStringShouldBeStringBuffer", "StringConcatenationInLoop"})
private ASTNode notBoundInExistingAst(@Nonnull PsiFileImpl file, @Nonnull FileElement treeElement) {
String message = "file=" + file + "; tree=" + treeElement;
PsiElement each = this;
while (each != null) {
message += "\n each of class " + each.getClass() + "; valid=" + each.isValid();
if (each instanceof StubBasedPsiElementBase) {
message += "; ref=" + ((StubBasedPsiElementBase<?>)each).mySubstrateRef;
each = ((StubBasedPsiElementBase<?>)each).getParentByStub();
}
else {
if (each instanceof PsiFile) {
message += "; same file=" + (each == file) + "; current tree= " + file.getTreeElement() + "; stubTree=" + file.getStubTree() + "; physical=" + file.isPhysical();
}
break;
}
}
StubElement<?> eachStub = getStub();
while (eachStub != null) {
message += "\n each stub " + (eachStub instanceof PsiFileStubImpl ? ((PsiFileStubImpl<?>)eachStub).getDiagnostics() : eachStub);
eachStub = eachStub.getParentStub();
}
if (ourTraceStubAstBinding) {
message += dumpCreationTraces(treeElement);
}
throw new AssertionError(message);
}
/**
* @return element base children merged with children provided by extensions
*/
@Nonnull
public static List<StructureViewTreeElement> mergeWithExtensions(@Nonnull PsiElement element, @Nonnull Collection<StructureViewTreeElement> baseChildren, boolean withCustomRegions) {
List<StructureViewTreeElement> result = new ArrayList<>(withCustomRegions ? CustomRegionStructureUtil.groupByCustomRegions(element, baseChildren) : baseChildren);
StructureViewFactoryEx structureViewFactory = StructureViewFactoryEx.getInstanceEx(element.getProject());
Class<? extends PsiElement> aClass = element.getClass();
for (StructureViewExtension extension : structureViewFactory.getAllExtensions(aClass)) {
StructureViewTreeElement[] children = extension.getChildren(element);
if (children != null) {
ContainerUtil.addAll(result, children);
}
extension.filterChildren(result, children == null || children.length == 0 ? Collections.emptyList() : Arrays.asList(children));
}
return result;
}
private boolean matchPattern(@Nullable final PsiElement pattern,
@Nullable final PsiElement candidate,
@Nonnull final SimpleMatch match) {
ProgressManager.checkCanceled();
if (pattern == null || candidate == null) return pattern == candidate;
final PsiElement[] children1 = PsiEquivalenceUtil.getFilteredChildren(pattern, null, true);
final PsiElement[] children2 = PsiEquivalenceUtil.getFilteredChildren(candidate, null, true);
final PsiElement patternParent = pattern.getParent();
final PsiElement candidateParent = candidate.getParent();
if (patternParent == null || candidateParent == null) return false;
if (pattern.getUserData(PARAMETER) != null && patternParent.getClass() == candidateParent.getClass()) {
match.changeParameter(pattern.getText(), candidate.getText());
return true;
}
if (children1.length != children2.length) return false;
for (int i = 0; i < children1.length; i++) {
final PsiElement child1 = children1[i];
final PsiElement child2 = children2[i];
if (!matchPattern(child1, child2, match)) return false;
}
if (children1.length == 0) {
if (pattern.getUserData(PARAMETER) != null && patternParent.getClass() == candidateParent.getClass()) {
match.changeParameter(pattern.getText(), candidate.getText());
return true;
}
if (myOutputVariables.contains(pattern.getText())) {
match.changeOutput(candidate.getText());
return true;
}
if (!pattern.textMatches(candidate)) {
return false;
}
}
return true;
}
public static boolean shouldSkip(PsiElement element, PsiElement elementToMatchWith) {
if (element == null || elementToMatchWith == null) {
return false;
}
if (element.getClass() == elementToMatchWith.getClass()) {
return false;
}
if (element.getFirstChild() == null && element.getTextLength() == 0 && !(element instanceof LeafElement)) {
return true;
}
return false;
}
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull final ProcessingContext context) {
project = element.getProject();
String elname = element.getClass().getName();
properties = PropertiesComponent.getInstance(project);
VirtualFile baseDir = project.getBaseDir();
projectPath = baseDir.getCanonicalPath();
if (elname.endsWith("StringLiteralExpressionImpl")) {
try {
PsiFile file = element.getContainingFile();
VirtualFile vfile = file.getVirtualFile();
if (vfile != null) {
String path = vfile.getPath();
String basePath = project.getBasePath();
if (basePath != null) {
String themeName = properties.getValue("themeName");
Class elementClass = element.getClass();
String protectedPath = YiiRefsHelper.getCurrentProtected(path);
path = path.replace(projectPath, "");
String viewPathTheme = YiiRefsHelper.getRenderViewPath(path, themeName);
String viewPath = YiiRefsHelper.getRenderViewPath(path, null);
protectedPath = protectedPath.replace(projectPath, "")
.replaceAll("/controllers/[a-zA-Z0-9_]+?.(php|tpl)+", "");
Method method = elementClass.getMethod("getValueRange");
Object obj = method.invoke(element);
TextRange textRange = (TextRange) obj;
Class _PhpPsiElement = elementClass.getSuperclass().getSuperclass().getSuperclass();
Method phpPsiElementGetText = _PhpPsiElement.getMethod("getText");
Object obj2 = phpPsiElementGetText.invoke(element);
String str = obj2.toString();
String uri = str.substring(textRange.getStartOffset(), textRange.getEndOffset());
int start = textRange.getStartOffset();
int len = textRange.getLength();
String controllerName = YiiRefsHelper.getControllerClassName(path);
if (controllerName != null) {
if (baseDir != null) {
String inThemeFullPath = viewPathTheme + controllerName + "/" + uri
+ (uri.endsWith(".tpl") ? "" : ".php");
if (baseDir.findFileByRelativePath(inThemeFullPath) != null) {
viewPath = viewPathTheme;
}
VirtualFile appDir = baseDir.findFileByRelativePath(viewPath);
VirtualFile protectedPathDir = (!protectedPath.equals("")) ?
baseDir.findFileByRelativePath(protectedPath) : null;
if (appDir != null) {
PsiReference ref = new ViewsReference(controllerName, uri, element,
new TextRange(start, start + len), project, protectedPathDir, appDir);
return new PsiReference[]{ref};
}
}
return PsiReference.EMPTY_ARRAY;
}
}
}
} catch (Exception e) {
System.err.println("error" + e.getMessage());
}
}
return PsiReference.EMPTY_ARRAY;
}