question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question how to Modify static variable through an Agent with AgentBuilder

See original GitHub issue

Hi Rafael, great api

The Main Class , very simple

user.getLife () returns 10

public class ReflectionOnClass extends JFrame {


    private static MyProgram2 user = new MyProgram2();
    private JLabel jLabel = new JLabel("X: ");

    public ReflectionOnClass() {
        jLabel.setText("X: " + user.getLife()); // return 10
        setLayout(new FlowLayout());
        setPreferredSize(new Dimension(200, 200));
        add(jLabel);
        setDefaultCloseOperation(3);
        setLocationRelativeTo(null);
        pack();
        setVisible(true);
    }

    public static void main(String ...blablabla) {
        new Thread(ReflectionOnClass::new).start();
    }
}
public class MyProgram2 {

    private static Usuario USUARIO = new Usuario();

    public MyProgram2() {}

    public int getLife() {
        return USUARIO.getLife();
    }

}
public class Usuario {

    private int life= 10;

    public Usuario() {

    }

    public int getLife() {
        return life;
    }
    @Override
    public String toString() {
        return "Hi";
    }

}

The Usuario class contains the value that is assigned to JLabel, how could that value change with instrumentation through an Agent?

I need to know which test that you have created could help me to make the modification in runtime.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
rucko24commented, Jun 19, 2019

Funciona perfecto jefe. Works perfectly boss thanks a lot.

Something happened to me with the project generated in see apparently. the target.jar has a new pom.xml

image

Boss like that also worked wtf, I had an error in the construction of my project. omggg

   public static void premain(String param, Instrumentation inst) {
        System.out.println("Give more life please!!!");

        new AgentBuilder.Default()
                .with(new AgentBuilder.InitializationStrategy.SelfInjection.Eager())
                .with(AgentBuilder.InstallationListener.StreamWriting.toSystemOut())
                .type((ElementMatchers.nameContains("Usuario")))
                .transform((builder, typeDescription, classLoader, module) ->
                        builder.visit(Advice.to(MoreLifeAdvice.class)
                                .on(ElementMatchers.isConstructor()))
                )
                .installOn(inst);
    }

This is the structure of my project, with the manifest I do not know if that was what I lacked.

image

2reactions
raphwcommented, Jun 19, 2019

Try with a regular listener, not an installation listener:

AgentBuilder.Listener.StreamWriting.toSystemOut()
Read more comments on GitHub >

github_iconTop Results From Across the Web

java - How to change static variable value using ASM?
The first thing, your agent has to do, is registering a ClassFileTransformer . The first thing, the class file transformer should do in...
Read more >
iReasoning SNMP Agent Builder User Guide
the CLASSPATH environment variable. On Windows: If the iReasoning SNMP Agent Builder is installed at. C:\lib\iReasoning\agent, use the command.
Read more >
Static Variables in C - GeeksforGeeks
Following are some interesting facts about static variables in C. 1) A static int variable remains in memory while the program is running....
Read more >
Easily Create Java Agents with Byte Buddy - InfoQ
In this article Rafael Winterhalter, creator of the bytecode manipulation tool Byte Buddy, provides detailed guidance on how to easily ...
Read more >
Static variable and updating variables
It would be nice if we can have a features like below., Creating static variable, without prompting question. Updating existing variable ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found