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

The following examples show how to use org.sonar.plugins.java.api.tree.AnnotationTree. 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: SessionShouldBeLoggedOut.java    From AEM-Rules-for-SonarQube with Apache License 2.0 6 votes vote down vote up
protected boolean checkIfLongSession(MethodTree method) {
    List<AnnotationTree> annotations = method.modifiers().annotations();
    for (AnnotationTree annotationTree : annotations) {
        if (annotationTree.annotationType().is(Tree.Kind.IDENTIFIER)) {
            IdentifierTree idf = (IdentifierTree) annotationTree.annotationType();
            if (idf.name().equals(ACTIVATE)) {
                collectLongSessionOpened(method);
                return true;
            } else if (idf.name().equals(DEACTIVATE)) {
                collectLongSessionClosed(method);
                return true;
            }
        }
    }
    return false;
}
 
Example #2
Source File: ResourceResolverShouldBeClosed.java    From AEM-Rules-for-SonarQube with Apache License 2.0 6 votes vote down vote up
protected boolean checkIfLongResourceResolver(MethodTree method) {
    List<AnnotationTree> annotations = method.modifiers().annotations();
    for (AnnotationTree annotationTree : annotations) {
        if (annotationTree.annotationType().is(Tree.Kind.IDENTIFIER)) {
            IdentifierTree idf = (IdentifierTree) annotationTree.annotationType();
            if (idf.name().equals(ACTIVATE)) {
                collectLongResourceResolverOpened(method);
                return true;
            } else if (idf.name().equals(DEACTIVATE)) {
                collectLongResourceResolverClosed(method);
                return true;
            }
        }
    }
    return false;
}
 
Example #3
Source File: BaseNonTestCheck.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
@Override
public void visitClass(ClassTree tree) {
    List<AnnotationTree> annotationTrees = getAnnotationTrees(tree);

    if (annotationTrees.isEmpty() || !annotationsContainAnnotationWhichIsPartOfTestPackage(annotationTrees)) {
        super.visitClass(tree);
    }
}
 
Example #4
Source File: DefaultInjectionStrategyAnnotationCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitAnnotation(AnnotationTree annotationTree) {
    if (isSearchedAnnotation(annotationTree, OPTIONAL_ANNOTATION_FULL_NAME)) {
        context.reportIssue(scanner, annotationTree, RULE_MESSAGE);
    }
    super.visitAnnotation(annotationTree);
}
 
Example #5
Source File: DefaultInjectionStrategyAnnotationCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitClass(ClassTree tree) {
    List<AnnotationTree> annotations = tree.modifiers().annotations();
    for (AnnotationTree annotationTree : annotations) {
        if (isSearchedAnnotation(annotationTree, MODEL_ANNOTATION_FULL_NAME)
            && isOptionalDefaultValue(annotationTree.arguments())) {
            CheckIfOptionalAnnotationIsPresent checkIfOptionalIsPresent = new CheckIfOptionalAnnotationIsPresent(
                this);
            tree.accept(checkIfOptionalIsPresent);
        }
    }
    super.visitClass(tree);
}
 
Example #6
Source File: BaseLocatorValueCheck.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
void checkAnnotationLocators(AnnotationTree tree, LocatorStrategyCheckType locatorStrategyCheckType) {
    Map<String, String> locatorsInAnnotationTree = getLocatorValueMapInAnnotationTree(tree);

    for (Map.Entry<String, String> locator : locatorsInAnnotationTree.entrySet()) {
        checkLocator(tree, locator.getKey(), locator.getValue(), locatorStrategyCheckType);
    }
}
 
Example #7
Source File: CommonUtil.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
public static boolean annotationsContainAnnotationWhichIsPartOfTestPackage(List<AnnotationTree> annotationTrees) {
    boolean annotationIsPartOfTestPackage = false;

    for (AnnotationTree annotationTree : annotationTrees) {
        if (annotationIsPartOfTestPackage(annotationTree)) {
            annotationIsPartOfTestPackage = true;
        }
    }

    return annotationIsPartOfTestPackage;
}
 
Example #8
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 #9
Source File: CommonUtil.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
public static List<AnnotationTree> getAnnotationTrees(ClassTree tree) {
    List<AnnotationTree> annotationTrees = new ArrayList<>();
    List<Tree> members = tree.members();

    for (Tree member : members) {
        if (METHOD_KIND.equals(member.kind().toString())) {
            annotationTrees.addAll(((MethodTree) member).modifiers().annotations());
        }
    }

    return annotationTrees;
}
 
Example #10
Source File: BaseTestCheck.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
@Override
public void visitClass(ClassTree tree) {
    List<AnnotationTree> annotationTrees = getAnnotationTrees(tree);

    if (annotationsContainAnnotationWhichIsPartOfTestPackage(annotationTrees)) {
        super.visitClass(tree);
    }
}
 
