Java Code Examples for com.intellij.psi.PsiClass#getMethods()

The following examples show how to use com.intellij.psi.PsiClass#getMethods() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequiredPropAnnotator.java    From litho with Apache License 2.0 6 votes vote down vote up
/**
 * @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;
}
 
Example 2
Source File: SqliteMagicStructureViewExtension.java    From sqlitemagic with Apache License 2.0 6 votes vote down vote up
@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;
  }
}
 
Example 3
Source File: MethodReferenceDiagramDataModel.java    From intellij-reference-diagram with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example 4
Source File: ExceptionTest.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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);
}
 
Example 5
Source File: InvalidDefaultValueResIdDetector.java    From aircon with MIT License 5 votes vote down vote up
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;
}
 
Example 6
Source File: QuarkusKubernetesProvider.java    From intellij-quarkus with Eclipse Public License 2.0 5 votes vote down vote up
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);
		}
	}
}
 
Example 7
Source File: PsiEventMethodExtractor.java    From litho with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 8
Source File: PsiDelegateMethodExtractor.java    From litho with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 9
Source File: PsiWorkingRangesMethodExtractor.java    From litho with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 10
Source File: PsiUpdateStateMethodExtractor.java    From litho with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 11
Source File: PsiUtils.java    From intellij-reference-diagram with Apache License 2.0 5 votes vote down vote up
private static boolean classHasMethod(PsiClass psiClass, PsiMethod other) {
    for (PsiMethod psiMethod : psiClass.getMethods()) {
        if (psiMethod.equals(other)) {
            return true;
        }
    }
    return false;
}
 
Example 12
Source File: SubscriberMetadata.java    From otto-intellij-plugin with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 13
Source File: PsiTriggerMethodExtractor.java    From litho with Apache License 2.0 4 votes vote down vote up
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);
}