Java Code Examples for org.sonar.plugins.java.api.tree.Tree#is()

The following examples show how to use org.sonar.plugins.java.api.tree.Tree#is() . 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: 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 3
Source File: UnusedPrivateFieldCheck.java    From vjtools with Apache License 2.0 5 votes vote down vote up
private static String fullQualifiedName(Tree tree) {
	if (tree.is(Tree.Kind.IDENTIFIER)) {
		return ((IdentifierTree) tree).name();
	} else if (tree.is(Tree.Kind.MEMBER_SELECT)) {
		MemberSelectExpressionTree m = (MemberSelectExpressionTree) tree;
		return fullQualifiedName(m.expression()) + "." + m.identifier().name();
	}
	throw new UnsupportedOperationException(String.format("Kind/Class '%s' not supported", tree.getClass()));
}
 
Example 4
Source File: UnusedPrivateFieldCheck.java    From vjtools with Apache License 2.0 5 votes vote down vote up
private static boolean isMethodIdentifier(IdentifierTree identifier) {
	Tree parent = identifier.parent();
	while (parent != null && !parent.is(Tree.Kind.METHOD_INVOCATION, Tree.Kind.METHOD_REFERENCE)) {
		parent = parent.parent();
	}
	if (parent == null) {
		return false;
	}
	if (parent.is(Tree.Kind.METHOD_INVOCATION)) {
		return identifier.equals(methodName((MethodInvocationTree) parent));
	} else {
		return identifier.equals(((MethodReferenceTree) parent).method());
	}
}
 
Example 5
Source File: UnusedPrivateFieldCheck.java    From vjtools with Apache License 2.0 5 votes vote down vote up
private static String fullQualifiedName(Tree tree) {
	if (tree.is(Tree.Kind.IDENTIFIER)) {
		return ((IdentifierTree) tree).name();
	} else if (tree.is(Tree.Kind.MEMBER_SELECT)) {
		MemberSelectExpressionTree m = (MemberSelectExpressionTree) tree;
		return fullQualifiedName(m.expression()) + "." + m.identifier().name();
	}
	throw new UnsupportedOperationException(String.format("Kind/Class '%s' not supported", tree.getClass()));
}
 
Example 6
Source File: UnusedPrivateFieldCheck.java    From vjtools with Apache License 2.0 5 votes vote down vote up
private static boolean isMethodIdentifier(IdentifierTree identifier) {
	Tree parent = identifier.parent();
	while (parent != null && !parent.is(Tree.Kind.METHOD_INVOCATION, Tree.Kind.METHOD_REFERENCE)) {
		parent = parent.parent();
	}
	if (parent == null) {
		return false;
	}
	if (parent.is(Tree.Kind.METHOD_INVOCATION)) {
		return identifier.equals(methodName((MethodInvocationTree) parent));
	} else {
		return identifier.equals(((MethodReferenceTree) parent).method());
	}
}
 
Example 7
Source File: AdministrativeAccessUsageCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private void checkInvocation(Tree tree, MethodMatcher invocationMatcher) {
    if (tree.is(Kind.METHOD_INVOCATION)) {
        MethodInvocationTree methodInvocationTree = (MethodInvocationTree) tree;
        if (invocationMatcher.matches(methodInvocationTree)) {
            this.onMethodInvocationFound(methodInvocationTree);
        }
    }
}
 
Example 8
Source File: ThreadSafeFieldCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private void checkMember(Tree member) {
    boolean isVariableField = member.is(Kind.VARIABLE);
    if (isVariableField) {
        VariableTree variableField = (VariableTree) member;
        String name = variableField.type().symbolType().fullyQualifiedName();
        if (NON_THREAD_SAFE_TYPES.contains(name)) {
            context.reportIssue(this, member, String.format(RULE_MESSAGE, name));
        }
    }
}
 
Example 9
Source File: ModifiableValueMapUsageCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private void checkIfMapVariableIsModified(List<IdentifierTree> usagesOfMVM) {
    for (IdentifierTree modifiableValueMapUsageIdentifier : usagesOfMVM) {
        Tree usageOfMVM = modifiableValueMapUsageIdentifier.parent();
        if (usageOfMVM != null) {
            if (usageOfMVM.is(Tree.Kind.ARGUMENTS)) {
                visitMethodWithMVM(modifiableValueMapUsageIdentifier, usageOfMVM);
            } else if (usageOfMVM.is(Tree.Kind.MEMBER_SELECT) && isSomeoneCallingMutableMethodsOnMap((MemberSelectExpressionTree) usageOfMVM)) {
                isModified = true;
                break;
            }
        }
    }
}