Java Code Examples for com.intellij.psi.PsiAnnotation#findAttributeValue()

The following examples show how to use com.intellij.psi.PsiAnnotation#findAttributeValue() . 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: PsiEventDeclarationsExtractor.java    From litho with Apache License 2.0 6 votes vote down vote up
public static ImmutableList<EventDeclarationModel> getEventDeclarations(PsiClass psiClass) {
  final PsiAnnotation layoutSpecAnnotation =
      AnnotationUtil.findAnnotation(psiClass, LayoutSpec.class.getName());
  if (layoutSpecAnnotation == null) {
    throw new RuntimeException("LayoutSpec annotation not found on class");
  }

  PsiAnnotationMemberValue psiAnnotationMemberValue =
      layoutSpecAnnotation.findAttributeValue("events");

  ArrayList<EventDeclarationModel> eventDeclarationModels = new ArrayList<>();
  if (psiAnnotationMemberValue instanceof PsiArrayInitializerMemberValue) {
    PsiArrayInitializerMemberValue value =
        (PsiArrayInitializerMemberValue) psiAnnotationMemberValue;
    for (PsiAnnotationMemberValue annotationMemberValue : value.getInitializers()) {
      PsiClassObjectAccessExpression accessExpression =
          (PsiClassObjectAccessExpression) annotationMemberValue;
      eventDeclarationModels.add(getEventDeclarationModel(accessExpression));
    }
  } else if (psiAnnotationMemberValue instanceof PsiClassObjectAccessExpression) {
    eventDeclarationModels.add(
        getEventDeclarationModel((PsiClassObjectAccessExpression) psiAnnotationMemberValue));
  }

  return ImmutableList.copyOf(eventDeclarationModels);
}
 
Example 2
Source File: PsiAnnotationProxyUtils.java    From litho with Apache License 2.0 6 votes vote down vote up
private Object invoke(Method method, Object[] args) throws IllegalAccessException {
  Class<?> returnType = method.getReturnType();
  if (returnType.isEnum()) {
    PsiAnnotation currentAnnotation =
        AnnotationUtil.findAnnotationInHierarchy(
            mListOwner, Collections.singleton(mAnnotationClass.getCanonicalName()));
    PsiReferenceExpression declaredValue =
        (PsiReferenceExpression) currentAnnotation.findAttributeValue(method.getName());
    if (declaredValue == null) {
      return method.getDefaultValue();
    }
    PsiIdentifier identifier = PsiTreeUtil.getChildOfType(declaredValue, PsiIdentifier.class);
    return Enum.valueOf((Class<Enum>) returnType, identifier.getText());
  }

  try {
    if (args == null) {
      return method.invoke(mStubbed);
    }
    return method.invoke(mStubbed, args);
  } catch (InvocationTargetException e) {
    return method.getDefaultValue();
  }
}
 
Example 3
Source File: SuggestionServiceImpl.java    From component-runtime with Apache License 2.0 6 votes vote down vote up
private Stream<Suggestion> fromComponent(final PsiClass clazz, final String defaultFamily) {
    final PsiAnnotation componentAnnotation =
            AnnotationUtil.findAnnotation(clazz, PARTITION_MAPPER, PROCESSOR, EMITTER);
    final PsiAnnotationMemberValue name = componentAnnotation.findAttributeValue("name");
    if (name == null || "\"\"".equals(name.getText())) {
        return Stream.empty();
    }

    final PsiAnnotationMemberValue familyValue = componentAnnotation.findAttributeValue("family");
    final String componentFamily = (familyValue == null || removeQuotes(familyValue.getText()).isEmpty()) ? null
            : removeQuotes(familyValue.getText());

    final String family = ofNullable(componentFamily).orElseGet(() -> ofNullable(defaultFamily).orElse(null));
    if (family == null) {
        return Stream.empty();
    }

    return Stream
            .of(new Suggestion(family + "." + DISPLAY_NAME, Suggestion.Type.Family), new Suggestion(
                    family + "." + removeQuotes(name.getText()) + "." + DISPLAY_NAME, Suggestion.Type.Component));
}
 
