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

The following examples show how to use com.intellij.psi.PsiClass#findMethodsByName() . 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: ComponentsMethodDeclarationHandler.java    From litho with Apache License 2.0 6 votes vote down vote up
private static PsiMethod[] findSpecMethods(PsiMethod componentMethod, Project project) {
  if (!componentMethod.getModifierList().hasModifierProperty(PsiModifier.STATIC)) {
    return PsiMethod.EMPTY_ARRAY;
  }

  final PsiClass containingCls =
      (PsiClass) PsiTreeUtil.findFirstParent(componentMethod, PsiClass.class::isInstance);
  if (containingCls == null) return PsiMethod.EMPTY_ARRAY;

  if (!LithoPluginUtils.isGeneratedClass(containingCls)) return PsiMethod.EMPTY_ARRAY;

  // For Unit testing we don't care about package
  final String containingClsName =
      ApplicationManager.getApplication().isUnitTestMode()
          ? containingCls.getName()
          : containingCls.getQualifiedName();
  final PsiClass specCls =
      PsiSearchUtils.findOriginalClass(
          project, LithoPluginUtils.getLithoComponentSpecNameFromComponent(containingClsName));
  if (specCls == null) return PsiMethod.EMPTY_ARRAY;

  return specCls.findMethodsByName(componentMethod.getName(), true);
}
 
Example 2
Source File: BlazeAndroidTestLocator.java    From intellij with Apache License 2.0 6 votes vote down vote up
private static PsiMethod findTestMethod(PsiClass psiClass, String methodName) {
  final PsiMethod[] methods = psiClass.findMethodsByName(methodName, true);

  if (methods.length == 0) {
    return null;
  }
  if (methods.length == 1) {
    return methods[0];
  }
  for (PsiMethod method : methods) {
    if (method.getParameterList().getParametersCount() == 0) {
      return method;
    }
  }
  return methods[0];
}
 
Example 3
Source File: BlazeJavaTestEventsHandlerTest.java    From intellij with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 4
Source File: BlazeJavaTestEventsHandlerTest.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Test
public void testParameterizedMethodLocationResolves() {
  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,
          "testMethod",
          "[0] true (testMethod)",
          "com.google.lib.JavaClass");
  Location<?> location = getLocation(url);
  assertThat(location.getPsiElement()).isEqualTo(method);
}
 
Example 5
Source File: CleanupProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void validateCleanUpMethodExists(@NotNull PsiLocalVariable psiVariable, @NotNull String cleanupName, @NotNull ProblemNewBuilder problemNewBuilder) {
  final PsiType psiType = psiVariable.getType();
  if (psiType instanceof PsiClassType) {
    final PsiClassType psiClassType = (PsiClassType) psiType;
    final PsiClass psiClassOfField = psiClassType.resolve();
    final PsiMethod[] methods;

    if (psiClassOfField != null) {
      methods = psiClassOfField.findMethodsByName(cleanupName, true);
      boolean hasCleanupMethod = false;
      for (PsiMethod method : methods) {
        if (0 == method.getParameterList().getParametersCount()) {
          hasCleanupMethod = true;
        }
      }

      if (!hasCleanupMethod) {
        problemNewBuilder.addError("'@Cleanup': method '%s()' not found on target class", cleanupName);
      }
    }
  } else {
    problemNewBuilder.addError("'@Cleanup': is legal only on a local variable declaration inside a block");
  }
}
 
Example 6
Source File: QuarkusConfigPropertiesProvider.java    From intellij-quarkus with Eclipse Public License 2.0 5 votes vote down vote up
private static PsiMethod findMethod(PsiClass configPropertiesType, String setterName, PsiType fieldType) {
	PsiMethod[] methods = configPropertiesType.findMethodsByName(setterName, true);
	for(PsiMethod method : methods) {
		if (method.getParameterList().getParametersCount() == 1 && method.getParameterList().getParameters()[0].getType().equals(fieldType)) {
			return method;
		}
	}
	return null;
}
 
