use of com.android.tools.klint.detector.api.ConstantEvaluator in project kotlin by JetBrains.
the class UastLintUtils method findLastValue.
@Nullable
public static Object findLastValue(@NonNull PsiVariable variable, @NonNull UElement call, @NonNull JavaContext context, @NonNull ConstantEvaluator evaluator) {
Object value = null;
if (!variable.hasModifierProperty(PsiModifier.FINAL) && (variable instanceof PsiLocalVariable || variable instanceof PsiParameter)) {
UMethod containingFunction = UastUtils.getContainingUMethod(call);
if (containingFunction != null) {
ConstantEvaluator.LastAssignmentFinder finder = new ConstantEvaluator.LastAssignmentFinder(variable, call, context, evaluator, 1);
containingFunction.getUastBody().accept(finder);
value = finder.getCurrentValue();
}
} else {
UExpression initializer = context.getUastContext().getInitializerBody(variable);
if (initializer != null) {
value = initializer.evaluate();
}
}
return value;
}
use of com.android.tools.klint.detector.api.ConstantEvaluator in project kotlin by JetBrains.
the class PermissionRequirement method getAnnotationStringValues.
@Nullable
public static String[] getAnnotationStringValues(@Nullable UAnnotation annotation, @NonNull String name) {
if (annotation != null) {
UExpression attributeValue = annotation.findDeclaredAttributeValue(name);
if (attributeValue == null && ATTR_VALUE.equals(name)) {
attributeValue = annotation.findDeclaredAttributeValue(null);
}
if (attributeValue == null) {
return null;
}
if (UastExpressionUtils.isArrayInitializer(attributeValue)) {
List<UExpression> initializers = ((UCallExpression) attributeValue).getValueArguments();
List<String> result = Lists.newArrayListWithCapacity(initializers.size());
ConstantEvaluator constantEvaluator = new ConstantEvaluator(null);
for (UExpression element : initializers) {
Object o = constantEvaluator.evaluate(element);
if (o instanceof String) {
result.add((String) o);
}
}
if (result.isEmpty()) {
return null;
} else {
return result.toArray(new String[0]);
}
} else {
// Use constant evaluator since we want to resolve field references as well
Object o = ConstantEvaluator.evaluate(null, attributeValue);
if (o instanceof String) {
return new String[] { (String) o };
} else if (o instanceof String[]) {
return (String[]) o;
} else if (o instanceof Object[]) {
Object[] array = (Object[]) o;
List<String> strings = Lists.newArrayListWithCapacity(array.length);
for (Object element : array) {
if (element instanceof String) {
strings.add((String) element);
}
}
return strings.toArray(new String[0]);
}
}
}
return null;
}
Aggregations