Example #11
Source File: PreferSlingServletAnnotation.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitAnnotation(AnnotationTree annotationTree) {
    if (annotations.hasSlingServletAnnotation() && isPropertyAnnotation(annotationTree)) {
        for (ExpressionTree expression : annotationTree.arguments()) {
            if (isAssignmentToName(expression)) {
                checkIfPropertyInsteadSlingServletIsUsed(annotationTree, (AssignmentExpressionTree) expression);
            }
        }
    }
    super.visitAnnotation(annotationTree);
}
 
Example #12
Source File: PreferSlingServletAnnotation.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitAnnotation(AnnotationTree annotationTree) {
    standardComponentAnnotation |= isOfType(annotationTree, "org.osgi.service.component.annotations.Component");
    legacyServiceAnnotation |= isOfType(annotationTree, "org.apache.felix.scr.annotations.Service");
    legacyComponentAnnotation |= isOfType(annotationTree, "org.apache.felix.scr.annotations.Component");
    slingServletAnnotation |= isOfType(annotationTree, "org.apache.felix.scr.annotations.sling.SlingServlet");
    super.visitAnnotation(annotationTree);
}
 
Example #13
Source File: AbstractSmellCheck.java    From qualinsight-plugins-sonarqube-smell with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void visitNode(final Tree tree) {
    final AnnotationTree annotationTree = (AnnotationTree) tree;
    final Type symbolType = annotationTree.annotationType()
        .symbolType();
    if (symbolType.is("com.qualinsight.plugins.sonarqube.smell.api.annotation.Smell")) {
        handleSmellAnnotation(annotationTree);
    }
}
 
Example #14
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 #15
Source File: CommonUtil.java    From sonar-webdriver-plugin with MIT License 4 votes vote down vote up
private static boolean annotationIsPartOfTestPackage(AnnotationTree annotationTree) {
    String annotation = annotationTree.annotationType().toString();
    String fullyQualifiedName = annotationTree.annotationType().symbolType().fullyQualifiedName();

    return TEST_ANNOTATION_NAMES.contains(annotation) && isPartOfTestPackage(fullyQualifiedName);
}
 
Example #16
Source File: PreferSlingServletAnnotation.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private boolean isPropertyAnnotation(AnnotationTree annotationTree) {
    return isOfType(annotationTree, "org.apache.felix.scr.annotations.Property");
}
 
Example #17
Source File: PreferSlingServletAnnotation.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private boolean isOfType(AnnotationTree annotationTree, String fullyQualifiedName) {
    return annotationTree.annotationType().symbolType().fullyQualifiedName().equals(fullyQualifiedName);
}
 
Example #18
Source File: DefaultInjectionStrategyAnnotationCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private boolean isSearchedAnnotation(AnnotationTree annotationTree, String fullyQualifiedName) {
    return annotationTree.annotationType().symbolType().fullyQualifiedName().equals(fullyQualifiedName);
}
 
Example #19
Source File: AnnotationsConstantsCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
@Override
public void visitAnnotation(AnnotationTree annotationTree) {
    inAnnotation = true;
    super.visitAnnotation(annotationTree);
    inAnnotation = false;
}
 
Example #20
Source File: LocatorStrategyByXpathCheck.java    From sonar-webdriver-plugin with MIT License 4 votes vote down vote up
@Override
public void visitAnnotation(AnnotationTree tree) {
    checkAnnotationLocators(tree, XPATH_LOCATOR_STRATEGY);
}
 
Example #21
Source File: IdLocatorValueCheck.java    From sonar-webdriver-plugin with MIT License 4 votes vote down vote up
@Override
public void visitAnnotation(AnnotationTree tree) {
    checkAnnotationLocators(tree, ID_VALUE);
}
 
Example #22
Source File: CssLocatorValueCheck.java    From sonar-webdriver-plugin with MIT License 4 votes vote down vote up
@Override
public void visitAnnotation(AnnotationTree tree) {
    checkAnnotationLocators(tree, CSS_VALUE);
}
 
Example #23
Source File: LocatorStrategyByLinkTextAndTagNameCheck.java    From sonar-webdriver-plugin with MIT License 4 votes vote down vote up
@Override
public void visitAnnotation(AnnotationTree tree) {
    checkAnnotationLocators(tree, LINK_TEXT_AND_TAG_NAME_LOCATOR_STRATEGY);
}
 
Example #24
Source File: XpathLocatorValueCheck.java    From sonar-webdriver-plugin with MIT License 4 votes vote down vote up
@Override
public void visitAnnotation(AnnotationTree tree) {
    checkAnnotationLocators(tree, XPATH_VALUE);
}
 
Example #25
Source File: ClassNameLocatorValueCheck.java    From sonar-webdriver-plugin with MIT License 4 votes vote down vote up
@Override
public void visitAnnotation(AnnotationTree tree) {
    checkAnnotationLocators(tree, CLASS_NAME_VALUE);
}