org.sonar.plugins.java.api.JavaFileScannerContext Java Examples

The following examples show how to use org.sonar.plugins.java.api.JavaFileScannerContext. 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: HardCodedSleepCheck.java    From sonar-webdriver-plugin with MIT License 6 votes vote down vote up
@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
    JavaFileScannerContext context = getContext();

    if (!tree.methodSelect().is(Tree.Kind.IDENTIFIER)) {
        MemberSelectExpressionTree memberSelectExpressionTree = ((MemberSelectExpressionTree) tree.methodSelect());

        String methodName = memberSelectExpressionTree.identifier().name();
        String fullyQualifiedNameOfExpression = memberSelectExpressionTree.expression().symbolType()
            .fullyQualifiedName();

        if (SLEEP_METHOD_NAME.equals(methodName) &&
            fullyQualifiedNameOfExpression.equals(THREAD)) {
            context.reportIssue(this, tree, "Avoid using hard coded sleeps, use explicit wait instead");
        }
    }
}
 
Example #2
Source File: ImplicitWaitCheck.java    From sonar-webdriver-plugin with MIT License 6 votes vote down vote up
@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
    JavaFileScannerContext context = getContext();

    if (!tree.methodSelect().is(Tree.Kind.IDENTIFIER)) {
        MemberSelectExpressionTree memberSelectExpressionTree = ((MemberSelectExpressionTree) tree.methodSelect());

        String methodName = memberSelectExpressionTree.identifier().name();
        String fullyQualifiedNameOfExpression = memberSelectExpressionTree.expression().symbolType()
            .fullyQualifiedName();

        if (IMPLICITLY_WAIT_METHOD_NAME.equals(methodName) &&
            isPartOfWebDriverPackage(fullyQualifiedNameOfExpression)) {
            context.reportIssue(this, tree, "Avoid using implicit wait, use explicit wait instead");
        }
    }
}
 
Example #3
Source File: BaseLocatorValueCheck.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
private void reportLinkTextAndTagNameLocatorIssue(ExpressionTree expressionTree, String locatorKey) {
    JavaFileScannerContext context = getContext();

    if (LOCATORS_TO_AVOID.contains(locatorKey)) {
        context.reportIssue(this, expressionTree,
            "Avoid using " + locatorKey + " locator, try using " + LOCATORS_RECOMMENDED.toString());
    }
}
 
Example #4
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);
    }
}
 
Example #5
Source File: CatchUsesExceptionWithContextCheck.java    From vjtools with Apache License 2.0 5 votes vote down vote up
@Override
public void scanFile(JavaFileScannerContext context) {
    this.context = context;
    validUsagesStack = new ArrayDeque<>();
    exceptions = Splitter.on(",").trimResults().split(exceptionsCommaSeparated);
    exceptionIdentifiers = Lists.newArrayList();
    for (String exception : exceptions) {
        exceptionIdentifiers.add(exception.substring(exception.lastIndexOf('.') + 1));
    }
    if (context.getSemanticModel() != null) {
        scan(context.getTree());
    }
    excludedCatchTrees.clear();
}
 
Example #6
Source File: UnusedPrivateFieldCheck.java    From vjtools with Apache License 2.0 5 votes vote down vote up
@Override
public void scanFile(JavaFileScannerContext context) {
	super.scanFile(context);
	if (!hasNativeMethod && !lombokClass) {
		classes.forEach(this::checkClassFields);
	}
	classes.clear();
	assignments.clear();
	unknownIdentifiers.clear();
	hasNativeMethod = false;
	lombokClass = false;
}
 
Example #7
Source File: OperatorPrecedenceCheck.java    From vjtools with Apache License 2.0 5 votes vote down vote up
@Override
public void scanFile(JavaFileScannerContext context) {
    this.context = context;
    reportedLines.clear();
    scan(context.getTree());
    reportedLines.clear();
}
 
Example #8
Source File: BadConstantNameCheck.java    From vjtools with Apache License 2.0 5 votes vote down vote up
@Override
public void scanFile(JavaFileScannerContext context) {
	if (pattern == null) {
		pattern = Pattern.compile(format, Pattern.DOTALL);
	}
	super.scanFile(context);
}
 
