/** * @param psiAnnotation that needs to be checked * * @return {@code true} if the {@code psiAnnotation} is the {@link org.mapstruct.Mapping} annotation, {@code * false} otherwise */ private static boolean isMappingAnnotation(PsiAnnotation psiAnnotation) { return Objects.equals( psiAnnotation.getQualifiedName(), MAPPING_ANNOTATION_FQN ); }
@NotNull private static List<String> getAnnotationsInner(PsiModifierListOwner element) { PsiModifierList modifierList = element.getModifierList(); if (modifierList == null) return ContainerUtilRt.emptyList(); List<String> result = new ArrayList<>(); for (PsiAnnotation annotation : modifierList.getAnnotations()) { if (annotation.getParameterList().getAttributes().length > 0) continue; ContainerUtil.addIfNotNull(result, annotation.getQualifiedName()); } return result; } }
private String getAttributeFromAnnotation( PsiAnnotation annotation, String annotationType, final String attribute) throws InvalidAnnotationException, MissingAttributeException { String annotationQualifiedName = annotation.getQualifiedName(); if (annotationQualifiedName == null) { throw new InvalidAnnotationException(annotation, annotationType); } if (annotationQualifiedName.equals(annotationType)) { PsiAnnotationMemberValue annotationMemberValue = annotation.findAttributeValue(attribute); if (annotationMemberValue == null) { throw new MissingAttributeException(annotation, attribute); } String httpMethodWithQuotes = annotationMemberValue.getText(); return httpMethodWithQuotes.substring(1, httpMethodWithQuotes.length() - 1); } else { throw new InvalidAnnotationException(annotation, annotationType); } }
@Nullable public static String getStringAttributeValue(@NotNull PsiAnnotation anno, @Nullable final String attributeName) { PsiAnnotationMemberValue attrValue = anno.findAttributeValue(attributeName); Object constValue = JavaPsiFacade.getInstance(anno.getProject()).getConstantEvaluationHelper().computeConstantExpression(attrValue); return constValue instanceof String ? (String)constValue : null; } @Nullable
public static boolean appendAnnotations(@NotNull StringBuilder sb, @NotNull List<PsiAnnotation> annotations, boolean canonical) { boolean updated = false; for (PsiAnnotation annotation : annotations) { if (canonical) { String name = annotation.getQualifiedName(); if (name != null) { sb.append('@').append(name).append(annotation.getParameterList().getText()).append(' '); updated = true; } } else { PsiJavaCodeReferenceElement refElement = annotation.getNameReferenceElement(); if (refElement != null) { sb.append('@').append(refElement.getText()).append(' '); updated = true; } } } return updated; } }
@Override protected void run(final Result result) throws Throwable { // @A(ownerName = "YourCo", ownerDomain = "your-company.com") String annotationString = "@A(" + API_NAMESPACE_NAME_ATTRIBUTE + " = \"" + SUGGESTED_OWNER_ATTRIBUTE + "\", " + API_NAMESPACE_DOMAIN_ATTRIBUTE + " = \"" + "your-company.com" + "\")"; PsiAnnotation newAnnotation = JavaPsiFacade.getInstance(project) .getElementFactory() .createAnnotationFromText(annotationString, null); PsiAnnotationMemberValue newDomainMemberValue = newAnnotation.findDeclaredAttributeValue(API_NAMESPACE_DOMAIN_ATTRIBUTE); PsiAnnotationMemberValue newNameMemberValue = newAnnotation.findDeclaredAttributeValue(API_NAMESPACE_NAME_ATTRIBUTE); annotation.setDeclaredAttributeValue(API_NAMESPACE_NAME_ATTRIBUTE, newNameMemberValue); annotation.setDeclaredAttributeValue( API_NAMESPACE_DOMAIN_ATTRIBUTE, newDomainMemberValue); } }.execute();
if (!GctConstants.APP_ENGINE_ANNOTATION_API.equals(annotation.getQualifiedName())) { return; PsiAnnotationParameterList parameterList = annotation.getParameterList(); if (parameterList.getAttributes().length == 0) { return; annotation.findAttributeValue(API_NAME_ATTRIBUTE); if (annotationMemberValue == null) { return;
@Override public void visitAnnotation(PsiAnnotation annotation) { String type = annotation.getQualifiedName(); if (type == null || type.startsWith("java.lang.")) { return; PsiAnnotationOwner owner = annotation.getOwner(); if (owner == null) { return; PsiNameValuePair[] attributes = annotation.getParameterList().getAttributes(); if (attributes.length == 1) { PsiNameValuePair attribute = attributes[0]; if (annotation.getParent() instanceof PsiModifierList && annotation.getParent().getParent() instanceof PsiMethod) { PsiMethod method = (PsiMethod) annotation.getParent().getParent(); if (!method.isConstructor() && PsiType.VOID.equals(method.getReturnType())) { if (annotation.getParent() instanceof PsiModifierList && annotation.getParent().getParent() instanceof PsiMethod) { String value = PermissionRequirement.getAnnotationStringValue(annotation, ATTR_VALUE); String[] anyOf = PermissionRequirement.getAnnotationStringValues(annotation, ATTR_ANY_OF); .getNameReferenceElement(); if (referenceElement != null) { PsiElement resolved = referenceElement.resolve(); if (cls.isAnnotationType() && cls.getModifierList() != null) {
if (!"javax.inject.Named".equals(annotation.getQualifiedName()) && !GctConstants.APP_ENGINE_ANNOTATION_NAMED.equals(annotation.getQualifiedName())) { return; PsiElement modifierList = annotation.getParent(); if (modifierList != null) { PsiElement annotationParent = modifierList.getParent(); .getElementFactory() .createAnnotationFromText(newNamedValue, null); assert (newAnnotation.getParameterList().getAttributes().length == 1); annotation.getParameterList().replace(newAnnotation.getParameterList());
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement element = descriptor.getPsiElement(); if (!(element instanceof PsiAnnotation)) { return; } PsiAnnotation annotation = (PsiAnnotation) element; if (!GctConstants.APP_ENGINE_ANNOTATION_API_METHOD.equals(annotation.getQualifiedName())) { return; } PsiAnnotationMemberValue apiMethodNameAttribute = annotation.findAttributeValue(API_METHOD_NAME_ATTRIBUTE); if (apiMethodNameAttribute == null) { return; } // Get name String nameValueWithQuotes = apiMethodNameAttribute.getText(); String nameValue = EndpointUtilities.removeBeginningAndEndingQuotes(nameValueWithQuotes); // @A(name = "someName") PsiAnnotationMemberValue newMemberValue = JavaPsiFacade.getInstance(project) .getElementFactory() .createAnnotationFromText( "@A(" + API_METHOD_NAME_ATTRIBUTE + " = \"" + nameValue + "_1\")", null) .findDeclaredAttributeValue(API_METHOD_NAME_ATTRIBUTE); apiMethodNameAttribute.replace(newMemberValue); } }
private static boolean getPropInfosOfFields(PsiAnnotation psa_fields, Set<PropertyDetector.PropInfo> mSet) { String qualifiedName = psa_fields.getQualifiedName(); if(qualifiedName == null || !qualifiedName.equals(FOCUS_FIELDS)){ return false; PsiConstantEvaluationHelper evaluation = JavaPsiFacade.getInstance(psa_fields.getProject()) .getConstantEvaluationHelper(); PsiAnnotationMemberValue tempValues = psa_fields.findAttributeValue("value"); if(tempValues == null){ return false; if(child instanceof PsiAnnotation){ PsiAnnotation expect = (PsiAnnotation) child; String prop = (String) evaluation.computeConstantExpression(expect.findAttributeValue("propName")); PsiAnnotationMemberValue typeVal = expect.findAttributeValue("type"); boolean is_bool = false; if(typeVal instanceof PsiExpression){
@NotNull public static Optional<PsiAnnotationMemberValue> getAnnotationAttributeValue(@NotNull PsiModifierListOwner target, @NotNull Annotation annotation, @NotNull String attrName) { if (!isAnnotationPresent(target, annotation)) { return Optional.absent(); } Optional<PsiAnnotation> psiAnnotation = getPsiAnnotation(target, annotation); return psiAnnotation.isPresent() ? Optional.fromNullable(psiAnnotation.get().findAttributeValue(attrName)) : Optional.<PsiAnnotationMemberValue>absent(); }
@Override public Object invoke(Object proxy, Method method, Object[] args) { Class<?>[] paramTypes = method.getParameterTypes(); assert paramTypes.length == 0: Arrays.toString(paramTypes); String member = method.getName(); if (member.equals("toString")) { return toStringImpl(); } if (member.equals("annotationType")) { return type; } // Handle annotation member accessors PsiAnnotationMemberValue value = myAnnotation.findAttributeValue(member); if (value == null) { throw new IncompleteAnnotationException(type, member+". (Unable to find attribute in '"+myAnnotation.getText()+"')"); } Object result = JavaPsiFacade.getInstance(myAnnotation.getProject()).getConstantEvaluationHelper().computeConstantExpression(value); if (result == null) { throw new IncompleteAnnotationException(type, member+". (Unable to evaluate annotation value '"+value+"')"); } // todo arrays return result; }
String signature = annotation.getQualifiedName(); if (signature == null || signature.startsWith("java.")) { PsiJavaCodeReferenceElement ref = annotation.getNameReferenceElement(); if (ref == null) { continue; for (int j = 0; j < innerAnnotations.length; j++) { PsiAnnotation inner = innerAnnotations[j]; String a = inner.getQualifiedName(); if (a == null || a.startsWith("java.")) {
public MissingAttributeException(PsiAnnotation annotation, String attribute) { super( "Attribute \"" + attribute + "\" does not exist in annotation \"" + annotation.getText() + "\""); } }
return mappingAnnotation; if ( !containerAnnotation.getText().contains( "{" ) ) { final PsiNameValuePair[] attributes = containerAnnotation.getParameterList().getAttributes(); if ( attributes.length == 1 ) { final String currentMappings = attributes[0].getText(); return JavaPsiFacade.getInstance( project ).getElementFactory().createAnnotationFromText( "@" + MapstructUtil.MAPPINGS_ANNOTATION_FQN + "({\n" + currentMappings + ",\n " + mappingAnnotation.getText() + "\n})", container ); final int curlyBraceIndex = containerAnnotation.getText().lastIndexOf( '}' ); if ( curlyBraceIndex > 0 ) { final String textBeforeCurlyBrace = containerAnnotation.getText().substring( 0, curlyBraceIndex ); int braceIndex = textBeforeCurlyBrace.lastIndexOf( ')' ); final String textToPreserve = braceIndex < 0 ? textBeforeCurlyBrace : textBeforeCurlyBrace.substring( 0, braceIndex ) + "),\n"; return JavaPsiFacade.getInstance( project ).getElementFactory().createAnnotationFromText( textToPreserve + " " + mappingAnnotation.getText() + "\n})", container ); throw new IncorrectOperationException( containerAnnotation.getText() );
/** * Implementation of dynamicProxy.toString() */ private String toStringImpl() { StringBuilder result = new StringBuilder(128); result.append('@'); result.append(type.getName()); result.append('('); boolean firstMember = true; PsiNameValuePair[] attributes = myAnnotation.getParameterList().getAttributes(); for (PsiNameValuePair e : attributes) { if (firstMember) { firstMember = false; } else { result.append(", "); } result.append(e.getName()); result.append('='); PsiAnnotationMemberValue value = e.getValue(); result.append(value == null ? "null" : value.getText()); } result.append(')'); return result.toString(); } }
private static boolean isNullabilityDefault(@NotNull PsiAnnotation annotation, boolean nullable, PsiAnnotation.TargetType[] placeTargetTypes) { PsiJavaCodeReferenceElement element = annotation.getNameReferenceElement(); PsiElement declaration = element == null ? null : element.resolve(); if (!(declaration instanceof PsiClass)) return false; if (!AnnotationUtil.isAnnotated((PsiClass)declaration, nullable ? JAVAX_ANNOTATION_NULLABLE : JAVAX_ANNOTATION_NONNULL, false, true)) { return false; } PsiAnnotation tqDefault = AnnotationUtil.findAnnotation((PsiClass)declaration, true, "javax.annotation.meta.TypeQualifierDefault"); if (tqDefault == null) return false; Set<PsiAnnotation.TargetType> required = AnnotationTargetUtil.extractRequiredAnnotationTargets(tqDefault.findAttributeValue(null)); if (required == null) return false; return required.isEmpty() || ContainerUtil.intersects(required, Arrays.asList(placeTargetTypes)); }
); if ( newAnnotation != null ) { if ( containerAnnotation != null && containerAnnotation.isPhysical() ) { runWriteCommandAction( project, () -> containerAnnotation.replace( newAnnotation ), containingFile ); String fqn = containerAnnotation != null ? MapstructUtil.MAPPINGS_ANNOTATION_FQN : MapstructUtil.MAPPING_ANNOTATION_FQN; PsiNameValuePair[] attributes = newAnnotation.getParameterList().getAttributes(); Optional<String> annotationToRemove = mappingsPair.getSecond() .map( PsiAnnotation::getQualifiedName );
@Nullable public static Long getAnnotationLongValue(@Nullable PsiAnnotation annotation, @NonNull String name) { if (annotation != null) { PsiAnnotationMemberValue attributeValue = annotation.findDeclaredAttributeValue(name); if (attributeValue == null && ATTR_VALUE.equals(name)) { attributeValue = annotation.findDeclaredAttributeValue(null); } // Use constant evaluator since we want to resolve field references as well if (attributeValue != null) { Object o = ConstantEvaluator.evaluate(null, attributeValue); if (o instanceof Number) { return ((Number)o).longValue(); } } } return null; }