com.intellij.psi.PsiClass#findMethodsByName ( )源码实例Demo

下面列出了com.intellij.psi.PsiClass#findMethodsByName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

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);
}
 
源代码2 项目: intellij   文件: BlazeAndroidTestLocator.java
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];
}
 
源代码3 项目: intellij   文件: BlazeJavaTestEventsHandlerTest.java
@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);
}
 
源代码4 项目: intellij   文件: BlazeJavaTestEventsHandlerTest.java
@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);
}
 
源代码5 项目: lombok-intellij-plugin   文件: CleanupProcessor.java
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");
  }
}
 
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;
}
 
源代码7 项目: intellij-quarkus   文件: PropertiesManager.java
/** * 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;
}
 
源代码8 项目: intellij-spring-assistant   文件: PsiCustomUtil.java
@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;
}
 
@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);
}
 
@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\\])$");
}
 
源代码11 项目: aircon   文件: ConfigElementsUtils.java
private static PsiMethod getDefaultValueMethod(final PsiClass annotationClass) {
	return annotationClass.findMethodsByName(ATTRIBUTE_DEFAULT_VALUE, false)[0];
}
 
@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);
}
 
@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);
}
 
@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);
}
 
@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);
}
 
@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$"));
}