ObjectMapper not being read in MethodMatchers.onConstructorFound()

Hi Team,

I am trying to build a custom rule in which I want to identify the count of ObjectMapper constructor called.

@Override
public List<Kind> nodesToVisit() {       
  return Collections.singletonList(Kind.NEW_CLASS);    
}

@Override
protected MethodMatchers getMethodInvocationMatchers() {        
  return MethodMatchers.create().ofTypes("com.fasterxml.jackson.databind.ObjectMapper").constructor().withAnyParameters().build();
}
 
@Override
protected void onConstructorFound(NewClassTree newClassTree) {        
  if (!isInConstructorOrStaticMain(newClassTree) && isUsedOnlyLocally(newClassTree)) {            
    reportIssue(newClassTree.identifier(), "new instance created with each invocation");        
  }
}

This is not working as it says “No issues Found. Expected one.” which I understand. But the same onConstructorFound is working with

java.util.Random or any jdk classes

Is there any other configuration of dependecies. I tried adding jackson dependency in plugin code as well but it did not work.

Does sonar support only jdk classes for constructor invocation check?

Regards,
Hetal

Hey @hetal_rathod,

It should work with any class at analysis time. However, in order to make your test pass, you need to make sure that you provided the JAR containing the classes and bytecode of the library you target to the project. In particular, this part of the tutorial should help you: How to test sources requiring external binaries

Hope this helps,
Michael

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.