Files
cherrypick/cherrypick_generator/lib/src/metadata_utils.dart

77 lines
3.2 KiB
Dart
Raw Normal View History

2025-05-21 15:50:24 +03:00
//
// Copyright 2021 Sergey Penkovsky (sergey.penkovsky@gmail.com)
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import 'package:analyzer/dart/element/element.dart';
2025-05-22 16:05:09 +03:00
/// ---------------------------------------------------------------------------
/// MetadataUtils -- utilities for analyzing method and parameter annotations.
2025-05-21 15:50:24 +03:00
///
2025-05-22 16:05:09 +03:00
/// ENGLISH
/// Provides static utility methods to analyze Dart annotations on methods or
/// parameters. For instance, helps to find if an element is annotated with
/// `@named()`, `@singleton()`, or other meta-annotations used in this DI framework.
2025-05-21 15:50:24 +03:00
///
2025-05-22 16:05:09 +03:00
/// RUSSIAN
/// Утилиты для разбора аннотаций методов и параметров.
/// Позволяют находить наличие аннотаций, например, @named() и @singleton(),
/// у методов и параметров. Используется для анализа исходного кода при генерации.
/// ---------------------------------------------------------------------------
2025-05-21 15:50:24 +03:00
class MetadataUtils {
2025-05-22 16:05:09 +03:00
/// -------------------------------------------------------------------------
/// anyMeta
///
/// ENGLISH
/// Checks if any annotation in the list has a type name that contains
/// [typeName] (case insensitive).
///
/// RUSSIAN
2025-05-21 15:50:24 +03:00
/// Проверяет: есть ли среди аннотаций метка, имя которой содержит [typeName]
2025-05-22 16:05:09 +03:00
/// (регистр не учитывается).
/// -------------------------------------------------------------------------
2025-05-21 15:50:24 +03:00
static bool anyMeta(List<ElementAnnotation> meta, String typeName) {
return meta.any((m) =>
m
.computeConstantValue()
?.type
?.getDisplayString()
.toLowerCase()
.contains(typeName.toLowerCase()) ??
false);
}
2025-05-22 16:05:09 +03:00
/// -------------------------------------------------------------------------
/// getNamedValue
///
/// ENGLISH
/// Retrieves the value from a `@named('value')` annotation if present.
/// Returns the string value or null if not found.
///
/// RUSSIAN
2025-05-21 15:50:24 +03:00
/// Находит значение из аннотации @named('значение').
/// Возвращает строку значения, если аннотация присутствует; иначе null.
2025-05-22 16:05:09 +03:00
/// -------------------------------------------------------------------------
2025-05-21 15:50:24 +03:00
static String? getNamedValue(List<ElementAnnotation> meta) {
for (final m in meta) {
final cv = m.computeConstantValue();
final typeStr = cv?.type?.getDisplayString().toLowerCase();
if (typeStr?.contains('named') ?? false) {
return cv?.getField('value')?.toStringValue();
}
}
return null;
}
}