All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.jball.sonar.hybris.java.checks.ModelServiceSaveAll Maven / Gradle / Ivy

There is a newer version: 0.7.0
Show newest version
package de.jball.sonar.hybris.java.checks;

import org.apache.commons.collections4.CollectionUtils;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.tree.MethodInvocationTree;
import org.sonar.plugins.java.api.tree.Tree;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;

@Rule(key = "ModelServiceSaveAll")
public class ModelServiceSaveAll extends IssuableSubscriptionVisitor {
	private static final List NODES_TO_VISIT = Collections.singletonList(Tree.Kind.METHOD_INVOCATION);
	private static final String MODEL_SERVICE_FQDN = "de.hybris.platform.servicelayer.model.ModelService";

	@Override
    public List nodesToVisit() {
        return NODES_TO_VISIT;
    }

	@Override
	public void visitNode(final Tree tree) {
		MethodInvocationTree methodInvocationTree = (MethodInvocationTree) tree;
		if (isModelServiceSaveAll(methodInvocationTree)) {
			reportIssue(tree, "Use one of the other save methods of ModelService instead.");
		}
	}

	private boolean isModelServiceSaveAll(final MethodInvocationTree methodInvocationTree) {
		Symbol symbol = methodInvocationTree.symbol();

		return (isHybrisModelService(symbol.enclosingClass()) // enclosingClass is null only for package symbols
				&& "saveAll".equals(symbol.name()) //
				&& CollectionUtils.isEmpty(methodInvocationTree.arguments()));
	}

	private boolean isHybrisModelService(final @Nonnull Symbol.TypeSymbol enclosingClass) {
		return enclosingClass.type().isSubtypeOf(MODEL_SERVICE_FQDN);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy