Java Code Examples for com.intellij.psi.PsiMethod#getName()

The following examples show how to use com.intellij.psi.PsiMethod#getName() . 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: AbstractBuckTestAction.java    From buck with Apache License 2.0 6 votes vote down vote up
/** Setup and execute a test configuration. */
protected void setupAndExecuteTestConfiguration(
    PsiClass psiClass, @Nullable PsiMethod psiMethod) {
  Project project = psiClass.getProject();
  VirtualFile virtualFile = psiClass.getContainingFile().getVirtualFile();
  String name;
  String testSelectors;
  if (psiMethod != null) {
    name = psiClass.getName() + "#" + psiMethod.getName();
    testSelectors = psiClass.getQualifiedName() + "#" + psiMethod.getName();
  } else {
    name = psiClass.getName();
    testSelectors = psiClass.getQualifiedName();
  }
  createTestConfigurationFromContext(name, testSelectors, project, virtualFile);
}
 
Example 2
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 3
Source File: InstalledBeforeSuperDetector.java    From Folivora with Apache License 2.0 5 votes vote down vote up
@Override
public void visitMethod(JavaContext context,
                        UCallExpression call,
                        PsiMethod method) {
  JavaEvaluator evaluator = context.getEvaluator();

  //check Folivora.installViewFactory() call
  String methodName = method.getName();
  if (!methodName.equals(INSTALL_METHOD) || !evaluator.isMemberInClass(method, TYPE_FOLIVORA))
    return;

  //check current class is decent of AppCompatActivity
  PsiClass legacyCompatActClass = evaluator.findClass(LEGACY_COMPAT_ACTIVITY);
  PsiClass compatActClass = evaluator.findClass(COMPAT_ACTIVITY);
  PsiClass c = UastUtils.getContainingClass(call);
  boolean isAppCompatActivity = false;
  if (c != null) {
    isAppCompatActivity = (legacyCompatActClass != null && c.isInheritor(legacyCompatActClass,
      true/*deep check*/))
      || compatActClass != null && c.isInheritor(compatActClass, true/*deep check*/);
  }
  if (!isAppCompatActivity) return;

  //check current method is onCreate
  @SuppressWarnings("unchecked")
  UMethod uMethod = UastUtils.getParentOfType(call, true, UMethod.class);
  if (uMethod == null || !"onCreate".equals(uMethod.getName())) return;

  SuperOnCreateFinder finder = new SuperOnCreateFinder(call);
  uMethod.accept(finder);
  if (!finder.isSuperOnCreateCalled()) {
    context.report(ISSUE, call, context.getLocation(call),
      "calling `Folivora.installViewFactory()` before super.onCreate can cause AppCompatViews unavailable");
  }
}
 
Example 4
Source File: InternalFolivoraApiDetector.java    From Folivora with Apache License 2.0 5 votes vote down vote up
@Override
public void visitMethod(@NotNull JavaContext context,
                        @NotNull UCallExpression call,
                        @NotNull PsiMethod method) {
  JavaEvaluator evaluator = context.getEvaluator();

  //check Folivora.applyDrawableToView() call
  String methodName = method.getName();
  if (!methodName.equals(APPLY_METHOD) || !evaluator.isMemberInClass(method, FOLIVORA_CLASS)) return;

  PsiClass viewClass = evaluator.findClass(VIEW_CLASS);
  PsiClass currentClass = UastUtils.getContainingClass(call);
  if(currentClass == null || viewClass == null) return;
  if (!currentClass.isInheritor(viewClass, true/*deep check*/)) {
    report(context, call);
    return;
  }

  UMethod uMethod = UastUtils.getParentOfType(call, UMethod.class, false);
  if(uMethod == null) return;

  //check it is a view's constructor
  if (!uMethod.isConstructor()) {
    report(context, call);
    return;
  }

  MethodSignature signature = uMethod.getSignature(PsiSubstitutor.EMPTY);
  PsiType[] types = signature.getParameterTypes();
  if (types.length != 2) {
    report(context, call);
    return;
  }

  if (!types[0].equalsToText(CONTEXT_CLASS)
    || !types[1].equalsToText(ATTRS_CLASS)) {
    report(context, call);
  }
}
 
