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

The following examples show how to use org.sonar.plugins.java.api.tree.ClassTree. 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: BadConstantNameCheck.java    From vjtools with Apache License 2.0 6 votes vote down vote up
@Override
public void visitNode(Tree tree) {
	ClassTree classTree = (ClassTree) tree;
	for (Tree member : classTree.members()) {
		if (member.is(Tree.Kind.VARIABLE) && hasSemantic()) {
			VariableTree variableTree = (VariableTree) member;
			Type symbolType = variableTree.type().symbolType();
			 if (isConstantType(symbolType) && (classTree.is(Tree.Kind.INTERFACE, Tree.Kind.ANNOTATION_TYPE) || isStaticFinal(variableTree))) {
		          checkName(variableTree);
		     }
		}
		// VJ Remove //
		// else if (member.is(Tree.Kind.ENUM_CONSTANT)) {
		// checkName((VariableTree) member);
		// }
	}
}
 
Example #2
Source File: UnusedPrivateFieldCheck.java    From vjtools with Apache License 2.0 6 votes vote down vote up
@Override
public void visitNode(Tree tree) {
	if (!hasSemantic()) {
		return;
	}
	switch (tree.kind()) {
	case METHOD:
		checkIfNativeMethod((MethodTree) tree);
		break;
	case CLASS:
		classes.add((ClassTree) tree);
		break;
	case IMPORT:// VJ
		checkIfLombokClass((ImportTree) tree);
		break;
	case EXPRESSION_STATEMENT:
		collectAssignment(((ExpressionStatementTree) tree).expression());
		break;
	case IDENTIFIER:
		collectUnknownIdentifier((IdentifierTree) tree);
		break;
	default:
		throw new IllegalStateException("Unexpected subscribed tree.");
	}
}
 
Example #3
Source File: BadConstantNameCheck.java    From vjtools with Apache License 2.0 6 votes vote down vote up
@Override
public void visitNode(Tree tree) {
	ClassTree classTree = (ClassTree) tree;
	for (Tree member : classTree.members()) {
		if (member.is(Tree.Kind.VARIABLE) && hasSemantic()) {
			VariableTree variableTree = (VariableTree) member;
			Type symbolType = variableTree.type().symbolType();
			 if (isConstantType(symbolType) && (classTree.is(Tree.Kind.INTERFACE, Tree.Kind.ANNOTATION_TYPE) || isStaticFinal(variableTree))) {
		          checkName(variableTree);
		     }
		}
		// VJ Remove //
		// else if (member.is(Tree.Kind.ENUM_CONSTANT)) {
		// checkName((VariableTree) member);
		// }
	}
}
 
Example #4
Source File: UnusedPrivateFieldCheck.java    From vjtools with Apache License 2.0 6 votes vote down vote up
@Override
public void visitNode(Tree tree) {
	if (!hasSemantic()) {
		return;
	}
	switch (tree.kind()) {
	case METHOD:
		checkIfNativeMethod((MethodTree) tree);
		break;
	case CLASS:
		classes.add((ClassTree) tree);
		break;
	case IMPORT:// VJ
		checkIfLombokClass((ImportTree) tree);
		break;
	case EXPRESSION_STATEMENT:
		collectAssignment(((ExpressionStatementTree) tree).expression());
		break;
	case IDENTIFIER:
		collectUnknownIdentifier((IdentifierTree) tree);
		break;
	default:
		throw new IllegalStateException("Unexpected subscribed tree.");
	}
}
 
Example #5
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 #6
Source File: PreferSlingServletAnnotation.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitClass(ClassTree tree) {
    if (isSlingServlet(tree)) {
        scan(tree.modifiers());
        if (!annotations.hasStandardComponentAnnotation()) {
            if (!annotations.hasSlingServletAnnotation()) {
                context.reportIssue(this, tree, RULE_MESSAGE);
            } else if (annotations.hasMixedUpAnnotations()) {
                context.reportIssue(this, tree, "@Component nor @Service annotation is not needed when @SlingServlet is used.");
            }
        }
    }
}
 
Example #7
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 #8
Source File: ThreadSafeFieldCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private boolean implementsVulnerableInterface(ClassTree clazz) {
    boolean implementsInterface = false;
    for (TypeTree typeTree : clazz.superInterfaces()) {
        String name = typeTree.symbolType().fullyQualifiedName();
        implementsInterface |= VULNERABLE_INTERFACES.contains(name);
    }
    return implementsInterface;
}
 
Example #9
Source File: ThreadSafeFieldCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private boolean extendsVulnerableClass(ClassTree clazz) {
    boolean extendsClass = false;
    TypeTree type = clazz.superClass();
    if (type != null) {
        String name = type.symbolType().fullyQualifiedName();
        extendsClass = VULNERABLE_CLASSES.contains(name);
    }
    return extendsClass;
}
 
Example #10
Source File: ThreadSafeFieldCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitClass(ClassTree classTree) {
    boolean extendsClass = extendsVulnerableClass(classTree);
    boolean implementsInterface = implementsVulnerableInterface(classTree);
    boolean hasAnnotation = hasAnnotation(classTree);
    boolean vulnerable = extendsClass || implementsInterface || hasAnnotation;
    if (vulnerable) {
        checkMembers(classTree);
    }
    super.visitClass(classTree);
}
 
Example #11
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 #12
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 #13
Source File: ThreadSafeFieldCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private void checkMembers(ClassTree classTree) {
    for (Tree member : classTree.members()) {
        checkMember(member);
    }
}
 
Example #14
Source File: UnusedPrivateFieldCheck.java    From vjtools with Apache License 2.0 4 votes vote down vote up
private void checkClassFields(ClassTree classTree) {
	classTree.members().stream().filter(member -> member.is(Tree.Kind.VARIABLE)).map(VariableTree.class::cast)
			.forEach(this::checkIfUnused);
}
 
Example #15
Source File: UnusedPrivateFieldCheck.java    From vjtools with Apache License 2.0 4 votes vote down vote up
private void checkClassFields(ClassTree classTree) {
	classTree.members().stream().filter(member -> member.is(Tree.Kind.VARIABLE)).map(VariableTree.class::cast)
			.forEach(this::checkIfUnused);
}
 
Example #16
Source File: PreferSlingServletAnnotation.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private boolean isSlingServlet(ClassTree tree) {
    Type type = tree.symbol().type();
    boolean allMethodsServlet = type.isSubtypeOf("org.apache.sling.api.servlets.SlingAllMethodsServlet");
    boolean safeMethodsServlet = type.isSubtypeOf("org.apache.sling.api.servlets.SlingSafeMethodsServlet");
    return allMethodsServlet || safeMethodsServlet;
}
 
Example #17
Source File: ContentResourceShouldBeNullCheckedCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
@Override
public void visitClass(ClassTree tree) {
    contentResources.clear();
    super.visitClass(tree);
}
 
Example #18
Source File: MethodMatcherTest.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private void givenMethodInvocationTree(String codeToParse) {
    CompilationUnitTree compilationUnitTree = parse(codeToParse);
    ClassTree classTree = (ClassTree) compilationUnitTree.types().get(CLASS_INDEX);
    StatementTree statementTree = ((MethodTree) classTree.members().get(CLASS_METHOD_INDEX)).block().body().get(METHOD_INVOCATION_INDEX);
    this.methodInvocationTree = (MethodInvocationTree) ((ExpressionStatementTree) statementTree).expression();
}