下面列出了怎么用com.intellij.psi.PsiClass的API类实例代码及写法,或者点击链接到github查看源代码。
private static Map<String, PsiClass> addToCache(
Collection<String> allRedSymbols, Project project, GlobalSearchScope symbolsScope) {
Map<String, PsiClass> redSymbolToClass = new HashMap<>();
ComponentsCacheService componentsCache = ComponentsCacheService.getInstance(project);
for (String redSymbol : allRedSymbols) {
Arrays.stream(
PsiSearchUtils.findClassesByShortName(project, symbolsScope, redSymbol + "Spec"))
.filter(LithoPluginUtils::isLayoutSpec)
.forEach(
specCls -> {
final PsiClass resolved = componentsCache.maybeUpdate(specCls, false);
redSymbolToClass.put(redSymbol, resolved);
});
}
return redSymbolToClass;
}
@Override
public final void visit(final UAnnotation node) {
final PsiAnnotation nodePsi = node.getJavaPsi();
if (nodePsi == null || !ConfigElementsUtils.isJsonConfigAnnotation(nodePsi)) {
return;
}
final PsiClass jsonType = ConfigElementsUtils.getJsonTypeAttribute(nodePsi);
if (jsonType == null) {
return;
}
final int genericTypesCount = ConfigElementsUtils.getGenericTypesCount(nodePsi);
visitJsonConfigAnnotation(node, jsonType, genericTypesCount);
}
@Nullable
public static String getQualifiedName(@NotNull Activity activity) {
ApplicationManager.getApplication().assertReadAccessAllowed();
PsiClass psiClass = activity.getActivityClass().getValue();
if (psiClass == null) {
Module module = activity.getModule();
if (module != null && ApkFacet.getInstance(module) != null) {
// In APK project we doesn't necessarily have the source/class file of the activity.
return activity.getActivityClass().getStringValue();
}
return null;
}
return getQualifiedActivityName(psiClass);
}
@Override
protected void visitEnumConfigAnnotation(final UAnnotation node, final PsiClass enumClass) {
final PsiField[] fields = enumClass.getFields();
if (fields.length == 0) {
report(node);
return;
}
for (PsiField psiField : fields) {
if (ElementUtils.isEnumConst(psiField) && !ConfigElementsUtils.hasRemoteValueAnnotation(psiField)) {
report(node);
return;
}
}
}
public void testUtilityClassModifiersInnerClass() {
PsiFile file = myFixture.configureByFile(getTestName(false) + ".java");
PsiClass innerClass = PsiTreeUtil.getParentOfType(file.findElementAt(myFixture.getCaretOffset()), PsiClass.class);
assertNotNull(innerClass);
assertNotNull(innerClass.getModifierList());
PsiElement parent = innerClass.getParent();
assertNotNull(parent);
assertTrue(parent instanceof PsiClass);
PsiClass parentClass = (PsiClass) parent;
assertNotNull(parentClass.getModifierList());
assertTrue("@UtilityClass should make parent class final", ((PsiClass) innerClass.getParent()).getModifierList().hasModifierProperty(PsiModifier.FINAL));
assertTrue("@UtilityClass should make inner class static", innerClass.getModifierList().hasModifierProperty(PsiModifier.STATIC));
}
@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);
}
}
});
}
}
@Nullable
private static TestContext fromClass(PsiClass testClass) {
String testFilter = getTestFilterForClass(testClass);
if (testFilter == null) {
return null;
}
TestSize testSize = TestSizeFinder.getTestSize(testClass);
ListenableFuture<TargetInfo> target =
TestTargetHeuristic.targetFutureForPsiElement(testClass, testSize);
if (target == null) {
return null;
}
return TestContext.builder(testClass, ExecutorType.FAST_DEBUG_SUPPORTED_TYPES)
.setTarget(target)
.setTestFilter(testFilter)
.setDescription(testClass.getName())
.build();
}
@Nullable
@Override
public BinaryRunContext getRunContext(ConfigurationContext context) {
PsiClass mainClass = getMainClass(context);
if (mainClass == null) {
return null;
}
TargetIdeInfo target = getTarget(context.getProject(), mainClass);
if (target == null) {
return null;
}
// Try setting source element to a main method so ApplicationConfigurationProducer
// can't override our configuration by producing a more specific one.
PsiMethod mainMethod = PsiMethodUtil.findMainMethod(mainClass);
return BinaryRunContext.create(
/* sourceElement= */ mainMethod != null ? mainMethod : mainClass, target.toTargetInfo());
}
/**
* Make the class implementing Parcelable
*/
private void makeClassImplementParcelable(PsiElementFactory elementFactory, JavaCodeStyleManager styleManager) {
final PsiClassType[] implementsListTypes = psiClass.getImplementsListTypes();
final String implementsType = "android.os.Parcelable";
for (PsiClassType implementsListType : implementsListTypes) {
PsiClass resolved = implementsListType.resolve();
// Already implements Parcelable, no need to add it
if (resolved != null && implementsType.equals(resolved.getQualifiedName())) {
return;
}
}
PsiJavaCodeReferenceElement implementsReference =
elementFactory.createReferenceFromText(implementsType, psiClass);
PsiReferenceList implementsList = psiClass.getImplementsList();
if (implementsList != null) {
styleManager.shortenClassReferences(implementsList.add(implementsReference));
}
}
/** Same as {@link JUnitUtil#getTestClass}, but handles classes outside the project. */
@Nullable
public static PsiClass getTestClass(final Location<?> location) {
for (Iterator<Location<PsiClass>> iterator = location.getAncestors(PsiClass.class, false);
iterator.hasNext(); ) {
final Location<PsiClass> classLocation = iterator.next();
if (isTestClass(classLocation.getPsiElement())) {
return classLocation.getPsiElement();
}
}
PsiElement element = location.getPsiElement();
if (element instanceof PsiClassOwner) {
PsiClass[] classes = ((PsiClassOwner) element).getClasses();
if (classes.length == 1 && isTestClass(classes[0])) {
return classes[0];
}
}
return null;
}
@Test
public void daemonFinished_settingsTrue_resolved() throws IOException {
final PsiFile specPsiFile = testHelper.configure("LayoutSpec.java");
testHelper.configure("ResolveRedSymbolsActionTest.java");
final Project project = testHelper.getFixture().getProject();
AppSettingsState.getInstance(project).getState().resolveRedSymbols = true;
ApplicationManager.getApplication()
.invokeAndWait(
() -> {
PsiSearchUtils.addMock(
"LayoutSpec", PsiTreeUtil.findChildOfType(specPsiFile, PsiClass.class));
new OnCodeAnalysisFinishedListener(project).daemonFinished();
});
final PsiClass cached = ComponentsCacheService.getInstance(project).getComponent("Layout");
assertThat(cached).isNotNull();
}
@Test
public void proxyEquals_not_equal() {
testHelper.getPsiClass(
psiClasses -> {
assertNotNull(psiClasses);
PsiClass psiClass = psiClasses.get(0);
PsiParameter[] parameters =
PsiTreeUtil.findChildOfType(psiClass, PsiParameterList.class).getParameters();
Prop prop1 = PsiAnnotationProxyUtils.findAnnotationInHierarchy(parameters[0], Prop.class);
Prop prop2 = PsiAnnotationProxyUtils.findAnnotationInHierarchy(parameters[1], Prop.class);
// Calls proxy
assertNotEquals(prop1, prop2);
return true;
},
"WithAnnotationClass.java");
}
@Test
public void getPrimaryElements() {
testHelper.getPsiClass(
psiClasses -> {
final PsiClass targetCls = psiClasses.get(0);
final PsiMethod[] methods = targetCls.getMethods();
final PsiMethod staticMethod = methods[0];
final PsiClass withMethodsClass = targetCls.getInnerClasses()[0];
final PsiElement[] primaryElements =
new SpecMethodFindUsagesHandler(staticMethod, cls -> withMethodsClass)
.getPrimaryElements();
assertThat(primaryElements.length).isEqualTo(2);
assertThat(primaryElements[0].getParent()).isSameAs(withMethodsClass);
assertThat(primaryElements[1].getParent()).isSameAs(targetCls);
return true;
},
"WithMethods.java");
}
@NotNull
@Override
public List<? super PsiElement> process(@NotNull PsiClass psiClass) {
if (psiClass.getParent() instanceof PsiClass) {
PsiClass parentClass = (PsiClass) psiClass.getParent();
PsiAnnotation psiAnnotation = PsiAnnotationSearchUtil.findAnnotation(parentClass, getSupportedAnnotationClasses());
if (null != psiAnnotation && supportAnnotationVariant(psiAnnotation)) {
ProblemEmptyBuilder problemBuilder = ProblemEmptyBuilder.getInstance();
if (super.validate(psiAnnotation, parentClass, problemBuilder)) {
final String typeName = FieldNameConstantsHandler.getTypeName(parentClass, psiAnnotation);
if (typeName.equals(psiClass.getName())) {
if (validate(psiAnnotation, parentClass, problemBuilder)) {
List<? super PsiElement> result = new ArrayList<>();
generatePsiElements(parentClass, psiClass, psiAnnotation, result);
return result;
}
}
}
}
}
return Collections.emptyList();
}
@Test
public void testMethodLocationResolves() {
PsiFile javaFile =
workspace.createPsiFile(
new WorkspacePath("java/com/google/lib/JavaClass.java"),
"package com.google.lib;",
"public class JavaClass {",
" public void testMethod() {}",
"}");
PsiClass javaClass = ((PsiClassOwner) javaFile).getClasses()[0];
PsiMethod method = javaClass.findMethodsByName("testMethod", false)[0];
assertThat(method).isNotNull();
String url =
handler.testLocationUrl(
Label.create("//java/com/google/lib:JavaClass"),
null,
null,
"testMethod",
"com.google.lib.JavaClass");
Location<?> location = getLocation(url);
assertThat(location.getPsiElement()).isEqualTo(method);
}
/**
* @param generatedCls class containing inner Builder class with methods annotated as {@link
* RequiredProp} marking which Props are required for this class.
* @param methodNames methods from the given class.
* @return names of the generatedCls required props, that were not set after all methodNames
* calls.
*/
private static Collection<String> collectMissingRequiredProps(
PsiClass generatedCls, Collection<String> methodNames) {
Map<String, Set<String>> propToMethods = getRequiredPropsToMethodNames(generatedCls);
if (propToMethods.isEmpty()) {
return Collections.emptySet();
}
Set<String> missingRequiredProps = new HashSet<>(propToMethods.keySet());
Map<String, String> methodToProp = inverse(propToMethods);
for (String methodName : methodNames) {
if (methodToProp.containsKey(methodName)) {
String prop = methodToProp.get(methodName);
missingRequiredProps.remove(prop);
}
}
return missingRequiredProps;
}
@Nullable
private static PsiClass getMainClass(ConfigurationContext context) {
Location location = context.getLocation();
if (location == null) {
return null;
}
location = JavaExecutionUtil.stepIntoSingleClass(location);
if (location == null) {
return null;
}
PsiElement element = location.getPsiElement();
if (!element.isPhysical()) {
return null;
}
return ApplicationConfigurationType.getMainClass(element);
}
private void processPsiField(final PsiField refPsiField, final SearchRequestCollector collector) {
final PsiClass containingClass = refPsiField.getContainingClass();
if (null != containingClass) {
processClassMethods(refPsiField, collector, containingClass);
final PsiClass[] innerClasses = containingClass.getInnerClasses();
Arrays.stream(innerClasses)
.forEach(psiClass -> processClassMethods(refPsiField, collector, psiClass));
Arrays.stream(innerClasses)
.forEach(psiClass -> processClassFields(refPsiField, collector, psiClass));
}
}
@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;
}
public static List<PsiFile> getAllJavaFiles(Module module) {
Collection<VirtualFile> javaVirtualFiles = FileTypeIndex.getFiles(JavaFileType.INSTANCE, moduleScope(module));
List<PsiFile> javaFiles = new ArrayList<>();
for (VirtualFile javaVFile : javaVirtualFiles) {
PsiFile file = PsiManager.getInstance(module.getProject()).findFile(javaVFile);
if (file != null && PsiTreeUtil.findChildrenOfType(file, PsiClass.class).size() > 0) {
javaFiles.add(file);
}
}
Collections.sort(javaFiles, (o1, o2) -> FileManager.getJavaFileName(o1).compareToIgnoreCase(FileManager.getJavaFileName(o2)));
return javaFiles;
}
private PsiReference[] createCamelBeanMethodReference(@NotNull PsiElement element) {
if (element.getText().contains("IntellijIdeaRulezzz")) {
return PsiReference.EMPTY_ARRAY;
}
PsiClass psiClass = getCamelIdeaUtils().getBean(element);
if (psiClass != null) {
String methodName = StringUtils.stripDoubleQuotes(element.getText());
if (!methodName.isEmpty()) {
return new PsiReference[] {new CamelBeanMethodReference(element, psiClass, methodName, new TextRange(1, methodName.length() + 1))};
}
}
return PsiReference.EMPTY_ARRAY;
}
private static PsiClass getPsiClass(PsiClass wrapperClass, PsiType psiFieldType) {
PsiClassType psiClassType = (PsiClassType) psiFieldType;
PsiClassType.ClassResolveResult classResolveResult = psiClassType.resolveGenerics();
PsiClass outerClass = classResolveResult.getElement();
// If Lazy<Foo> or Provider<Foo>, extract Foo as the interesting type.
if (PsiConsultantImpl.isLazyOrProvider(outerClass)) {
PsiType genericType = extractFirstTypeParameter(psiClassType);
// Convert genericType to its PsiClass and store in psiClass
wrapperClass = getClass(genericType);
}
return wrapperClass;
}
protected void processClass(@NotNull PsiClass psiClass) {
for (BaseLombokHandler handler : handlers) {
handler.processClass(psiClass);
}
removeDefaultAnnotation(psiClass, Getter.class);
removeDefaultAnnotation(psiClass, Setter.class);
removeDefaultAnnotation(psiClass, ToString.class);
removeDefaultAnnotation(psiClass, EqualsAndHashCode.class);
addAnnotation(psiClass, Data.class);
}
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);
}
}
};
}
private static Optional<PsiClass> getNavigatableComponent(AnActionEvent e) {
final Project project = e.getProject();
if (project == null) return Optional.empty();
return Optional.ofNullable(e.getData(CommonDataKeys.PSI_FILE))
.flatMap(psiFile -> LithoPluginUtils.getFirstClass(psiFile, LithoPluginUtils::isLithoSpec))
.map(PsiClass::getQualifiedName)
.map(LithoPluginUtils::getLithoComponentNameFromSpec)
.map(
qualifiedComponentName ->
PsiSearchUtils.findOriginalClass(project, qualifiedComponentName))
// Copied from the GotoDeclarationAction#gotoTargetElement
.filter(Navigatable::canNavigate);
}
PsiClass getPsiClass(PsiFile psiFile, String className) {
for (PsiClass psiClass : PsiTreeUtil.findChildrenOfType(psiFile, PsiClass.class)) {
if (className.equals(psiClass.getQualifiedName())) {
return psiClass;
}
}
throw new AssertionFailedError(
"Could not find class " + className + " in file " + psiFile.getName());
}
/** @return a list of objects to insert into generated code. */
@NotNull
@Override
protected List<? extends GenerationInfo> generateMemberPrototypes(
PsiClass aClass, ClassMember[] members) throws IncorrectOperationException {
final List<GenerationInfo> prototypes = new ArrayList<>();
for (ClassMember member : members) {
if (member instanceof PsiMethodMember) {
PsiMethodMember methodMember = (PsiMethodMember) member;
prototypes.add(new PsiGenerationInfo<>(methodMember.getElement()));
}
}
return prototypes;
}
static LookupElementBuilder createMethodLookup(
PsiMethod method, PsiClass documentationCls, String lookupString, Runnable postProcessor) {
return LookupElementBuilder.createWithIcon(method)
.withPresentableText(lookupString)
.withLookupString(lookupString)
.withCaseSensitivity(false)
.withInsertHandler(
(context, item) -> {
handleInsert(method, context);
postProcessor.run();
})
.appendTailText(" {...}", true)
.withTypeText("Litho", true)
.withPsiElement(documentationCls);
}
/** Get the prop defaults from the given {@link PsiClass}. */
public static ImmutableList<PropDefaultModel> getPropDefaults(PsiClass psiClass) {
final List<PropDefaultModel> propDefaults = new ArrayList<>();
for (PsiField psiField : psiClass.getFields()) {
propDefaults.addAll(extractFromField(psiField));
}
return ImmutableList.copyOf(propDefaults);
}
@Test
public void hasLithoSectionSpecAnnotation() {
PsiClass withLithoSection =
createSpecWithAnnotation("com.facebook.litho.sections.annotations.Any");
Assert.assertTrue(LithoPluginUtils.hasLithoSectionSpecAnnotation(withLithoSection));
PsiClass withoutLithoSection = createSpecWithAnnotation("com.facebook.litho.annotations.Any");
Assert.assertFalse(LithoPluginUtils.hasLithoSectionSpecAnnotation(withoutLithoSection));
PsiClass notSpec =
createWithAnnotation(PsiClass.class, "com.facebook.litho.sections.annotations.Any");
Assert.assertFalse(LithoPluginUtils.hasLithoSectionSpecAnnotation(notSpec));
}