Example 5
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 6
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 7
Source File: DocGenerateAction.java    From CodeMaker with Apache License 2.0 5 votes vote down vote up
/**
 * Create doc for method.
 *
 * @param psiMethod the psi method
 * @param psiElementFactory the psi element factory
 */
private void createDocForMethod(PsiMethod psiMethod, PsiElementFactory psiElementFactory) {
    try {
        checkFilesAccess(psiMethod);
        PsiDocComment psiDocComment = psiMethod.getDocComment();
        //return if the method has comment
        if (psiDocComment != null) {
            return;
        }
        String interfaceName = CodeMakerUtil.findClassNameOfSuperMethod(psiMethod);
        if (interfaceName == null) {
            return;
        }
        String methodName = psiMethod.getName();
        Map<String, Object> map = Maps.newHashMap();
        map.put("interface", interfaceName);
        map.put("method", methodName);
        map.put("paramsType", generateMethodParamsType(psiMethod));
        String seeDoc = VelocityUtil.evaluate(seeDocTemplate, map);
        PsiDocComment seeDocComment = psiElementFactory.createDocCommentFromText(seeDoc);
        WriteCommandAction writeCommandAction = new DocWriteAction(psiMethod.getProject(), seeDocComment, psiMethod,
                psiMethod.getContainingFile());
        RunResult result = writeCommandAction.execute();
        if (result.hasException()) {
            LOGGER.error(result.getThrowable());
            Messages.showErrorDialog("CodeMaker plugin is not available, cause: " + result.getThrowable().getMessage(),
                    "CodeMaker plugin");
        }
    } catch (Exception e) {
        LOGGER.error("Create @see Doc failed", e);
    }
}
 
Example 8
Source File: JavaMethodUtils.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param method - method to generate display text for
 * @return a presentable text with parameters for the specific method
 */
public String getPresentableMethodWithParameters(PsiMethod method) {
    final String parameters = getMethodParameters(method);
    String presentableMethod = method.getName();
    if (!parameters.isEmpty()) {
        presentableMethod = String.format("%s(%s)", method.getName(), parameters);
    }
    return presentableMethod;
}
 
Example 9
Source File: BlazeJUnitTestFilterFlags.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static String methodFilter(PsiMethod method, @Nullable String testSuffixRegex) {
  if (testSuffixRegex != null) {
    return method.getName() + testSuffixRegex;
  } else if (AnnotationUtil.findAnnotation(method, "Parameters") != null) {
    // Supports @junitparams.Parameters, an annotation that applies to an individual test method
    return method.getName() + JUnitParameterizedClassHeuristic.STANDARD_JUNIT_TEST_SUFFIX;
  } else {
    return method.getName();
  }
}
 
Example 10
Source File: MethodFQN.java    From intellij-reference-diagram with Apache License 2.0 5 votes vote down vote up
public static MethodFQN create(PsiMethod psiMethod) {
    String classFqn = ClassFQN.create(psiMethod.getContainingClass()).getFQN();
    String methodName = psiMethod.getName();
    Builder builder = new Builder(classFqn, methodName);

    List<String> parameters = getParameterArray(psiMethod);

    for (String parameter : parameters) {
        builder.addParameter(parameter);
    }

    return builder.create();
}
 
Example 11
Source File: ASTSliceGroup.java    From IntelliJDeodorant with MIT License 4 votes vote down vote up
@Override
public String toString() {
    PsiMethod psiMethod = candidates.iterator().next().getSourceMethodDeclaration();
    return psiMethod.getContainingClass() == null ? "" : psiMethod.getContainingClass().getQualifiedName() + "::" + psiMethod.getName();
}
 