Example 4
Source File: OkJsonUpdateLineMarkerProvider.java    From NutzCodeInsight with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public LineMarkerInfo getLineMarkerInfo(@NotNull PsiElement psiElement) {
    try {
        PsiAnnotation psiAnnotation = this.getPsiAnnotation(psiElement);
        if (psiAnnotation != null) {
            PsiLiteralExpression literalExpression = (PsiLiteralExpression) psiAnnotation.findAttributeValue("value");
            String value = String.valueOf(literalExpression.getValue());
            if (value.startsWith(JSON_PREFIX)) {
                return new LineMarkerInfo<>(psiAnnotation, psiAnnotation.getTextRange(), NutzCons.NUTZ,
                        new FunctionTooltip("快速配置"),
                        new OkJsonUpdateNavigationHandler(value),
                        GutterIconRenderer.Alignment.LEFT);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 5
Source File: CyclicDefaultValueConfigDetector.java    From aircon with MIT License 5 votes vote down vote up
private boolean hasCyclicDefaultValueConfigReference(final PsiField configField, final ArrayList<PsiField> visitedFields) {
	if (!ConfigElementsUtils.hasDefaultConfigAnnotation(configField)) {
		return false;
	}
	final PsiAnnotation defaultConfigAnnotation = ConfigElementsUtils.getDefaultConfigAnnotation(configField);
	final PsiElement defaultValueAttribute = defaultConfigAnnotation.findAttributeValue(null);
	final PsiField referencedDefaultConfigValue = ElementUtils.getReferencedField(defaultValueAttribute);
	if (visitedFields.contains(referencedDefaultConfigValue)) {
		return true;
	}

	visitedFields.add(referencedDefaultConfigValue);
	return hasCyclicDefaultValueConfigReference(referencedDefaultConfigValue, visitedFields);
}
 
Example 6
Source File: ConfigElementsUtils.java    From aircon with MIT License 5 votes vote down vote up
public static <T> T getAttributeValue(final PsiAnnotation configAnnotation, String attribute) {
	final PsiAnnotationMemberValue attributeValue = configAnnotation.findAttributeValue(attribute);

	if (attributeValue == null) {
		return null;
	}

	final PsiConstantEvaluationHelper evaluationHelper = JavaPsiFacade.getInstance(attributeValue.getProject())
	                                                                  .getConstantEvaluationHelper();
	return (T) evaluationHelper.computeConstantExpression(attributeValue);
}
 
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: JavaClassUtils.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Searching for the specific bean name and annotation to find it's {@link PsiClass}
 * @param beanName - Name of the bean to search for.
 * @param annotation - Type of bean annotation to filter on.
 * @param project - Project reference to narrow the search inside.
 * @return the {@link PsiClass} matching the bean name and annotation.
 */
public Optional<PsiClass> findBeanClassByName(String beanName, String annotation, Project project) {
    for (PsiClass psiClass : getClassesAnnotatedWith(project, annotation)) {
        final PsiAnnotation classAnnotation = psiClass.getAnnotation(annotation);
        PsiAnnotationMemberValue attribute = classAnnotation.findAttributeValue("value");
        if (attribute != null) {
            if (attribute instanceof PsiReferenceExpressionImpl) {
                //if the attribute value is field reference eg @bean(value = MyClass.BEAN_NAME)
                final PsiField psiField = (PsiField) attribute.getReference().resolve();
                String staticBeanName = StringUtils.stripDoubleQuotes(PsiTreeUtil.getChildOfAnyType(psiField, PsiLiteralExpression.class).getText());
                if (beanName.equals(staticBeanName)) {
                    return Optional.of(psiClass);
                }
            } else {
                final String value = attribute.getText();
                if (beanName.equals(StringUtils.stripDoubleQuotes(value))) {
                    return Optional.of(psiClass);
                }

            }
        } else {
            if (Introspector.decapitalize(psiClass.getName()).equalsIgnoreCase(StringUtils.stripDoubleQuotes(beanName))) {
                return Optional.of(psiClass);
            }
        }
    }

    return Optional.empty();
}
 
Example 9
Source File: JavaClassUtils.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Return the bean name for the {@link PsiClass} and the specific bean annotation
 * @param clazz - class to return bean name for
 * @param annotationFqn - the lookup FQN string for the annotation
 * @return the bean name
 */
private Optional<String> getBeanName(PsiClass clazz, String annotationFqn) {
    String returnName = null;
    final PsiAnnotation annotation = clazz.getAnnotation(annotationFqn);
    if (annotation != null) {
        final PsiAnnotationMemberValue componentAnnotation = annotation.findAttributeValue("value");
        returnName = componentAnnotation != null ? StringUtils.stripDoubleQuotes(componentAnnotation.getText()) : Introspector.decapitalize(clazz.getName());
    }
    return Optional.ofNullable(returnName);
}
 
Example 10
Source File: LombokProcessorUtil.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static Collection<String> getOnX(@NotNull PsiAnnotation psiAnnotation, @NotNull String parameterName) {
  PsiAnnotationMemberValue onXValue = psiAnnotation.findAttributeValue(parameterName);
  if (!(onXValue instanceof PsiAnnotation)) {
    return Collections.emptyList();
  }
  Collection<PsiAnnotation> annotations = PsiAnnotationUtil.getAnnotationValues((PsiAnnotation) onXValue, "value", PsiAnnotation.class);
  Collection<String> annotationStrings = new ArrayList<>();
  for (PsiAnnotation annotation : annotations) {
    PsiAnnotationParameterList params = annotation.getParameterList();
    annotationStrings.add(PsiAnnotationSearchUtil.getSimpleNameOf(annotation) + params.getText());
  }
  return annotationStrings;
}
 
Example 11
Source File: PsiConsultantImpl.java    From dagger-intellij-plugin with Apache License 2.0 5 votes vote down vote up
public static PsiAnnotationMemberValue findTypeAttributeOfProvidesAnnotation(
    PsiElement element ) {
  PsiAnnotation annotation = findAnnotation(element, CLASS_PROVIDES);
  if (annotation != null) {
    return annotation.findAttributeValue(ATTRIBUTE_TYPE);
  }
  return null;
}
 
Example 12
Source File: ConfigElementsUtils.java    From aircon with MIT License 4 votes vote down vote up
public static PsiArrayInitializerMemberValue getConfigGroupValuesAttribute(final PsiAnnotation configGroupAnnotation) {
	return (PsiArrayInitializerMemberValue) configGroupAnnotation.findAttributeValue(null);
}
 
Example 13
Source File: ConfigElementsUtils.java    From aircon with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
private static PsiClass getClassAttribute(final PsiAnnotation annotation, String attribute) {
	final PsiClassObjectAccessExpressionImpl attributeValue = (PsiClassObjectAccessExpressionImpl) annotation.findAttributeValue(attribute);
	return resolveClass(attributeValue);
}
 
Example 14
Source File: ConfigElementsUtils.java    From aircon with MIT License 4 votes vote down vote up
public static int getGenericTypesCount(final PsiAnnotation annotation) {
	final PsiArrayInitializerMemberValue attributeValue = (PsiArrayInitializerMemberValue) annotation.findAttributeValue(ATTRIBUTE_GENERIC_TYPES);
	return attributeValue != null ? attributeValue.getInitializers().length : 0;
}
 
Example 15
Source File: AbstractFieldNameConstantsProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected boolean supportAnnotationVariant(@NotNull PsiAnnotation psiAnnotation) {
  // new version of @FieldNameConstants has an attribute "asEnum", the old one not
  return null != psiAnnotation.findAttributeValue("asEnum");
}