use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class FileCreateTempFileCheck method checkAndAdvanceState.
@Nullable
private State checkAndAdvanceState(MethodInvocationTree mit, State requiredState, State nextState) {
ExpressionTree methodSelect = mit.methodSelect();
if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
ExpressionTree expressionTree = ((MemberSelectExpressionTree) methodSelect).expression();
if (expressionTree.is(Tree.Kind.IDENTIFIER)) {
Symbol symbol = ((IdentifierTree) expressionTree).symbol();
Map<Symbol, State> symbolStateMap = symbolStack.peek();
if (symbolStateMap != null && symbolStateMap.containsKey(symbol) && requiredState.equals(symbolStateMap.get(symbol))) {
symbolStateMap.put(symbol, nextState);
return nextState;
}
}
}
return null;
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class DefaultEncodingUsageCheck method onMethodInvocationFound.
@Override
protected void onMethodInvocationFound(MethodInvocationTree mit) {
if (COMMONS_IO_CHARSET_MATCHERS.anyMatch(mit)) {
Arguments arguments = mit.arguments();
ExpressionTree lastArgument = arguments.get(arguments.size() - 1);
testNullLiteralPassedForEncoding(lastArgument);
} else if (FILEUTILS_WRITE_WITH_CHARSET_MATCHERS.anyMatch(mit)) {
testNullLiteralPassedForEncoding(mit.arguments().get(2));
} else {
reportIssue(ExpressionUtils.methodName(mit), "Remove this use of \"" + mit.symbol().name() + "\"");
}
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class IndexOfStartPositionCheck method checkIndexOfInvocation.
private void checkIndexOfInvocation(MethodInvocationTree mit, ExpressionTree other) {
if (INDEX_OF_METHOD.matches(mit)) {
String replaceMessage;
ExpressionTree firstPar = mit.arguments().get(0);
if (firstPar.is(Tree.Kind.STRING_LITERAL)) {
replaceMessage = ((LiteralTree) firstPar).value();
} else if (firstPar.is(Tree.Kind.IDENTIFIER)) {
replaceMessage = ((IdentifierTree) firstPar).name();
} else {
replaceMessage = "xxx";
}
Long otherValue = LiteralUtils.longLiteralValue(other);
if (otherValue != null && otherValue != -1 && otherValue != 0) {
reportIssue(ExpressionUtils.methodName(mit), "Use \".indexOf(" + replaceMessage + ",n) > -1\" instead.");
}
}
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class LiteralUtils method longLiteralValue.
@CheckForNull
public static Long longLiteralValue(ExpressionTree tree) {
ExpressionTree expression = tree;
int sign = tree.is(Kind.UNARY_MINUS) ? -1 : 1;
if (tree.is(Kind.UNARY_MINUS, Kind.UNARY_PLUS)) {
expression = ((UnaryExpressionTree) tree).expression();
}
if (expression.is(Kind.INT_LITERAL, Kind.LONG_LITERAL)) {
String value = trimLongSuffix(((LiteralTree) expression).value());
// long as hexadecimal can be written using underscore to separate groups
value = value.replaceAll("\\_", "");
try {
return sign * Long.decode(value);
} catch (NumberFormatException e) {
// Long.decode() may fail in case of very large long number written in hexadecimal. In such situation, we ignore the number.
// Note that Long.MAX_VALUE = "0x7FFF_FFFF_FFFF_FFFFL", but it is possible to write larger numbers in hexadecimal
// to be used as mask in bitwise operation. For instance:
// 0x8000_0000_0000_0000L (MAX_VALUE + 1),
// 0xFFFF_FFFF_FFFF_FFFFL (only ones),
// 0xFFFF_FFFF_FFFF_FFFEL (only ones except least significant bit), ...
}
}
return null;
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class PackageUtils method packageName.
public static String packageName(@Nullable PackageDeclarationTree packageDeclarationTree, String separator) {
if (packageDeclarationTree == null) {
return "";
}
Deque<String> pieces = new LinkedList<>();
ExpressionTree expr = packageDeclarationTree.packageName();
while (expr.is(Tree.Kind.MEMBER_SELECT)) {
MemberSelectExpressionTree mse = (MemberSelectExpressionTree) expr;
pieces.push(mse.identifier().name());
pieces.push(separator);
expr = mse.expression();
}
if (expr.is(Tree.Kind.IDENTIFIER)) {
IdentifierTree idt = (IdentifierTree) expr;
pieces.push(idt.name());
}
StringBuilder sb = new StringBuilder();
for (String piece : pieces) {
sb.append(piece);
}
return sb.toString();
}
Aggregations