use of org.jetbrains.uast.UReferenceExpression in project kotlin by JetBrains.
the class ConstantEvaluator method isArrayLiteral.
/**
* Returns true if the node is pointing to a an array literal
*/
public static boolean isArrayLiteral(@Nullable UElement node, @NonNull JavaContext context) {
if (node instanceof UReferenceExpression) {
PsiElement resolved = ((UReferenceExpression) node).resolve();
if (resolved instanceof PsiVariable) {
PsiVariable variable = (PsiVariable) resolved;
UExpression lastAssignment = UastLintUtils.findLastAssignment(variable, node, context);
if (lastAssignment != null) {
return isArrayLiteral(lastAssignment, context);
}
}
} else if (UastExpressionUtils.isNewArrayWithDimensions(node)) {
return true;
} else if (UastExpressionUtils.isNewArrayWithInitializer(node)) {
return true;
} else if (node instanceof UParenthesizedExpression) {
UParenthesizedExpression parenthesizedExpression = (UParenthesizedExpression) node;
UExpression expression = parenthesizedExpression.getExpression();
return isArrayLiteral(expression, context);
} else if (UastExpressionUtils.isTypeCast(node)) {
UBinaryExpressionWithType castExpression = (UBinaryExpressionWithType) node;
assert castExpression != null;
UExpression operand = castExpression.getOperand();
return isArrayLiteral(operand, context);
}
return false;
}
use of org.jetbrains.uast.UReferenceExpression in project kotlin by JetBrains.
the class ServiceCastDetector method visitMethod.
@Override
public void visitMethod(@NonNull JavaContext context, @Nullable UastVisitor visitor, @NonNull UCallExpression call, @NonNull UMethod method) {
UElement parent = LintUtils.skipParentheses(UastUtils.getQualifiedParentOrThis(call).getUastParent());
if (UastExpressionUtils.isTypeCast(parent)) {
UBinaryExpressionWithType cast = (UBinaryExpressionWithType) parent;
List<UExpression> args = call.getValueArguments();
if (args.size() == 1 && args.get(0) instanceof UReferenceExpression) {
PsiElement resolvedServiceConst = ((UReferenceExpression) args.get(0)).resolve();
if (!(resolvedServiceConst instanceof PsiField)) {
return;
}
String name = ((PsiField) resolvedServiceConst).getName();
String expectedClass = getExpectedType(name);
if (expectedClass != null && cast != null) {
String castType = cast.getType().getCanonicalText();
if (castType.indexOf('.') == -1) {
expectedClass = stripPackage(expectedClass);
}
if (!castType.equals(expectedClass)) {
// android.content.ClipboardManager and android.text.ClipboardManager
if (isClipboard(castType) && isClipboard(expectedClass)) {
return;
}
String message = String.format("Suspicious cast to `%1$s` for a `%2$s`: expected `%3$s`", stripPackage(castType), name, stripPackage(expectedClass));
context.report(ISSUE, call, context.getUastLocation(cast), message);
}
}
}
}
}
use of org.jetbrains.uast.UReferenceExpression in project kotlin by JetBrains.
the class StringFormatDetector method checkStringFormatCall.
/**
* Check the given String.format call (with the given arguments) to see if the string format is
* being used correctly
* @param context the context to report errors to
* @param calledMethod the method being called
* @param call the AST node for the {@link String#format}
* @param specifiesLocale whether the first parameter is a locale string, shifting the
*/
private void checkStringFormatCall(JavaContext context, PsiMethod calledMethod, UCallExpression call, boolean specifiesLocale) {
int argIndex = specifiesLocale ? 1 : 0;
List<UExpression> args = call.getValueArguments();
if (args.size() <= argIndex) {
return;
}
UExpression argument = args.get(argIndex);
ResourceUrl resource = ResourceEvaluator.getResource(context, argument);
if (resource == null || resource.framework || resource.type != ResourceType.STRING) {
return;
}
String name = resource.name;
if (mIgnoreStrings != null && mIgnoreStrings.contains(name)) {
return;
}
boolean passingVarArgsArray = false;
int callCount = args.size() - 1 - argIndex;
if (callCount == 1) {
// If instead of a varargs call like
// getString(R.string.foo, arg1, arg2, arg3)
// the code is calling the varargs method with a packed Object array, as in
// getString(R.string.foo, new Object[] { arg1, arg2, arg3 })
// we'll need to handle that such that we don't think this is a single
// argument
UExpression lastArg = args.get(args.size() - 1);
PsiParameterList parameterList = calledMethod.getParameterList();
int parameterCount = parameterList.getParametersCount();
if (parameterCount > 0 && parameterList.getParameters()[parameterCount - 1].isVarArgs()) {
boolean knownArity = false;
boolean argWasReference = false;
if (lastArg instanceof UReferenceExpression) {
PsiElement resolved = ((UReferenceExpression) lastArg).resolve();
if (resolved instanceof PsiVariable) {
UExpression initializer = context.getUastContext().getInitializerBody((PsiVariable) resolved);
if (initializer != null && (UastExpressionUtils.isNewArray(initializer) || UastExpressionUtils.isArrayInitializer(initializer))) {
argWasReference = true;
// Now handled by check below
lastArg = initializer;
}
}
}
if (UastExpressionUtils.isNewArray(lastArg) || UastExpressionUtils.isArrayInitializer(lastArg)) {
UCallExpression arrayInitializer = (UCallExpression) lastArg;
if (UastExpressionUtils.isNewArrayWithInitializer(lastArg) || UastExpressionUtils.isArrayInitializer(lastArg)) {
callCount = arrayInitializer.getValueArgumentCount();
knownArity = true;
} else if (UastExpressionUtils.isNewArrayWithDimensions(lastArg)) {
List<UExpression> arrayDimensions = arrayInitializer.getValueArguments();
if (arrayDimensions.size() == 1) {
UExpression first = arrayDimensions.get(0);
if (first instanceof ULiteralExpression) {
Object o = ((ULiteralExpression) first).getValue();
if (o instanceof Integer) {
callCount = (Integer) o;
knownArity = true;
}
}
}
}
if (!knownArity) {
if (!argWasReference) {
return;
}
} else {
passingVarArgsArray = true;
}
}
}
}
if (callCount > 0 && mNotFormatStrings.containsKey(name)) {
checkNotFormattedHandle(context, call, name, mNotFormatStrings.get(name));
return;
}
List<Pair<Handle, String>> list = mFormatStrings != null ? mFormatStrings.get(name) : null;
if (list == null) {
LintClient client = context.getClient();
if (client.supportsProjectResources() && !context.getScope().contains(Scope.RESOURCE_FILE)) {
AbstractResourceRepository resources = client.getProjectResources(context.getMainProject(), true);
List<ResourceItem> items;
if (resources != null) {
items = resources.getResourceItem(ResourceType.STRING, name);
} else {
// Must be a non-Android module
items = null;
}
if (items != null) {
for (final ResourceItem item : items) {
ResourceValue v = item.getResourceValue(false);
if (v != null) {
String value = v.getRawXmlValue();
if (value != null) {
// Make sure it's really a formatting string,
// not for example "Battery remaining: 90%"
boolean isFormattingString = value.indexOf('%') != -1;
for (int j = 0, m = value.length(); j < m && isFormattingString; j++) {
char c = value.charAt(j);
if (c == '\\') {
j++;
} else if (c == '%') {
Matcher matcher = FORMAT.matcher(value);
if (!matcher.find(j)) {
isFormattingString = false;
} else {
String conversion = matcher.group(6);
int conversionClass = getConversionClass(conversion.charAt(0));
if (conversionClass == CONVERSION_CLASS_UNKNOWN || matcher.group(5) != null) {
// Some date format etc - don't process
return;
}
}
// Don't process second % in a %%
j++;
}
// If the user marked the string with
}
Handle handle = client.createResourceItemHandle(item);
if (isFormattingString) {
if (list == null) {
list = Lists.newArrayList();
if (mFormatStrings == null) {
mFormatStrings = Maps.newHashMap();
}
mFormatStrings.put(name, list);
}
list.add(Pair.of(handle, value));
} else if (callCount > 0) {
checkNotFormattedHandle(context, call, name, handle);
}
}
}
}
}
} else {
return;
}
}
if (list != null) {
Set<String> reported = null;
for (Pair<Handle, String> pair : list) {
String s = pair.getSecond();
if (reported != null && reported.contains(s)) {
continue;
}
int count = getFormatArgumentCount(s, null);
Handle handle = pair.getFirst();
if (count != callCount) {
Location location = context.getUastLocation(call);
Location secondary = handle.resolve();
secondary.setMessage(String.format("This definition requires %1$d arguments", count));
location.setSecondary(secondary);
String message = String.format("Wrong argument count, format string `%1$s` requires `%2$d` but format " + "call supplies `%3$d`", name, count, callCount);
context.report(ARG_TYPES, call, location, message);
if (reported == null) {
reported = Sets.newHashSet();
}
reported.add(s);
} else {
if (passingVarArgsArray) {
// flag parameters on the Object[] instead of the wrapped parameters
return;
}
for (int i = 1; i <= count; i++) {
int argumentIndex = i + argIndex;
PsiType type = args.get(argumentIndex).getExpressionType();
if (type != null) {
boolean valid = true;
String formatType = getFormatArgumentType(s, i);
if (formatType == null) {
continue;
}
char last = formatType.charAt(formatType.length() - 1);
if (formatType.length() >= 2 && Character.toLowerCase(formatType.charAt(formatType.length() - 2)) == 't') {
// TODO
continue;
}
switch(last) {
// unusual and probably not intended.
case 'b':
case 'B':
valid = isBooleanType(type);
break;
// Numeric: integer and floats in various formats
case 'x':
case 'X':
case 'd':
case 'o':
case 'e':
case 'E':
case 'f':
case 'g':
case 'G':
case 'a':
case 'A':
valid = isNumericType(type, true);
break;
case 'c':
case 'C':
// Unicode character
valid = isCharacterType(type);
break;
case 'h':
// Hex print of hash code of objects
case 'H':
case 's':
case 'S':
// String. Can pass anything, but warn about
// numbers since you may have meant more
// specific formatting. Use special issue
// explanation for this?
valid = !isBooleanType(type) && !isNumericType(type, false);
break;
}
if (!valid) {
Location location = context.getUastLocation(args.get(argumentIndex));
Location secondary = handle.resolve();
secondary.setMessage("Conflicting argument declaration here");
location.setSecondary(secondary);
String suggestion = null;
if (isBooleanType(type)) {
suggestion = "`b`";
} else if (isCharacterType(type)) {
suggestion = "'c'";
} else if (PsiType.INT.equals(type) || PsiType.LONG.equals(type) || PsiType.BYTE.equals(type) || PsiType.SHORT.equals(type)) {
suggestion = "`d`, 'o' or `x`";
} else if (PsiType.FLOAT.equals(type) || PsiType.DOUBLE.equals(type)) {
suggestion = "`e`, 'f', 'g' or `a`";
} else if (type instanceof PsiClassType) {
String fqn = type.getCanonicalText();
if (TYPE_INTEGER_WRAPPER.equals(fqn) || TYPE_LONG_WRAPPER.equals(fqn) || TYPE_BYTE_WRAPPER.equals(fqn) || TYPE_SHORT_WRAPPER.equals(fqn)) {
suggestion = "`d`, 'o' or `x`";
} else if (TYPE_FLOAT_WRAPPER.equals(fqn) || TYPE_DOUBLE_WRAPPER.equals(fqn)) {
suggestion = "`d`, 'o' or `x`";
} else if (TYPE_OBJECT.equals(fqn)) {
suggestion = "'s' or 'h'";
}
}
if (suggestion != null) {
suggestion = " (Did you mean formatting character " + suggestion + "?)";
} else {
suggestion = "";
}
String canonicalText = type.getCanonicalText();
canonicalText = canonicalText.substring(canonicalText.lastIndexOf('.') + 1);
String message = String.format("Wrong argument type for formatting argument '#%1$d' " + "in `%2$s`: conversion is '`%3$s`', received `%4$s` " + "(argument #%5$d in method call)%6$s", i, name, formatType, canonicalText, argumentIndex + 1, suggestion);
context.report(ARG_TYPES, call, location, message);
if (reported == null) {
reported = Sets.newHashSet();
}
reported.add(s);
}
}
}
}
}
}
}
use of org.jetbrains.uast.UReferenceExpression in project kotlin by JetBrains.
the class ResourceEvaluator method getResourceTypes.
/**
* Evaluates the given node and returns the resource types applicable to the
* node, if any.
*
* @param element the element to compute the types for
* @return the corresponding resource types
*/
@Nullable
public EnumSet<ResourceType> getResourceTypes(@Nullable UElement element) {
if (element == null) {
return null;
}
if (element instanceof UIfExpression) {
UIfExpression expression = (UIfExpression) element;
Object known = ConstantEvaluator.evaluate(null, expression.getCondition());
if (known == Boolean.TRUE && expression.getThenExpression() != null) {
return getResourceTypes(expression.getThenExpression());
} else if (known == Boolean.FALSE && expression.getElseExpression() != null) {
return getResourceTypes(expression.getElseExpression());
} else {
EnumSet<ResourceType> left = getResourceTypes(expression.getThenExpression());
EnumSet<ResourceType> right = getResourceTypes(expression.getElseExpression());
if (left == null) {
return right;
} else if (right == null) {
return left;
} else {
EnumSet<ResourceType> copy = EnumSet.copyOf(left);
copy.addAll(right);
return copy;
}
}
} else if (element instanceof UParenthesizedExpression) {
UParenthesizedExpression parenthesizedExpression = (UParenthesizedExpression) element;
return getResourceTypes(parenthesizedExpression.getExpression());
} else if ((element instanceof UQualifiedReferenceExpression && mAllowDereference) || element instanceof UCallExpression) {
UElement probablyCallExpression = element;
if (element instanceof UQualifiedReferenceExpression) {
UQualifiedReferenceExpression qualifiedExpression = (UQualifiedReferenceExpression) element;
probablyCallExpression = qualifiedExpression.getSelector();
}
if ((probablyCallExpression instanceof UCallExpression)) {
UCallExpression call = (UCallExpression) probablyCallExpression;
PsiMethod method = call.resolve();
PsiClass containingClass = UastUtils.getContainingClass(method);
if (method != null && containingClass != null) {
EnumSet<ResourceType> types = getTypesFromAnnotations(method);
if (types != null) {
return types;
}
String qualifiedName = containingClass.getQualifiedName();
String name = call.getMethodName();
if ((CLASS_RESOURCES.equals(qualifiedName) || CLASS_CONTEXT.equals(qualifiedName) || CLASS_FRAGMENT.equals(qualifiedName) || CLASS_V4_FRAGMENT.equals(qualifiedName) || CLS_TYPED_ARRAY.equals(qualifiedName)) && name != null && name.startsWith("get")) {
List<UExpression> args = call.getValueArguments();
if (!args.isEmpty()) {
types = getResourceTypes(args.get(0));
if (types != null) {
return types;
}
}
}
}
}
}
if (element instanceof UReferenceExpression) {
ResourceUrl url = getResourceConstant(element);
if (url != null) {
return EnumSet.of(url.type);
}
PsiElement resolved = ((UReferenceExpression) element).resolve();
if (resolved instanceof PsiVariable) {
PsiVariable variable = (PsiVariable) resolved;
UElement lastAssignment = UastLintUtils.findLastAssignment(variable, element, mContext);
if (lastAssignment != null) {
return getResourceTypes(lastAssignment);
}
return null;
}
}
return null;
}
use of org.jetbrains.uast.UReferenceExpression in project kotlin by JetBrains.
the class ResourceEvaluator method getResource.
/**
* Evaluates the given node and returns the resource reference (type and name) it
* points to, if any
*
* @param element the node to compute the constant value for
* @return the corresponding constant value - a String, an Integer, a Float, and so on
*/
@Nullable
public ResourceUrl getResource(@Nullable UElement element) {
if (element == null) {
return null;
}
if (element instanceof UIfExpression) {
UIfExpression expression = (UIfExpression) element;
Object known = ConstantEvaluator.evaluate(null, expression.getCondition());
if (known == Boolean.TRUE && expression.getThenExpression() != null) {
return getResource(expression.getThenExpression());
} else if (known == Boolean.FALSE && expression.getElseExpression() != null) {
return getResource(expression.getElseExpression());
}
} else if (element instanceof UParenthesizedExpression) {
UParenthesizedExpression parenthesizedExpression = (UParenthesizedExpression) element;
return getResource(parenthesizedExpression.getExpression());
} else if (mAllowDereference && element instanceof UQualifiedReferenceExpression) {
UQualifiedReferenceExpression qualifiedExpression = (UQualifiedReferenceExpression) element;
UExpression selector = qualifiedExpression.getSelector();
if ((selector instanceof UCallExpression)) {
UCallExpression call = (UCallExpression) selector;
PsiMethod function = call.resolve();
PsiClass containingClass = UastUtils.getContainingClass(function);
if (function != null && containingClass != null) {
String qualifiedName = containingClass.getQualifiedName();
String name = call.getMethodName();
if ((CLASS_RESOURCES.equals(qualifiedName) || CLASS_CONTEXT.equals(qualifiedName) || CLASS_FRAGMENT.equals(qualifiedName) || CLASS_V4_FRAGMENT.equals(qualifiedName) || CLS_TYPED_ARRAY.equals(qualifiedName)) && name != null && name.startsWith("get")) {
List<UExpression> args = call.getValueArguments();
if (!args.isEmpty()) {
return getResource(args.get(0));
}
}
}
}
}
if (element instanceof UReferenceExpression) {
ResourceUrl url = getResourceConstant(element);
if (url != null) {
return url;
}
PsiElement resolved = ((UReferenceExpression) element).resolve();
if (resolved instanceof PsiVariable) {
PsiVariable variable = (PsiVariable) resolved;
UElement lastAssignment = UastLintUtils.findLastAssignment(variable, element, mContext);
if (lastAssignment != null) {
return getResource(lastAssignment);
}
return null;
}
}
return null;
}
Aggregations