Need Information on CobolPreprocessorEvent Interface and CopyPreprocessorEvent

Hi Team,

Please help me with more information on the Interface CopyPreprocessorEvent and the method “void onCopyPreprocessorEvent". It will help me to collect the list of COPY statements present in the Cobol program. Not able to find any information regarding the same in the Sonar Docs.

Thanks in Advance!

Hi,

I answered the same question on the other thread you created:

@pynicolas Thanks a lot for the clarification. I will try to implement the same. My understanding is this method need to be overridden before the call for the method visitNode. We can call the tokens method inside the overridden method, Something like "
CopyPreprocessorEventContext tok = (CopyPreprocessorEventContext) context.tokens(); "
Is my understanding correct ? I’m not getting any javadoc reference, hence getting repeated queries. My apologies regarding the same,

Well, it would look like this:

public class MyCustomCheck extends CobolCheck implements CopyPreprocessorEvent {

  @Override
  public void onCopyPreprocessorEvent(CopyPreprocessorEventContext context) {
    List<Token> tokens = context.tokens();
    ...
  }

}

Even if no javadoc is available for CopyPreprocessorEventContext, your IDE (such as IntelliJ or Eclipse) should be able to provide autocompletion for its methods.

@pynicolas Thanks a lot for helping me out. I added the above code and tried debugging. However, the control first comes to the method public void init(); onCopyPreprocessorEvent method is not getting hit if i override it before or after init() method. When i tried to call the method “onCopyPreprocessorEvent” from init(), i’m not sure of the parameter to be passed. I’m not able to get any information in the logs of debug too.

The code i used looked roughly like this.

public class MyCustomCheck extends CobolCheck implements CopyPreprocessorEvent {

@Override
public void onCopyPreprocessorEvent(CopyPreprocessorEventContext context) {
List tokens = context.tokens();

}
@Override
public void init() {
subscribeTo(getCobolGrmmar.CompilationUnit):

}
@Override
public void visitNode(AstNode ) {

}

Yes, that one is called before analysing any code.

What do you mean?
What do you run? A unit test? Do you call sonar-scanner?
Which code do you analyze?

You’re not supposed to call onCopyPreprocessorEvent in your code: onCopyPreprocessorEvent is called when analyzing a file that contains a COPY statement.

@pynicolas Thanks for looking into my queries. Below are my answers.
What do you mean?
What do you run? A unit test? Do you call sonar-scanner ?
Which code do you analyze?

I was trying to run the program as a JUNIT test case in Eclipse environment. Didn’t call “sonar-scanner”. Was using CobolCheckVerifier to test the sample written.

GetCopybookListRule getCopy = new GetCopybookListRule();
cobolConfiguration.addCopyExtension(“cpy”);
cobolConfiguration.setPreprocessorsActivated(true);

    CobolCheckVerifier.verify(
        new File("src/test/resources/SRC/sample.cbl"),
        cobolConfiguration,
        getCopy);

In the sample program, there is a copy statement “COPY TESTCPY”. The source for this Copybook is stored in the folder in the project environment.
Is it like “onCopyPreprocessorEvent” will be recognized only when it runs with “Sonar Scanner”. ?

Thanks for this additional information.

onCopyPreprocessorEvent is called only on objects which are registered as preprocessor listeners. This is automatically handled when running an analysis with sonar-scanner.

However, CobolCheckVerifier does not behave the same way.
You can register your check as a preprocessor listener when creating the instance of CobolConfiguration that you pass to CobolCheckVerifier:

CobolConfiguration myConfig = new CobolConfiguration();
// ...
myConfig.setPreprocessorListeners(Collections.singletonList(myCustomCheck));
CobolCheckVerifier.verify(myFile, myConfig, myCustomCheck);

@pynicolas Thanks a lot for helping out. I’m now able to do Unit test. I’m able to retrieve the list of Copybooks. Thanks so much for all the support.

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