Example 7
Source File: PropertiesManager.java    From intellij-quarkus with Eclipse Public License 2.0 5 votes vote down vote up
/** * Returns the Java field from the given property source
*
* @param module  the Java project
* @param sourceType   the source type (class or interface)
* @param sourceField  the source field and null otherwise.
* @param sourceMethod the source method and null otherwise.
* @return the Java field from the given property sources
*/
public PsiMember findDeclaredProperty(Module module, String sourceType, String sourceField,
                                    String sourceMethod, IPsiUtils utils) {
   if (sourceType == null) {
       return null;
   }
   // Try to find type with standard classpath
   PsiClass type = utils.findClass(module, sourceType);
   if (type == null) {
       return null;
   }
   if (sourceField != null) {
       return type.findFieldByName(sourceField, true);
   }
   if (sourceMethod != null) {
        int startBracketIndex = sourceMethod.indexOf('(');
        String methodName = sourceMethod.substring(0, startBracketIndex);
       // Method signature has been generated with PSI API, so we are sure that we have
       // a ')' character.
       for(PsiMethod method : type.findMethodsByName(methodName, true)) {
           String signature = PsiTypeUtils.getSourceMethod(method);
           if (signature.equals(sourceMethod)) {
               return method;
           }
       }
    }
   return type;
}
 
Example 8
Source File: PsiCustomUtil.java    From intellij-spring-assistant with MIT License 5 votes vote down vote up
@Nullable
private static PsiMethod findInstancePropertySetter(@NotNull PsiClass psiClass,
    @Nullable String propertyName) {
  if (StringUtil.isEmpty(propertyName))
    return null;
  final String suggestedSetterName = PropertyUtil.suggestSetterName(propertyName);
  final PsiMethod[] setters = psiClass.findMethodsByName(suggestedSetterName, true);
  for (PsiMethod setter : setters) {
    if (setter.hasModifierProperty(PUBLIC) && !setter.hasModifierProperty(STATIC)
        && isSimplePropertySetter(setter)) {
      return setter;
    }
  }
  return null;
}
 
Example 9
Source File: BlazeAndroidTestEventsHandlerTest.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Test
public void testMethodLocationResolves() {
  PsiFile javaFile =
      workspace.createPsiFile(
          new WorkspacePath("java/com/google/lib/JavaClass.java"),
          "package com.google.lib;",
          "import org.junit.Test;",
          "import org.junit.runner.RunWith;",
          "import org.junit.runners.JUnit4;",
          "@RunWith(JUnit4.class)",
          "public class JavaClass {",
          "  @Test",
          "  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,
          "JavaClass-testMethod",
          "com.google.test.AndroidTestBase");
  Location<?> location = getLocation(url);
  assertThat(location.getPsiElement()).isEqualTo(method);
}
 
Example 10
Source File: BlazeJUnitTestFilterFlagsIntegrationTest.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Test
public void testParameterizedMethods() {
  PsiFile javaFile =
      workspace.createPsiFile(
          new WorkspacePath("java/com/google/lib/JavaClass.java"),
          "package com.google.lib;",
          "import org.junit.Test;",
          "import org.junit.runner.RunWith;",
          "import org.junit.runners.JUnit4;",
          "@RunWith(JUnit4.class)",
          "public class JavaClass {",
          "  @Test",
          "  public void testMethod1() {}",
          "  @Test",
          "  public void testMethod2() {}",
          "}");
  PsiClass javaClass = ((PsiClassOwner) javaFile).getClasses()[0];

  PsiMethod method1 = javaClass.findMethodsByName("testMethod1", false)[0];
  Location<?> location1 =
      new PsiMemberParameterizedLocation(getProject(), method1, javaClass, "[param]");

  PsiMethod method2 = javaClass.findMethodsByName("testMethod2", false)[0];
  Location<?> location2 =
      new PsiMemberParameterizedLocation(getProject(), method2, javaClass, "[3]");

  assertThat(
          BlazeJUnitTestFilterFlags.testFilterForClassesAndMethods(
              ImmutableMap.of(javaClass, ImmutableList.of(location1, location2))))
      .isEqualTo("com.google.lib.JavaClass#(testMethod1\\[param\\]|testMethod2\\[3\\])$");
}
 
