下面列出了com.intellij.psi.PsiClass#getMethods ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @param generatedCls class containing inner Builder class with methods annotated as {@link
* RequiredProp} marking which Props are required for this class.
* @return mapping from required prop name to the method names setting this prop. Return empty map
* if the provided class doesn't contain Builder inner class, or doesn't have methods with
* {@link RequiredProp} annotation.
*/
private static Map<String, Set<String>> getRequiredPropsToMethodNames(PsiClass generatedCls) {
PsiClass builder = generatedCls.findInnerClassByName("Builder", false);
if (builder == null) {
return Collections.emptyMap();
}
Map<String, Set<String>> propToMethods = new HashMap<>();
PsiMethod[] methods = builder.getMethods();
for (PsiMethod method : methods) {
RequiredProp requiredProp =
PsiAnnotationProxyUtils.findAnnotationInHierarchy(method, RequiredProp.class);
if (requiredProp == null) {
continue;
}
Set<String> methodNames = propToMethods.get(requiredProp.value());
if (methodNames == null) {
methodNames = new HashSet<>();
propToMethods.put(requiredProp.value(), methodNames);
}
methodNames.add(method.getName());
}
return propToMethods;
}
@Override
public StructureViewTreeElement[] getChildren(PsiElement parent) {
Collection<StructureViewTreeElement> result = new ArrayList<StructureViewTreeElement>();
final PsiClass psiClass = (PsiClass) parent;
for (PsiField psiField : psiClass.getFields()) {
if (psiField instanceof SqliteMagicLightFieldBuilder) {
result.add(new PsiFieldTreeElement(psiField, false));
}
}
for (PsiMethod psiMethod : psiClass.getMethods()) {
if (psiMethod instanceof SqliteMagicLightMethodBuilder) {
result.add(new PsiMethodTreeElement(psiMethod, false));
}
}
if (!result.isEmpty()) {
return result.toArray(new StructureViewTreeElement[result.size()]);
} else {
return StructureViewTreeElement.EMPTY_ARRAY;
}
}
private void collectNodes(PsiClass psiClass) {
for (PsiMethod psiMethod : psiClass.getMethods()) {
addUserElement(psiMethod);
}
for (PsiField psiField : psiClass.getFields()) {
addUserElement(psiField);
}
for (PsiClassInitializer psiClassInitializer : psiClass.getInitializers()) {
addUserElement(psiClassInitializer);
}
for (PsiClass innerClass : psiClass.getInnerClasses()) {
addUserElement(innerClass);
}
}
public void testError526() {
//TODO disable assertions for the moment
RecursionManager.disableMissedCacheAssertions(myFixture.getProjectDisposable());
PsiFile psiFile = loadToPsiFile(getTestName(false) + ".java");
assertNotNull(psiFile);
PsiClass psiClass = PsiTreeUtil.getParentOfType(psiFile.findElementAt(myFixture.getCaretOffset()), PsiClass.class);
assertNotNull(psiClass);
// call augmentprovider first time
final PsiMethod[] psiClassMethods = psiClass.getMethods();
assertEquals(8, psiClassMethods.length);
// change something to trigger cache drop
WriteCommandAction.writeCommandAction(getProject(), psiFile).compute(() ->
{
psiClass.getModifierList().addAnnotation("java.lang.SuppressWarnings");
return true;
}
);
// call augment provider second time
final PsiMethod[] psiClassMethods2 = psiClass.getMethods();
assertArrayEquals(psiClassMethods, psiClassMethods2);
}
private PsiType getConfigType(final PsiAnnotation configAnnotation) {
final PsiClass configAnnotationClass = ElementUtils.getAnnotationDeclarationClass(configAnnotation);
if (configAnnotationClass != null) {
for (PsiMethod method : configAnnotationClass.getMethods()) {
if (method.getName()
.equals(ATTRIBUTE_DEFAULT_VALUE)) {
return method.getReturnType();
}
}
}
return null;
}
private void collectProperties(String prefix, PsiClass configType, IPropertiesCollector collector, IPsiUtils utils,
DocumentFormat documentFormat) {
String sourceType = configType.getQualifiedName();
PsiMethod[] methods = configType.getMethods();
for (PsiMethod method : methods) {
String resultTypeName = PsiTypeUtils.getResolvedResultTypeName(method);
PsiClass resultTypeClass = PsiTypeUtils.findType(method.getManager(), resultTypeName);
String methodName = method.getName();
String propertyName = prefix + "." + StringUtil.hyphenate(methodName);
boolean isArray = method.getReturnType().getArrayDimensions() > 0;
if (isArray) {
propertyName += "[*]";
}
if (isSimpleFieldType(resultTypeClass, resultTypeName)) {
String type = getPropertyType(resultTypeClass, resultTypeName);
String description = utils.getJavadoc(method, documentFormat);
String sourceMethod = getSourceMethod(method);
String defaultValue = PsiTypeUtils.getDefaultValue(method);
String extensionName = null;
super.updateHint(collector, resultTypeClass);
super.addItemMetadata(collector, propertyName, type, description, sourceType, null, sourceMethod,
defaultValue, extensionName, PsiTypeUtils.isBinary(method));
} else {
collectProperties(propertyName, resultTypeClass, collector, utils, documentFormat);
}
}
}
static ImmutableList<SpecMethodModel<EventMethod, EventDeclarationModel>> getOnEventMethods(
PsiClass psiClass, List<Class<? extends Annotation>> permittedInterStageInputAnnotations) {
final List<SpecMethodModel<EventMethod, EventDeclarationModel>> delegateMethods =
new ArrayList<>();
for (PsiMethod psiMethod : psiClass.getMethods()) {
final PsiAnnotation onEventAnnotation =
AnnotationUtil.findAnnotation(psiMethod, OnEvent.class.getName());
if (onEventAnnotation == null) {
continue;
}
PsiClassObjectAccessExpression accessExpression =
(PsiClassObjectAccessExpression) onEventAnnotation.findAttributeValue("value");
final List<MethodParamModel> methodParams =
getMethodParams(
psiMethod,
EventMethodExtractor.getPermittedMethodParamAnnotations(
permittedInterStageInputAnnotations),
permittedInterStageInputAnnotations,
ImmutableList.of());
final SpecMethodModel<EventMethod, EventDeclarationModel> eventMethod =
new SpecMethodModel<>(
ImmutableList.of(),
PsiModifierExtractor.extractModifiers(psiMethod.getModifierList()),
psiMethod.getName(),
PsiTypeUtils.generateTypeSpec(psiMethod.getReturnType()),
ImmutableList.copyOf(getTypeVariables(psiMethod)),
ImmutableList.copyOf(methodParams),
psiMethod,
PsiEventDeclarationsExtractor.getEventDeclarationModel(accessExpression));
delegateMethods.add(eventMethod);
}
return ImmutableList.copyOf(delegateMethods);
}
public static ImmutableList<SpecMethodModel<DelegateMethod, Void>> getDelegateMethods(
PsiClass psiClass,
List<Class<? extends Annotation>> permittedMethodAnnotations,
List<Class<? extends Annotation>> permittedInterStageInputAnnotations,
List<Class<? extends Annotation>> delegateMethodAnnotationsThatSkipDiffModels) {
final List<SpecMethodModel<DelegateMethod, Void>> delegateMethods = new ArrayList<>();
for (PsiMethod psiMethod : psiClass.getMethods()) {
final List<Annotation> methodAnnotations =
getMethodAnnotations(psiMethod, permittedMethodAnnotations);
if (!methodAnnotations.isEmpty()) {
final List<MethodParamModel> methodParams =
getMethodParams(
psiMethod,
DelegateMethodExtractor.getPermittedMethodParamAnnotations(
permittedInterStageInputAnnotations),
permittedInterStageInputAnnotations,
delegateMethodAnnotationsThatSkipDiffModels);
final SpecMethodModel<DelegateMethod, Void> delegateMethod =
new SpecMethodModel<>(
ImmutableList.copyOf(methodAnnotations),
PsiModifierExtractor.extractModifiers(psiMethod.getModifierList()),
psiMethod.getName(),
PsiTypeUtils.generateTypeSpec(psiMethod.getReturnType()),
ImmutableList.<TypeVariableName>of(),
ImmutableList.copyOf(methodParams),
psiMethod,
null);
delegateMethods.add(delegateMethod);
}
}
return ImmutableList.copyOf(delegateMethods);
}
@Nullable
static SpecMethodModel<EventMethod, Void> getRegisterMethod(
PsiClass psiClass, List<Class<? extends Annotation>> permittedInterStageInputAnnotations) {
for (PsiMethod psiMethod : psiClass.getMethods()) {
final OnRegisterRanges onRegisterRangesAnnotation =
PsiAnnotationProxyUtils.findAnnotationInHierarchy(psiMethod, OnRegisterRanges.class);
if (onRegisterRangesAnnotation != null) {
final List<MethodParamModel> methodParams =
getMethodParams(
psiMethod,
getPermittedMethodParamAnnotations(permittedInterStageInputAnnotations),
permittedInterStageInputAnnotations,
ImmutableList.of());
return SpecMethodModel.<EventMethod, Void>builder()
.annotations(ImmutableList.of())
.modifiers(PsiModifierExtractor.extractModifiers(psiMethod.getModifierList()))
.name(psiMethod.getName())
.returnTypeSpec(PsiTypeUtils.generateTypeSpec(psiMethod.getReturnType()))
.typeVariables(ImmutableList.copyOf(getTypeVariables(psiMethod)))
.methodParams(ImmutableList.copyOf(methodParams))
.representedObject(psiMethod)
.build();
}
}
return null;
}
public static ImmutableList<SpecMethodModel<UpdateStateMethod, Void>> getOnUpdateStateMethods(
PsiClass psiClass,
List<Class<? extends Annotation>> permittedInterStageInputAnnotations,
boolean isTransition) {
final List<SpecMethodModel<UpdateStateMethod, Void>> delegateMethods = new ArrayList<>();
for (PsiMethod psiMethod : psiClass.getMethods()) {
final Annotation onUpdateStateAnnotation =
isTransition
? PsiAnnotationProxyUtils.findAnnotationInHierarchy(
psiMethod, OnUpdateStateWithTransition.class)
: PsiAnnotationProxyUtils.findAnnotationInHierarchy(psiMethod, OnUpdateState.class);
if (onUpdateStateAnnotation != null) {
final List<MethodParamModel> methodParams =
getMethodParams(
psiMethod,
getPermittedMethodParamAnnotations(permittedInterStageInputAnnotations),
permittedInterStageInputAnnotations,
ImmutableList.<Class<? extends Annotation>>of());
final SpecMethodModel<UpdateStateMethod, Void> delegateMethod =
SpecMethodModel.<UpdateStateMethod, Void>builder()
.annotations(ImmutableList.<Annotation>of(onUpdateStateAnnotation))
.modifiers(PsiModifierExtractor.extractModifiers(psiMethod.getModifierList()))
.name(psiMethod.getName())
.returnTypeSpec(PsiTypeUtils.generateTypeSpec(psiMethod.getReturnType()))
.typeVariables(ImmutableList.<TypeVariableName>of())
.methodParams(copyOf(methodParams))
.representedObject(psiMethod)
.build();
delegateMethods.add(delegateMethod);
}
}
return copyOf(delegateMethods);
}
private static boolean classHasMethod(PsiClass psiClass, PsiMethod other) {
for (PsiMethod psiMethod : psiClass.getMethods()) {
if (psiMethod.equals(other)) {
return true;
}
}
return false;
}
@Nullable
public PsiMethod getBusPostMethod(Project project) {
JavaPsiFacade javaPsiFacade = JavaPsiFacade.getInstance(project);
GlobalSearchScope globalSearchScope = GlobalSearchScope.allScope(project);
PsiClass busClass = javaPsiFacade.findClass(getBusClassName(), globalSearchScope);
if (busClass != null) {
for (PsiMethod psiMethod : busClass.getMethods()) {
if (psiMethod.getName().equals("post")) {
return psiMethod;
}
}
}
return null;
}
public static ImmutableList<SpecMethodModel<EventMethod, EventDeclarationModel>>
getOnTriggerMethods(
PsiClass psiClass,
List<Class<? extends Annotation>> permittedInterStageInputAnnotations) {
final List<SpecMethodModel<EventMethod, EventDeclarationModel>> delegateMethods =
new ArrayList<>();
for (PsiMethod psiMethod : psiClass.getMethods()) {
final OnTrigger onTriggerAnnotation =
PsiAnnotationProxyUtils.findAnnotationInHierarchy(psiMethod, OnTrigger.class);
if (onTriggerAnnotation != null) {
final List<MethodParamModel> methodParams =
getMethodParams(
psiMethod,
TriggerMethodExtractor.getPermittedMethodParamAnnotations(
permittedInterStageInputAnnotations),
permittedInterStageInputAnnotations,
ImmutableList.<Class<? extends Annotation>>of());
PsiAnnotation psiOnTriggerAnnotation =
AnnotationUtil.findAnnotation(psiMethod, OnTrigger.class.getName());
PsiNameValuePair valuePair =
AnnotationUtil.findDeclaredAttribute(psiOnTriggerAnnotation, "value");
PsiClassObjectAccessExpression valueClassExpression =
(PsiClassObjectAccessExpression) valuePair.getValue();
// Reuse EventMethodModel and EventDeclarationModel because we are capturing the same info
TypeSpec returnTypeSpec = PsiTypeUtils.generateTypeSpec(psiMethod.getReturnType());
final SpecMethodModel<EventMethod, EventDeclarationModel> eventMethod =
new SpecMethodModel<EventMethod, EventDeclarationModel>(
ImmutableList.<Annotation>of(),
PsiModifierExtractor.extractModifiers(psiMethod.getModifierList()),
psiMethod.getName(),
returnTypeSpec,
ImmutableList.copyOf(getTypeVariables(psiMethod)),
ImmutableList.copyOf(methodParams),
psiMethod,
PsiEventDeclarationsExtractor.getEventDeclarationModel(valueClassExpression));
delegateMethods.add(eventMethod);
}
}
return ImmutableList.copyOf(delegateMethods);
}