Example #9
Source File: UnusedMethodParameterCheck.java    From vjtools with Apache License 2.0 5 votes vote down vote up
private void reportUnusedParameters(List<IdentifierTree> unused) {
	List<JavaFileScannerContext.Location> locations = new ArrayList<>();
	for (IdentifierTree identifier : unused) {
		locations.add(new JavaFileScannerContext.Location(
				"Remove this unused method parameter " + identifier.name() + "\".", identifier));
	}
	IdentifierTree firstUnused = unused.get(0);
	String msg;
	if (unused.size() > 1) {
		msg = "Remove these unused method parameters.";
	} else {
		msg = "Remove this unused method parameter \"" + firstUnused.name() + "\".";
	}
	reportIssue(firstUnused, msg, locations, null);
}
 
Example #10
Source File: CatchUsesExceptionWithContextCheck.java    From vjtools with Apache License 2.0 5 votes vote down vote up
@Override
public void scanFile(JavaFileScannerContext context) {
    this.context = context;
    validUsagesStack = new ArrayDeque<>();
    exceptions = Splitter.on(",").trimResults().split(exceptionsCommaSeparated);
    exceptionIdentifiers = Lists.newArrayList();
    for (String exception : exceptions) {
        exceptionIdentifiers.add(exception.substring(exception.lastIndexOf('.') + 1));
    }
    if (context.getSemanticModel() != null) {
        scan(context.getTree());
    }
    excludedCatchTrees.clear();
}
 
Example #11
Source File: UnusedPrivateFieldCheck.java    From vjtools with Apache License 2.0 5 votes vote down vote up
@Override
public void scanFile(JavaFileScannerContext context) {
	super.scanFile(context);
	if (!hasNativeMethod && !lombokClass) {
		classes.forEach(this::checkClassFields);
	}
	classes.clear();
	assignments.clear();
	unknownIdentifiers.clear();
	hasNativeMethod = false;
	lombokClass = false;
}
 
Example #12
Source File: OperatorPrecedenceCheck.java    From vjtools with Apache License 2.0 5 votes vote down vote up
@Override
public void scanFile(JavaFileScannerContext context) {
    this.context = context;
    reportedLines.clear();
    scan(context.getTree());
    reportedLines.clear();
}
 
Example #13
Source File: BadConstantNameCheck.java    From vjtools with Apache License 2.0 5 votes vote down vote up
@Override
public void scanFile(JavaFileScannerContext context) {
	if (pattern == null) {
		pattern = Pattern.compile(format, Pattern.DOTALL);
	}
	super.scanFile(context);
}
 
Example #14
Source File: BaseLocatorValueCheck.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
private void reportIdIssue(ExpressionTree expressionTree, String locatorKey, String locatorValue) {
    JavaFileScannerContext context = getContext();

    if (ID_LOCATORS.contains(locatorKey.toLowerCase()) && !locatorValue.matches(VALID_ID_LOCATOR_REGEX)) {
        context.reportIssue(this, expressionTree,
            "Invalid id locator");
    }
}
 
Example #15
Source File: UnusedMethodParameterCheck.java    From vjtools with Apache License 2.0 5 votes vote down vote up
private void reportUnusedParameters(List<IdentifierTree> unused) {
	List<JavaFileScannerContext.Location> locations = new ArrayList<>();
	for (IdentifierTree identifier : unused) {
		locations.add(new JavaFileScannerContext.Location(
				"Remove this unused method parameter " + identifier.name() + "\".", identifier));
	}
	IdentifierTree firstUnused = unused.get(0);
	String msg;
	if (unused.size() > 1) {
		msg = "Remove these unused method parameters.";
	} else {
		msg = "Remove this unused method parameter \"" + firstUnused.name() + "\".";
	}
	reportIssue(firstUnused, msg, locations, null);
}
 
Example #16
Source File: BaseLocatorValueCheck.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
private void reportXpathLocatorIssue(ExpressionTree expressionTree, String locatorKey) {
    JavaFileScannerContext context = getContext();

    if (XPATH_LOCATORS.contains(locatorKey.toLowerCase())) {
        context.reportIssue(this, expressionTree,
            "Xpath locator is not recommended, try using " + LOCATORS_RECOMMENDED.toString());
    }
}
 
