org.sonar.plugins.java.api.tree.LiteralTree Java Examples

The following examples show how to use org.sonar.plugins.java.api.tree.LiteralTree. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HardcodedIpCheck.java    From vjtools with Apache License 2.0 6 votes vote down vote up
@Override
public void visitLiteral(LiteralTree tree) {
	if (tree.is(Tree.Kind.STRING_LITERAL)) {
		String value = LiteralUtils.trimQuotes(tree.value());
		IP.reset(value);
		if (IP.matches()) {
			String ip = IP.group("ip");

			// VJ:ADD 忽略127.0.0.1
			if ("127.0.0.1".equals(ip)) {
				return;
			}
			// VJ:END
			if (areAllBelow256(Splitter.on('.').split(ip))) {
				context.reportIssue(this, tree, "Make this IP \"" + ip + "\" address configurable.");
			}
		}
	}
}
 
Example #2
Source File: HardcodedIpCheck.java    From vjtools with Apache License 2.0 6 votes vote down vote up
@Override
public void visitLiteral(LiteralTree tree) {
	if (tree.is(Tree.Kind.STRING_LITERAL)) {
		String value = LiteralUtils.trimQuotes(tree.value());
		IP.reset(value);
		if (IP.matches()) {
			String ip = IP.group("ip");

			// VJ:ADD 忽略127.0.0.1
			if ("127.0.0.1".equals(ip)) {
				return;
			}
			// VJ:END
			if (areAllBelow256(Splitter.on('.').split(ip))) {
				context.reportIssue(this, tree, "Make this IP \"" + ip + "\" address configurable.");
			}
		}
	}
}
 
Example #3
Source File: AbstractSmellCheck.java    From qualinsight-plugins-sonarqube-smell with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static final String extractMessage(final ExpressionTree expressionTree) {
    String message = "";
    switch (expressionTree.kind()) {
        case STRING_LITERAL:
            message = ((LiteralTree) expressionTree).value();
            break;
        case PLUS:
            final BinaryExpressionTree bet = (BinaryExpressionTree) expressionTree;
            message = extractMessage(bet.leftOperand()) + extractMessage(bet.rightOperand());
            break;
        case IDENTIFIER:
            final IdentifierTree it = (IdentifierTree) expressionTree;
            message = extractMessage(((VariableTree) (it.symbol()
                .declaration())).initializer());
            break;
        default:
            LOGGER.warn("Cannot extract message due to unexpected expressionTree kind: {}", expressionTree.kind());
            break;
    }
    return trimQuotes(message);
}
 
Example #4
Source File: CommonUtil.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
public static Map<String, String> getLocatorValueMapInMethodInvocationTree(MethodInvocationTree tree) {
    Map<String, String> methodInvocationLocatorValueMap = new HashMap<>();

    if (methodInvocationIsElementFinder(tree) && !tree.arguments().isEmpty()) {
        String locatorStrategy = getIdentifier(tree).name();
        String locatorValue = ((LiteralTree) tree.arguments().get(0)).value().replace("\"", "");

        methodInvocationLocatorValueMap.put(locatorStrategy, locatorValue);
    }

    return methodInvocationLocatorValueMap;
}
 
Example #5
Source File: CommonUtil.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
public static Map<String, String> getLocatorValueMapInAnnotationTree(AnnotationTree annotationTree) {
    Map<String, String> locatorMap = new HashMap<>();
    String annotationType = annotationTree.annotationType().toString();
    String fullyQualifiedName = annotationTree.annotationType().symbolType().fullyQualifiedName();

    if (FIND_BY_ANNOTATION_NAME.equals(annotationType) &&
        isPartOfWebDriverPackage(fullyQualifiedName)) {
        for (ExpressionTree expressionTree : annotationTree.arguments()) {
            AssignmentExpressionTree assignmentExpressionTree = (AssignmentExpressionTree) expressionTree;

            String property = assignmentExpressionTree.variable().toString();
            String locator = HOW_PROPERTY.equals(property)
                ? ((MemberSelectExpressionTree) ((AssignmentExpressionTree) expressionTree)
                .expression()).identifier().name()
                : ((AssignmentExpressionTree) expressionTree).variable().toString();

            String propertyValue = assignmentExpressionTree.expression().is(Kind.STRING_LITERAL)
                ? ((LiteralTree) assignmentExpressionTree.expression()).value().replace("\"", "")
                : null;

            ExpressionTree howExpressionTree = annotationTree.arguments()
                .stream()
                .filter(aet -> HOW_PROPERTY.equals(((AssignmentExpressionTree) aet).variable().toString()))
                // Only one "how" property allowed in annotation
                .findFirst()
                .orElse(null);

            if (howExpressionTree != null && USING_PROPERTY.equals(property)) {
                String howLocator = ((MemberSelectExpressionTree) ((AssignmentExpressionTree) howExpressionTree)
                    .expression()).identifier().name();
                locatorMap.put(howLocator, propertyValue);
            } else {
                locatorMap.put(locator, propertyValue);
            }
        }
    }

    return locatorMap;
}
 
Example #6
Source File: AnnotationsConstantsCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitLiteral(LiteralTree tree) {
    if (inAnnotation && tree.is(Kind.STRING_LITERAL)) {
        String literalValue = removeQuotes(tree.value());
        if (ConstantsChecker.isAnnotationConstant(literalValue)) {
            context.reportIssue(this, tree,
                String.format("Use constant %s instead of hardcoded value.", ConstantsChecker.getAnnotationMessageForConstant(literalValue)));
        }
    }
    super.visitLiteral(tree);
}
 
Example #7
Source File: ConstantsCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitNode(Tree stringLiteral) {
    String literalValue = removeQuotes(((LiteralTree) stringLiteral).value());
    if (ConstantsChecker.isConstant(literalValue)) {
        String message = ConstantsChecker.getMessageForConstant(literalValue);
        reportIssue(stringLiteral, String.format("Use constant %s instead of hardcoded value.", message));
    }
    super.visitNode(stringLiteral);
}
 
Example #8
Source File: AbstractSmellCheck.java    From qualinsight-plugins-sonarqube-smell with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void handleSmellAnnotation(final AnnotationTree annotationTree) {
    String message = "";
    Integer minutes = 0;
    SmellType type = null;
    final Arguments arguments = annotationTree.arguments();
    for (final ExpressionTree expressionTree : arguments) {
        if (expressionTree.is(Tree.Kind.ASSIGNMENT)) {
            final AssignmentExpressionTree aet = (AssignmentExpressionTree) expressionTree;
            final String variable = ((IdentifierTree) aet.variable()).name();
            switch (variable) {
                case "minutes":
                    minutes += Integer.valueOf(((LiteralTree) aet.expression()).value());
                    LOGGER.debug("{} = {}", variable, minutes);
                    break;
                case "reason":
                    message = extractMessage(aet.expression());
                    LOGGER.debug("{} = {}", variable, message);
                    break;
                case "type":
                    type = SmellType.valueOf(((MemberSelectExpressionTree) aet.expression()).identifier()
                        .name());
                    break;
                default:
                    break;
            }
        }
    }
    if (smellType().equals(type)) {
        final Matcher matcher = PATTERN.matcher(message);
        if (matcher.matches()) {
            message = matcher.group(1);
        }
        reportIssue(annotationTree, message, new ArrayList<JavaFileScannerContext.Location>(), minutes);
    }
}