0

I'm working with ast-grep and trying to modify Java code by adding a new method at the end of the class (not necessarily at end, it can be anywhere in class )and inserting a method call. Despite several attempts, I haven't been able to figure out the correct approach.

Here's what I'm trying to achieve:

  1. Add required imports in the import section of the class.
import com.company.package.agent.OverBuilder;
import com.company.package.agent.config.OverApplication;
import com.company.package.agent.config.OverGroup;
import com.company.package.agent.OverNoOpImpl;
  1. In the method containing SpecificClassName.initialize(arg1, arg2, arg3); line, After this line, add a new line to call method initOverAgent();
SpecificClassName.initialize(arg1, arg2, arg3);
initOverAgent();
  1. Declare initOverAgent method in the class.

Notes:

  1. The class can be a simple class, or it can be extending some class or implementing an interface, e.g.:

    @Log4j2
    public class StoneModule implements Module {
    

    or

    @Log4j2
    public final class FitnessReportTool {
    

    or

    public class ConfigModule extends AbstractModule {
    
  2. The class can have some, none, or many annotations like:

    @Module
    @Log4j2
    
  3. If SpecificClassName.initialize(arg1, arg2, arg3); line is within a static method, the declaration of initOverAgent should be static; otherwise, it should be non-static.

Here's an example of the original class and the desired output:

Original class:

package com.company.package;

import com.google.inject.Injector;
import org.slf4j.Logger;

public class Remapter {
    public static void main(String[] args) throws Throwable {
        ...
    }
    private static synchronized void stopServer(BobcatServer server) {
        ...
    }
    private static void initSpecificClassName(String[] args3) throws InterruptedException 
    {
        ...someCode
        SpecificClassName.initialize(arg1, arg2, arg3);
        ...someCode
    }
}

Desired output:

package com.company.package;

import com.google.inject.Injector;
import org.slf4j.Logger;
import com.company.package.agent.OverBuilder;
import com.company.package.agent.config.OverApplication;
import com.company.package.agent.config.OverGroup;
import com.company.package.agent.OverNoOpImpl;

public class Remapter {
    public static void main(String[] args) throws Throwable {
        ...
    }
    private static synchronized void stopServer(BobcatServer server) {
        ...
    }
    private static void initSpecificClassName(String[] args3) throws InterruptedException 
    {
        ...someCode
        SpecificClassName.initialize(arg1, arg2, arg3);
        initOverAgent();
        ...someCode
    }  
    public static void initOverAgent() {
        if (!staticCondition) {
            new OverNoOpImpl().start();
        }
        new OverBuilder()
            .withOverGroup(new OverGroup().withName(NAME)
            .withOverApplication(new OverApplication().withApplicationName(APP_NAME)
            .build().start();
    }
}

I've tried using some YAML configuration, but I'm not able to satisfy all my required conditions.

id: declareOverAgent
language: java
rule:
  kind: class_declaration
  pattern: |
    @$A
    public class $CLASS {
      $$$CLASS_BODY
    }
fix: |-
  @$A
  public class $CLASS {
    $$$CLASS_BODY
    public static void initOverAgent() {
        if (!staticCondition) {
            new OverNoOpImpl().start();
        }
        new OverBuilder()
            .withOverGroup(new OverGroup().withName(NAME)
            .withOverApplication(new OverApplication().withApplicationName(APP_NAME)
            .build().start();
    }
  }
---
id: initializeOverAgent
language: java
rule:
  kind: expression_statement
  pattern: SpecificClassName.initialize(arg1, arg2, arg3);
fix: |-
  SpecificClassName.initialize(arg1, arg2, arg3);
  initializeCloudCoverJavaAgent();

I've been looking through the ast-grep documentation but haven't found a clear way to achieve this. Could someone please help me with how to add new method in the class and the method call ?

Any help or guidance would be greatly appreciated!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.