Example #17
Source File: BaseLocatorValueCheck.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
private void reportXpathIssue(ExpressionTree expressionTree, String locatorKey, String locatorValue) {
    JavaFileScannerContext context = getContext();

    if (XPATH_LOCATORS.contains(locatorKey.toLowerCase()) && !locatorValue
        .matches(RECOMMENDED_XPATH_LOCATOR_REGEX)) {
        context.reportIssue(this, expressionTree,
            "Avoid using xpath locator tied to page layout");
    }
}
 
Example #18
Source File: BaseLocatorValueCheck.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
private void reportCssIssue(ExpressionTree expressionTree, String locatorKey, String locatorValue) {
    JavaFileScannerContext context = getContext();

    if (CSS_LOCATORS.contains(locatorKey.toLowerCase()) && locatorValue.matches(AVOID_CSS_LOCATOR_REGEX)) {
        context.reportIssue(this, expressionTree,
            "Avoid using css locator tied to page layout");
    }
}
 
Example #19
Source File: BaseLocatorValueCheck.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
private void reportClassNameIssue(ExpressionTree expressionTree, String locatorKey, String locatorValue) {
    JavaFileScannerContext context = getContext();

    if (CLASS_NAME_LOCATORS.contains(locatorKey.toLowerCase()) && locatorValue.contains(SPACE)) {
        context.reportIssue(this, expressionTree,
            "Avoid compound class names with by class name locator strategy");
    }
}
 
Example #20
Source File: ExplicitWaitInTestCheck.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
@Override
public void visitNewClass(NewClassTree tree) {
    JavaFileScannerContext context = getContext();

    String fullyQualifiedName = tree.symbolType().fullyQualifiedName();
    String identifier = tree.identifier().toString().toLowerCase();

    if (identifier.contains(WAIT) &&
        isPartOfWebDriverPackage(fullyQualifiedName)) {
        context.reportIssue(this, tree, "Avoid using explicit waits in Test classes.");
    }
}
 
Example #21
Source File: WebDriverCommandInTestCheck.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
    JavaFileScannerContext context = getContext();

    if (methodInvocationIsPartOfWebDriverPackage(tree)) {
        context.reportIssue(this, tree, "Should not use WebDriver commands in Test classes.");
    }
}
 
Example #22
Source File: AssertionInNonTestCheck.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
    JavaFileScannerContext context = getContext();

    if (methodInvocationIsAssertion(tree)) {
        context.reportIssue(this, tree, "Should not use assertions in non-test classes.");
    }
}
 
Example #23
Source File: ModifiableValueMapUsageCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
@Override
public void scanFile(JavaFileScannerContext context) {
    this.context = context;
    scan(context.getTree());
}
 
Example #24
Source File: ResourceResolverTryWithResourcesCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
public void scanFile(JavaFileScannerContext javaFileScannerContext) {
  context = javaFileScannerContext;
  scan(context.getTree());
}
 
Example #25
Source File: ResourceResolverShouldBeClosed.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
@Override
public void scanFile(JavaFileScannerContext javaFileScannerContext) {
    context = javaFileScannerContext;
    scan(context.getTree());
}
 
Example #26
Source File: PreferSlingServletAnnotation.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
@Override
public void scanFile(JavaFileScannerContext context) {
    this.context = context;
    gatherAppliedAnnotations(context);
    scan(context.getTree());
}
 
Example #27
Source File: PreferSlingServletAnnotation.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private void gatherAppliedAnnotations(JavaFileScannerContext context) {
    annotations = new CheckAppliedAnnotationsVisitor();
    context.getTree().accept(annotations);
}
 
Example #28
Source File: ContentResourceShouldBeNullCheckedCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
@Override
public void scanFile(JavaFileScannerContext context) {
    this.context = context;
    scan(context.getTree());
}
 
Example #29
Source File: SessionShouldBeLoggedOut.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
@Override
public void scanFile(JavaFileScannerContext javaFileScannerContext) {
    context = javaFileScannerContext;
    scan(context.getTree());
}
 
Example #30
Source File: DefaultInjectionStrategyAnnotationCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
@Override
public void scanFile(JavaFileScannerContext context) {
    this.context = context;
    scan(context.getTree());
}