Example 11
Source File: ConfigElementsUtils.java    From aircon with MIT License 4 votes vote down vote up
private static PsiMethod getDefaultValueMethod(final PsiClass annotationClass) {
	return annotationClass.findMethodsByName(ATTRIBUTE_DEFAULT_VALUE, false)[0];
}
 
Example 12
Source File: BlazeJavaWebTestEventsHandlerTest.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Test
public void testMethodLocationResolves() {
  TargetMap targetMap =
      TargetMapBuilder.builder()
          .addTarget(
              TargetIdeInfo.builder()
                  .setLabel("//java/com/google/lib:JavaClass_chrome-linux")
                  .setKind("java_web_test")
                  .setBuildFile(src("java/com/google/lib/BUILD"))
                  .addDependency("//java/com/google/lib:JavaClass_wrapped_test"))
          .addTarget(
              TargetIdeInfo.builder()
                  .setLabel("//java/com/google/lib:JavaClass_wrapped_test")
                  .setKind("java_test")
                  .setBuildFile(src("java/com/google/lib/BUILD"))
                  .addSource(src("java/com/google/lib/JavaClass.java")))
          .build();

  registerProjectService(
      BlazeProjectDataManager.class,
      new MockBlazeProjectDataManager(
          MockBlazeProjectDataBuilder.builder(workspaceRoot).setTargetMap(targetMap).build()));

  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_chrome-linux"),
          RuleTypes.JAVA_WEB_TEST.getKind(),
          null,
          "testMethod",
          "com.google.lib.JavaClass");
  Location<?> location = getLocation(url);
  assertThat(location).isNotNull();
  assertThat(location.getPsiElement()).isEqualTo(method);
}
 
Example 13
Source File: BlazeJavaWebTestEventsHandlerTest.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Test
public void testParameterizedMethodLocationResolves() {
  TargetMap targetMap =
      TargetMapBuilder.builder()
          .addTarget(
              TargetIdeInfo.builder()
                  .setLabel("//java/com/google/lib:JavaClass_chrome-linux")
                  .setKind("java_web_test")
                  .setBuildFile(src("java/com/google/lib/BUILD"))
                  .addDependency("//java/com/google/lib:JavaClass_wrapped_test"))
          .addTarget(
              TargetIdeInfo.builder()
                  .setLabel("//java/com/google/lib:JavaClass_wrapped_test")
                  .setKind("java_test")
                  .setBuildFile(src("java/com/google/lib/BUILD"))
                  .addSource(src("java/com/google/lib/JavaClass.java")))
          .build();

  registerProjectService(
      BlazeProjectDataManager.class,
      new MockBlazeProjectDataManager(
          MockBlazeProjectDataBuilder.builder(workspaceRoot).setTargetMap(targetMap).build()));

  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_chrome-linux"),
          RuleTypes.JAVA_WEB_TEST.getKind(),
          "testMethod",
          "[0] true (testMethod)",
          "com.google.lib.JavaClass");
  Location<?> location = getLocation(url);
  assertThat(location).isNotNull();
  assertThat(location.getPsiElement()).isEqualTo(method);
}
 
Example 14
Source File: BlazeWebJavaTestEventsHandlerTest.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Test
public void testMethodLocationResolves() {
  TargetMap targetMap =
      TargetMapBuilder.builder()
          .addTarget(
              TargetIdeInfo.builder()
                  .setLabel("//java/com/google/lib:JavaClass_chrome-linux")
                  .setKind("web_test")
                  .setBuildFile(src("java/com/google/lib/BUILD"))
                  .addDependency("//java/com/google/lib:JavaClass_wrapped_test"))
          .addTarget(
              TargetIdeInfo.builder()
                  .setLabel("//java/com/google/lib:JavaClass_wrapped_test")
                  .setKind("java_test")
                  .setBuildFile(src("java/com/google/lib/BUILD"))
                  .addSource(src("java/com/google/lib/JavaClass.java")))
          .build();

  registerProjectService(
      BlazeProjectDataManager.class,
      new MockBlazeProjectDataManager(
          MockBlazeProjectDataBuilder.builder(workspaceRoot).setTargetMap(targetMap).build()));

  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_chrome-linux"),
          RuleTypes.WEB_TEST.getKind(),
          null,
          "testMethod",
          "com.google.lib.JavaClass");
  Location<?> location = getLocation(url);
  assertThat(location).isNotNull();
  assertThat(location.getPsiElement()).isEqualTo(method);
}
 