Example 12
Source File: MicroProfileFaultToleranceProvider.java    From intellij-quarkus with Eclipse Public License 2.0 4 votes vote down vote up
private void collectProperties(IPropertiesCollector collector, AnnotationInfo info, PsiMember annotatedClassOrMethod,
		PsiAnnotation mpftAnnotation, MicroProfileFaultToleranceContext mpftContext) {
	String annotationName = info.getSimpleName();
	String className = null;
	String methodName = null;
	boolean binary = false;
	String sourceType = null;
	String sourceMethod = null;
	if (annotatedClassOrMethod != null) {
		binary = PsiTypeUtils.isBinary(annotatedClassOrMethod);
		if (annotatedClassOrMethod instanceof PsiClass) {
			PsiClass annotatedClass = (PsiClass) annotatedClassOrMethod;
			className = annotatedClass.getQualifiedName();
			// Check if properties has been generated for the <classname><annotation>:
			if (isProcessed(className, annotationName, mpftContext)) {
				return;
			}
			sourceType = getPropertyType(annotatedClass, className);
		} else if (annotatedClassOrMethod instanceof PsiMethod) {
			PsiMethod annotatedMethod = (PsiMethod) annotatedClassOrMethod;
			className = annotatedMethod.getContainingClass().getQualifiedName();
			methodName = annotatedMethod.getName();
			sourceType = getSourceType(annotatedMethod);
			sourceMethod = getSourceMethod(annotatedMethod);
		}
	} else {
		// Check if properties has been generated for the <annotation>:
		if (isProcessed(null, annotationName, mpftContext)) {
			return;
		}
	}

	String prefix = createPrefix(className, methodName, annotationName);

	// parameter
	List<AnnotationParameter> parameters = info.getParameters();
	for (AnnotationParameter parameter : parameters) {
		String propertyName = new StringBuilder(prefix).append('/').append(parameter.getName()).toString();
		String parameterType = parameter.getType();
		String description = parameter.getDescription();
		String defaultValue = getParameterDefaultValue(parameter, mpftAnnotation);
		String extensionName = null;
		if (annotatedClassOrMethod == null) {
			sourceType = parameter.getSourceType();
			sourceMethod = parameter.getSourceMethod();
		}
		// Enumerations
		PsiClass jdtType = parameter.getJDTType();
		super.updateHint(collector, jdtType);

		super.addItemMetadata(collector, propertyName, parameterType, description, sourceType, null, sourceMethod,
				defaultValue, extensionName, binary);
	}
}
 
Example 13
Source File: PsiTypeUtils.java    From intellij-quarkus with Eclipse Public License 2.0 4 votes vote down vote up
public static String getSourceMethod(PsiMethod method) {
    //TODO: check method signature
    return method.getName() + ClassUtil.getAsmMethodSignature(method);
}
 
Example 14
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);
}
 
Example 15
Source File: PsiUtils.java    From intellij-reference-diagram with Apache License 2.0 4 votes vote down vote up
public static String getPresentableName(PsiElement psiElement) {
    PsiElementDispatcher<String> psiElementDispatcher = new PsiElementDispatcher<String>() {

        @Override
        public String processClass(PsiClass psiClass) {
            return psiClass.getName();
        }

        @Override
        public String processMethod(PsiMethod psiMethod) {
            List<String> parameterArray = MethodFQN.getParameterArray(psiMethod);
            String parameterRepresentation = MethodFQN.createParameterRepresentation(parameterArray);
            return psiMethod.getName() + "(" + parameterRepresentation + ")";
        }

        @Override
        public String processField(PsiField psiField) {
            return psiField.getName();
        }

        @Override
        public String processClassInitializer(PsiClassInitializer psiClassInitializer) {
            return getName(psiClassInitializer);
        }

        @Override
        public String processInnerClass(PsiClass innerClass) {
            return innerClass.getName();
        }

        @Override
        public String processStaticInnerClass(PsiClass staticInnerClass) {
            return staticInnerClass.getName();
        }

        @Override
        public String processEnum(PsiClass anEnum) {
            return anEnum.getName();
        }

        @Override
        public String processPackage(PsiJavaDirectoryImpl aPackage) {
            return aPackage.getName();
        }

        @Override
        public String processFile(PsiJavaFile aFile) {
            return aFile.getName();
        }
    };

    return psiElementDispatcher.dispatch(psiElement);
}