Example 15
Source File: BlazeWebJavaTestEventsHandlerTest.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Test
public void testParameterizedMethodLocationResolves() {
  TargetMap targetMap =
      TargetMapBuilder.builder()
          .addTarget(
              TargetIdeInfo.builder()
                  .setLabel("//java/com/google/lib:JavaClass_chrome-linux")
                  .setKind("web_test")
                  .setBuildFile(src("java/com/google/lib/BUILD"))
                  .addDependency("//java/com/google/lib:JavaClass_wrapped_test"))
          .addTarget(
              TargetIdeInfo.builder()
                  .setLabel("//java/com/google/lib:JavaClass_wrapped_test")
                  .setKind("java_test")
                  .setBuildFile(src("java/com/google/lib/BUILD"))
                  .addSource(src("java/com/google/lib/JavaClass.java")))
          .build();

  registerProjectService(
      BlazeProjectDataManager.class,
      new MockBlazeProjectDataManager(
          MockBlazeProjectDataBuilder.builder(workspaceRoot).setTargetMap(targetMap).build()));

  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_chrome-linux"),
          null,
          "testMethod",
          "[0] true (testMethod)",
          "com.google.lib.JavaClass");
  Location<?> location = getLocation(url);
  assertThat(location).isNotNull();
  assertThat(location.getPsiElement()).isEqualTo(method);
}
 
Example 16
Source File: BlazeJUnitTestFilterFlagsIntegrationTest.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultipleClassesWithParameterizedMethods() {
  PsiFile javaFile1 =
      workspace.createPsiFile(
          new WorkspacePath("java/com/google/lib/JavaClass1.java"),
          "package com.google.lib;",
          "import org.junit.Test;",
          "import org.junit.runner.RunWith;",
          "import org.junit.runners.JUnit4;",
          "@RunWith(JUnit4.class)",
          "public class JavaClass1 {",
          "  @Test",
          "  public void testMethod1() {}",
          "  @Test",
          "  public void testMethod2() {}",
          "}");
  PsiFile javaFile2 =
      workspace.createPsiFile(
          new WorkspacePath("java/com/google/lib/JavaClass2.java"),
          "package com.google.lib;",
          "import org.junit.Test;",
          "import org.junit.runner.RunWith;",
          "import org.junit.runners.JUnit4;",
          "@RunWith(JUnit4.class)",
          "public class JavaClass2 {",
          "  @Test",
          "  public void testMethod() {}",
          "}");
  PsiClass javaClass1 = ((PsiClassOwner) javaFile1).getClasses()[0];

  PsiMethod class1Method1 = javaClass1.findMethodsByName("testMethod1", false)[0];
  Location<?> class1Location1 =
      new PsiMemberParameterizedLocation(getProject(), class1Method1, javaClass1, "[param]");

  PsiMethod class1Method2 = javaClass1.findMethodsByName("testMethod2", false)[0];
  Location<?> class1Location2 =
      new PsiMemberParameterizedLocation(getProject(), class1Method2, javaClass1, "[3]");

  PsiClass javaClass2 = ((PsiClassOwner) javaFile2).getClasses()[0];
  PsiMethod class2Method = javaClass2.findMethodsByName("testMethod", false)[0];

  assertThat(
          BlazeJUnitTestFilterFlags.testFilterForClassesAndMethods(
              ImmutableMap.of(
                  javaClass1,
                  ImmutableList.of(class1Location1, class1Location2),
                  javaClass2,
                  ImmutableList.of(new PsiLocation<>(class2Method)))))
      .isEqualTo(
          Joiner.on('|')
              .join(
                  "com.google.lib.JavaClass1#(testMethod1\\[param\\]|testMethod2\\[3\\])$",
                  "com.google.lib.JavaClass2#testMethod$"));
}